-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathutils.js
403 lines (360 loc) · 11.2 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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/* eslint-disable no-proto */
/* global Deno */
const canSetPrototype =
Object.setPrototypeOf || { __proto__: [] } instanceof Array;
const greedyIntervalPacker = require('greedy-interval-packer');
const magicpen = require('magicpen');
const setPrototypeOf =
Object.setPrototypeOf ||
function setPrototypeOf(obj, proto) {
obj.__proto__ = proto;
return obj;
};
/* eslint-enable no-proto */
const utils = (module.exports = {
objectIs:
Object.is ||
((a, b) => {
// Polyfill from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
if (a === 0 && b === 0) {
return 1 / a === 1 / b;
}
// eslint-disable-next-line no-self-compare
if (a !== a) {
// eslint-disable-next-line no-self-compare
return b !== b;
}
return a === b;
}),
checkObjectEqualityUsingType(a, b, type, isEqual) {
if (a === b) {
return true;
}
if (b.constructor !== a.constructor) {
return false;
}
const actualKeys = type
.getKeys(a)
.filter(key => typeof type.valueForKey(a, key) !== 'undefined');
const expectedKeys = type
.getKeys(b)
.filter(key => typeof type.valueForKey(b, key) !== 'undefined');
// having the same number of owned properties (keys incorporates hasOwnProperty)
if (actualKeys.length !== expectedKeys.length) {
return false;
}
// the same set of keys (although not necessarily the same order),
actualKeys.sort(type.keyComparator);
expectedKeys.sort(type.keyComparator);
// cheap key test
for (let i = 0; i < actualKeys.length; i += 1) {
if (actualKeys[i] !== expectedKeys[i]) {
return false;
}
}
// equivalent values for every corresponding key, and
// possibly expensive deep test
for (let j = 0; j < actualKeys.length; j += 1) {
const key = actualKeys[j];
if (!isEqual(type.valueForKey(a, key), type.valueForKey(b, key))) {
return false;
}
}
return true;
},
duplicateArrayLikeUsingType(obj, type) {
const keys = type.getKeys(obj);
let numericalKeyLength = keys.length;
if (!type.numericalPropertiesOnly) {
let nonNumericalKeyLength = 0;
// find non-numerical keys in reverse order to keep iteration minimal
for (let i = keys.length - 1; i > -1; i -= 1) {
const key = keys[i];
if (typeof key === 'symbol' || !utils.numericalRegExp.test(key)) {
nonNumericalKeyLength += 1;
} else {
break;
}
}
// remove non-numerical keys to ensure the copy is sized correctly
numericalKeyLength -= nonNumericalKeyLength;
}
const arr = new Array(numericalKeyLength);
keys.forEach(function(key, index) {
const isNonNumericKey = index >= numericalKeyLength;
if (isNonNumericKey && !type.hasKey(obj, key)) {
// do not add non-numerical keys that are not actually attached
// to the array-like to ensure they will be treated as "missing"
return;
}
arr[key] = type.hasKey(obj, key) ? type.valueForKey(obj, key) : undefined;
});
return arr;
},
isArray(ar) {
return Object.prototype.toString.call(ar) === '[object Array]';
},
isPromise(obj) {
return obj && typeof obj.then === 'function';
},
isRegExp(re) {
return Object.prototype.toString.call(re) === '[object RegExp]';
},
isError(err) {
return (
typeof err === 'object' &&
(Object.prototype.toString.call(err) === '[object Error]' ||
err instanceof Error)
);
},
extend(target) {
for (let i = 1; i < arguments.length; i += 1) {
const source = arguments[i];
if (source) {
Object.keys(source).forEach(key => {
target[key] = source[key];
});
}
}
return target;
},
findFirst(arr, predicate) {
for (let i = 0; i < arr.length; i += 1) {
if (predicate(arr[i])) {
return arr[i];
}
}
return null;
},
leftPad(str, width, ch = ' ') {
while (str.length < width) {
str = ch + str;
}
return str;
},
escapeRegExpMetaChars(str) {
return str.replace(/[[\]{}()*+?.\\^$|]/g, '\\$&');
},
escapeChar(ch) {
if (ch === '\t') {
return '\\t';
} else if (ch === '\r') {
return '\\r';
} else {
const charCode = ch.charCodeAt(0);
const hexChars = charCode.toString(16).toUpperCase();
if (charCode < 256) {
return `\\x${utils.leftPad(hexChars, 2, '0')}`;
} else {
return `\\u${utils.leftPad(hexChars, 4, '0')}`;
}
}
},
getFunctionName(f) {
if (typeof f.name === 'string') {
return f.name;
}
const matchFunctionName = Function.prototype.toString
.call(f)
.match(/function ([^(]+)/);
if (matchFunctionName) {
return matchFunctionName[1];
}
if (f === Object) {
return 'Object';
}
if (f === Function) {
return 'Function';
}
return '';
},
wrapConstructorNameAroundOutput(output, obj) {
const constructor = obj.constructor;
const constructorName =
constructor &&
constructor !== Object &&
utils.getFunctionName(constructor);
if (constructorName && constructorName !== 'Object') {
return output
.clone()
.text(`${constructorName}(`)
.append(output)
.text(')');
} else {
return output;
}
},
setPrototypeOfOrExtend: canSetPrototype
? setPrototypeOf
: function extend(target, source) {
for (const prop in source) {
if (Object.prototype.hasOwnProperty.call(source, prop)) {
target[prop] = source[prop];
}
}
return target;
},
uniqueStringsAndSymbols(...args) {
// [filterFn], item1, item2...
let filterFn;
if (typeof args[0] === 'function') {
filterFn = args[0];
}
const index = {};
const uniqueStringsAndSymbols = [];
function visit(item) {
if (Array.isArray(item)) {
item.forEach(visit);
} else if (
!Object.prototype.hasOwnProperty.call(index, item) &&
(!filterFn || filterFn(item))
) {
index[item] = true;
uniqueStringsAndSymbols.push(item);
}
}
for (let i = filterFn ? 1 : 0; i < args.length; i += 1) {
visit(args[i]);
}
return uniqueStringsAndSymbols;
},
uniqueNonNumericalStringsAndSymbols(...args) {
// ...
return utils.uniqueStringsAndSymbols(
stringOrSymbol =>
typeof stringOrSymbol === 'symbol' ||
!utils.numericalRegExp.test(stringOrSymbol),
Array.prototype.slice.call(args)
);
},
forwardFlags(testDescriptionString, flags) {
return testDescriptionString
.replace(/\[(!?)([^\]]+)\] ?/g, (match, negate, flag) =>
Boolean(flags[flag]) !== Boolean(negate) ? `${flag} ` : ''
)
.trim();
},
numericalRegExp: /^(?:0|[1-9][0-9]*)$/,
packArrows(changes) {
const moveSourceAndTargetByActualIndex = {};
changes.forEach((diffItem, index) => {
if (diffItem.type === 'moveSource') {
diffItem.changeIndex = index;
(moveSourceAndTargetByActualIndex[diffItem.actualIndex] =
moveSourceAndTargetByActualIndex[diffItem.actualIndex] ||
{}).source = diffItem;
} else if (diffItem.type === 'moveTarget') {
diffItem.changeIndex = index;
(moveSourceAndTargetByActualIndex[diffItem.actualIndex] =
moveSourceAndTargetByActualIndex[diffItem.actualIndex] ||
{}).target = diffItem;
}
});
const moveIndices = Object.keys(moveSourceAndTargetByActualIndex);
if (moveIndices.length > 0) {
const arrowSpecs = [];
moveIndices
.sort(
(
a,
b // Order by distance between change indices descending
) =>
Math.abs(
moveSourceAndTargetByActualIndex[b].source.changeIndex -
moveSourceAndTargetByActualIndex[b].target.changeIndex
) -
Math.abs(
moveSourceAndTargetByActualIndex[a].source.changeIndex -
moveSourceAndTargetByActualIndex[a].target.changeIndex
)
)
.forEach((actualIndex, i, keys) => {
const moveSourceAndMoveTarget =
moveSourceAndTargetByActualIndex[actualIndex];
const firstChangeIndex = Math.min(
moveSourceAndMoveTarget.source.changeIndex,
moveSourceAndMoveTarget.target.changeIndex
);
const lastChangeIndex = Math.max(
moveSourceAndMoveTarget.source.changeIndex,
moveSourceAndMoveTarget.target.changeIndex
);
arrowSpecs.push({
start: firstChangeIndex,
end: lastChangeIndex,
direction:
moveSourceAndMoveTarget.source.changeIndex <
moveSourceAndMoveTarget.target.changeIndex
? 'down'
: 'up'
});
});
const packing = greedyIntervalPacker(arrowSpecs);
while (packing.length > 3) {
// The arrow packing takes up too many lanes. Turn the moveSource/moveTarget items into inserts and removes
packing.shift().forEach(({ direction, start, end }) => {
changes[direction === 'up' ? start : end].type = 'insert';
changes[direction === 'up' ? end : start].type = 'remove';
});
}
return packing;
}
},
truncateSubjectStringForBegin(subject, value) {
var contextLength = value.length + 25;
if (subject.length <= contextLength) {
return null;
}
var truncationIndex = subject.indexOf(' ', value.length + 1);
if (truncationIndex > -1 && truncationIndex < contextLength) {
return subject.substring(0, truncationIndex);
} else {
return subject.substring(0, contextLength);
}
},
truncateSubjectStringForEnd(subject, value) {
var contextLength = subject.length - value.length - 25;
if (contextLength <= 0) {
return null;
}
var truncationIndex = subject.lastIndexOf(' ', value.length + 1);
if (truncationIndex > -1 && truncationIndex >= contextLength) {
return subject.substring(truncationIndex + 1, subject.length);
} else {
return subject.substring(contextLength, subject.length);
}
},
getEnv(varName) {
if (typeof Deno === 'object') {
try {
return Deno.env()[varName];
} catch (err) {
// Probably a permissions error because we don't have permission to read the environment variables
// Unfortunately the whole permissions API is async now:
// https://github.com/denoland/deno/pull/3200/files
// ... so we can't detect whether we do have access
}
} else if (typeof process === 'object' && process.env) {
return process.env[varName];
}
},
createDeprecationWarning(message) {
let deprecationWarningDisplayed = false;
return () => {
if (deprecationWarningDisplayed) {
return;
}
deprecationWarningDisplayed = true;
let format = magicpen.defaultFormat;
if (format === 'html') {
// override given this will be output in the browser console
format = 'text';
}
console.warn(
magicpen()
.text(message, 'bgYellow', 'black')
.toString(format)
);
};
}
});