-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
107 lines (96 loc) · 2.82 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'use strict'
var Promise = require('promise')
module.exports = ArrayPromise
function slice(arr) {
return Array.prototype.slice.call(arr)
}
function ArrayPromise(factory) {
Promise.call(this, factory)
this.then = arrayify(this.then)
}
ArrayPromise.prototype = Object.create(Promise.prototype)
ArrayPromise.prototype.constructor = ArrayPromise
var proto = (function () {
function A() {}
return (new A()).__proto__ === A.prototype
}())
ArrayPromise.from = function (value) {
if (value instanceof ArrayPromise) return value
if (value instanceof Promise && proto) {
value.__proto__ = ArrayPromise.prototype
value.then = arrayify(value.then)
}
return new ArrayPromise(function (resolve) { resolve(value) })
}
function arrayify(fn) {
return function () {
var res = fn.apply(this, arguments)
if (typeof res === 'function') return arrayify(res)
else return ArrayPromise.from(res)
}
}
ArrayPromise.denodeify = arrayify(Promise.denodeify)
ArrayPromise.nodeify = arrayify(Promise.nodeify)
ArrayPromise.all = arrayify(Promise.all)
ArrayPromise.prototype.reverse = function () {
return this.then(function (res) {
return slice(res).reverse()
})
}
ArrayPromise.prototype.concat = function () {
var args = slice(arguments)
return this.then(function (res) {
return ArrayPromise.all(args)
.then(function (args) {
return Array.prototype.concat.apply(res, args)
})
})
}
ArrayPromise.prototype.slice = function (begin) {
var end = arguments[1]
return this.then(function (res) {
return Array.prototype.slice.call(res, begin, end)
})
}
ArrayPromise.prototype.map = function (fn) {
return this.then(function (res) {
return Promise.all(Array.prototype.map.call(res, fn))
})
}
ArrayPromise.prototype.filter = function (fn) {
var self = this
return this.map(fn)
.then(function (includes) {
return self.then(function (res) {
return res.filter(function (res, i) { return includes[i] })
})
})
}
ArrayPromise.prototype.reduce = function (fn) {
var length = arguments.length
var initial = arguments[1]
return this.then(function (res) {
function reducer(acc, val) {
return Promise.all(acc, val)
.then(function (res) {
return fn(res[0], res[1])
})
}
if (length > 1) return Array.prototype.reduce.call(res, reducer, initial)
else return Array.prototype.reduce.call(res, reducer)
})
}
ArrayPromise.prototype.reduceRight = function (fn) {
var length = arguments.length
var initial = arguments[1]
return this.then(function (res) {
function reducer(acc, val) {
return Promise.all(acc, val)
.then(function (res) {
return fn(res[0], res[1])
})
}
if (length > 1) return Array.prototype.reduceRight.call(res, reducer, initial)
else return Array.prototype.reduceRight.call(res, reducer)
})
}