1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | export function defer() {
|
18 | let resolve, reject;
|
19 | let promise = new Promise(function (success, failure) {
|
20 | resolve = success;
|
21 | reject = failure;
|
22 | });
|
23 | if (!resolve || !reject) throw "defer() error";
|
24 |
|
25 | return {
|
26 | promise,
|
27 | resolve,
|
28 | reject
|
29 | };
|
30 | }
|
31 |
|
32 | export function splitPath(path) {
|
33 | let result = [];
|
34 | let components = path.split("/");
|
35 | components.forEach(element => {
|
36 | let number = parseInt(element, 10);
|
37 |
|
38 | if (isNaN(number)) {
|
39 | return;
|
40 | }
|
41 |
|
42 | if (element.length > 1 && element[element.length - 1] === "'") {
|
43 | number += 0x80000000;
|
44 | }
|
45 |
|
46 | result.push(number);
|
47 | });
|
48 | return result;
|
49 | }
|
50 |
|
51 | export function eachSeries(arr, fun) {
|
52 | return arr.reduce((p, e) => p.then(() => fun(e)), Promise.resolve());
|
53 | }
|
54 | export function foreach(arr, callback) {
|
55 | function iterate(index, array, result) {
|
56 | if (index >= array.length) {
|
57 | return result;
|
58 | } else return callback(array[index], index).then(function (res) {
|
59 | result.push(res);
|
60 | return iterate(index + 1, array, result);
|
61 | });
|
62 | }
|
63 |
|
64 | return Promise.resolve().then(() => iterate(0, arr, []));
|
65 | }
|
66 | export function doIf(condition, callback) {
|
67 | return Promise.resolve().then(() => {
|
68 | if (condition) {
|
69 | return callback();
|
70 | }
|
71 | });
|
72 | }
|
73 | export function asyncWhile(predicate, callback) {
|
74 | function iterate(result) {
|
75 | if (!predicate()) {
|
76 | return result;
|
77 | } else {
|
78 | return callback().then(res => {
|
79 | result.push(res);
|
80 | return iterate(result);
|
81 | });
|
82 | }
|
83 | }
|
84 |
|
85 | return Promise.resolve([]).then(iterate);
|
86 | }
|
87 |
|
\ | No newline at end of file |