-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
147 lines (114 loc) · 2.71 KB
/
utils.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
143
144
145
146
147
module.exports.array_iterator = function array_iterator(array) {
array = array_clone(array).reverse();
return function pop_value() {
return array.pop();
};
};
module.exports.array_clone = function array_clone(array) {
return array.concat([]);
};
module.exports.format_map = function format_map(string, map) {
function get_param(match, param) {
return map[param];
}
return string.replace(/<([^<>]+)>/g, get_param);
};
module.exports.object_map = function object_map(obj, f) {
var rval = {};
for(var key in obj) {
rval[key] = f(obj[key], key, obj);
}
return rval;
};
module.exports.object_valuemap = function object_valuemap(obj, f) {
var rval = [];
for(var key in obj) {
rval.push(f(obj[key], key, obj));
}
return rval;
};
module.exports.regex_fold = function regex_fold(regex, string, f, initial_value) {
var match = regex.exec(string);
var accumulator = initial_value;
while(match) {
accumulator = f(accumulator, match);
match = regex.exec(string);
}
return accumulator;
};
module.exports.object_values = function object_values(obj) {
function get_field(field) {
return obj[field];
}
return Object.keys(obj).map(get_field);
};
module.exports.object_filter = function object_filter(obj, cb) {
var rval = {};
for(var key in obj) {
if(cb(obj[key], key, obj)) {
rval[key] = obj[key];
}
}
return rval;
};
module.exports.object_merge = function object_merge(dest, orig) {
for(var key in orig) {
dest[key] = orig[key];
}
return dest;
};
module.exports.object_reduce = function object_reduce(obj, operation, initial_value) {
var rval = initial_value;
for(var key in obj) {
rval = operation(rval, obj[key], key, obj);
}
return rval;
};
module.exports.get_field = function get_field(field) {
return function getter(obj) {
return obj[field];
};
};
module.exports.Waiter = function Waiter(cb, cb_this, args) {
if(!(this instanceof Waiter)) {
return new Waiter(cb, cb_this, args);
}
if(cb) {
this.set_cb(cb, cb_this, args);
}
this._waits = [];
};
module.exports.Waiter.prototype = {
/* Returns a callback that waits */
wait: function wait() {
var self = this;
var wait = {
done: false,
called: function called(called) {
wait.done = true;
wait.arguments = arguments;
if(self.cb && self._waits.every(is_fullfiled)) {
self.cb(self._waits);
}
}
};
function is_fullfiled(wait) {
return wait.done;
}
this._waits.push(wait);
return wait.called;
},
set_cb: function set_cb(cb, cb_this, args) {
function is_fullfiled(wait) {
return wait.done;
}
if(cb_this || args) {
this.cb = cb.bind.apply(cb, [cb_this].concat(args));
} else {
this.cb = cb;
}
if(this._waits.every(is_fullfiled)) {
this.cb(this._waits);
}
}
};