-
Notifications
You must be signed in to change notification settings - Fork 784
/
utilities.js
181 lines (157 loc) 路 4.37 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
import { window } from "../globals";
import Logger from "../logger";
export const toString = Object.prototype.toString;
export const hasOwn = Object.prototype.hasOwnProperty;
export const now = Date.now || function() {
return new Date().getTime();
};
const nativePerf = getNativePerf();
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 ) :
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 ) {
const result = a.slice();
for ( let i = 0; i < result.length; i++ ) {
for ( let j = 0; j < b.length; j++ ) {
if ( result[ i ] === b[ j ] ) {
result.splice( i, 1 );
i--;
break;
}
}
}
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;
}
/**
* Makes a clone of an object using only Array or Object as base,
* and copies over the own enumerable properties.
*
* @param {Object} obj
* @return {Object} New object with only the own properties (recursively).
*/
export function objectValues( obj ) {
const vals = is( "array", obj ) ? [] : {};
for ( const key in obj ) {
if ( hasOwn.call( obj, key ) ) {
const val = obj[ key ];
vals[ key ] = val === Object( val ) ? objectValues( val ) : val;
}
}
return vals;
}
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;
}
}