-
Notifications
You must be signed in to change notification settings - Fork 785
/
utilities.js
207 lines (183 loc) 路 5.5 KB
/
utilities.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
import { window } from '../globals';
import Logger from '../logger';
export const toString = Object.prototype.toString;
export const hasOwn = Object.prototype.hasOwnProperty;
const nativePerf = getNativePerf();
// TODO: Consider using globalThis instead so that perf marks work
// in Node.js as well. As they can have overhead, we should also
// have a way to disable these, and/or make them an opt-in reporter
// in QUnit 3 and then support globalThis.
// For example: `QUnit.addReporter(QUnit.reporters.perf)`.
function getNativePerf () {
if (window &&
typeof window.performance !== 'undefined' &&
typeof window.performance.mark === 'function' &&
typeof window.performance.measure === 'function'
) {
return window.performance;
} else {
return undefined;
}
}
export const performance = {
now: nativePerf
? nativePerf.now.bind(nativePerf)
: Date.now,
measure: nativePerf
? function (comment, startMark, endMark) {
// `performance.measure` may fail if the mark could not be found.
// reasons a specific mark could not be found include: outside code invoking `performance.clearMarks()`
try {
nativePerf.measure(comment, startMark, endMark);
} catch (ex) {
Logger.warn('performance.measure could not be executed because of ', ex.message);
}
}
: function () {},
mark: nativePerf ? nativePerf.mark.bind(nativePerf) : function () {}
};
// Returns a new Array with the elements that are in a but not in b
export function diff (a, b) {
return a.reduce((result, item) => {
if (b.indexOf(item) === -1) {
result.push(item);
}
return result;
}, []);
}
/**
* Determines whether an element exists in a given array or not.
*
* @method inArray
* @param {any} elem
* @param {Array} array
* @return {boolean}
*/
export function inArray (elem, array) {
return array.indexOf(elem) !== -1;
}
/**
* Recursively clone an object into a plain array or object, taking only the
* own enumerable properties.
*
* @param {any} obj
* @param {bool} [allowArray=true]
* @return {Object|Array}
*/
export function objectValues (obj, allowArray = true) {
const vals = (allowArray && is('array', obj)) ? [] : {};
for (const key in obj) {
if (hasOwn.call(obj, key)) {
const val = obj[key];
vals[key] = val === Object(val) ? objectValues(val, allowArray) : val;
}
}
return vals;
}
/**
* Recursively clone an object into a plain object, taking only the
* subset of own enumerable properties that exist a given model.
*
* @param {any} obj
* @param {any} model
* @return {Object}
*/
export function objectValuesSubset (obj, model) {
// Return primitive values unchanged to avoid false positives or confusing
// results from assert.propContains().
// E.g. an actual null or false wrongly equaling an empty object,
// or an actual string being reported as object not matching a partial object.
if (obj !== Object(obj)) {
return obj;
}
// Unlike objectValues(), subset arrays to a plain objects as well.
// This enables subsetting [20, 30] with {1: 30}.
const subset = {};
for (const key in model) {
if (hasOwn.call(model, key) && hasOwn.call(obj, key)) {
subset[key] = objectValuesSubset(obj[key], model[key]);
}
}
return subset;
}
export function extend (a, b, undefOnly) {
for (const prop in b) {
if (hasOwn.call(b, prop)) {
if (b[prop] === undefined) {
delete a[prop];
} else if (!(undefOnly && typeof a[prop] !== 'undefined')) {
a[prop] = b[prop];
}
}
}
return a;
}
export function objectType (obj) {
if (typeof obj === 'undefined') {
return 'undefined';
}
// Consider: typeof null === object
if (obj === null) {
return 'null';
}
const match = toString.call(obj).match(/^\[object\s(.*)\]$/);
const type = match && match[1];
switch (type) {
case 'Number':
if (isNaN(obj)) {
return 'nan';
}
return 'number';
case 'String':
case 'Boolean':
case 'Array':
case 'Set':
case 'Map':
case 'Date':
case 'RegExp':
case 'Function':
case 'Symbol':
return type.toLowerCase();
default:
return typeof obj;
}
}
// Safe object type checking
export function is (type, obj) {
return objectType(obj) === type;
}
// Based on Java's String.hashCode, a simple but not
// rigorously collision resistant hashing function
export function generateHash (module, testName) {
const str = module + '\x1C' + testName;
let hash = 0;
for (let i = 0; i < str.length; i++) {
hash = ((hash << 5) - hash) + str.charCodeAt(i);
hash |= 0;
}
// Convert the possibly negative integer hash code into an 8 character hex string, which isn't
// strictly necessary but increases user understanding that the id is a SHA-like hash
let hex = (0x100000000 + hash).toString(16);
if (hex.length < 8) {
hex = '0000000' + hex;
}
return hex.slice(-8);
}
/**
* Converts an error into a simple string for comparisons.
*
* @param {Error|any} error
* @return {string}
*/
export function errorString (error) {
// Use String() instead of toString() to handle non-object values like undefined or null.
const resultErrorString = String(error);
// If the error wasn't a subclass of Error but something like
// an object literal with name and message properties...
if (resultErrorString.slice(0, 7) === '[object') {
// Based on https://es5.github.io/#x15.11.4.4
return (error.name || 'Error') + (error.message ? `: ${error.message}` : '');
} else {
return resultErrorString;
}
}