@applitools/functional-commons
Advanced tools
Comparing version 1.5.2 to 1.5.3
{ | ||
"name": "@applitools/functional-commons", | ||
"version": "1.5.2", | ||
"version": "1.5.3", | ||
"description": "", | ||
@@ -37,9 +37,9 @@ "main": "src/functional-commons.js", | ||
"chai-as-promised": "^7.1.1", | ||
"eslint": "^6.2.2", | ||
"eslint": "^6.7.2", | ||
"eslint-plugin-mocha-no-only": "^1.1.0", | ||
"eslint-plugin-node": "^9.1.0", | ||
"eslint-plugin-prettier": "^3.1.0", | ||
"mocha": "^6.2.0", | ||
"prettier": "^1.18.2" | ||
"eslint-plugin-node": "^9.2.0", | ||
"eslint-plugin-prettier": "^3.1.1", | ||
"mocha": "^6.2.2", | ||
"prettier": "^1.19.1" | ||
} | ||
} |
@@ -212,3 +212,6 @@ 'use strict' | ||
function presult(promise) { | ||
return promise.then(v => [undefined, v], err => [err]) | ||
return promise.then( | ||
v => [undefined, v], | ||
err => [err], | ||
) | ||
} | ||
@@ -420,2 +423,52 @@ | ||
function isFunction(functionToCheck) { | ||
return functionToCheck && typeof functionToCheck == 'function' | ||
} | ||
async function promiseProps(object) { | ||
const result = {} | ||
const promises = [] | ||
for (let key of Object.keys(object)) { | ||
async function resolve() { | ||
return object[key] | ||
} | ||
async function populate() { | ||
const value = await resolve() | ||
result[key] = value | ||
} | ||
promises.push(populate()) | ||
} | ||
await Promise.all(promises) | ||
return result | ||
} | ||
async function promiseMap(collection, func, {concurrency} = {concurrency: 4}) { | ||
if (concurrency <= 0 || concurrency > 100) { | ||
throw new Error('concurrency should be in range: 1..100') | ||
} | ||
if (!isFunction(func)) { | ||
throw new Error('func must be a function') | ||
} | ||
if (collection.length == 0) { | ||
return [] | ||
} | ||
const collectionWithIndexes = [] | ||
for (let i = 0; i < collection.length; i++) { | ||
collectionWithIndexes.push({object: collection[i], index: i}) | ||
} | ||
const promises = [] | ||
const result = new Array(collection.length) | ||
for (let i = 0; i < concurrency; i++) { | ||
async function worker() { | ||
while (collectionWithIndexes.length > 0) { | ||
const element = collectionWithIndexes.pop() | ||
result[element.index] = await func(element.object) | ||
} | ||
} | ||
promises.push(worker()) | ||
} | ||
await Promise.all(promises) | ||
return result | ||
} | ||
module.exports = { | ||
@@ -452,2 +505,5 @@ cacheFunctionSync, | ||
groupBy, | ||
promiseMap, | ||
promiseProps, | ||
isFunction, | ||
} |
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
36555
471