Skip to content

Commit

Permalink
all transform accept non-array arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
tmhglnd committed Nov 15, 2021
1 parent 684a08e commit d06b8ca
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 17 deletions.
17 changes: 15 additions & 2 deletions docs/transform-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,13 @@ Filter one or multiple values from an array based on their type

**arguments**
- {Array} -> array to filter
- {String} -> datatype to filter
- {String} -> datatype to filter (optional, default=number)

```js
// default filter is set as number
Mod.filterType([0, 'foo', {bar : true}, 1, undefined]);
//=> [ 0, 1 ]

// return only a specific datatype (in this case you specify the type to return)
Mod.filterType([0, 1, [1, 2], 'foo', 2, null, true, {bar: 5}, 3.14, undefined], 'number');
//=> [ 0, 1, 2, 3.14 ]
Expand Down Expand Up @@ -450,7 +454,16 @@ Mod.stretch([0, 12, 3, 7], 24);
// 3.48 ┤ ╭╯ ╰──╯
// 2.32 ┤ │
// 1.16 ┤╭╯
// 0.00 ┼╯
// 0.00 ┼╯

// set interpolation to 'none'
Mod.stretch([0, 12, 3, 7], 10, 'none');
//=> 12.00 ┼ ╭──╮
// 9.60 ┤ │ │
// 7.20 ┤ │ │ ╭
// 4.80 ┤ │ │ │
// 2.40 ┤ │ ╰──╯
// 0.00 ┼──╯
```

## unique
Expand Down
25 changes: 19 additions & 6 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const Util = require('./utility');
// -> or string concatenation
//
function clone(a=[0], ...c){
// flatten array if multi-dimensional
a = Array.isArray(a)? a : [a];
// flatten clone array if multi-dimensional
if (!c.length) {
return a;
} else {
Expand Down Expand Up @@ -151,10 +152,10 @@ exports.filter = filter;
// In this case the input type is the type that is output
//
// @param {Array} -> array to filter
// @param {String/Array} -> types to filter
// @param {String/Array} -> types to filter (default = number)
// @return (Array} -> filtered array
//
function filterType(a=[0], t){
function filterType(a=[0], t='number'){
a = (Array.isArray(a))? a.slice() : [a];
t = (Array.isArray(t))? t : [t];

Expand Down Expand Up @@ -186,6 +187,7 @@ exports.tFilter = filterType;
// @return {Array}
//
function invert(arr=[0], lo, hi){
arr = Array.isArray(arr)? arr : [arr];
if (lo === undefined){
hi = Math.max(...Util.flatten(arr));
lo = Math.min(...Util.flatten(arr));
Expand All @@ -208,9 +210,9 @@ exports.invert = invert;
//
function lace(...args){
if (!args.length){ return [0]; }

var l = 0;
for (let i in args){
args[i] = Array.isArray(args[i])? args[i] : [args[i]];
l = Math.max(args[i].length, l);
}
var arr = [];
Expand Down Expand Up @@ -263,6 +265,7 @@ function merge(...args){
if (!args.length){ return [0]; }
let l = 0;
for (let i in args){
args[i] = Array.isArray(args[i])? args[i] : [args[i]];
l = Math.max(args[i].length, l);
}
let arr = [];
Expand Down Expand Up @@ -290,6 +293,7 @@ exports.merge = merge;
//
function palindrome(arr, noDouble=false){
if (arr === undefined){ return [0] };
if (!Array.isArray(arr)){ return [arr] };

let rev = arr.slice().reverse();
if (noDouble){
Expand Down Expand Up @@ -329,6 +333,7 @@ exports.repeat = repeat;
// @return {Array}
//
function reverse(a=[0]){
if (!Array.isArray(a)){ return [a]; }
return a.slice().reverse();
}
exports.reverse = reverse;
Expand All @@ -341,6 +346,7 @@ exports.reverse = reverse;
// @return {Array}
//
function rotate(a=[0], r=0){
if (!Array.isArray(a)){ return [a]; }
var l = a.length;
var arr = [];
for (var i=0; i<l; i++){
Expand All @@ -365,6 +371,9 @@ exports.sort = Stat.sort;
// return {Array}
//
function spray(values=[0], beats=[0]){
values = Array.isArray(values)? values : [values];
beats = Array.isArray(beats)? beats : [beats];

var arr = beats.slice();
var c = 0;
for (let i in beats){
Expand All @@ -384,15 +393,18 @@ exports.spray = spray;
// param {Array} -> outputlength of array
// param {String/Int} -> interpolation function (optional, default=linear)
//
function stretch(a=[0], len=5, mode='linear'){
function stretch(a=[0], len=1, mode='linear'){
a = Array.isArray(a)? a : [a];
if (len < 2){ return a; }

let arr = [];
let l = a.length;
for (let i=0; i<len; i++){
// 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.trunc(val), 0)];
let a1 = a[Math.min(Math.trunc(val)+1, l-1)];
let a1 = a[Math.min(Math.trunc(val)+1, l-1) % a.length];

if (mode === 'none' || mode === null || mode === false){
arr.push(a0);
Expand All @@ -412,6 +424,7 @@ exports.stretch = stretch;
// @return {Array}
//
function unique(a=[0]){
a = Array.isArray(a)? a : [a];
return [...new Set(a)];
}
exports.unique = unique;
1 change: 1 addition & 0 deletions src/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ exports.div = divide;
// @return {Array} -> flattened array
//
function flatten(a=[0], depth=Infinity){
a = Array.isArray(a)? a : [a];
return a.flat(depth);
}
exports.flatten = flatten;
Expand Down
32 changes: 23 additions & 9 deletions test/serialism.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ function testMod(){
pagebreak("Transform");

test("Mod.clone()");
test("Mod.clone(3)");
test("Mod.clone([0, 5, 7], 1)");
test('Mod.clone([0, 5, 7], 0, 12, -12)');
test('Mod.clone([0, [5, 9], [7, 12]], 0, 10, -10)');
Expand All @@ -316,6 +317,7 @@ function testMod(){
test("Mod.combine([['c4', 'e4']], ['g4', 'f4'])");

test("Mod.duplicate()");
test("Mod.duplicate(5, 4)");
test("Mod.duplicate([0, 7, 12], 3)");
test("Mod.duplicate([0, [3, 7], 12], 2)");
test("Mod.duplicate(['c', 'f', 'g'], 4)");
Expand All @@ -329,31 +331,35 @@ function testMod(){
test("Mod.pad([3, 7, 11, 12], 9)");
test("Mod.pad(['c', 'f', 'g'], 11, '-', 4)");

test("Util.flat(2)");
test("Util.flat([1, [2, 3, [4], 5], 6])");

// console.log(Mod.filter([0, 1, 2, [2, 4, 5], 2, 3], [2, 3]));

test("Mod.filter()");
test("Mod.filter([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [3, 8, 10])");
test("Mod.filter([0, 1.618, 2, 3.14, 4], 3.14)");
test("Mod.filter([0, 1, 'foo', 'bar', 2, 3], ['1', 'foo'])");

test("Mod.filterType()");
test("Mod.filterType([0, 'foo', {bar : true}, 1, undefined])");
test("Mod.filterType([0, 1, [1, 2], 'foo', 2, null, true, {bar: 5}, 3.14, undefined], 'number')");

test("Mod.invert()");
test("Mod.invert()");
test("Mod.invert([-1, 2, 7, 9, 14])");
test("Mod.invert([-1, 2, 7, 9, 14], 5)");
test("Mod.invert([-1, 2, 7, 9, 14], 0, 12)");

test("Mod.invert([-1, 2, [[7, 9], 14]])");

// test("Mod.lace([0, 2, 4], [1, 3, 5], ['hello'])");
test("Mod.lace()");
test("Mod.lace(2, 5)");
test("Mod.lace([0, 0, 0], [7, 7], [9, 9, 9, 9])");
test("Mod.lace([0, [0, 0]], [[7,7]], [9, [[9, 9], 9], 9])");
test("Mod.lace(['c', 'c', 'c', 'c'], ['g', 'g'], ['e'])");

// test("Mod.lookup()");
test("Mod.lookup()");
test("Mod.lookup(10)");
test("Mod.lookup([0, 1, 1, 2, 0, 2, 2, 1], ['c4', 'e4', 'f4', 'g4'])");
test("Mod.lookup([0, [1, 1, [2, 3 ], 0], 2], ['c4', 'e4', 'f4', 'g4'])");
test("Mod.lookup([-2, 5, 7, 12], ['c4', 'e4', 'f4', 'g4'])");
Expand All @@ -366,6 +372,7 @@ function testMod(){
// console.log(Mod.merge(merArr1, merArr2, merArr3));
// console.log(merArr1, merArr2, merArr3);
test("Mod.merge()");
test("Mod.merge(1, 2)");
test("Mod.merge([0, 0, 0], [5, 5], [7, 7, 7, 7])");
test("Mod.merge(['c4', 'c4'], ['f4'], ['g4', 'g4', 'g4'])");
test("Mod.merge([['c4', 'e4'], 'c4'], [['f4', 'a4']], ['g4', 'g4'])");
Expand All @@ -375,11 +382,13 @@ function testMod(){
// console.log(Mod.palindrome(palArr, false));
// console.log(palArr);
test("Mod.palindrome()");
test("Mod.palindrome(5)");
test("Mod.palindrome([0, 5, 7, 12])");
test("Mod.palindrome([0, [5, 7], 9, 12], true)");
test("Mod.palindrome(['c4', 'f4', 'g4'], true)");
// test("Mod.palindrome([0, 1, 2, 3], 1)");

test("Mod.repeat(1, 4)");
test("Mod.repeat([0, 5, 7], 3)");
test("Mod.repeat(['c4', 'e4', 'f4', 'g4'], [1, 4, 2, 0])");
// test("Mod.repeat(['kick', 'hat'], [1, 4])");
Expand All @@ -389,13 +398,15 @@ function testMod(){
// console.log(Mod.reverse(revArr));
// console.log(revArr);
test("Mod.reverse()");
test("Mod.reverse(4)");
test("Mod.reverse([0, 5, 7, 12])");
test("Mod.reverse(['c4', ['e4', 'f4'], 'g4'])");

// var rotArr = [0, 1, 2, 3];
// console.log(Mod.rotate(rotArr, -1));
// console.log(rotArr);
test("Mod.rotate()");
test("Mod.rotate(4, 1)");
test("Mod.rotate([0, 5, 7, 12], 1)");
test("Mod.rotate(['c4', ['e4', 'f4'], 'g4', 'a4'], -1)");

Expand All @@ -405,27 +416,30 @@ function testMod(){

test("Mod.sort([-1, [3, 5, -2], 5, 10])");
test("Mod.sort(10)");
/*

// var sprArr1 = [12, 19, 24];
// var sprArr2 = [1, 0, 0, 1, 1, 0, 1, 0, 0.2];
// console.log(Mod.spray(sprArr1, sprArr2));
// console.log(sprArr1, sprArr2);
test("Mod.spray()");
test("Mod.spray(4, [0, 1, 1])");
test("Mod.spray([7, 9, 12], [1, 0, 0, 1, 1, 0, 1, 0])");
test("Mod.spray([[5, 7, 9], [12, 14]], [1, 0, 1, 1, 0])");
test("Mod.spray(['c4', 'f4', 'g4'], [1, 0, 0, 1, 1, 0, 1, 0])");
// test("Mod.spray([12, 19, 24], [1, 0, 0, 1, 1, 0, 1, 0.3, 0])");

test("Mod.stretch()");
test("Mod.stretch(5, 1)");
test("Mod.stretch([0, 12, 3, 7], 24)");
Util.plot(Mod.stretch([0, 12, 3, 7], 24), { height: 10 });
Util.plot(Mod.stretch([0, 12, 3, 7], 10, 'none'), { height: 5 });
test("Util.plot(Mod.stretch([0, 12, 3, 7], 10, 'none'), { height: 5 })");
// var unArr = [5, 7, 5, 0, 12, 7, 5];
// console.log(Mod.unique(unArr));
// console.log(unArr);
test("Mod.unique()");
test("Mod.unique([5, 7, 5, 0, 12, 7, 5])"); */
test("Mod.unique(4)");
test("Mod.unique([5, 7, 5, 0, 12, 7, 5])");
}

function testStat(){
Expand Down

0 comments on commit d06b8ca

Please sign in to comment.