@@ -19,36 +19,67 @@ Immutable versions of normally mutable array methods
1919$ npm install immutable-arrays
2020```
2121
22- ## Test
22+ ## Usage
23+
24+ ### Old school browser global
25+ ``` html
26+ <script src =" immutable-arrays/lib/immutableArrays.min.js" ></script >
27+ <script >
28+ var res = immutableArrays .push ([1 , 2 , 3 , 4 ], 10 );
29+ </script >
30+ ```
2331
24- ``` sh
25- $ npm test
32+ ### Node.js
33+ ``` js
34+ var immutableArrays = require (' immutable-arrays' );
35+ var res = immutableArrays .push ([1 , 2 , 3 , 4 ], 10 );
36+ ```
37+
38+ ### ES6 imports
39+
40+ #### Importing defaults
41+
42+ ``` js
43+ import immutableArrays from ' immutable-arrays' ;
44+ const res = immutableArrays .push ([1 , 2 , 3 , 4 ], 10 );
45+ ```
46+
47+ #### Importing a single method
48+
49+ ``` js
50+ import {push } from ' immutable-arrays' ;
51+ const res = push ([1 , 2 , 3 , 4 ], 10 );
52+
53+ // or
54+
55+ import {push as immutablePush } from ' immutable-arrays' ;
56+ const res = immutablePush ([1 , 2 , 3 , 4 ], 10 );
2657```
2758
28- ## Functions
59+ ## API
2960
3061<dl >
31- <dt ><a href =" #immutablePush " >immutablePush (array, ...elementN)</a > ⇒ <code >Array</code ></dt >
62+ <dt ><a href =" #immutableArrays.push " >immutableArrays.push (array, ...elementN)</a > ⇒ <code >Array</code ></dt >
3263<dd ><p >Adds one or more elements to the end of an array by returning a new array instead of mutating the original one.</p ></dd >
33- <dt ><a href =" #immutablePop " >immutablePop (array)</a > ⇒ <code >Array</code ></dt >
64+ <dt ><a href =" #immutableArrays.pop " >immutableArrays.pop (array)</a > ⇒ <code >Array</code ></dt >
3465<dd ><p >Removes the last element from an array by returning a new array instead of mutating the original one.</p ></dd >
35- <dt ><a href =" #immutableDelete " >immutableDelete (array, index)</a > ⇒ <code >Array</code ></dt >
66+ <dt ><a href =" #immutableArrays.del " >immutableArrays.del (array, index)</a > ⇒ <code >Array</code ></dt >
3667<dd ><p >Deletes an element from an array by its index in the array.</p ></dd >
37- <dt ><a href =" #immutableShift " >immutableShift (array)</a > ⇒ <code >Array</code ></dt >
68+ <dt ><a href =" #immutableArrays.shift " >immutableArrays.shift (array)</a > ⇒ <code >Array</code ></dt >
3869<dd ><p >Removes the first element from an array.</p ></dd >
39- <dt ><a href =" #immutableUnshift " >immutableUnshift (array, ...elementN)</a > ⇒ <code >Array</code ></dt >
70+ <dt ><a href =" #immutableArrays.unshift " >immutableArrays.unshift (array, ...elementN)</a > ⇒ <code >Array</code ></dt >
4071<dd ><p >Adds one or more elements to the beginning of an array.</p ></dd >
41- <dt ><a href =" #immutableReverse " >immutableReverse (array)</a > ⇒ <code >Array</code ></dt >
72+ <dt ><a href =" #immutableArrays.reverse " >immutableArrays.reverse (array)</a > ⇒ <code >Array</code ></dt >
4273<dd ><p >Reverses an array (not in place). The first array element becomes the last, and the last array element becomes the first.</p ></dd >
43- <dt ><a href =" #immutableSort " >immutableSort (array, [compareFunction])</a > ⇒ <code >Array</code ></dt >
74+ <dt ><a href =" #immutableArrays.sort " >immutableArrays.sort (array, [compareFunction])</a > ⇒ <code >Array</code ></dt >
4475<dd ><p >Sorts the elements of an array (not in place) and returns a sorted array.</p ></dd >
45- <dt ><a href =" #immutableSplice " >immutableSplice (array, [start], [deleteCount], [...elementN])</a > ⇒ <code >Array</code ></dt >
76+ <dt ><a href =" #immutableArrays.splice " >immutableArrays.splice (array, [start], [deleteCount], [...elementN])</a > ⇒ <code >Array</code ></dt >
4677<dd ><p >Removes existing elements and/or adds new elements to an array.</p ></dd >
4778</dl >
4879
49- <a name =" immutablePush " ></a >
80+ <a name =" immutableArrays.push " ></a >
5081
51- ## immutablePush (array, ...elementN) ⇒ <code >Array</code >
82+ ## immutableArrays.push (array, ...elementN) ⇒ <code >Array</code >
5283Adds one or more elements to the end of an array by returning
5384a new array instead of mutating the original one.
5485
@@ -62,14 +93,14 @@ a new array instead of mutating the original one.
6293** Example**
6394``` js
6495const originalArray = [' a' , ' b' , ' c' , ' d' , ' e' ];
65- const resultArray = immutablePush (originalArray, ' f' , ' g' );
96+ const resultArray = immutableArrays . push (originalArray, ' f' , ' g' );
6697// -> originalArray ['a', 'b', 'c', 'd', 'e']
6798// -> resultArray ['a', 'b', 'c', 'd', 'e', 'f', 'g']
6899```
69100
70- <a name =" immutablePop " ></a >
101+ <a name =" immutableArrays.pop " ></a >
71102
72- ## immutablePop (array) ⇒ <code >Array</code >
103+ ## immutableArrays.pop (array) ⇒ <code >Array</code >
73104Removes the last element from an array by returning
74105a new array instead of mutating the original one.
75106
@@ -82,14 +113,14 @@ a new array instead of mutating the original one.
82113** Example**
83114``` js
84115const originalArray = [' a' , ' b' , ' c' , ' d' , ' e' ];
85- const resultArray = immutablePop (originalArray);
116+ const resultArray = immutableArrays . pop (originalArray);
86117// -> originalArray ['a', 'b', 'c', 'd', 'e']
87118// -> resultArray ['a', 'b', 'c', 'd']
88119```
89120
90- <a name =" immutableDelete " ></a >
121+ <a name =" immutableArrays.del " ></a >
91122
92- ## immutableDelete (array, index) ⇒ <code >Array</code >
123+ ## immutableArrays.del (array, index) ⇒ <code >Array</code >
93124Deletes an element from an array by its index in the array.
94125
95126** Returns** : <code >Array</code > - A new array with the element removed.
@@ -102,14 +133,14 @@ Deletes an element from an array by its index in the array.
102133** Example**
103134``` js
104135const originalArray = [' a' , ' b' , ' c' , ' d' , ' e' ];
105- const resultArray = immutableDelete (originalArray, 2 );
136+ const resultArray = immutableArrays . del (originalArray, 2 );
106137// -> originalArray ['a', 'b', 'c', 'd', 'e']
107138// -> resultArray ['a', 'b', 'd', 'e']
108139```
109140
110- <a name =" immutableShift " ></a >
141+ <a name =" immutableArrays.shift " ></a >
111142
112- ## immutableShift (array) ⇒ <code >Array</code >
143+ ## immutableArrays.shift (array) ⇒ <code >Array</code >
113144Removes the first element from an array.
114145
115146** Returns** : <code >Array</code > - A new array with the first element removed.
@@ -121,14 +152,14 @@ Removes the first element from an array.
121152** Example**
122153``` js
123154const originalArray = [' a' , ' b' , ' c' , ' d' , ' e' ];
124- const resultArray = immutableShift (originalArray);
155+ const resultArray = immutableArrays . shift (originalArray);
125156// -> originalArray ['a', 'b', 'c', 'd', 'e']
126157// -> resultArray ['b', 'c', 'd', 'e']
127158```
128159
129- <a name =" immutableUnshift " ></a >
160+ <a name =" immutableArrays.unshift " ></a >
130161
131- ## immutableUnshift (array, ...elementN) ⇒ <code >Array</code >
162+ ## immutableArrays.unshift (array, ...elementN) ⇒ <code >Array</code >
132163Adds one or more elements to the beginning of an array.
133164
134165** Returns** : <code >Array</code > - A new array with the new elements added to the front.
@@ -141,14 +172,14 @@ Adds one or more elements to the beginning of an array.
141172** Example**
142173``` js
143174const originalArray = [' a' , ' b' , ' c' , ' d' , ' e' ];
144- const resultArray = immutableUnshift (originalArray, ' f' , ' g' );
175+ const resultArray = immutableArrays . unshift (originalArray, ' f' , ' g' );
145176// -> originalArray ['a', 'b', 'c', 'd', 'e']
146177// -> resultArray ['f', 'g', 'a', 'b', 'c', 'd', 'e']
147178```
148179
149- <a name =" immutableReverse " ></a >
180+ <a name =" immutableArrays.reverse " ></a >
150181
151- ## immutableReverse (array) ⇒ <code >Array</code >
182+ ## immutableArrays.reverse (array) ⇒ <code >Array</code >
152183Reverses an array (not in place).
153184The first array element becomes the last, and the last array element becomes the first.
154185
@@ -161,14 +192,14 @@ The first array element becomes the last, and the last array element becomes the
161192** Example**
162193``` js
163194const originalArray = [' a' , ' b' , ' c' , ' d' , ' e' ];
164- const resultArray = immutableReverse (originalArray);
195+ const resultArray = immutableArrays . reverse (originalArray);
165196// -> originalArray ['a', 'b', 'c', 'd', 'e']
166197// -> resultArray ['e', 'd', 'c', 'b', 'a']
167198```
168199
169- <a name =" immutableSort " ></a >
200+ <a name =" immutableArrays.sort " ></a >
170201
171- ## immutableSort (array, [ compareFunction] ) ⇒ <code >Array</code >
202+ ## immutableArrays.sort (array, [ compareFunction] ) ⇒ <code >Array</code >
172203Sorts the elements of an array (not in place) and returns a sorted array.
173204
174205** Returns** : <code >Array</code > - A new sorted array.
@@ -183,26 +214,26 @@ Sorts the elements of an array (not in place) and returns a sorted array.
183214const numberArray = [20 , 3 , 4 , 10 , - 3 , 1 , 0 , 5 ];
184215const stringArray = [' Blue' , ' Humpback' , ' Beluga' ];
185216
186- const resultArray = immutableSort (numberArray, (a , b ) => a - b);
217+ const resultArray = immutableArrays . sort (numberArray, (a , b ) => a - b);
187218// -> numberArray [20, 3, 4, 10, -3, 1, 0, 5]
188219// -> resultArray [-3, 0, 1, 3, 4, 5, 10, 20]
189220
190- const resultArray = immutableSort (numberArray, (a , b ) => b - a);
221+ const resultArray = immutableArrays . sort (numberArray, (a , b ) => b - a);
191222// -> numberArray [20, 3, 4, 10, -3, 1, 0, 5]
192223// -> resultArray [20, 10, 5, 4, 3, 1, 0, -3]
193224
194- const resultArray = immutableSort (stringArray);
225+ const resultArray = immutableArrays . sort (stringArray);
195226// -> stringArray ['Blue', 'Humpback', 'Beluga']
196227// -> resultArray ['Beluga', 'Blue', 'Humpback']
197228
198- const resultArray = immutableSort (stringArray, (a , b ) => a .toLowerCase () < b .toLowerCase ());
229+ const resultArray = immutableArrays . sort (stringArray, (a , b ) => a .toLowerCase () < b .toLowerCase ());
199230// -> stringArray ['Blue', 'Humpback', 'Beluga']
200231// -> resultArray ['Humpback', 'Blue', 'Beluga']
201232```
202233
203- <a name =" immutableSplice " ></a >
234+ <a name =" immutableArrays.splice " ></a >
204235
205- ## immutableSplice (array, [ start] , [ deleteCount] , [ ...elementN] ) ⇒ <code >Array</code >
236+ ## immutableArrays.splice (array, [ start] , [ deleteCount] , [ ...elementN] ) ⇒ <code >Array</code >
206237Removes existing elements and/or adds new elements to an array.
207238
208239** Returns** : <code >Array</code > - The result array.
@@ -217,51 +248,63 @@ Removes existing elements and/or adds new elements to an array.
217248** Example**
218249``` js
219250const originalArray = [' a' , ' b' , ' c' , ' d' , ' e' ];
220- const resultArray = immutableSplice (originalArray, 0 );
251+ const resultArray = immutableArrays . splice (originalArray, 0 );
221252// -> originalArray ['a', 'b', 'c', 'd', 'e']
222253// -> resultArray []
223254
224255const originalArray = [' a' , ' b' , ' c' , ' d' , ' e' ];
225- const resultArray = immutableSplice (originalArray, 0 , 1 );
256+ const resultArray = immutableArrays . splice (originalArray, 0 , 1 );
226257// -> originalArray ['a', 'b', 'c', 'd', 'e']
227258// -> resultArray ['b', 'c', 'd', 'e']
228259
229260const originalArray = [' a' , ' b' , ' c' , ' d' , ' e' ];
230- const resultArray = immutableSplice (originalArray, 0 , 3 );
261+ const resultArray = immutableArrays . splice (originalArray, 0 , 3 );
231262// -> originalArray ['a', 'b', 'c', 'd', 'e']
232263// -> resultArray ['d', 'e']
233264
234265const originalArray = [' a' , ' b' , ' c' , ' d' , ' e' ];
235- const resultArray = immutableSplice (originalArray, 0 , originalArray .length );
266+ const resultArray = immutableArrays . splice (originalArray, 0 , originalArray .length );
236267// -> originalArray ['a', 'b', 'c', 'd', 'e']
237268// -> resultArray []
238269
239270const originalArray = [' a' , ' b' , ' c' , ' d' , ' e' ];
240- const resultArray = immutableSplice (originalArray, 0 , - 3 );
271+ const resultArray = immutableArrays . splice (originalArray, 0 , - 3 );
241272// -> originalArray ['a', 'b', 'c', 'd', 'e']
242273// -> resultArray ['a', 'b', 'c', 'd', 'e']
243274
244275const originalArray = [' a' , ' b' , ' c' , ' d' , ' e' ];
245- const resultArray = immutableSplice (originalArray, 0 , 0 , ' lorem' , ' ipsum' );
276+ const resultArray = immutableArrays . splice (originalArray, 0 , 0 , ' lorem' , ' ipsum' );
246277// -> originalArray ['a', 'b', 'c', 'd', 'e']
247278// -> resultArray ['lorem', 'ipsum', 'a', 'b', 'c', 'd', 'e']
248279
249280const originalArray = [' a' , ' b' , ' c' , ' d' , ' e' ];
250- const resultArray = immutableSplice (originalArray, originalArray .length , 0 , ' lorem' , ' ipsum' );
281+ const resultArray = immutableArrays . splice (originalArray, originalArray .length , 0 , ' lorem' , ' ipsum' );
251282// -> originalArray ['a', 'b', 'c', 'd', 'e']
252283// -> resultArray ['a', 'b', 'c', 'd', 'e', 'lorem', 'ipsum']
253284
254285const originalArray = [' a' , ' b' , ' c' , ' d' , ' e' ];
255- const resultArray = immutableSplice (originalArray, 0 , 2 , ' lorem' , ' ipsum' );
286+ const resultArray = immutableArrays . splice (originalArray, 0 , 2 , ' lorem' , ' ipsum' );
256287// -> originalArray ['a', 'b', 'c', 'd', 'e']
257288// -> resultArray ['lorem', 'ipsum', 'c', 'd', 'e']
258289
259290const originalArray = [' a' , ' b' , ' c' , ' d' , ' e' ];
260- const resultArray = immutableSplice (originalArray, originalArray .length - 2 , 2 , ' lorem' , ' ipsum' );
291+ const resultArray = immutableArrays . splice (originalArray, originalArray .length - 2 , 2 , ' lorem' , ' ipsum' );
261292// -> originalArray ['a', 'b', 'c', 'd', 'e']
262293// -> resultArray ['a', 'b', 'c', 'lorem', 'ipsum']
263294```
264295
296+ ## Test
297+
298+ ``` sh
299+ $ npm test
300+ ```
301+
302+ ## Test Coverage
303+
304+ ``` sh
305+ $ npm run coverage
306+ ```
307+
265308## License
266309
267310[ The MIT License (MIT)] ( https://georapbox.mit-license.org/@2017 )
0 commit comments