UNPKG

prray

Version:

'Promisified' Array, comes with async method supports(such as mapAsync). And it is compatible with normal array.

147 lines (146 loc) 4.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class PrrayPromise extends Promise { constructor(executor) { super(executor); } mapAsync(func) { return prraypromise(this.then(prray => prray.mapAsync(func))); } filterAsync(func) { return prraypromise(this.then(prray => prray.filterAsync(func))); } reduceAsync(callback, initialValue) { return this.then(prray => prray.reduceAsync(callback, initialValue)); } reduceRightAsync(callback, initialValue) { return this.then(prray => prray.reduceRightAsync(callback, initialValue)); } sortAsync(func) { return prraypromise(this.then(prray => prray.sortAsync(func))); } findAsync(func) { return this.then(prray => prray.findAsync(func)); } findIndexAsync(func) { return this.then(prray => prray.findIndexAsync(func)); } everyAsync(func) { return this.then(prray => prray.everyAsync(func)); } someAsync(func) { return this.then(prray => prray.someAsync(func)); } forEachAsync(func) { return this.then(prray => prray.forEachAsync(func)); } slice(start, end) { return prraypromise(this.then(prray => prray.slice(start, end))); } map(func) { return prraypromise(this.then(prray => prray.map(func))); } filter(func) { return prraypromise(this.then(prray => prray.filter(func))); } find(func) { return this.then(prray => prray.find(func)); } findIndex(func) { return this.then(prray => prray.findIndex(func)); } every(func) { return this.then(prray => prray.every(func)); } some(func) { return this.then(prray => prray.some(func)); } 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()); } forEach(callback) { return this.then(prray => prray.forEach(callback)); } pop() { return this.then(prray => prray.pop()); } push(...items) { return this.then(prray => prray.push(...items)); } reverse() { return prraypromise(this.then(prray => prray.reverse())); } reduce(callback, 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(callback, initialValue) : prray.reduce(callback))); } reduceRight(callback, 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(callback, initialValue) : prray.reduceRight(callback)); } 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 :( return prraypromise(this.then(prray => (arguments.length >= 2 ? prray.splice(start, deleteCount, ...items) : prray.splice(start)))); } toArray() { return this.then(prray => prray.toArray()); } } 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;