main
Last change
on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago |
F4 Finalna Verzija
|
-
Property mode
set to
100644
|
File size:
1.5 KB
|
Rev | Line | |
---|
[79a0317] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | /**
|
---|
| 9 | * Compare two arrays or strings by performing strict equality check for each value.
|
---|
| 10 | * @template T [T=any]
|
---|
| 11 | * @param {ArrayLike<T>} a Array of values to be compared
|
---|
| 12 | * @param {ArrayLike<T>} b Array of values to be compared
|
---|
| 13 | * @returns {boolean} returns true if all the elements of passed arrays are strictly equal.
|
---|
| 14 | */
|
---|
| 15 |
|
---|
| 16 | module.exports.equals = (a, b) => {
|
---|
| 17 | if (a.length !== b.length) return false;
|
---|
| 18 | for (let i = 0; i < a.length; i++) {
|
---|
| 19 | if (a[i] !== b[i]) return false;
|
---|
| 20 | }
|
---|
| 21 | return true;
|
---|
| 22 | };
|
---|
| 23 |
|
---|
| 24 | /**
|
---|
| 25 | * Partition an array by calling a predicate function on each value.
|
---|
| 26 | * @template T [T=any]
|
---|
| 27 | * @param {Array<T>} arr Array of values to be partitioned
|
---|
| 28 | * @param {(value: T) => boolean} fn Partition function which partitions based on truthiness of result.
|
---|
| 29 | * @returns {[Array<T>, Array<T>]} returns the values of `arr` partitioned into two new arrays based on fn predicate.
|
---|
| 30 | */
|
---|
| 31 |
|
---|
| 32 | module.exports.groupBy = (
|
---|
| 33 | // eslint-disable-next-line default-param-last
|
---|
| 34 | arr = [],
|
---|
| 35 | fn
|
---|
| 36 | ) =>
|
---|
| 37 | arr.reduce(
|
---|
| 38 | /**
|
---|
| 39 | * @param {[Array<T>, Array<T>]} groups An accumulator storing already partitioned values returned from previous call.
|
---|
| 40 | * @param {T} value The value of the current element
|
---|
| 41 | * @returns {[Array<T>, Array<T>]} returns an array of partitioned groups accumulator resulting from calling a predicate on the current value.
|
---|
| 42 | */
|
---|
| 43 | (groups, value) => {
|
---|
| 44 | groups[fn(value) ? 0 : 1].push(value);
|
---|
| 45 | return groups;
|
---|
| 46 | },
|
---|
| 47 | [[], []]
|
---|
| 48 | );
|
---|
Note:
See
TracBrowser
for help on using the repository browser.