appolo-utils
Advanced tools
Comparing version 0.0.9 to 0.0.10
@@ -7,4 +7,52 @@ "use strict"; | ||
} | ||
static map(iterable, mapper, options = { concurrency: Infinity }) { | ||
let concurrency = options.concurrency || Infinity; | ||
let params = { index: 0 }, results = [], iterator = iterable[Symbol.iterator](), promises = []; | ||
while (concurrency-- > 0) { | ||
let promise = Promises._mapWrapper(mapper, iterator, results, params); | ||
if (!promise) { | ||
break; | ||
} | ||
promises.push(promise); | ||
} | ||
return Promise.all(promises).then(() => results); | ||
} | ||
static _mapWrapper(mapper, iterator, results, params) { | ||
let next = iterator.next(); | ||
if (next.done) { | ||
return null; | ||
} | ||
let i = params.index++, mapped = mapper(next.value, i); | ||
return Promise.resolve(mapped).then(resolved => { | ||
results[i] = resolved; | ||
return Promises._mapWrapper(mapper, iterator, results, params); | ||
}); | ||
} | ||
static filter(iterable, filterer, options = { concurrency: Infinity }) { | ||
let concurrency = options.concurrency || Infinity; | ||
let params = { index: 0 }, results = [], predicates = [], iterator = iterable[Symbol.iterator](), promises = []; | ||
while (concurrency-- > 0) { | ||
let promise = Promises._filterWrapper(filterer, iterator, results, predicates, params); | ||
if (!promise) { | ||
break; | ||
} | ||
promises.push(promise); | ||
} | ||
return Promise.all(promises).then(() => results.filter((v, i) => predicates[i])); | ||
} | ||
static _filterWrapper(filterer, iterator, results, predicates, params) { | ||
let next = iterator.next(); | ||
if (next.done) { | ||
return null; | ||
} | ||
let i = params.index++; | ||
results.push(next.value); | ||
let predicate = filterer(next.value, i); | ||
return Promise.resolve(predicate).then(resolved => { | ||
predicates[i] = resolved; | ||
return Promises._filterWrapper(filterer, iterator, results, predicates, params); | ||
}); | ||
} | ||
} | ||
exports.Promises = Promises; | ||
//# sourceMappingURL=promises.js.map |
@@ -0,1 +1,4 @@ | ||
type Resolvable<R> = R | PromiseLike<R> | ||
type IterateFunction<T, R> = (item: T, index: number | string) => Resolvable<R>; | ||
export class Promises { | ||
@@ -5,2 +8,75 @@ public static delay(delay: number): Promise<void> { | ||
} | ||
public static map<R, U>(iterable: Resolvable<Iterable<Resolvable<R>>>, mapper: IterateFunction<R, U>, options: { concurrency: number } = {concurrency: Infinity}) { | ||
let concurrency = options.concurrency || Infinity; | ||
let params = {index:0}, results = [], iterator = iterable[Symbol.iterator](), promises = []; | ||
while (concurrency-- > 0) { | ||
let promise = Promises._mapWrapper(mapper, iterator, results, params); | ||
if (!promise) { | ||
break; | ||
} | ||
promises.push(promise); | ||
} | ||
return Promise.all(promises).then(() => results); | ||
} | ||
private static _mapWrapper<R, U>(mapper: IterateFunction<R, U>, iterator: IterableIterator<R>, results: any[], params: { index: number }) { | ||
let next = iterator.next(); | ||
if (next.done) { | ||
return null; | ||
} | ||
let i = params.index++, mapped = mapper(next.value, i); | ||
return Promise.resolve(mapped).then(resolved => { | ||
results[i] = resolved; | ||
return Promises._mapWrapper(mapper, iterator, results, params) | ||
}) | ||
} | ||
public static filter<R, U>(iterable: Resolvable<Iterable<Resolvable<R>>>, filterer: IterateFunction<R, U>, options: { concurrency: number } = {concurrency: Infinity}) { | ||
let concurrency = options.concurrency || Infinity; | ||
let params = {index:0}, results = [], predicates = [], iterator = iterable[Symbol.iterator](), promises = []; | ||
while (concurrency-- > 0) { | ||
let promise = Promises._filterWrapper(filterer, iterator, results, predicates, params); | ||
if (!promise) { | ||
break | ||
} | ||
promises.push(promise); | ||
} | ||
return Promise.all(promises).then(() => results.filter((v, i) => predicates[i])); | ||
} | ||
private static _filterWrapper<R, U>(filterer: IterateFunction<R, U>, iterator: IterableIterator<R>, results: any[], predicates: any[], params: { index: number }) { | ||
let next = iterator.next(); | ||
if (next.done) { | ||
return null | ||
} | ||
let i = params.index++; | ||
results.push(next.value); | ||
let predicate = filterer(next.value, i); | ||
return Promise.resolve(predicate).then(resolved => { | ||
predicates[i] = resolved; | ||
return Promises._filterWrapper(filterer, iterator, results, predicates, params) | ||
}) | ||
} | ||
} |
@@ -17,3 +17,3 @@ { | ||
"main": "./index.js", | ||
"version": "0.0.9", | ||
"version": "0.0.10", | ||
"license": "MIT", | ||
@@ -29,3 +29,3 @@ "repository": { | ||
"@types/benchmark": "^1.0.31", | ||
"@types/bluebird": "^3.5.24", | ||
"@types/bluebird": "^3.5.27", | ||
"@types/chai": "^4.1.6", | ||
@@ -37,6 +37,6 @@ "@types/lodash": "^4.14.116", | ||
"benchmark": "^2.1.4", | ||
"bluebird": "^3.5.2", | ||
"bluebird": "^3.5.5", | ||
"chai": "^4.2.0", | ||
"crypto-js": "^3.1.9-1", | ||
"lodash": "^4.17.11", | ||
"lodash": "^4.17.14", | ||
"mocha": "^5.2.0", | ||
@@ -43,0 +43,0 @@ "tslib": "^1.9.3", |
Sorry, the diff of this file is not supported yet
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
42457
795