necessary 13.2.2 → 13.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +7 -0
- package/package.json +1 -1
- package/src/utilities/array.js +45 -24
package/README.md
CHANGED
|
@@ -701,6 +701,7 @@ pathWithoutTopmostDirectoryNameFromPath("root/etc/init.conf"); // returns 'etc/i
|
|
|
701
701
|
- `clear()`
|
|
702
702
|
- `copy()`
|
|
703
703
|
- `merge()`
|
|
704
|
+
- `match()`
|
|
704
705
|
- `find()`
|
|
705
706
|
- `replace()`
|
|
706
707
|
- `splice()`
|
|
@@ -757,6 +758,12 @@ copy([1, 2, 3], [4, 5, 6, 7]); // the first array argument becomes [4, 5, 6, 7]
|
|
|
757
758
|
merge([1, 2, 3], [4, 5, 6, 7]); // the first array argument becomes [1, 2, 3, 4, 5, 6, 7]
|
|
758
759
|
```
|
|
759
760
|
|
|
761
|
+
* The `match()` function compares the first and second array arguments. If they are of the same length and the callback argument supplied returns a truthy value when invoked with each pair of elements then it returns true:
|
|
762
|
+
|
|
763
|
+
```
|
|
764
|
+
match([1, 2, 3], [-1, -2, -3], (valueA, valueB) => (valueA === -valueB)); // returns true
|
|
765
|
+
```
|
|
766
|
+
|
|
760
767
|
* The `find()` function is like its native counterpart, however it returns an array of all the elements for which the callback function returns a truthy value, rather than just the first:
|
|
761
768
|
|
|
762
769
|
```
|
package/package.json
CHANGED
package/src/utilities/array.js
CHANGED
|
@@ -30,16 +30,16 @@ export function back(array) { return array.slice(array.length - 1); }
|
|
|
30
30
|
|
|
31
31
|
export function front(array) { return array.slice(0, Math.max(1, array.length - 1)); }
|
|
32
32
|
|
|
33
|
-
export function push(
|
|
33
|
+
export function push(arrayA, arrayB) { Array.prototype.push.apply(arrayA, arrayB); }
|
|
34
34
|
|
|
35
|
-
export function unshift(
|
|
35
|
+
export function unshift(arrayA, arrayB) { Array.prototype.unshift.apply(arrayA, arrayB); }
|
|
36
36
|
|
|
37
|
-
export function concat(
|
|
38
|
-
const
|
|
37
|
+
export function concat(arrayA, elementOrArray2) {
|
|
38
|
+
const arrayB = (elementOrArray2 instanceof Array) ?
|
|
39
39
|
elementOrArray2 :
|
|
40
40
|
[ elementOrArray2 ];
|
|
41
41
|
|
|
42
|
-
push(
|
|
42
|
+
push(arrayA, arrayB);
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
export function clear(array) {
|
|
@@ -48,14 +48,34 @@ export function clear(array) {
|
|
|
48
48
|
return array.splice(start);
|
|
49
49
|
}
|
|
50
50
|
|
|
51
|
-
export function copy(
|
|
51
|
+
export function copy(arrayA, arrayB) {
|
|
52
52
|
const start = 0,
|
|
53
|
-
deleteCount =
|
|
53
|
+
deleteCount = arrayB.length; ///
|
|
54
54
|
|
|
55
|
-
splice(
|
|
55
|
+
splice(arrayA, start, deleteCount, arrayB);
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
export function merge(
|
|
58
|
+
export function merge(arrayA, arrayB) { Array.prototype.push.apply(arrayA, arrayB); }
|
|
59
|
+
|
|
60
|
+
export function match(arrayA, arrayB, callback) {
|
|
61
|
+
let matches = false;
|
|
62
|
+
|
|
63
|
+
const arrayALength = arrayA.length,
|
|
64
|
+
arrayBLength = arrayB.length;
|
|
65
|
+
|
|
66
|
+
if (arrayALength === arrayBLength) {
|
|
67
|
+
matches = arrayA.every((elementA, index) => {
|
|
68
|
+
const elementB = arrayB[index],
|
|
69
|
+
passed = callback(elementA, elementB, index);
|
|
70
|
+
|
|
71
|
+
if (passed) {
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return matches;
|
|
78
|
+
}
|
|
59
79
|
|
|
60
80
|
export function find(array, callback) {
|
|
61
81
|
const elements = [];
|
|
@@ -93,9 +113,9 @@ export function replace(array, element, callback) {
|
|
|
93
113
|
return found;
|
|
94
114
|
}
|
|
95
115
|
|
|
96
|
-
export function splice(
|
|
97
|
-
const args = [ start, deleteCount, ...
|
|
98
|
-
deletedElements = Array.prototype.splice.apply(
|
|
116
|
+
export function splice(arrayA, start, deleteCount = Infinity, arrayB = []) {
|
|
117
|
+
const args = [ start, deleteCount, ...arrayB ],
|
|
118
|
+
deletedElements = Array.prototype.splice.apply(arrayA, args);
|
|
99
119
|
|
|
100
120
|
return deletedElements;
|
|
101
121
|
}
|
|
@@ -183,11 +203,11 @@ export function compress(array, callback) {
|
|
|
183
203
|
length = array.length;
|
|
184
204
|
|
|
185
205
|
while (index1 < length) {
|
|
186
|
-
const
|
|
206
|
+
const elementB = array[index1];
|
|
187
207
|
|
|
188
208
|
for (let index2 = length - 1; index2 > index1; index2--) {
|
|
189
|
-
const
|
|
190
|
-
passed = callback(
|
|
209
|
+
const elementA = array[index2],
|
|
210
|
+
passed = callback(elementA, elementB);
|
|
191
211
|
|
|
192
212
|
if (passed) {
|
|
193
213
|
const start = index2, ///
|
|
@@ -203,10 +223,10 @@ export function compress(array, callback) {
|
|
|
203
223
|
}
|
|
204
224
|
}
|
|
205
225
|
|
|
206
|
-
export function combine(
|
|
226
|
+
export function combine(arrayA, arrayB, callback) {
|
|
207
227
|
const array = [
|
|
208
|
-
...
|
|
209
|
-
...
|
|
228
|
+
...arrayA,
|
|
229
|
+
...arrayB
|
|
210
230
|
];
|
|
211
231
|
|
|
212
232
|
compress(array, callback);
|
|
@@ -214,23 +234,23 @@ export function combine(array1, array2, callback) {
|
|
|
214
234
|
return array;
|
|
215
235
|
}
|
|
216
236
|
|
|
217
|
-
export function augment(
|
|
218
|
-
|
|
237
|
+
export function augment(arrayA, arrayB, callback) {
|
|
238
|
+
arrayB.forEach((element, index) => {
|
|
219
239
|
const passed = callback(element, index);
|
|
220
240
|
|
|
221
241
|
if (passed) {
|
|
222
|
-
|
|
242
|
+
arrayA.push(element);
|
|
223
243
|
}
|
|
224
244
|
});
|
|
225
245
|
}
|
|
226
246
|
|
|
227
|
-
export function separate(array,
|
|
247
|
+
export function separate(array, arrayA, arrayB, callback) {
|
|
228
248
|
array.forEach((element, index) => {
|
|
229
249
|
const passed = callback(element, index);
|
|
230
250
|
|
|
231
251
|
passed ?
|
|
232
|
-
|
|
233
|
-
|
|
252
|
+
arrayA.push(element) :
|
|
253
|
+
arrayB.push(element);
|
|
234
254
|
});
|
|
235
255
|
}
|
|
236
256
|
|
|
@@ -394,6 +414,7 @@ export default {
|
|
|
394
414
|
clear,
|
|
395
415
|
copy,
|
|
396
416
|
merge,
|
|
417
|
+
match,
|
|
397
418
|
find,
|
|
398
419
|
replace,
|
|
399
420
|
splice,
|