-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
143 lines (125 loc) · 4.19 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const getKeys = val => {
if (val && typeof val === 'object')
{
if (Array.isArray(val))
{
return val.map((val, index) => index);
}
return Object.keys(val);
}
return [];
};
/**
* x-loop function (batch processing)
* @param {Array|Object} arr array sources
* @param {Function} fn callback processor
* @param {Function} done callback when done
*/
const xLoop = (arr, fn, done) => {
if (
typeof fn === 'function' &&
typeof done === 'function' && (
Array.isArray(arr) || (arr && typeof arr === 'object')
)
) {
const len = getKeys(arr).length;
let iy = 0;
const result = {};
const blacklist = [];
let doneCalled = false;
getKeys(arr).forEach(i => {
((val, ix) => {
const cbx = (res, con) => {
if (blacklist.indexOf(ix) >= 0) return false;
blacklist.push(ix);
if (res !== undefined) result[ix] = res;
iy += 1;
if (iy === len || con) {
if (doneCalled) return false;
doneCalled = true;
done(result, iy, con !== false);
}
return true;
}
if (fn(cbx, val, ix, arr) === false) cbx(undefined);
})(arr[i], i);
});
return len;
}
return false;
};
module.exports.xLoop = xLoop;
const xLoopEx = (arr, fn) => {
return new Promise((resolve, reject) => {
xLoop(arr, fn, (data, length, result) => {
const ndata = Object.keys({}, data || {});
ndata._length = length;
ndata._result = result;
ndata._lastKey = Object.keys(ndata).length > 0 ? Object.keys(data).pop() : null;
ndata._lastValue = ndata._lastKey !== null ? ndata[ndata._lastKey] : null;
if (result) resolve(ndata);
else reject(ndata);
});
});
};
module.exports.xLoopEx = xLoopEx;
/**
* y-loop function (one-by-one processing)
* @param {Array|Object} arr array source
* @param {Function} fn callback processor
* @param {Function} done callback when done
*/
const yLoop = (arr, fn, done) => {
if (
typeof fn === 'function' &&
typeof done === 'function' && (
Array.isArray(arr) || (arr && typeof arr === 'object')
)
) {
const len = getKeys(arr).length;
let iy = 0;
const result = {};
const blacklist = [];
let resultLen = 0;
const keys = getKeys(arr);
const doNext = resume => {
if (resume === false) return done(result, false, (iy + 1));
if (iy >= len) return done(result, true, (iy + 1));
iy += 1;
const key = keys[iy - 1];
((val, ix) => {
const cbx = (res, rsm) => {
if (blacklist.indexOf(ix) >= 0) return false;
blacklist.push(ix);
if (res !== undefined) {
result[ix] = res;
resultLen += 1;
}
if (res instanceof Error && rsm === false) throw res;
doNext(rsm !== false);
return true;
}
if (fn(cbx, val, ix, arr, result, resultLen) === false) cbx(undefined, false);
})(arr[key], key);
return true;
}
doNext();
return len;
}
return false;
};
module.exports.yLoop = yLoop;
const yLoopEx = (arr, fn, silent) => {
return new Promise((resolve, reject) => {
yLoop(arr, fn, (data, result, length) => {
const ndata = Object.assign({}, data || {});
ndata._length = length;
ndata._result = result;
ndata._lastKey = Object.keys(ndata).length > 0 ? Object.keys(data).pop() : null;
ndata._lastValue = ndata._lastKey !== null ? ndata[ndata._lastKey] : null;
if (result || silent === true) resolve(ndata);
else if (silent !== true) reject(ndata);
});
});
};
module.exports.yLoopEx = yLoopEx;