-
Notifications
You must be signed in to change notification settings - Fork 2
/
object.js
102 lines (93 loc) · 2.62 KB
/
object.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
define(['exports', './has'], function(exports, has){
has.add('object-is', typeof Object.is === 'function');
has.add('object-isnt', typeof Object.isnt === 'function');
exports.is = has('object-is') ?
function(x, y){
return Object.is(x, y);
} :
function(x, y){
// taken from http://wiki.ecmascript.org/doku.php?id=harmony:egal
if(x === y){
// 0 === -0, but they aren't identical
return x !== 0 || 1 / x === 1 / y;
}
// NaN !== NaN, but they are identical
// NaNs are the only non-reflexive value, i.e., if x !== x,
// then x is a NaN.
// isNaN is broken: it converts its argument to number, so
// isNaN("foo") => true
return x !== x && y !== y;
};
exports.isnt = has('object-isnt') ?
function(x, y){
return Object.isnt(x, y);
} :
has('object-is') ?
function(x, y){
return !Object.is(x, y);
} :
function(x, y){
return !exports.is(x, y);
};
exports.forIn = function(object, callback, thisObject){
thisObject = thisObject || null;
for(var key in object){
callback.call(thisObject, object[key], key, object);
}
};
var hop = {}.hasOwnProperty;
exports.forOwn = function(object, callback, thisObject){
thisObject = thisObject || null;
for(var key in object){
if(hop.call(object, key)){
callback.call(thisObject, object[key], key, object);
}
}
};
function copy(funcName){
return function(target, source, copyFunc){
exports[funcName](source, copyFunc || function(value, key){
target[key] = value;
});
return target;
};
}
exports.copy = copy('forIn');
exports.copyOwn = copy('forOwn');
exports.defineProperty = function(object, key, value){
var property = {
enumerable: true,
writable: true,
configurable: true
};
if(typeof value === 'object' && value && !Array.isArray(value) &&
(hop.call(value, 'value') || hop.call(value, 'get') || hop.call(value, 'set')) &&
(hop.call(value, 'enumerable') || hop.call(value, 'configurable') || hop.call(value, 'writable'))){
exports.copy(property, value);
if(hop.call(property, 'get') || hop.call(property, 'set')){
delete property.writable;
}
}else{
property.value = value;
}
return Object.defineProperty(object, key, property);
};
exports.defineProperties = function(object, properties){
exports.forOwn(properties, function(value, key){
exports.defineProperty(object, key, value);
});
return object;
};
exports.get = function(context, lookup){
if(!context){
return null;
}
var parts = lookup.split('.'),
i = 0,
p;
while(context && (p = parts[i++])){
context = (p in context ? context[p] : undefined);
}
return context;
};
});