core-functions
Advanced tools
Comparing version 2.0.2 to 2.0.3
{ | ||
"name": "core-functions", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"description": "Core functions, utilities and classes for working with Node/JavaScript primitives and built-in objects, including strings, booleans, Promises, base 64, Arrays, Objects, standard AppErrors, etc.", | ||
@@ -5,0 +5,0 @@ "author": "Byron du Preez", |
@@ -273,1 +273,38 @@ 'use strict'; | ||
} | ||
/** | ||
* @typedef {{result:*}|{error:Error}} ResultOrError - An object containing either a result or an error. | ||
*/ | ||
/** | ||
* Returns a promise that returns a list of either a result or an error for each of the given promises in the same | ||
* sequence as the given promises when every one of the given promises has resolved. | ||
* @param {Promise[]|Promise...} promises - a list of promises from which to collect their resolved results or their rejected errors | ||
* @returns {Promise.<ResultOrError[]>} promises - a promise of a list of either a result or an error for each of the | ||
* given promises in the same sequence as the given promises | ||
*/ | ||
function every(promises) { | ||
const ps = Array.isArray(promises) ? promises : isPromise(promises) ? arguments : []; | ||
const n = ps.length; | ||
if (n <= 0) { | ||
return Promise.resolve([]); | ||
} | ||
const results = new Array(n); | ||
function next(i) { | ||
return ps[i].then( | ||
result => | ||
results[i] = {result: result}, | ||
err => | ||
results[i] = {error: err}) | ||
.then(() => | ||
// If remaining list (after i) contains at least one more promise, then continue; otherwise done | ||
i < n - 1 ? next(i + 1) : results | ||
); | ||
} | ||
return next(0); | ||
} | ||
if (!Promise.every) { // polyfill-safe guard check | ||
Promise.every = every; | ||
} |
@@ -1,2 +0,2 @@ | ||
# core-functions v2.0.1 | ||
# core-functions v2.0.3 | ||
@@ -105,9 +105,23 @@ Core functions, utilities and classes for working with Node/JavaScript primitives and built-in objects, including | ||
## Changes | ||
### 1.1.0 | ||
- strings: Added `trimOrEmpty` function | ||
- strings: Renamed `safeTrim` function to `trim` and changed `safeTrim` to an alias for `trim` | ||
### 1.1.1 | ||
- Simple increment of version number to fix issue of 1.1.0 tag pointing to wrong version | ||
### 2.0.3 | ||
- Change to `promises.js`: | ||
- Added an `every` function that accepts multiple promises and returns a single promise that returns a list of either a result or an error for each and every one of the given promises | ||
### 2.0.2 | ||
- Changes to `promises.js`: | ||
- Minor patch for `isPromise` to survive undefined and null | ||
- Added `isArrayOfPromises` function to check if a result is an array of promises | ||
- Added `allOrOne` function to transform a result into a single promise | ||
using `Promise.all`, the promise-result or `Promise.resolve` | ||
- Changed implementation of `Promise.try` to use new `Promise.allOrOne` | ||
- Added unit tests for `allOrOne` & `isArrayOfPromises` and more tests for `try` | ||
### 2.0.1 | ||
- Replaced all `x instanceof Array` checks with safer `Array.isArray(x)` | ||
### 2.0.0 | ||
- Removed unnecessary functions.js module | ||
- Patches to testing.js `checkMethodEqual` and `checkMethodOkNotOk` functions to show method prefixes properly | ||
### 1.2.0 | ||
@@ -126,16 +140,8 @@ - strings: | ||
### 2.0.0 | ||
- Removed unnecessary functions.js module | ||
- Patches to testing.js `checkMethodEqual` and `checkMethodOkNotOk` functions to show method prefixes properly | ||
### 1.1.1 | ||
- Simple increment of version number to fix issue of 1.1.0 tag pointing to wrong version | ||
### 2.0.1 | ||
- Replaced all `x instanceof Array` checks with safer `Array.isArray(x)` | ||
### 2.0.2 | ||
- Changes to `promises.js`: | ||
- Minor patch for `isPromise` to survive undefined and null | ||
- Added `isArrayOfPromises` function to check if a result is an array of promises | ||
- Added `allOrOne` function to transform a result into a single promise | ||
using `Promise.all`, the promise-result or `Promise.resolve` | ||
- Changed implementation of `Promise.try` to use new `Promise.allOrOne` | ||
- Added unit tests for `allOrOne` & `isArrayOfPromises` and more tests for `try` | ||
### 1.1.0 | ||
- strings: Added `trimOrEmpty` function | ||
- strings: Renamed `safeTrim` function to `trim` and changed `safeTrim` to an alias for `trim` | ||
{ | ||
"name": "core-functions-tests", | ||
"version": "2.0.2", | ||
"version": "2.0.3", | ||
"author": "Byron du Preez", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0", |
@@ -553,3 +553,3 @@ 'use strict'; | ||
t.ok(Promise.isPromise(Promise.allOrOne([thenable])), 'allOrOne([then-able]) is a promise'); | ||
t.ok(Promise.isPromise(Promise.allOrOne([thenable,thenable])), 'allOrOne([then-able,then-able]) is a promise'); | ||
t.ok(Promise.isPromise(Promise.allOrOne([thenable, thenable])), 'allOrOne([then-able,then-able]) is a promise'); | ||
@@ -572,5 +572,87 @@ const promise = new Promise((resolve, reject) => resolve('Bob')); | ||
}); | ||
}); | ||
}); | ||
}); | ||
// --------------------------------------------------------------------------------------------------------------------- | ||
// Promise.every | ||
// --------------------------------------------------------------------------------------------------------------------- | ||
test('Promise.every', t => { | ||
t.plan(11); | ||
// Empty array or undefined | ||
Promise.every([]) | ||
.then(results => { | ||
t.deepEqual(results, [], 'Promise.every([]) must be []'); | ||
}); | ||
Promise.every(undefined) | ||
.then(results => { | ||
t.deepEqual(results, [], 'Promise.every(undefined) must be []'); | ||
}); | ||
Promise.every(null) | ||
.then(results => { | ||
t.deepEqual(results, [], 'Promise.every(null) must be []'); | ||
}); | ||
// Single Promise | ||
const p1 = Promise.resolve('p1'); | ||
const expected = [{result: 'p1'}]; | ||
Promise.every(p1) | ||
.then(results => { | ||
t.deepEqual(results, expected, `Promise.every(p1) must be ${stringify(expected)}`); | ||
}); | ||
Promise.every([p1]) | ||
.then(results => { | ||
t.deepEqual(results, expected, `Promise.every([p1]) must be ${stringify(expected)}`); | ||
}); | ||
// Multiple Promises | ||
const p2Error = new Error('p2 error'); | ||
const p2 = Promise.reject(p2Error); | ||
const p3 = Promise.resolve('p3'); | ||
const p4Error = new Error('p4 error'); | ||
const p4 = Promise.reject(p4Error); | ||
// Promises as arguments | ||
Promise.every(p1, p2) | ||
.then(results => { | ||
const expected = [{result: 'p1'}, {error: p2Error}]; | ||
t.deepEqual(results, expected, `Promise.every(p1,p2) must be ${stringify(expected)}`); | ||
}); | ||
Promise.every(p1, p2, p3) | ||
.then(results => { | ||
const expected = [{result: 'p1'}, {error: p2Error}, {result: 'p3'}]; | ||
t.deepEqual(results, expected, `Promise.every(p1,p2,p3) must be ${stringify(expected)}`); | ||
}); | ||
Promise.every(p1, p2, p3, p4) | ||
.then(results => { | ||
const expected = [{result: 'p1'}, {error: p2Error}, {result: 'p3'}, {error: p4Error},]; | ||
t.deepEqual(results, expected, `Promise.every(p1,p2,p3,p4) must be ${stringify(expected)}`); | ||
}); | ||
// Promises in arrays | ||
Promise.every([p1, p2]) | ||
.then(results => { | ||
const expected = [{result: 'p1'}, {error: p2Error}]; | ||
t.deepEqual(results, expected, `Promise.every([p1,p2]) must be ${stringify(expected)}`); | ||
}); | ||
Promise.every([p1, p2, p3]) | ||
.then(results => { | ||
const expected = [{result: 'p1'}, {error: p2Error}, {result: 'p3'}]; | ||
t.deepEqual(results, expected, `Promise.every([p1,p2,p3]) must be ${stringify(expected)}`); | ||
}); | ||
Promise.every([p1, p2, p3, p4]) | ||
.then(results => { | ||
const expected = [{result: 'p1'}, {error: p2Error}, {result: 'p3'}, {error: p4Error},]; | ||
t.deepEqual(results, expected, `Promise.every([p1,p2,p3,p4]) must be ${stringify(expected)}`); | ||
}); | ||
}); |
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
164908
3303
146