UNPKG

prray

Version:

'Promisified' Array, it compatible with the original Array but comes with async versions of native Array methods, such as `mapAsync`, `filterAsync`, `reduceAsync`, `sortAsync`, `findAsync`, `findIndexAsync`, `everyAsync`, `someAsync`, `forEachAsync`...

153 lines (152 loc) 5.16 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class PrrayPromise extends Promise { constructor(executor) { super(executor); } mapAsync(func, opts) { return prraypromise(this.then(prray => prray.mapAsync(func, opts))); } map(func) { return prraypromise(this.then(prray => prray.map(func))); } filterAsync(func, opts) { return prraypromise(this.then(prray => prray.filterAsync(func, opts))); } filter(func) { return prraypromise(this.then(prray => prray.filter(func))); } reduceAsync(func, initialValue) { const promise = this.then(prray => prray.reduceAsync(func, initialValue)); return promise; } reduce(func, initialValue) { // Why? If pass parameter initialValue as undefined, the initial value will be undefined instead array[0] actually :( return this.then(prray => (arguments.length >= 2 ? prray.reduce(func, initialValue) : prray.reduce(func))); } reduceRightAsync(func, initialValue) { return this.then(prray => prray.reduceRightAsync(func, initialValue)); } reduceRight(func, initialValue) { // Why? If pass parameter initialValue as undefined, the initial value will be undefined instead array[0] actually :( return this.then(prray => (arguments.length >= 2 ? prray.reduceRight(func, initialValue) : prray.reduceRight(func))); } sortAsync(func) { return prraypromise(this.then(prray => prray.sortAsync(func))); } findAsync(func) { return this.then(prray => prray.findAsync(func)); } find(func) { return this.then(prray => prray.find(func)); } findIndexAsync(func) { return this.then(prray => prray.findIndexAsync(func)); } findIndex(func) { return this.then(prray => prray.findIndex(func)); } everyAsync(func, opts) { return this.then(prray => prray.everyAsync(func, opts)); } every(func) { return this.then(prray => prray.every(func)); } someAsync(func, opts) { return this.then(prray => prray.someAsync(func, opts)); } some(func) { return this.then(prray => prray.some(func)); } forEachAsync(func, opts) { return this.then(prray => prray.forEachAsync(func, opts)); } forEach(func) { return this.then(prray => prray.forEach(func)); } slice(start, end) { return prraypromise(this.then(prray => prray.slice(start, end))); } includes(element, fromIndex) { return this.then(prray => prray.includes(element, fromIndex)); } indexOf(element, fromIndex) { return this.then(prray => prray.indexOf(element, fromIndex)); } lastIndexOf(element, fromIndex) { return this.then(prray => { fromIndex = fromIndex === undefined ? prray.length - 1 : fromIndex; // fix odd bug return prray.lastIndexOf(element, fromIndex); }); } join(separator) { return this.then(prray => prray.join(separator)); } keys() { return this.then(prray => prray.keys()); } values() { return this.then(prray => prray.values()); } entries() { return this.then(prray => prray.entries()); } fill(value, start, end) { return prraypromise(this.then(prray => prray.fill(value, start, end))); } sort(func) { return prraypromise(this.then(prray => prray.sort(func))); } concat(...items) { return prraypromise(this.then(prray => prray.concat(...items))); } copyWithin(target, start, end) { return prraypromise(this.then(prray => prray.copyWithin(target, start, end))); } toString() { return this.then(prray => prray.toString()); } toLocaleString() { return this.then(prray => prray.toLocaleString()); } pop() { return this.then(prray => prray.pop()); } push(...items) { return this.then(prray => prray.push(...items)); } reverse() { return prraypromise(this.then(prray => prray.reverse())); } shift() { return this.then(prray => prray.shift()); } unshift(...items) { return this.then(prray => prray.unshift(...items)); } splice(start, deleteCount, ...items) { // Why? If pass parameter deleteCount as undefined directly, the delete count will be zero actually :( const promise = this.then(prray => deleteCount === undefined ? prray.splice(start) : prray.splice(start, deleteCount, ...items)); return prraypromise(promise); } toArray() { return this.then(prray => prray.toArray()); } delay(ms) { const promise = this.then(prray => prray.delay(ms)); return prraypromise(promise); } } exports.PrrayPromise = PrrayPromise; function prraypromise(promise) { if (promise instanceof PrrayPromise) { return promise; } else { return new PrrayPromise((resolve, reject) => { promise.then(resolve); promise.catch(reject); }); } } exports.prraypromise = prraypromise;