|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
1.5 KB
|
| Line | |
|---|
| 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
|
|---|
| 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 | module.exports.equals = (a, b) => {
|
|---|
| 16 | if (a.length !== b.length) return false;
|
|---|
| 17 | for (let i = 0; i < a.length; i++) {
|
|---|
| 18 | if (a[i] !== b[i]) return false;
|
|---|
| 19 | }
|
|---|
| 20 | return true;
|
|---|
| 21 | };
|
|---|
| 22 |
|
|---|
| 23 | /**
|
|---|
| 24 | * Partition an array by calling a predicate function on each value.
|
|---|
| 25 | * @template T
|
|---|
| 26 | * @param {T[]} arr Array of values to be partitioned
|
|---|
| 27 | * @param {(value: T) => boolean} fn Partition function which partitions based on truthiness of result.
|
|---|
| 28 | * @returns {[T[], T[]]} returns the values of `arr` partitioned into two new arrays based on fn predicate.
|
|---|
| 29 | */
|
|---|
| 30 | module.exports.groupBy = (
|
|---|
| 31 | // eslint-disable-next-line default-param-last
|
|---|
| 32 | arr = [],
|
|---|
| 33 | fn
|
|---|
| 34 | ) =>
|
|---|
| 35 | arr.reduce(
|
|---|
| 36 | /**
|
|---|
| 37 | * Handles the callback logic for this hook.
|
|---|
| 38 | * @param {[T[], T[]]} groups An accumulator storing already partitioned values returned from previous call.
|
|---|
| 39 | * @param {T} value The value of the current element
|
|---|
| 40 | * @returns {[T[], T[]]} returns an array of partitioned groups accumulator resulting from calling a predicate on the current value.
|
|---|
| 41 | */
|
|---|
| 42 | (groups, value) => {
|
|---|
| 43 | groups[fn(value) ? 0 : 1].push(value);
|
|---|
| 44 | return groups;
|
|---|
| 45 | },
|
|---|
| 46 | [[], []]
|
|---|
| 47 | );
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.