-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
239 lines (212 loc) · 6.33 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
var slinfo = require('debug')('sugarlisp:core:utils:info');
if (!String.prototype.repeat) {
String.prototype.repeat = function(num) {
num = (num >= 0 ? num : 0);
return new Array(num + 1).join(this)
}
}
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g, '');
};
}
// borrowed from https://github.com/paulmillr/Array.prototype.find
if (!Array.prototype.find) {
function find(predicate) {
var list = Object(this);
var length = list.length < 0 ? 0 : list.length >>> 0; // ES.ToUint32;
if (length === 0) return undefined;
if (typeof predicate !== 'function' || Object.prototype.toString.call(predicate) !== '[object Function]') {
throw new TypeError('Array#find: predicate must be a function');
}
var thisArg = arguments[1];
for (var i = 0, value; i < length; i++) {
value = list[i];
if (predicate.call(thisArg, value, i, list)) return value;
}
return undefined;
};
if (Object.defineProperty) {
try {
Object.defineProperty(Array.prototype, 'find', {
value: find, configurable: true, enumerable: false, writable: true
});
} catch(e) {}
}
if (!Array.prototype.find) {
Array.prototype.find = find;
}
}
if (!Array.prototype.contains) {
function contains(val) {
return this.indexOf(val) !== -1;
};
if (Object.defineProperty) {
try {
Object.defineProperty(Array.prototype, 'contains', {
value: contains, configurable: true, enumerable: false, writable: true
});
} catch(e) {}
}
if (!Array.prototype.contains) {
Array.prototype.contains = contains;
}
}
if (!Array.prototype.findIndex) {
function findIndex(predicate) {
var list = Object(this);
var length = list.length < 0 ? 0 : list.length >>> 0; // ES.ToUint32;
if (length === 0) return -1;
if (typeof predicate !== 'function' || Object.prototype.toString.call(predicate) !== '[object Function]') {
throw new TypeError('Array#findIndex: predicate must be a function');
}
var thisArg = arguments[1];
for (var i = 0, value; i < length; i++) {
value = list[i];
if (predicate.call(thisArg, value, i, list)) return i;
}
return -1;
};
if (Object.defineProperty) {
try {
Object.defineProperty(Array.prototype, 'findIndex', {
value: findIndex, configurable: true, enumerable: false, writable: true
});
} catch(e) {}
}
if (!Array.prototype.findIndex) {
Array.prototype.findIndex = findIndex;
}
}
if (!Array.prototype.contains) {
function contains(val) {
return this.indexOf(val) !== -1;
};
if (Object.defineProperty) {
try {
Object.defineProperty(Array.prototype, 'contains', {
value: contains, configurable: true, enumerable: false, writable: true
});
} catch(e) {}
}
if (!Array.prototype.contains) {
Array.prototype.contains = contains;
}
}
// THIS MERGE STUFF NEEDS TO BE MERGED - ALL THREE SHOULD BE SHORT AND USE
// A COMMON HELPER
/**
* Takes any number of objects and returns one (new) merged object
* note: later properties with the same name as earlier ones
* do *not* replace them (a warning is logged when such
* conflicts are found).
*/
exports.merge = function(){
var out = {};
if(!arguments.length)
return out;
for(var i=0; i<arguments.length; i++) {
for(var key in arguments[i]) {
if (arguments[i].hasOwnProperty(key)) {
if(typeof out[key] === 'undefined') {
out[key] = arguments[i][key];
}
else {
slinfo("Not merging already existing key \"" + key + "\"");
}
}
}
}
return out;
}
// merge is also a convenient way to clone an object:
exports.clone = exports.merge;
/**
* Takes any number of objects and merges the properties from later
* ones into the first.
* (otherwise identical to merge above)
*/
exports.mergeInto = function(){
if(!arguments.length || arguments.length === 0)
return {};
var out = arguments[0];
for(var i=1; i<arguments.length; i++) {
for(var key in arguments[i]) {
if (arguments[i].hasOwnProperty(key)) {
if(typeof out[key] === 'undefined') {
out[key] = arguments[i][key];
}
else {
slinfo("Not merging already existing key \"" + key + "\"");
}
}
}
}
return out;
}
/**
* Set properties in the first argument from the other arguments
* (in the order of the arguments, later properties win)
*/
exports.mergeOnto = function(){
if(!arguments.length || arguments.length === 0)
return {};
var out = arguments[0];
for(var i=1; i<arguments.length; i++) {
for(var key in arguments[i]) {
if (arguments[i].hasOwnProperty(key)) {
out[key] = arguments[i][key];
}
}
}
return out;
}
/**
* Fill undefined properties in the first argument from the other arguments
* (in the order of the arguments, earlier properties win)
*/
exports.mergeUndefined = function(){
if(!arguments.length || arguments.length === 0)
return {};
var out = arguments[0];
for(var i=1; i<arguments.length; i++) {
for(var key in arguments[i]) {
if (arguments[i].hasOwnProperty(key) && typeof out[key] === 'undefined') {
out[key] = arguments[i][key];
}
}
}
return out;
}
// the below is based on the answer from Bergi here:
// http://stackoverflow.com/questions/9479046/is-there-any-non-eval-way-to-create-a-function-with-a-runtime-determined-name
exports.anonymousFunction = function(args, body, scope, values) {
// args is optional (shift *our* args if none given)
if (typeof args == "string")
values = scope, scope = body, body = args, args = [];
if (!Array.isArray(scope) || !Array.isArray(values)) {
if (typeof scope == "object") {
// place the object keys in scope
var keys = Object.keys(scope);
values = keys.map(function(p) { return scope[p]; });
scope = keys;
} else {
values = [];
scope = [];
}
}
return Function(scope, "return function(" + args.join(", ") + ") {\n" +
body +
"\n};").apply(null, values);
}
// Get the file extension (without the ".") otherwise defaultExt
exports.getFileExt = function(filename, defaultExt) {
var fileext;
if(filename.indexOf(".") !== -1) {
fileext = filename.split('.').pop();
}
else {
fileext = defaultExt;
}
return fileext
}