@supercharge/collections
Advanced tools
Comparing version 1.10.0 to 1.11.0
# Changelog | ||
## [1.10.0](https://github.com/superchargejs/collections/compare/v1.9.1...v1.10.0) - 2020-01-22 | ||
## [1.11.0](https://github.com/supercharge/collections/compare/v1.10.0...v1.11.0) - 2020-01-30 | ||
### Added | ||
- `.groupBy()` method | ||
### Updated | ||
- bump deps | ||
## [1.10.0](https://github.com/supercharge/collections/compare/v1.9.1...v1.10.0) - 2020-01-22 | ||
### Added | ||
- `.tap()` method | ||
@@ -13,3 +22,3 @@ - `.hasDuplicates()` method | ||
## [1.9.1](https://github.com/superchargejs/collections/compare/v1.9.0...v1.9.1) - 2020-01-07 | ||
## [1.9.1](https://github.com/supercharge/collections/compare/v1.9.0...v1.9.1) - 2020-01-07 | ||
@@ -27,3 +36,3 @@ ### Added | ||
## [1.9.0](https://github.com/superchargejs/collections/compare/v1.8.0...v1.9.0) - 2019-10-26 | ||
## [1.9.0](https://github.com/supercharge/collections/compare/v1.8.0...v1.9.0) - 2019-10-26 | ||
@@ -34,3 +43,3 @@ ### Added | ||
## [1.8.0](https://github.com/superchargejs/collections/compare/v1.7.0...v1.8.0) - 2019-10-24 | ||
## [1.8.0](https://github.com/supercharge/collections/compare/v1.7.0...v1.8.0) - 2019-10-24 | ||
@@ -44,3 +53,3 @@ ### Added | ||
## [1.7.0](https://github.com/superchargejs/collections/compare/v1.6.0...v1.7.0) - 2019-10-11 | ||
## [1.7.0](https://github.com/supercharge/collections/compare/v1.6.0...v1.7.0) - 2019-10-11 | ||
@@ -51,3 +60,3 @@ ### Added | ||
## [1.6.0](https://github.com/superchargejs/collections/compare/v1.5.1...v1.6.0) - 2019-10-10 | ||
## [1.6.0](https://github.com/supercharge/collections/compare/v1.5.1...v1.6.0) - 2019-10-10 | ||
@@ -71,3 +80,3 @@ ### Added | ||
## [1.5.1](https://github.com/superchargejs/collections/compare/v1.5.0...v1.5.1) - 2019-09-24 | ||
## [1.5.1](https://github.com/supercharge/collections/compare/v1.5.0...v1.5.1) - 2019-09-24 | ||
@@ -79,3 +88,3 @@ ### Updated | ||
## [1.5.0](https://github.com/superchargejs/collections/compare/v1.4.1...v1.5.0) - 2019-09-23 | ||
## [1.5.0](https://github.com/supercharge/collections/compare/v1.4.1...v1.5.0) - 2019-09-23 | ||
@@ -89,3 +98,3 @@ ### Added | ||
## [1.4.1](https://github.com/superchargejs/collections/compare/v1.4.0...v1.4.1) - 2019-07-25 | ||
## [1.4.1](https://github.com/supercharge/collections/compare/v1.4.0...v1.4.1) - 2019-07-25 | ||
@@ -96,3 +105,3 @@ ### Fixed | ||
## [1.4.0](https://github.com/superchargejs/collections/compare/v1.3.0...v1.4.0) - 2019-07-24 | ||
## [1.4.0](https://github.com/supercharge/collections/compare/v1.3.0...v1.4.0) - 2019-07-24 | ||
@@ -107,3 +116,3 @@ ### Added | ||
## [1.3.0](https://github.com/superchargejs/collections/compare/v1.2.1...v1.3.0) - 2019-07-23 | ||
## [1.3.0](https://github.com/supercharge/collections/compare/v1.2.1...v1.3.0) - 2019-07-23 | ||
@@ -116,3 +125,3 @@ ### Added | ||
## [1.2.1](https://github.com/superchargejs/collections/compare/v1.2.0...v1.2.1) - 2019-07-19 | ||
## [1.2.1](https://github.com/supercharge/collections/compare/v1.2.0...v1.2.1) - 2019-07-19 | ||
@@ -123,3 +132,3 @@ ### Updated | ||
## [1.2.0](https://github.com/superchargejs/collections/compare/v1.1.0...v1.2.0) - 2019-07-19 | ||
## [1.2.0](https://github.com/supercharge/collections/compare/v1.1.0...v1.2.0) - 2019-07-19 | ||
@@ -142,3 +151,3 @@ ### Added | ||
## [1.1.0](https://github.com/superchargejs/collections/compare/v1.0.0...v1.1.0) - 2019-07-15 | ||
## [1.1.0](https://github.com/supercharge/collections/compare/v1.0.0...v1.1.0) - 2019-07-15 | ||
@@ -145,0 +154,0 @@ ### Added |
{ | ||
"name": "@supercharge/collections", | ||
"description": "Supercharge collections", | ||
"version": "1.10.0", | ||
"version": "1.11.0", | ||
"author": "Marcus Pöhls <marcus@futurestud.io>", | ||
"bugs": { | ||
"url": "https://github.com/superchargejs/collections/issues" | ||
"url": "https://github.com/supercharge/collections/issues" | ||
}, | ||
@@ -21,3 +21,3 @@ "dependencies": { | ||
"eslint-plugin-standard": "~4.0.1", | ||
"sinon": "~8.1.0" | ||
"sinon": "~8.1.1" | ||
}, | ||
@@ -31,3 +31,3 @@ "engines": { | ||
], | ||
"homepage": "https://github.com/superchargejs/collections", | ||
"homepage": "https://github.com/supercharge/collections", | ||
"keywords": [ | ||
@@ -55,3 +55,3 @@ "async/await", | ||
"type": "git", | ||
"url": "git+https://github.com/superchargejs/collections.git" | ||
"url": "git+https://github.com/supercharge/collections.git" | ||
}, | ||
@@ -58,0 +58,0 @@ "scripts": { |
@@ -99,3 +99,3 @@ <div align="center"> | ||
> [superchargejs.com](https://superchargejs.com) · | ||
> GitHub [@superchargejs](https://github.com/superchargejs/) · | ||
> GitHub [@supercharge](https://github.com/supercharge/) · | ||
> Twitter [@superchargejs](https://twitter.com/superchargejs) |
@@ -13,2 +13,13 @@ 'use strict' | ||
/** | ||
* Returns the average of all collection items | ||
* | ||
* @returns {Number} | ||
*/ | ||
async avg () { | ||
return this.all( | ||
this._enqueue('avg') | ||
) | ||
} | ||
/** | ||
* Breaks the collection into multiple, smaller collections | ||
@@ -70,2 +81,13 @@ * of the given `size`. | ||
/** | ||
* Removes all values from the collection that are present in the given array. | ||
* | ||
* @param {*} items | ||
* | ||
* @returns {CollectionProxy} | ||
*/ | ||
diff (items) { | ||
return this._enqueue('diff', null, { items }) | ||
} | ||
/** | ||
* Asynchrounous version of Array#every(). Checks whether | ||
@@ -79,3 +101,3 @@ * the `callback` returns `true` for all items in the | ||
*/ | ||
every (callback) { | ||
async every (callback) { | ||
return this.all( | ||
@@ -95,3 +117,3 @@ this._enqueue('every', callback) | ||
*/ | ||
everySeries (callback) { | ||
async everySeries (callback) { | ||
return this.all( | ||
@@ -137,3 +159,3 @@ this._enqueue('everySeries', callback) | ||
*/ | ||
find (callback) { | ||
async find (callback) { | ||
return this.all( | ||
@@ -153,3 +175,3 @@ this._enqueue('find', callback) | ||
*/ | ||
findSeries (callback) { | ||
async findSeries (callback) { | ||
return this.all( | ||
@@ -169,3 +191,3 @@ this._enqueue('findSeries', callback) | ||
*/ | ||
first (callback) { | ||
async first (callback) { | ||
return this.all( | ||
@@ -197,3 +219,3 @@ this._enqueue('first', callback) | ||
*/ | ||
forEach (callback) { | ||
async forEach (callback) { | ||
return this.all( | ||
@@ -211,3 +233,3 @@ this._enqueue('forEach', callback) | ||
*/ | ||
forEachSeries (callback) { | ||
async forEachSeries (callback) { | ||
return this.all( | ||
@@ -219,2 +241,15 @@ this._enqueue('forEachSeries', callback) | ||
/** | ||
* Group the collection items into arrays using the given `key`. | ||
* | ||
* @param {String} key | ||
* | ||
* @returns {Object} | ||
*/ | ||
async groupBy (key) { | ||
return this.all( | ||
this._enqueue('groupBy', null, key) | ||
) | ||
} | ||
/** | ||
* Returns `true` when the collection satisfies the given | ||
@@ -227,3 +262,3 @@ * `callback` testing function, `false` otherwise. | ||
*/ | ||
has (callback) { | ||
async has (callback) { | ||
return this.all( | ||
@@ -235,2 +270,13 @@ this._enqueue('has', callback) | ||
/** | ||
* Returns `true` when the collection contains duplicate items, `false` otherwise. | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
async hasDuplicates () { | ||
return this.all( | ||
this._enqueue('hasDuplicates') | ||
) | ||
} | ||
/** | ||
* Creates an array of unique values that are included in both given array | ||
@@ -251,3 +297,3 @@ * | ||
*/ | ||
isEmpty () { | ||
async isEmpty () { | ||
return this.all( | ||
@@ -263,3 +309,3 @@ this._enqueue('isEmpty') | ||
*/ | ||
isNotEmpty () { | ||
async isNotEmpty () { | ||
return this.all( | ||
@@ -275,3 +321,3 @@ this._enqueue('isNotEmpty') | ||
*/ | ||
join (separator) { | ||
async join (separator) { | ||
return this.all( | ||
@@ -291,3 +337,3 @@ this._enqueue('join', null, separator) | ||
*/ | ||
last (callback) { | ||
async last (callback) { | ||
return this.all( | ||
@@ -325,2 +371,35 @@ this._enqueue('last', callback) | ||
/** | ||
* Returns the max value in the collection. | ||
* | ||
* @returns {Number} | ||
*/ | ||
async max () { | ||
return this.all( | ||
this._enqueue('max') | ||
) | ||
} | ||
/** | ||
* Returns median of the current collection | ||
* | ||
* @returns {Number} | ||
*/ | ||
async median () { | ||
return this.all( | ||
this._enqueue('median') | ||
) | ||
} | ||
/** | ||
* Returns the min value in the collection. | ||
* | ||
* @returns {Number} | ||
*/ | ||
async min () { | ||
return this.all( | ||
this._enqueue('min') | ||
) | ||
} | ||
/** | ||
* Add one or more items to the end of the collection. | ||
@@ -361,3 +440,3 @@ * | ||
*/ | ||
reduce (reducer, initial) { | ||
async reduce (reducer, initial) { | ||
return this.all( | ||
@@ -378,3 +457,3 @@ this._enqueue('reduce', reducer, initial) | ||
*/ | ||
reduceRight (reducer, initial) { | ||
async reduceRight (reducer, initial) { | ||
return this.all( | ||
@@ -412,2 +491,12 @@ this._enqueue('reduceRight', reducer, initial) | ||
/** | ||
* Returns a reversed collection. The first item becomes the last one, | ||
* the second item becomes the second to last, and so on. | ||
* | ||
* @returns {CollectionProxy} | ||
*/ | ||
reverse () { | ||
return this.clone()._enqueue('reverse') | ||
} | ||
/** | ||
* Removes and returns the first item from the collection. | ||
@@ -430,3 +519,3 @@ * | ||
*/ | ||
size () { | ||
async size () { | ||
return this.all( | ||
@@ -438,24 +527,2 @@ this._enqueue('size') | ||
/** | ||
* Returns the max value in the collection. | ||
* | ||
* @returns {Number} | ||
*/ | ||
max () { | ||
return this.all( | ||
this._enqueue('max') | ||
) | ||
} | ||
/** | ||
* Removes all values from the collection that are present in the given array. | ||
* | ||
* @param {*} items | ||
* | ||
* @returns {CollectionProxy} | ||
*/ | ||
diff (items) { | ||
return this._enqueue('diff', null, { items }) | ||
} | ||
/** | ||
* Returns a chunk of items beginning at the `start` | ||
@@ -494,12 +561,2 @@ * index without removing them from the collectin. | ||
/** | ||
* Returns a reversed collection. The first item becomes the last one, | ||
* the second item becomes the second to last, and so on. | ||
* | ||
* @returns {CollectionProxy} | ||
*/ | ||
reverse () { | ||
return this.clone()._enqueue('reverse') | ||
} | ||
/** | ||
* Asynchronous version of `Array#some()`. This function | ||
@@ -513,3 +570,3 @@ * tests whether at least one element in the `array` | ||
*/ | ||
some (callback) { | ||
async some (callback) { | ||
return this.all( | ||
@@ -529,3 +586,3 @@ this._enqueue('some', callback) | ||
*/ | ||
someSeries (callback) { | ||
async someSeries (callback) { | ||
return this.all( | ||
@@ -552,3 +609,3 @@ this._enqueue('someSeries', callback) | ||
*/ | ||
sum () { | ||
async sum () { | ||
return this.all( | ||
@@ -592,2 +649,11 @@ this._enqueue('sum') | ||
/** | ||
* Tap into the chain, run the given `callback` and retreive the original value. | ||
* | ||
* @returns {CollectionProxy} | ||
*/ | ||
tap (callback) { | ||
return this._enqueue('tap', callback) | ||
} | ||
/** | ||
* Returns JSON representation of collection | ||
@@ -597,3 +663,3 @@ * | ||
*/ | ||
toJSON () { | ||
async toJSON () { | ||
return this.all( | ||
@@ -605,19 +671,19 @@ this._enqueue('toJSON') | ||
/** | ||
* Returns all the unique items in the collection. | ||
* Creates an array of unique values, in order, from all given arrays. | ||
* | ||
* @param {Array} items | ||
* | ||
* @returns {CollectionProxy} | ||
*/ | ||
unique () { | ||
return this._enqueue('unique') | ||
union (items) { | ||
return this.concat(items).unique() | ||
} | ||
/** | ||
* Creates an array of unique values, in order, from all given arrays. | ||
* Returns all the unique items in the collection. | ||
* | ||
* @param {Array} items | ||
* | ||
* @returns {CollectionProxy} | ||
*/ | ||
union (items) { | ||
return this.concat(items).unique() | ||
unique () { | ||
return this._enqueue('unique') | ||
} | ||
@@ -635,55 +701,2 @@ | ||
/** | ||
* Returns the min value in the collection. | ||
* | ||
* @returns {Number} | ||
*/ | ||
min () { | ||
return this.all( | ||
this._enqueue('min') | ||
) | ||
} | ||
/** | ||
* Returns the average of all collection items | ||
* | ||
* @returns {Number} | ||
*/ | ||
avg () { | ||
return this.all( | ||
this._enqueue('avg') | ||
) | ||
} | ||
/** | ||
* Returns median of the current collection | ||
* | ||
* @returns {Number} | ||
*/ | ||
median () { | ||
return this.all( | ||
this._enqueue('median') | ||
) | ||
} | ||
/** | ||
* Returns `true` when the collection contains duplicate items, `false` otherwise. | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
async hasDuplicates () { | ||
return this.all( | ||
this._enqueue('hasDuplicates') | ||
) | ||
} | ||
/** | ||
* Tap into the chain, run the given `callback` and retreive the original value. | ||
* | ||
* @returns {CollectionProxy} | ||
*/ | ||
tap (callback) { | ||
return this._enqueue('tap', callback) | ||
} | ||
/** | ||
* Enqueues an operation in the collection pipeline | ||
@@ -690,0 +703,0 @@ * for processing at a later time. |
@@ -19,2 +19,11 @@ 'use strict' | ||
/** | ||
* Returns the average of all collection items. | ||
* | ||
* @returns {Number} | ||
* */ | ||
async avg () { | ||
return await this.sum() / this.size() | ||
} | ||
/** | ||
* Breaks the collection into multiple, smaller collections | ||
@@ -73,2 +82,13 @@ * of the given `size`. | ||
/** | ||
* Removes all values from the collection that are present in the given array. | ||
* | ||
* @param {*} items | ||
* | ||
* @returns {CollectionProxy} | ||
*/ | ||
diff ({ items }) { | ||
return this.items.filter(item => !items.includes(item)) | ||
} | ||
/** | ||
* Asynchrounous version of Array#every(). Checks whether | ||
@@ -221,2 +241,27 @@ * the `callback` returns `true` for all items in the | ||
/** | ||
* Group the collection items into arrays using the given `key`. | ||
* | ||
* @param {String} key | ||
* | ||
* @returns {Object} | ||
*/ | ||
async groupBy (key) { | ||
if (key.includes('.')) { | ||
throw new Error('We do not support nested grouping yet. Please send a PR for that feature?') | ||
} | ||
return this.reduce((carry, item) => { | ||
const group = item[key] || '' | ||
if (carry[group] === undefined) { | ||
carry[group] = [] | ||
} | ||
carry[group].push(item) | ||
return carry | ||
}, {}) | ||
} | ||
/** | ||
* Returns `true` when the collection satisfies the given | ||
@@ -238,2 +283,11 @@ * `callback` testing function, `false` otherwise. | ||
/** | ||
* Returns `true` when the collection contains duplicate items, `false` otherwise. | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
async hasDuplicates () { | ||
return (new Set(this.items)).size !== this.size() | ||
} | ||
/** | ||
* Creates an array of unique values that are included in both given array. | ||
@@ -337,2 +391,37 @@ * | ||
/** | ||
* Returns the max value in the collection. | ||
* | ||
* @returns {Number} | ||
*/ | ||
max () { | ||
return Math.max(...this.items) | ||
} | ||
/** | ||
* Returns median of the current collection. | ||
* | ||
* @param {} | ||
* | ||
* @returns {Number} | ||
*/ | ||
median () { | ||
this.sort((a, b) => a - b) | ||
const mid = Math.floor(this.size() / 2) | ||
return this.size() % 2 !== 0 | ||
? this.items[mid] | ||
: (this.items[mid] + this.items[(mid - 1)]) / 2 | ||
} | ||
/** | ||
* Returns the min value in the collection. | ||
* | ||
* @returns {Number} | ||
*/ | ||
min () { | ||
return Math.min(...this.items) | ||
} | ||
/** | ||
* Add one or more items to the end of the colleciton. | ||
@@ -430,2 +519,13 @@ * | ||
/** | ||
* Returns reversed version of original collection. | ||
* | ||
* @returns {Array} | ||
*/ | ||
reverse () { | ||
this.items.reverse() | ||
return this.items | ||
} | ||
/** | ||
* Removes and returns the first item from the collection. | ||
@@ -449,31 +549,2 @@ * | ||
/** | ||
* Returns the min value in the collection. | ||
* | ||
* @returns {Number} | ||
*/ | ||
min () { | ||
return Math.min(...this.items) | ||
} | ||
/** | ||
* Returns the max value in the collection. | ||
* | ||
* @returns {Number} | ||
*/ | ||
max () { | ||
return Math.max(...this.items) | ||
} | ||
/** | ||
* Removes all values from the collection that are present in the given array. | ||
* | ||
* @param {*} items | ||
* | ||
* @returns {CollectionProxy} | ||
*/ | ||
diff ({ items }) { | ||
return this.items.filter(item => !items.includes(item)) | ||
} | ||
/** | ||
* Returns a chunk of items beginning at the `start` | ||
@@ -513,13 +584,2 @@ * index without removing them from the collection. | ||
/** | ||
* Returns reversed version of original collection. | ||
* | ||
* @returns {Array} | ||
*/ | ||
reverse () { | ||
this.items.reverse() | ||
return this.items | ||
} | ||
/** | ||
* Asynchronous version of `Array#some()`. This function | ||
@@ -566,19 +626,2 @@ * tests whether at least one element in the `array` | ||
/** | ||
* Returns median of the current collection. | ||
* | ||
* @param {} | ||
* | ||
* @returns {Number} | ||
*/ | ||
median () { | ||
this.sort((a, b) => a - b) | ||
const mid = Math.floor(this.size() / 2) | ||
return this.size() % 2 !== 0 | ||
? this.items[mid] | ||
: (this.items[mid] + this.items[(mid - 1)]) / 2 | ||
} | ||
/** | ||
* Returns the sum of all collection items. | ||
@@ -609,2 +652,13 @@ * | ||
/** | ||
* Tap into the chain, run the given `callback` and retreive the original value. | ||
* | ||
* @returns {Number} | ||
*/ | ||
async tap (callback) { | ||
await this.forEach(callback) | ||
return this | ||
} | ||
/** | ||
* Returns JSON representation of collection. | ||
@@ -639,33 +693,4 @@ * | ||
} | ||
/** | ||
* Returns the average of all collection items. | ||
* | ||
* @returns {Number} | ||
* */ | ||
async avg () { | ||
return await this.sum() / this.size() | ||
} | ||
/** | ||
* Tap into the chain, run the given `callback` and retreive the original value. | ||
* | ||
* @returns {Number} | ||
*/ | ||
async tap (callback) { | ||
await this.forEach(callback) | ||
return this | ||
} | ||
/** | ||
* Returns `true` when the collection contains duplicate items, `false` otherwise. | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
async hasDuplicates () { | ||
return (new Set(this.items)).size !== this.size() | ||
} | ||
} | ||
module.exports = Collection |
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
42488
1253