Skip to content

Commit

Permalink
added truncate() method to Utility
Browse files Browse the repository at this point in the history
  • Loading branch information
tmhglnd committed Nov 9, 2020
1 parent 5eddbfb commit 8696a93
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ function stretch(a=[0], len=5, mode='linear'){
// construct a lookup interpolation position for new array
let val = i / (len - 1) * (l - 1);
// lookup nearest neighbour left/right
let a0 = a[Math.max(Math.floor(val), 0)];
let a1 = a[Math.min(Math.floor(val)+1, l-1)];
let a0 = a[Math.max(Math.trunc(val), 0)];
let a1 = a[Math.min(Math.trunc(val)+1, l-1)];
// interpolate between the values according to decimal place
arr.push(Util.lerp(a0, a1, val % 1));
}
Expand Down
15 changes: 15 additions & 0 deletions src/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,21 @@ function mod(a, mod=12){
}
exports.mod = mod;

// Truncate all the values in an array towards 0,
// sometimes referred to as rounding down
//
// @param {Number/Array} -> input value
// @return {Int/Array} -> trucated value
function truncate(a){
if (!Array.isArray(a)){
return Math.trunc(a);
}
return a.map(x => Math.trunc(x));
}
exports.truncate = truncate;
exports.trunc = truncate;
exports.int = truncate;

// Plot an array of values to the console in the form of an
// ascii chart and return chart from function. If you just want the
// chart returned as text and not log to console set { log: false }.
Expand Down
9 changes: 3 additions & 6 deletions test/serialism.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,18 +76,15 @@ markov.clear();
console.log(markov.table);
*/

// let arr = [0, 12, 4, 9];
let arr = Rand.random(4, 0, 24);
let arr = [0, 9, 3, 19, 7];
// let arr = Rand.random(4, 0, 24);
console.log(arr);
Util.plot(arr);

let res = Mod.stretch(arr, 30);
let res = Util.trunc(Mod.stretch(arr, 15));
console.log(res);
Util.plot(res);

// console.log(Util.lerp(0, 10, 0.9));
// console.log(Util.mix(0, 10, 0.9));

// fullTest();

function fullTest(){
Expand Down

0 comments on commit 8696a93

Please sign in to comment.