Skip to content

Commit

Permalink
clone() accepts 2d-arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
tmhglnd committed Mar 20, 2021
1 parent 817fb20 commit 619406d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
6 changes: 6 additions & 0 deletions docs/transform-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const Mod = require('total-serialism').Transform;

Duplicate an array with an offset added to every value

Duplicate an array multiple times, optionaly add an offset to every value when duplicating. Also works with 2-dimensonal arrays. When using strings the values will be concatenated.

**arguments**
- {NumberArray} -> Array to clone
- {Int, Int2, ... Int-n} -> amount of clones with integer offset
Expand All @@ -20,6 +22,10 @@ Duplicate an array with an offset added to every value
// duplicate an array with an offset added to every value
Mod.clone([0, 5, 7], 0, 12, -12);
//=> [ 0, 5, 7, 12, 17, 19, -12, -7, -5 ]

// when using strings it works as string concatenation
Mod.clone(['kick', 'snare', 'hat'], ['808', '909']);
//=> [ 'kick808', 'snare808', 'hat808', 'kick909', 'snare909', 'hat909' ]
```

<iframe src="https://editor.p5js.org/tmhglnd/embed/6hmjQkbzj" width="100%" height="250px" frameBorder="0" scrolling="no"></iframe>
Expand Down
15 changes: 12 additions & 3 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,25 @@
const Stat = require('./statistic');
const Util = require('./utility');

// duplicate an array, but add an offset to every value
// Duplicate an array multiple times,
// optionaly add an offset to every value when duplicating
// Also works with 2-dimensonal arrays
// If string the values will be concatenated
//
// @param {Array} -> array to clone
// @param {Int, Int2, ... Int-n} -> amount of clones with integer offset
// -> or string concatenation
//
function clone(a=[0], ...c){
if (!c.length) { c = [0, 0]; }
// flatten array if multi-dimensional
if (!c.length) {
c = [0, 0];
} else {
c = c.flat();
}
var arr = [];
for (var i=0; i<c.length; i++){
arr = arr.concat(a.map(v => v + c[i]));
arr = arr.concat(a.map(v => Util.add(v, c[i])));
}
return arr;
}
Expand Down
23 changes: 11 additions & 12 deletions test/serialism.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
console.time('requires load');

const fs = require('fs');

Expand All @@ -16,7 +17,6 @@ const Stat = Srl.Statistic;
const TL = Srl.Translate;
const Util = Srl.Utility;

console.time('requires load');
console.timeEnd('requires load');
console.log();

Expand Down Expand Up @@ -61,19 +61,17 @@ console.log();

fullTest();

// console.log(Algo.bigCollatzMod(100000));

function fullTest(){
console.time('Total Time');

testSerial();
testGen();
testAlgo();
testRand();
// testSerial();
// testGen();
// testAlgo();
// testRand();
testMod();
testStat();
testTranslate();
testUtil();
// testStat();
// testTranslate();
// testUtil();

pagebreak("All Tests Passed");
console.timeEnd('Total Time');
Expand Down Expand Up @@ -304,8 +302,9 @@ function testMod(){
// console.log(modArr);
test("Mod.clone()");
test('Mod.clone([0, 5, 7], 0, 12, -12)');
test("Mod.clone(['hello', 'world'], 0, 1, 2)");

test('Mod.clone([0, 5, [7, 12]], 0, 12, -12)');
test('Mod.clone([0, 5, 7], [0, 12, -12])');
test("Mod.clone(['kick', 'snare', 'hat'], ['_808', '_909'])");
// var comArr1 = [0, 1];
// var comArr2 = [[22, 33], 4];
// console.log(Mod.combine(comArr1, comArr2));
Expand Down

0 comments on commit 619406d

Please sign in to comment.