js-split
What is it?
It is simple function that gets Object (or Array, or String) and returns parts of this.
Installation
npm install --save js-split
How it works?
Sometimes we need split object by keys and we cant do it by JS native functions. js-split
can help you with it!
This package includes two functions: main function that exported by default and extra function select
.
;...
Split Objects
jsSplit(myObject: Object, partKeys1: Array or Object, [partKeys2: Array or Object, ...]): Array
result
is Array that contains all parts and rest of myObject
([part1, part2, ..., rest])
; const v1 = ; // v1 = [{a: 1}, {c: 3}, {b: 2, d: 4}] const v2 = ; // v2 = [{a: 1}, {c: 3}, {b: 2, d: 4}]
Split Arrays
jsSplit(myArray: Array, partKeys1: Array, [partKeys2: Array, ...]): Array
result
is Array that contains all parts and rest of myArray
([part1, part2, ..., rest])
; const v1 = ; // v1 = [[1,4],[2],[3]]
or
jsSplit(myArray: Object, partLength: Number): Array
result
is Array that contains all parts of myArray
, each part is partLength
size.
; const v1 = ; // v1 = [[1,2], [3,4], [5]]
Split Strings
jsSplit(myText: String, partLength: Number): Array
result
is Array that contains all parts of myText
, each part is partLength
size.
; const v1 = ; // v1 = ['Hel', 'lo,', ' pe', 'opl', 'e!']
Select
Additional function select(...)
it is equal of jsSplit(...)[0]
and it has been added for extra situation when you need to get only first result.
;// ...const firstPart = 0;// ...
OR the same result by select
;// ...const firstPart = ;// ...
Examples
jsSplit
for String
; ; // = ['I', 't', ' ', 'i', 's', ' ', 't', 'e', 'x', 't']; // = ['It ', 'is ', 'tex', 't']; // = ['It is text']
select
for String
; ; // = 'I'; // = 'It '; // = 'It is text'
jsSplit
for Array
; ; // = [[1,2], [3,4]]; // = [[1,2,3], [4]]; // = [[1,2,3,4]]; // = [[1,2,3,4]]; // = [[],[],[1,2,3,4]]; // = [[2,4],[1,3]]; // = [[2,4],[1,3]]; // = [[2,4],[1],[3]]; // = [[2,4],[1],[],[3]]; // = [[2,4],[1],[3],[],[]] // you can reuse indexes; // = [[2,4],[1,2],[3]]
select
for Array
; ; // = [1,2]; // = [1,2,3]; // = [1,2,3,4]; // = [2,4]
jsSplit
for Object
; ; // = [{},{a: 1, b: 2, c: 3, d: 4}]; // = [{},{a: 1, b: 2, c: 3, d: 4}]; // = [{a: 1},{c: 3},{b: 2, d: 4}]; // = [{a: 1},{c: 3},{b: 2, d: 4}] // undefined values can't be added to result; // = [{a: 1},{c: 3},{b: 2, d: 4}] // you can reuse keys; // = [{a: 1, b: 2}, {b: 2, c: 3}, {c: 3, d: 4}, {}]
select
for Object
; ; // = {}; // = {a: 1, b: 2}; // = {a: 1, b: 2}