@supercharge/arrays
Advanced tools
Comparing version 4.0.0 to 4.1.0
@@ -100,4 +100,5 @@ declare type Values<T> = Array<T | Iterable<T> | undefined | null>; | ||
/** | ||
* Keeps the items in the array that meet the condition | ||
* specified in the given `predicate` callback function. | ||
* | ||
* | ||
* @param {Function} predicate | ||
@@ -296,2 +297,11 @@ * | ||
/** | ||
* Inverse of the `filter` method, removing all items in the array satisfying | ||
* the condition specified in the given `predicate` callback function. | ||
* | ||
* @param {Function} predicate | ||
* | ||
* @returns {Arr<T>} | ||
*/ | ||
reject(predicate: (value: T, index: number, array: Arr<T>) => unknown): Arr<T>; | ||
/** | ||
* Removes `undefined` and `null` values from the array. | ||
@@ -373,2 +383,16 @@ * | ||
/** | ||
* Keep only unique items in the array. | ||
* | ||
* @returns {Arr} | ||
*/ | ||
unique(): Arr<T>; | ||
/** | ||
* Keep only unique items in the array identified by the given `selector`. | ||
* | ||
* @param {Function} | ||
* | ||
* @returns {Array} | ||
*/ | ||
uniqueBy(selector: (item: T, index: number, array: Arr<T>) => unknown): Arr<T>; | ||
/** | ||
* Add one or more items to the beginning of the array. | ||
@@ -375,0 +399,0 @@ * |
@@ -31,3 +31,6 @@ 'use strict'; | ||
static isIterable(value) { | ||
return Array.from(value).length > 0; | ||
if (value == null) { | ||
return false; | ||
} | ||
return typeof value[Symbol.iterator] === 'function'; | ||
} | ||
@@ -364,5 +367,3 @@ /** | ||
push(...values) { | ||
for (const value of this.resolveValues(...values)) { | ||
this.values.push(value); | ||
} | ||
this.values.push(...this.resolveValues(...values)); | ||
return this; | ||
@@ -379,3 +380,3 @@ } | ||
return values.flatMap(value => { | ||
if (value === null || value === undefined) { | ||
if (value == null) { | ||
return value; | ||
@@ -401,2 +402,15 @@ } | ||
/** | ||
* Inverse of the `filter` method, removing all items in the array satisfying | ||
* the condition specified in the given `predicate` callback function. | ||
* | ||
* @param {Function} predicate | ||
* | ||
* @returns {Arr<T>} | ||
*/ | ||
reject(predicate) { | ||
return this.filter((item, index) => { | ||
return !predicate(item, index, this); | ||
}); | ||
} | ||
/** | ||
* Removes `undefined` and `null` values from the array. | ||
@@ -408,3 +422,3 @@ * | ||
return this.filter(item => { | ||
return item !== null && item !== undefined; | ||
return item != null; | ||
}); | ||
@@ -513,2 +527,27 @@ } | ||
/** | ||
* Keep only unique items in the array. | ||
* | ||
* @returns {Arr} | ||
*/ | ||
unique() { | ||
return new Arr(new Set(this.values)); | ||
} | ||
/** | ||
* Keep only unique items in the array identified by the given `selector`. | ||
* | ||
* @param {Function} | ||
* | ||
* @returns {Array} | ||
*/ | ||
uniqueBy(selector) { | ||
const exists = new Set(); | ||
return this.reject((item, index) => { | ||
const id = selector(item, index, this); | ||
if (exists.has(id)) { | ||
return true; | ||
} | ||
exists.add(id); | ||
}); | ||
} | ||
/** | ||
* Add one or more items to the beginning of the array. | ||
@@ -515,0 +554,0 @@ * |
{ | ||
"name": "@supercharge/arrays", | ||
"description": "Array utilities for Node.js and JavaScript", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"author": "Marcus Pöhls <marcus@superchargejs.com>", | ||
@@ -10,8 +10,8 @@ "bugs": { | ||
"devDependencies": { | ||
"@supercharge/eslint-config-typescript": "~2.3.0", | ||
"@supercharge/eslint-config-typescript": "~2.3.1", | ||
"@supercharge/tsconfig": "~4.0.0", | ||
"c8": "~7.12.0", | ||
"eslint": "~8.21.0", | ||
"expect": "~28.1.3", | ||
"typescript": "~4.7.4", | ||
"eslint": "~8.26.0", | ||
"expect": "~29.2.2", | ||
"typescript": "~4.8.4", | ||
"uvu": "~0.5.6" | ||
@@ -18,0 +18,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33077
1015