/
utils.js
224 lines (199 loc) · 8.22 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
/* eslint-disable no-proto */
var canSetPrototype = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array);
var greedyIntervalPacker = require('greedy-interval-packer');
var setPrototypeOf = Object.setPrototypeOf || function setPrototypeOf(obj, proto) {
obj.__proto__ = proto;
return obj;
};
/* eslint-enable no-proto */
var utils = module.exports = {
objectIs: Object.is || function (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;
}
if (a !== a) {
return b !== b;
}
return a === b;
},
isArray: function (ar) {
return Object.prototype.toString.call(ar) === '[object Array]';
},
isRegExp: function (re) {
return (Object.prototype.toString.call(re) === '[object RegExp]');
},
isError: function (err) {
return typeof err === 'object' && (Object.prototype.toString.call(err) === '[object Error]' || err instanceof Error);
},
extend: function (target) {
for (var i = 1; i < arguments.length; i += 1) {
var source = arguments[i];
if (source) {
Object.keys(source).forEach(function (key) {
target[key] = source[key];
});
}
}
return target;
},
findFirst: function (arr, predicate) {
for (var i = 0 ; i < arr.length ; i += 1) {
if (predicate(arr[i])) {
return arr[i];
}
}
return null;
},
leftPad: function (str, width, ch) {
ch = ch || ' ';
while (str.length < width) {
str = ch + str;
}
return str;
},
escapeRegExpMetaChars: function (str) {
return str.replace(/[[\]{}()*+?.\\^$|]/g, '\\$&');
},
escapeChar: function (ch) {
if (ch === '\t') {
return '\\t';
} else if (ch === '\r') {
return '\\r';
} else {
var charCode = ch.charCodeAt(0);
var hexChars = charCode.toString(16).toUpperCase();
if (charCode < 256) {
return '\\x' + utils.leftPad(hexChars, 2, '0');
} else {
return '\\u' + utils.leftPad(hexChars, 4, '0');
}
}
},
getFunctionName: function (f) {
if (typeof f.name === 'string') {
return f.name;
}
var 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: function (output, obj) {
var constructor = obj.constructor;
var 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 (var prop in source) {
if (source.hasOwnProperty(prop)) {
target[prop] = source[prop];
}
}
return target;
},
uniqueStringsAndSymbols: function () { // [filterFn], item1, item2...
var filterFn;
if (typeof arguments[0] === 'function') {
filterFn = arguments[0];
}
var index = {};
var 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 (var i = filterFn ? 1 : 0 ; i < arguments.length ; i += 1) {
visit(arguments[i]);
}
return uniqueStringsAndSymbols;
},
uniqueNonNumericalStringsAndSymbols: function () { // ...
return utils.uniqueStringsAndSymbols(function (stringOrSymbol) {
return typeof stringOrSymbol === 'symbol' || !utils.numericalRegExp.test(stringOrSymbol);
}, Array.prototype.slice.call(arguments));
},
forwardFlags: function (testDescriptionString, flags) {
return testDescriptionString.replace(/\[(!?)([^\]]+)\] ?/g, function (match, negate, flag) {
return Boolean(flags[flag]) !== Boolean(negate) ? flag + ' ' : '';
}).trim();
},
numericalRegExp: /^(?:0|[1-9][0-9]*)$/,
packArrows: function (changes) {
var moveSourceAndTargetByActualIndex = {};
changes.forEach(function (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;
}
});
var moveIndices = Object.keys(moveSourceAndTargetByActualIndex);
if (moveIndices.length > 0) {
var arrowSpecs = [];
moveIndices.sort(function (a, b) {
// Order by distance between change indices descending
return Math.abs(moveSourceAndTargetByActualIndex[b].source.changeIndex - moveSourceAndTargetByActualIndex[b].target.changeIndex) - Math.abs(moveSourceAndTargetByActualIndex[a].source.changeIndex - moveSourceAndTargetByActualIndex[a].target.changeIndex);
}).forEach(function (actualIndex, i, keys) {
var moveSourceAndMoveTarget = moveSourceAndTargetByActualIndex[actualIndex];
var firstChangeIndex = Math.min(moveSourceAndMoveTarget.source.changeIndex, moveSourceAndMoveTarget.target.changeIndex);
var lastChangeIndex = Math.max(moveSourceAndMoveTarget.source.changeIndex, moveSourceAndMoveTarget.target.changeIndex);
arrowSpecs.push({
start: firstChangeIndex,
end: lastChangeIndex,
direction: moveSourceAndMoveTarget.source.changeIndex < moveSourceAndMoveTarget.target.changeIndex ? 'down' : 'up'
});
});
var 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(function (entry) {
changes[entry.direction === 'up' ? entry.start : entry.end].type = 'insert';
changes[entry.direction === 'up' ? entry.end : entry.start].type = 'remove';
});
}
return packing;
}
},
truncateSubjectStringForBegin: function (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: function (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);
}
}
};