Skip to content

Commit

Permalink
added split() method
Browse files Browse the repository at this point in the history
  • Loading branch information
tmhglnd committed Nov 15, 2021
1 parent d06b8ca commit 5371226
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
23 changes: 23 additions & 0 deletions docs/transform-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const Mod = require('total-serialism').Transform;
- [reverse](#reverse)
- [rotate](#rotate)
- [sort](#sort)
- [split](#split)
- [spray](#spray)
- [stretch](#stretch)
- [unique](#unique)
Expand Down Expand Up @@ -410,6 +411,28 @@ Mod.sort(['e4', 'g3', 'c4', 'f3', 'b5']);
//=> [ 'b5', 'c4', 'e4', 'f3', 'g3' ]
```

## split

Split an array in one or multiple parts. Slice lengths are determined by the second argument array. Outputs an array of arrays of the result

**arguments**
- {Array} -> array to split in parts
- {Number | Array} -> slice lengths to split array into
- {Bool} -> output rest flag (optional, default=false)

```js
Mod.split(Gen.spread(8), [3, 2]);
//=> [ [ 0, 1, 2 ], [ 3, 4 ], [ 5, 6, 7 ] ]

// set rest-flag to false removes last slice
Mod.split(Gen.spread(24), [3, 2, -1, 5], false);
//=> [ [ 0, 1, 2 ], [ 3, 4 ], [ 5, 6, 7, 8, 9 ] ]
```

## share



## spray

**arguments**
Expand Down
33 changes: 33 additions & 0 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,39 @@ exports.rotate = rotate;
//
exports.sort = Stat.sort;

// function share(a=[0], s=1){
//
// }
// exports.share = share;

// split an array in one or multiple parts
// slice lengths are determined by the second argument array
// outputs an array of arrays of the result
//
// @params {Array} -> array to split
// @params {Number|Array} -> split points
// @return {Array}
//
function split(a=[0], s=[1], r=true){
a = Array.isArray(a)? a : [a];
s = Array.isArray(s)? s : [s];

let arr = [];
let _s = 0;
for (let i=0; i<s.length; i++){
if (s[i] > 0){
let _t = _s + s[i];
arr.push(a.slice(_s, _t));
_s = _t;
}
}
if (r){
arr.push(a.slice(_s, a.length));
}
return arr;
}
exports.split = split;

// spray the values of one array on the
// places of values of another array if
// the value is greater than 0
Expand Down
5 changes: 5 additions & 0 deletions test/serialism.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,11 @@ function testMod(){
test("Mod.sort([-1, [3, 5, -2], 5, 10])");
test("Mod.sort(10)");

test("Mod.split()");
test("Mod.split(Gen.spread(8), [3, 2])");
test("Mod.split(Gen.spread(24), [3, 2, -1, 5], false)");
test("Mod.split([1, [2, 3], 4, [5, 6, [7, 8]]], [3, 2])");

// var sprArr1 = [12, 19, 24];
// var sprArr2 = [1, 0, 0, 1, 1, 0, 1, 0, 0.2];
// console.log(Mod.spray(sprArr1, sprArr2));
Expand Down

0 comments on commit 5371226

Please sign in to comment.