@supercharge/collections
Advanced tools
Comparing version 1.7.0 to 1.8.0
# Changelog | ||
## [1.8.0](https://github.com/superchargejs/collections/compare/v1.7.0...v1.8.0) - 2019-10-24 | ||
### Added | ||
- `.last()` method | ||
### Updated | ||
- bump deps | ||
## [1.7.0](https://github.com/superchargejs/collections/compare/v1.6.0...v1.7.0) - 2019-10-11 | ||
@@ -5,0 +14,0 @@ |
{ | ||
"name": "@supercharge/collections", | ||
"description": "Supercharge collections", | ||
"version": "1.7.0", | ||
"version": "1.8.0", | ||
"author": "Marcus Pöhls <marcus@futurestud.io>", | ||
@@ -13,4 +13,4 @@ "bugs": { | ||
"devDependencies": { | ||
"@hapi/code": "~6.0.0", | ||
"@hapi/lab": "~20.4.0", | ||
"@hapi/code": "~7.0.0", | ||
"@hapi/lab": "~21.0.0", | ||
"eslint": "~6.5.1", | ||
@@ -17,0 +17,0 @@ "eslint-config-standard": "~14.1.0", |
@@ -24,2 +24,3 @@ <div align="center"> | ||
<a href="https://www.npmjs.com/package/@supercharge/collections"><img src="https://img.shields.io/npm/v/@supercharge/collections.svg" alt="Latest Version"></a> | ||
<a href="https://www.npmjs.com/package/@supercharge/collections"><img src="https://img.shields.io/npm/dm/@supercharge/collections.svg" alt="Monthly downloads"></a> | ||
</p> | ||
@@ -61,3 +62,3 @@ <p> | ||
await user.subscribeToNewsletter() | ||
return user | ||
@@ -72,3 +73,3 @@ }) | ||
You can directly await the result for methods returning a definite value: | ||
You can directly await the result for methods returning a definite value. The function returns a new instance of the collection without altering the original input array: | ||
@@ -75,0 +76,0 @@ ```js |
@@ -268,2 +268,17 @@ 'use strict' | ||
/** | ||
* Returns the last item in the collection | ||
* that satisfies the `callback` testing | ||
* function, `undefined` otherwise. | ||
* | ||
* @param {Function} callback | ||
* | ||
* @returns {*} the found value | ||
*/ | ||
last (callback) { | ||
return this.all( | ||
this._enqueue('last', callback) | ||
) | ||
} | ||
/** | ||
* Asynchronous version of Array#map(), running all transformations | ||
@@ -270,0 +285,0 @@ * in parallel. It runs the given `callback` on each item of the |
@@ -275,2 +275,25 @@ 'use strict' | ||
/** | ||
* Returns the last item in the collection | ||
* that satisfies the `callback` testing | ||
* function, `undefined` otherwise. | ||
* | ||
* @param {Function} callback | ||
* | ||
* @returns {*} the found value | ||
*/ | ||
async last (callback) { | ||
if (!callback) { | ||
return this.items[this.items.length - 1] | ||
} | ||
if (typeof callback === 'function') { | ||
const mapped = await this.filter(callback) | ||
return mapped[mapped.length - 1] | ||
} | ||
throw new Error(`Collection.last() accepts only a callback function as an argument, received ${typeof callback}`) | ||
} | ||
/** | ||
* Asynchronous version of Array#map(), running all transformations | ||
@@ -584,3 +607,5 @@ * in parallel. It runs the given `callback` on each item of the | ||
async unique () { | ||
return Array.from(new Set(this.items)) | ||
return Array.from( | ||
new Set(this.items) | ||
) | ||
} | ||
@@ -587,0 +612,0 @@ |
@@ -830,2 +830,36 @@ 'use strict' | ||
}) | ||
it('last', async () => { | ||
expect( | ||
await Collect([1, 2, 3]).last() | ||
).to.equal(3) | ||
expect( | ||
Collect([1, 2, 3]).last(1) | ||
).to.reject(Error) // only callback functions are allowed | ||
expect( | ||
await Collect([ | ||
{ id: 1, name: '1' }, | ||
{ id: 2, name: '2' }, | ||
{ id: 1, name: '3' } | ||
]).last(item => { | ||
return item.id === 1 | ||
}) | ||
).to.equal({ id: 1, name: '3' }) | ||
expect( | ||
await Collect([{ id: 1, name: '1' }]).last(item => { | ||
return item.name === 'Marcus' | ||
}) | ||
).to.equal(undefined) | ||
const items = [1, 2, 3] | ||
expect( | ||
await Collect(items) | ||
.map(item => item * 10) | ||
.last() | ||
).to.equal(30) | ||
expect(items).to.equal([1, 2, 3]) | ||
}) | ||
}) |
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
61753
1898
102