-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
103 lines (84 loc) · 2.02 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
Promise.prototype.mapA = Array.prototype.mapA = async function (p) {
let self = this;
if(self instanceof Promise)
{
self = await self;
}
return await Promise.all(self.map((currentValue, index, array)=>p(currentValue, index, array)));
};
Promise.prototype.forEachA = Array.prototype.forEachA = async function (p) {
let self = this;
if(self instanceof Promise)
{
self = await self;
}
await self.mapA(p);
return this;
};
Promise.prototype.filterA = Array.prototype.filterA = async function (p) {
let self = this;
if(self instanceof Promise)
{
self = await self;
}
const r = await Promise.all(self.map(e=>p(e)));
return self.reduce((sum, e, id)=>{
if(r[id] === true)
{
sum.push(e);
}
}, []);
};
Promise.prototype.reduceA = Array.prototype.reduceA = async function (p, initialValue) {
let self = this;
if(self instanceof Promise)
{
self = await self;
}
let accumulator = initialValue || self[0];
for(let e in self)
{
accumulator = await p(accumulator, self[e], e, self)
}
return accumulator;
};
Promise.prototype.reduceRightA = Array.prototype.reduceRightA = async function (p, initialValue) {
let self = this;
if(self instanceof Promise)
{
self = await self;
}
return await self.reverse().reduceA(p, initialValue);
};
Promise.prototype.everyA = Array.prototype.everyA = async function (p) {
let self = this;
if(self instanceof Promise)
{
self = await self;
}
return (await self.filterA(p)).length === self.length;
};
Promise.prototype.someA = Array.prototype.someA = async function (p) {
let self = this;
if(self instanceof Promise)
{
self = await self;
}
return (await self.filterA(p)).length > 0;
};
Promise.prototype.findA = Array.prototype.findA = async function (p) {
let self = this;
if(self instanceof Promise)
{
self = await self;
}
return (await self.filterA(p))[0];
};
Promise.prototype.findIndexA = Array.prototype.findIndexA = async function (p) {
let self = this;
if(self instanceof Promise)
{
self = await self;
}
return self.indexOf((await self.findA(p))[0]);
};