Skip to content

Commit 4bc6e81

Browse files
committed
Add immutableSort method
1 parent 3246345 commit 4bc6e81

File tree

9 files changed

+179
-35
lines changed

9 files changed

+179
-35
lines changed

README.md

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,21 @@ $ npm test
2323

2424
<dl>
2525
<dt><a href="#immutablePush">immutablePush(array, ...elementN)</a> ⇒ <code>Array</code></dt>
26-
<dd><p>Adds one or more elements to the end of an array by returning
27-
a new array instead of mutating the original one.</p>
28-
</dd>
26+
<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>
2927
<dt><a href="#immutablePop">immutablePop(array)</a> ⇒ <code>Array</code></dt>
30-
<dd><p>Removes the last element from an array by returning
31-
a new array instead of mutating the original one.</p>
32-
</dd>
28+
<dd><p>Removes the last element from an array by returning a new array instead of mutating the original one.</p></dd>
3329
<dt><a href="#immutableDelete">immutableDelete(array, index)</a> ⇒ <code>Array</code></dt>
34-
<dd><p>Deletes an element from an array by its index in the array.</p>
35-
</dd>
30+
<dd><p>Deletes an element from an array by its index in the array.</p></dd>
3631
<dt><a href="#immutableShift">immutableShift(array)</a> ⇒ <code>Array</code></dt>
37-
<dd><p>Removes the first element from an array.</p>
38-
</dd>
32+
<dd><p>Removes the first element from an array.</p></dd>
3933
<dt><a href="#immutableUnshift">immutableUnshift(array, ...elementN)</a> ⇒ <code>Array</code></dt>
40-
<dd><p>Adds one or more elements to the beginning of an array.</p>
41-
</dd>
34+
<dd><p>Adds one or more elements to the beginning of an array.</p></dd>
4235
<dt><a href="#immutableReverse">immutableReverse(array)</a> ⇒ <code>Array</code></dt>
43-
<dd><p>Reverses an array (not in place).
44-
The first array element becomes the last, and the last array element becomes the first.</p>
45-
</dd>
36+
<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>
37+
<dt><a href="#immutableSort">immutableSort(array, [compareFunction])</a> ⇒ <code>Array</code></dt>
38+
<dd><p>Sorts the elements of an array (not in place) and returns a sorted array.</p></dd>
4639
<dt><a href="#immutableSplice">immutableSplice(array, [start], [deleteCount], [...elementN])</a> ⇒ <code>Array</code></dt>
47-
<dd><p>Removes existing elements and/or adds new elements to an array.</p>
48-
</dd>
40+
<dd><p>Removes existing elements and/or adds new elements to an array.</p></dd>
4941
</dl>
5042

5143
<a name="immutablePush"></a>
@@ -54,7 +46,6 @@ The first array element becomes the last, and the last array element becomes the
5446
Adds one or more elements to the end of an array by returning
5547
a new array instead of mutating the original one.
5648

57-
**Kind**: global function
5849
**Returns**: <code>Array</code> - A new array with the new entries added to the end.
5950

6051
| Param | Type | Description |
@@ -69,13 +60,13 @@ const resultArray = immutablePush(originalArray, 'f', 'g');
6960
// -> originalArray ['a', 'b', 'c', 'd', 'e']
7061
// -> resultArray ['a', 'b', 'c', 'd', 'e', 'f', 'g']
7162
```
63+
7264
<a name="immutablePop"></a>
7365

7466
## immutablePop(array) ⇒ <code>Array</code>
7567
Removes the last element from an array by returning
7668
a new array instead of mutating the original one.
7769

78-
**Kind**: global function
7970
**Returns**: <code>Array</code> - A new array with the last element removed.
8071

8172
| Param | Type | Description |
@@ -89,12 +80,12 @@ const resultArray = immutablePop(originalArray);
8980
// -> originalArray ['a', 'b', 'c', 'd', 'e']
9081
// -> resultArray ['a', 'b', 'c', 'd']
9182
```
83+
9284
<a name="immutableDelete"></a>
9385

9486
## immutableDelete(array, index) ⇒ <code>Array</code>
9587
Deletes an element from an array by its index in the array.
9688

97-
**Kind**: global function
9889
**Returns**: <code>Array</code> - A new array with the element removed.
9990

10091
| Param | Type | Description |
@@ -109,12 +100,12 @@ const resultArray = immutableDelete(originalArray, 2);
109100
// -> originalArray ['a', 'b', 'c', 'd', 'e']
110101
// -> resultArray ['a', 'b', 'd', 'e']
111102
```
103+
112104
<a name="immutableShift"></a>
113105

114106
## immutableShift(array) ⇒ <code>Array</code>
115107
Removes the first element from an array.
116108

117-
**Kind**: global function
118109
**Returns**: <code>Array</code> - A new array with the first element removed.
119110

120111
| Param | Type | Description |
@@ -128,12 +119,12 @@ const resultArray = immutableShift(originalArray);
128119
// -> originalArray ['a', 'b', 'c', 'd', 'e']
129120
// -> resultArray ['b', 'c', 'd', 'e']
130121
```
122+
131123
<a name="immutableUnshift"></a>
132124

133125
## immutableUnshift(array, ...elementN) ⇒ <code>Array</code>
134126
Adds one or more elements to the beginning of an array.
135127

136-
**Kind**: global function
137128
**Returns**: <code>Array</code> - A new array with the new elements added to the front.
138129

139130
| Param | Type | Description |
@@ -148,13 +139,13 @@ const resultArray = immutableUnshift(originalArray, 'f', 'g');
148139
// -> originalArray ['a', 'b', 'c', 'd', 'e']
149140
// -> resultArray ['f', 'g', 'a', 'b', 'c', 'd', 'e']
150141
```
142+
151143
<a name="immutableReverse"></a>
152144

153145
## immutableReverse(array) ⇒ <code>Array</code>
154146
Reverses an array (not in place).
155147
The first array element becomes the last, and the last array element becomes the first.
156148

157-
**Kind**: global function
158149
**Returns**: <code>Array</code> - A new array reversed.
159150

160151
| Param | Type | Description |
@@ -168,12 +159,46 @@ const resultArray = immutableReverse(originalArray);
168159
// -> originalArray ['a', 'b', 'c', 'd', 'e']
169160
// -> resultArray ['e', 'd', 'c', 'b', 'a']
170161
```
162+
163+
<a name="immutableSort"></a>
164+
165+
## immutableSort(array, [compareFunction]) ⇒ <code>Array</code>
166+
Sorts the elements of an array (not in place) and returns a sorted array.
167+
168+
**Returns**: <code>Array</code> - A new sorted array.
169+
170+
| Param | Type | Description |
171+
| --- | --- | --- |
172+
| array | <code>Array</code> | The original array. |
173+
| [compareFunction] | <code>Function</code> | Specifies a function that defines the sort order. If omitted, the array is sorted according to each character's Unicode code point value, according to the string conversion of each element. |
174+
175+
**Example**
176+
```js
177+
const numberArray = [20, 3, 4, 10, -3, 1, 0, 5];
178+
const stringArray = ['Blue', 'Humpback', 'Beluga'];
179+
180+
const resultArray = immutableSort(numberArray, (a, b) => a - b);
181+
// -> numberArray [20, 3, 4, 10, -3, 1, 0, 5]
182+
// -> resultArray [-3, 0, 1, 3, 4, 5, 10, 20]
183+
184+
const resultArray = immutableSort(numberArray, (a, b) => b - a);
185+
// -> numberArray [20, 3, 4, 10, -3, 1, 0, 5]
186+
// -> resultArray [20, 10, 5, 4, 3, 1, 0, -3]
187+
188+
const resultArray = immutableSort(stringArray);
189+
// -> stringArray ['Blue', 'Humpback', 'Beluga']
190+
// -> resultArray ['Beluga', 'Blue', 'Humpback']
191+
192+
const resultArray = immutableSort(stringArray, (a, b) => a.toLowerCase() < b.toLowerCase());
193+
// -> stringArray ['Blue', 'Humpback', 'Beluga']
194+
// -> resultArray ['Humpback', 'Blue', 'Beluga']
195+
```
196+
171197
<a name="immutableSplice"></a>
172198

173199
## immutableSplice(array, [start], [deleteCount], [...elementN]) ⇒ <code>Array</code>
174200
Removes existing elements and/or adds new elements to an array.
175201

176-
**Kind**: global function
177202
**Returns**: <code>Array</code> - The result array.
178203

179204
| Param | Type | Default | Description |

lib/immutableArrays.js

Lines changed: 58 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)