@supercharge/collections
Advanced tools
Comparing version 1.12.2 to 1.13.0
# Changelog | ||
## [1.13.0](https://github.com/supercharge/collections/compare/v1.12.2...v1.13.0) - 2020-03-26 | ||
### Updated | ||
- `unique` supports a string or callback function as a parameter to identify unique values | ||
## [1.12.2](https://github.com/supercharge/collections/compare/v1.12.1...v1.12.2) - 2020-03-12 | ||
@@ -4,0 +10,0 @@ |
{ | ||
"name": "@supercharge/collections", | ||
"description": "Supercharge collections", | ||
"version": "1.12.2", | ||
"version": "1.13.0", | ||
"author": "Marcus Pöhls <marcus@futurestud.io>", | ||
@@ -6,0 +6,0 @@ "bugs": { |
@@ -693,6 +693,8 @@ 'use strict' | ||
* | ||
* @param {String|Function} | ||
* | ||
* @returns {CollectionProxy} | ||
*/ | ||
unique () { | ||
return this._enqueue('unique') | ||
unique (key) { | ||
return this._enqueue('unique', null, key) | ||
} | ||
@@ -699,0 +701,0 @@ |
@@ -717,11 +717,45 @@ 'use strict' | ||
* | ||
* @param {String|Function} | ||
* | ||
* @returns {Array} | ||
*/ | ||
unique () { | ||
return Array.from( | ||
new Set(this.items) | ||
) | ||
async unique (key) { | ||
if (!key) { | ||
return Array.from( | ||
new Set(this.items) | ||
) | ||
} | ||
const exists = new Set() | ||
const callback = this.valueRetriever(key) | ||
return this.reject(async item => { | ||
const id = await callback(item) | ||
if (exists.has(id)) { | ||
return true | ||
} | ||
exists.add(id) | ||
}) | ||
} | ||
/** | ||
* Create a value receiving callback. | ||
* | ||
* @param {*} value | ||
* | ||
* @returns {Function} | ||
*/ | ||
valueRetriever (value) { | ||
if (typeof value === 'function') { | ||
return value | ||
} | ||
return function (item) { | ||
return item[value] | ||
} | ||
} | ||
/** | ||
* Add one or more items to the beginning of the collection. | ||
@@ -728,0 +762,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
45827
1363