@supercharge/collections
Advanced tools
Comparing version 2.2.0 to 2.3.0
# Changelog | ||
## [2.3.0](https://github.com/supercharge/collections/compare/v2.2.0...v2.3.0) - 2020-07-19 | ||
### Added | ||
- `flatten()` method: flatten the collection one level deep | ||
- typed collections: improved TypeScript type definitions enable IntelliSense (when possible) inside callback methods | ||
## [2.2.0](https://github.com/supercharge/collections/compare/v2.1.0...v2.2.0) - 2020-07-02 | ||
@@ -4,0 +11,0 @@ |
@@ -1,2 +0,2 @@ | ||
export declare class CollectionProxy { | ||
export declare class CollectionProxy<T> { | ||
/** | ||
@@ -17,3 +17,3 @@ * Stores the list of items in the collection. | ||
*/ | ||
constructor(items: any, callChain?: any[]); | ||
constructor(items: T[], callChain?: any[]); | ||
/** | ||
@@ -28,3 +28,3 @@ * Alias for the `.some` method. This function determines | ||
*/ | ||
any(callback: Function): Promise<boolean>; | ||
any(callback: (value: T, index: number, items: T[]) => unknown | Promise<unknown>): Promise<boolean>; | ||
/** | ||
@@ -50,3 +50,3 @@ * Returns the average of all collection items | ||
*/ | ||
clone(): CollectionProxy; | ||
clone(): CollectionProxy<T>; | ||
/** | ||
@@ -59,5 +59,4 @@ * Collapse a collection of arrays into a single, flat collection. | ||
/** | ||
* Removes all falsey values from the given `array`. | ||
* Falsey values are `null`, `undefined`, `''`, | ||
* `false`, `0`, `NaN`. | ||
* Removes all falsy values from the given `array`. Falsy values | ||
* are `null`, `undefined`, `''`, `false`, `0`, `-0`, `0n`, `NaN`. | ||
* | ||
@@ -75,3 +74,3 @@ * @returns {CollectionProxy} | ||
*/ | ||
concat(...items: any[]): CollectionProxy; | ||
concat(...items: T[]): CollectionProxy<T>; | ||
/** | ||
@@ -94,3 +93,3 @@ * Counts the items in the collection. By default, it behaves like an alias | ||
*/ | ||
diff(items: any[]): this; | ||
diff(items: T[]): this; | ||
/** | ||
@@ -105,3 +104,3 @@ * Asynchronous version of `Array#every()`, running the (async) testing | ||
*/ | ||
every(callback: Function): Promise<boolean>; | ||
every(callback: (value: T, index: number, items: T[]) => unknown | Promise<unknown>): Promise<boolean>; | ||
/** | ||
@@ -112,7 +111,7 @@ * Asynchronous version of Array#filter(), running the (async) testing | ||
* | ||
* @param {Function} callback | ||
* @param {Function} predicate | ||
* | ||
* @returns {Array} | ||
*/ | ||
filter(callback: Function): this; | ||
filter(predicate: (value: T, index: number, items: T[]) => unknown | Promise<unknown>): this; | ||
/** | ||
@@ -127,3 +126,3 @@ * A variant of the `filter` method running the (async) testing | ||
*/ | ||
filterIf(condition: boolean, callback: Function): this; | ||
filterIf(condition: boolean, callback: (value: T, index: number, items: T[]) => unknown | Promise<unknown>): this; | ||
/** | ||
@@ -134,7 +133,7 @@ * Asynchronous version of Array#find(), running the (async) testing | ||
* | ||
* @param {Function} callback | ||
* @param {Function} predicate | ||
* | ||
* @returns {*} the found value | ||
*/ | ||
find(callback: Function): Promise<any>; | ||
find<S extends T>(predicate: (value: T, index: number, items: T[]) => value is S): Promise<any>; | ||
/** | ||
@@ -149,4 +148,10 @@ * Alias for Array#find. Returns the first item in | ||
*/ | ||
first(callback: Function): Promise<any>; | ||
first<S extends T>(predicate: (value: T, index: number, items: T[]) => value is S): Promise<any>; | ||
/** | ||
* Flattens the collection one level deep. | ||
* | ||
* @returns {CollectionProxy} | ||
*/ | ||
flatten(): this; | ||
/** | ||
* Asynchronous version of Array#flatMap(). It invokes the `callback` | ||
@@ -161,3 +166,3 @@ * on each collection item. The callback can modify and return the | ||
*/ | ||
flatMap(callback: Function): this; | ||
flatMap<R>(callback: (value: T, index: number, items: T[]) => R[] | Promise<R[]>): CollectionProxy<any>; | ||
/** | ||
@@ -169,3 +174,3 @@ * Asynchrounous version of Array#forEach(), running the given | ||
*/ | ||
forEach(callback: Function): Promise<void>; | ||
forEach(callback: (value: T, index: number, items: T[]) => void | Promise<void>): Promise<void>; | ||
/** | ||
@@ -188,3 +193,3 @@ * Group the collection items into arrays using the given `key`. | ||
*/ | ||
has(callback: Function): Promise<boolean>; | ||
has(callback: (value: T, index: number, items: T[]) => boolean | Promise<boolean>): Promise<boolean>; | ||
/** | ||
@@ -203,3 +208,3 @@ * Returns `true` when the collection contains duplicate items, `false` otherwise. | ||
*/ | ||
intersect(items: any[]): this; | ||
intersect(items: T[]): this; | ||
/** | ||
@@ -227,5 +232,4 @@ * Returns `true` when the collection is empty, `false` otherwise. | ||
/** | ||
* Returns the last item in the collection | ||
* that satisfies the `callback` testing | ||
* function, `undefined` otherwise. | ||
* Returns the last item in the collection that satisfies the | ||
* `predicate` testing function, `undefined` otherwise. | ||
* | ||
@@ -236,3 +240,3 @@ * @param {Function} callback | ||
*/ | ||
last(callback: Function): Promise<any>; | ||
last<S extends T>(predicate: (value: T, index: number, items: T[]) => value is S): Promise<S>; | ||
/** | ||
@@ -247,3 +251,3 @@ * Asynchronous version of Array#map(), running all transformations | ||
*/ | ||
map(callback: Function): this; | ||
map<R>(callback: (value: T, index: number, items: T[]) => R[]): CollectionProxy<any>; | ||
/** | ||
@@ -288,3 +292,3 @@ * Returns the max value in the collection. | ||
*/ | ||
push(...items: any[]): this; | ||
push(...items: T[]): this; | ||
/** | ||
@@ -300,3 +304,3 @@ * Asynchronous version of Array#reduce(). It invokes the `reducer` | ||
*/ | ||
reduce(reducer: Function, initial: any): Promise<any>; | ||
reduce<R>(reducer: (carry: R, currentValue: T, currentIndex: number, items: T[]) => R | Promise<R>, initial: R): Promise<R>; | ||
/** | ||
@@ -312,3 +316,3 @@ * Asynchronous version of Array#reduceRight(). It invokes the `reducer` | ||
*/ | ||
reduceRight(reducer: Function, initial: any): Promise<any>; | ||
reduceRight<R>(reducer: (carry: R, currentValue: T, currentIndex: number, items: T[]) => R | Promise<R>, initial: R): Promise<R>; | ||
/** | ||
@@ -319,7 +323,7 @@ * Inverse of Array#filter(), **removing** all items satisfying the `callback` | ||
* | ||
* @param {Function} callback | ||
* @param {Function} predicate | ||
* | ||
* @returns {Array} | ||
*/ | ||
reject(callback: Function): this; | ||
reject(predicate: (value: T, index: number, items: T[]) => unknown | Promise<unknown>): this; | ||
/** | ||
@@ -331,3 +335,3 @@ * Returns a reversed collection. The first item becomes the last one, | ||
*/ | ||
reverse(): CollectionProxy; | ||
reverse(): CollectionProxy<T>; | ||
/** | ||
@@ -338,3 +342,3 @@ * Removes and returns the first item from the collection. | ||
*/ | ||
shift(): Promise<any>; | ||
shift(): Promise<T>; | ||
/** | ||
@@ -368,3 +372,3 @@ * Returns the number of items in the collection. | ||
*/ | ||
splice(start: number, limit: number, ...inserts: any[]): CollectionProxy; | ||
splice(start: number, limit: number, ...inserts: T[]): CollectionProxy<T>; | ||
/** | ||
@@ -379,3 +383,3 @@ * Asynchronous version of `Array#some()`, running the (async) testing function | ||
*/ | ||
some(callback: Function): Promise<boolean>; | ||
some(callback: (value: T, index: number, items: T[]) => unknown | Promise<unknown>): Promise<boolean>; | ||
/** | ||
@@ -388,3 +392,3 @@ * Returns a sorted list of all collection items, with an optional comparator | ||
*/ | ||
sort(comparator: Function): CollectionProxy; | ||
sort(comparator: (a: T, b: T) => number): CollectionProxy<T>; | ||
/** | ||
@@ -404,3 +408,3 @@ * Returns the sum of all collection items. | ||
*/ | ||
take(limit: number): CollectionProxy; | ||
take(limit: number): CollectionProxy<T>; | ||
/** | ||
@@ -414,3 +418,3 @@ * Take and remove `limit` items from the | ||
*/ | ||
takeAndRemove(limit: number): CollectionProxy; | ||
takeAndRemove(limit: number): CollectionProxy<T>; | ||
/** | ||
@@ -421,3 +425,3 @@ * Tap into the chain, run the given `callback` and retreive the original value. | ||
*/ | ||
tap(callback: Function): this; | ||
tap(callback: (item: T) => void): this; | ||
/** | ||
@@ -436,3 +440,3 @@ * Returns JSON representation of collection | ||
*/ | ||
union(items: any): CollectionProxy; | ||
union(items: T[]): CollectionProxy<T>; | ||
/** | ||
@@ -451,3 +455,3 @@ * Returns all the unique items in the collection. | ||
*/ | ||
unshift(...items: any[]): this; | ||
unshift(...items: T[]): this; | ||
/** | ||
@@ -454,0 +458,0 @@ * Enqueues an operation in the collection pipeline |
@@ -67,5 +67,4 @@ 'use strict'; | ||
/** | ||
* Removes all falsey values from the given `array`. | ||
* Falsey values are `null`, `undefined`, `''`, | ||
* `false`, `0`, `NaN`. | ||
* Removes all falsy values from the given `array`. Falsy values | ||
* are `null`, `undefined`, `''`, `false`, `0`, `-0`, `0n`, `NaN`. | ||
* | ||
@@ -127,8 +126,8 @@ * @returns {CollectionProxy} | ||
* | ||
* @param {Function} callback | ||
* @param {Function} predicate | ||
* | ||
* @returns {Array} | ||
*/ | ||
filter(callback) { | ||
return this.enqueue('filter', callback); | ||
filter(predicate) { | ||
return this.enqueue('filter', predicate); | ||
} | ||
@@ -152,8 +151,8 @@ /** | ||
* | ||
* @param {Function} callback | ||
* @param {Function} predicate | ||
* | ||
* @returns {*} the found value | ||
*/ | ||
async find(callback) { | ||
return this.enqueue('find', callback); | ||
async find(predicate) { | ||
return this.enqueue('find', predicate); | ||
} | ||
@@ -169,6 +168,14 @@ /** | ||
*/ | ||
async first(callback) { | ||
return this.enqueue('first', callback).all(); | ||
async first(predicate) { | ||
return this.enqueue('first', predicate).all(); | ||
} | ||
/** | ||
* Flattens the collection one level deep. | ||
* | ||
* @returns {CollectionProxy} | ||
*/ | ||
flatten() { | ||
return this.enqueue('collapse'); | ||
} | ||
/** | ||
* Asynchronous version of Array#flatMap(). It invokes the `callback` | ||
@@ -263,5 +270,4 @@ * on each collection item. The callback can modify and return the | ||
/** | ||
* Returns the last item in the collection | ||
* that satisfies the `callback` testing | ||
* function, `undefined` otherwise. | ||
* Returns the last item in the collection that satisfies the | ||
* `predicate` testing function, `undefined` otherwise. | ||
* | ||
@@ -272,4 +278,4 @@ * @param {Function} callback | ||
*/ | ||
async last(callback) { | ||
return this.enqueue('last', callback).all(); | ||
async last(predicate) { | ||
return this.enqueue('last', predicate).all(); | ||
} | ||
@@ -373,8 +379,8 @@ /** | ||
* | ||
* @param {Function} callback | ||
* @param {Function} predicate | ||
* | ||
* @returns {Array} | ||
*/ | ||
reject(callback) { | ||
return this.enqueue('reject', callback); | ||
reject(predicate) { | ||
return this.enqueue('reject', predicate); | ||
} | ||
@@ -518,3 +524,3 @@ /** | ||
union(items) { | ||
return this.concat(items).unique(); | ||
return this.concat(...items).unique(); | ||
} | ||
@@ -521,0 +527,0 @@ /** |
@@ -10,3 +10,3 @@ import { CollectionProxy } from './collection-proxy'; | ||
*/ | ||
declare const collect: (collection: any) => CollectionProxy; | ||
declare const collect: <T>(collection: T[]) => CollectionProxy<T>; | ||
export = collect; |
{ | ||
"name": "@supercharge/collections", | ||
"description": "Supercharge collections", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"author": "Marcus Pöhls <marcus@superchargejs.com>", | ||
@@ -61,2 +61,3 @@ "bugs": { | ||
"build": "tsc", | ||
"dev": "tsc --watch", | ||
"lint": "eslint src --ext .js,.ts", | ||
@@ -63,0 +64,0 @@ "lint:fix": "eslint src --ext .js,.ts --fix", |
71524
2087