-
Notifications
You must be signed in to change notification settings - Fork 476
/
Copy pathutils.js
64 lines (57 loc) · 1.91 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
import { migrateWarn } from "./main.js";
export function camelCase( string ) {
return string.replace( /-([a-z])/g, function( _, letter ) {
return letter.toUpperCase();
} );
}
// Make `object` inherit from `Object.prototype` via an additional object
// in between; that intermediate object proxies properties
// to `Object.prototype`, warning about their usage first.
export function patchProto( object, options ) {
var i,
warningId = options.warningId,
apiName = options.apiName,
// `Object.prototype` keys are not enumerable so list the
// official ones here. An alternative would be wrapping
// objects with a Proxy but that creates additional issues
// like breaking object identity on subsequent calls.
objProtoKeys = [
"__proto__",
"__defineGetter__",
"__defineSetter__",
"__lookupGetter__",
"__lookupSetter__",
"hasOwnProperty",
"isPrototypeOf",
"propertyIsEnumerable",
"toLocaleString",
"toString",
"valueOf"
],
// Use a null prototype at the beginning so that we can define our
// `__proto__` getter & setter. We'll reset the prototype afterward.
intermediateObj = Object.create( null );
for ( i = 0; i < objProtoKeys.length; i++ ) {
( function( key ) {
Object.defineProperty( intermediateObj, key, {
get: function() {
migrateWarn( warningId,
"Accessing properties from " + apiName +
" inherited from Object.prototype is removed" );
return ( key + "__cache" ) in intermediateObj ?
intermediateObj[ key + "__cache" ] :
Object.prototype[ key ];
},
set: function( value ) {
migrateWarn( warningId,
"Setting properties from " + apiName +
" inherited from Object.prototype is removed" );
intermediateObj[ key + "__cache" ] = value;
}
} );
} )( objProtoKeys[ i ] );
}
Object.setPrototypeOf( intermediateObj, Object.prototype );
Object.setPrototypeOf( object, intermediateObj );
return object;
}