-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.js
293 lines (293 loc) · 10.5 KB
/
util.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
define(["require", "exports", 'dojo/_base/array', 'dojo/aspect', 'dojo/Deferred', './has', 'dojo/request/util', 'dojo/promise/all'], function (require, exports, array, aspect, Deferred, has, requestUtil, whenAll) {
function addUnloadCallback(callback) {
if (has('host-node')) {
process.on('exit', callback);
return createHandle(function () {
process.removeListener('exit', callback);
});
}
else if (has('host-browser')) {
return aspect.before(window, 'onbeforeunload', callback);
}
else {
throw new Error('Not supported on this platform');
}
}
exports.addUnloadCallback = addUnloadCallback;
function createCompositeHandle() {
var handles = [];
for (var _i = 0; _i < arguments.length; _i++) {
handles[_i - 0] = arguments[_i];
}
return createHandle(function () {
for (var i = 0, handle; (handle = handles[i]); ++i) {
handle.remove();
}
});
}
exports.createCompositeHandle = createCompositeHandle;
function createHandle(destructor) {
return {
remove: function () {
this.remove = function () {
};
destructor.call(this);
}
};
}
exports.createHandle = createHandle;
function createTimer(callback, delay) {
if (delay === void 0) { delay = 0; }
var timerId;
if (has('raf') && delay === 0) {
timerId = requestAnimationFrame(callback);
return createHandle(function () {
cancelAnimationFrame(timerId);
timerId = null;
});
}
else {
timerId = setTimeout(callback, delay);
return createHandle(function () {
clearTimeout(timerId);
timerId = null;
});
}
}
exports.createTimer = createTimer;
function debounce(callback, delay) {
if (delay === void 0) { delay = 0; }
var timer;
return function () {
timer && timer.remove();
var self = this, args = arguments;
timer = createTimer(function () {
callback.apply(self, args);
self = args = timer = null;
}, delay);
};
}
exports.debounce = debounce;
exports.deepCopy = requestUtil.deepCopy;
exports.deepCreate = requestUtil.deepCreate;
function deferMethods(target, methods, untilMethod, instead) {
var _target = target;
var waiting = {};
var untilHandle = aspect.after(target, untilMethod, function () {
untilHandle.remove();
untilHandle = null;
for (var method in waiting) {
var info = waiting[method];
_target[method] = info.original;
info.args && _target[method].apply(_target, info.args);
}
_target = waiting = null;
}, true);
array.forEach(methods, function (method) {
var info = waiting[method] = {
original: _target[method],
args: null
};
_target[method] = function () {
info.args = instead && instead.call(target, method, arguments) || arguments;
};
});
}
exports.deferMethods = deferMethods;
function deferSetters(target, properties, untilMethod, instead) {
deferMethods(target, array.map(properties, function (property) { return '_' + property + 'Setter'; }), untilMethod, instead ? function (method, args) {
return instead.call(this, method.slice(1, -6), args[0]);
} : undefined);
}
exports.deferSetters = deferSetters;
function escapedIndexOf(source, searchString, position) {
var index;
if (source === '' || searchString === '' || position < 0) {
return -1;
}
do {
index = source.indexOf(searchString, position);
if (source.charAt(index - 1) !== '\\' || source.slice(index - 2, index) === '\\\\') {
break;
}
position = index + 1;
} while (index > -1);
return index;
}
exports.escapedIndexOf = escapedIndexOf;
function escapedSplit(source, separator) {
var result = [];
var part = '';
if (separator === '') {
result.push(source);
return result;
}
for (var i = 0, j = source.length; i < j; ++i) {
if (source.charAt(i) === '\\') {
if (source.slice(i + 1, i + separator.length + 1) === separator) {
part += separator;
i += separator.length;
}
else if (source.charAt(i + 1) === '\\') {
part += '\\';
i += 1;
}
else {
part += source.charAt(i);
}
}
else if (source.slice(i, i + separator.length) === separator) {
result.push(part);
part = '';
i += separator.length - 1;
}
else {
part += source.charAt(i);
}
}
result.push(part);
return result;
}
exports.escapedSplit = escapedSplit;
function escapeXml(text, forAttribute) {
if (forAttribute === void 0) { forAttribute = true; }
text = String(text).replace(/&/g, '&').replace(/</g, '<');
if (forAttribute) {
text = text.replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''');
}
return text;
}
exports.escapeXml = escapeXml;
function getModule(moduleId) {
return getModules([moduleId]).then(function (modules) {
return modules[0];
});
}
exports.getModule = getModule;
function getModules(moduleIds) {
var dfd = new Deferred();
var handle;
if (require.on) {
var moduleUrls = {};
for (var i = 0; i < moduleIds.length; i++) {
moduleUrls[require.toUrl(moduleIds[i])] = moduleIds[i];
}
handle = require.on('error', function (error) {
if (error.message === 'scriptError') {
var moduleUrl = error.info[0].slice(0, -3);
if (moduleUrl in moduleUrls) {
handle && handle.remove();
handle = null;
var reportedError = new Error('Couldn\'t load ' + moduleUrls[moduleUrl] + ' from ' + error.info[0]);
reportedError.url = error.info[0];
reportedError.originalError = error.info[1];
dfd.reject(reportedError);
if (require.undef) {
require.undef(moduleUrls[moduleUrl]);
}
}
}
});
}
require(moduleIds, function () {
var modules = [];
for (var _i = 0; _i < arguments.length; _i++) {
modules[_i - 0] = arguments[_i];
}
handle && handle.remove();
handle = null;
array.every(modules, function (module, index, modules) {
if (module === 'not-a-module') {
var reportedError = new Error('Couldn\'t load module ' + moduleIds[index]);
dfd.reject(reportedError);
return false;
}
return true;
});
dfd.resolve(modules);
});
return dfd.promise;
}
exports.getModules = getModules;
exports.getObjectKeys = has('es5') ? Object.keys : function (object) {
var keys = [], hasOwnProperty = Object.prototype.hasOwnProperty;
for (var key in object) {
hasOwnProperty.call(object, key) && keys.push(key);
}
return keys;
};
function isEqual(a, b) {
return a === b || (a !== a && b !== b);
}
exports.isEqual = isEqual;
function isObject(object) {
var type = typeof object;
return object != null && (type === 'object' || type === 'function');
}
exports.isObject = isObject;
function spliceMatch(haystack, needle) {
for (var i = 0; i < haystack.length; ++i) {
if (haystack[i] === needle) {
haystack.splice(i, 1);
return true;
}
}
return false;
}
exports.spliceMatch = spliceMatch;
function spread(values, resolved, rejected) {
return whenAll(values).then(function (values) {
return resolved.apply(undefined, values);
}, rejected);
}
exports.spread = spread;
function deepMixin(target, source) {
if (typeof source === 'object' && source !== null) {
if (source instanceof Array) {
target.length = source.length;
}
for (var name in source) {
var targetValue = target[name];
var sourceValue = source[name];
if (targetValue !== sourceValue) {
if (typeof sourceValue === 'object' && sourceValue !== null) {
if (targetValue === null || typeof targetValue !== 'object') {
target[name] = targetValue = (sourceValue instanceof Array) ? [] : {};
}
deepMixin(targetValue, sourceValue);
}
else {
target[name] = sourceValue;
}
}
}
}
return target;
}
exports.deepMixin = deepMixin;
function unescapeXml(text) {
var entityMap = {
lt: '<',
gt: '>',
amp: '&',
quot: '"',
apos: '\''
};
return text.replace(/&([^;]+);/g, function (_, entity) {
if (entityMap[entity]) {
return entityMap[entity];
}
if (entity.charAt(0) === '#') {
if (entity.charAt(1) === 'x') {
return String.fromCharCode(Number('0' + entity.slice(1)));
}
return String.fromCharCode(Number(entity.slice(1)));
}
else {
return '&' + entity + ';';
}
});
}
exports.unescapeXml = unescapeXml;
});
//# sourceMappingURL=_debug/util.js.map