forked from QUT-eResearch/jsx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.js
More file actions
60 lines (56 loc) · 1.72 KB
/
object.js
File metadata and controls
60 lines (56 loc) · 1.72 KB
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
'use strict';
/**
* Utilities for objects manipulation
*/
/**
* Iterate object's own property
* onEachProperty(key, value)
*/
var o = module.exports = {};
o.each = function(obj, onEachProperty) {
if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {
throw new TypeError('Object.keys called on non-object');
}
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
onEachProperty(prop, obj[prop]);
}
}
};
/**
* Add all object's own properties in source to target. If overwrite is true, existing property will be replaced.
*/
o.merge = function (source, target, overwrite) {
o.each(source, function(key, val) {
if (overwrite || !(key in target)) {
if (typeof val === 'object' && Object.getPrototypeOf(val) === Object.prototype) {
var tval = target[key];
if (typeof tval !== 'object' || Object.getPrototypeOf(tval) !== Object.prototype) {
tval = target[key] = {};
}
o.merge(val, tval);
} else {
target[key] = val;
}
}
});
return target;
}
/**
* Add all object's own properties in defaults to target, only if it does not exist in target.
* If the type if property is different, the defaults has more priority.
*/
o.mergeDefaults = function (defaults, target) {
o.each(defaults, function(key, val) {
if (typeof val === 'object' && Object.getPrototypeOf(val) === Object.prototype) {
var tval = target[key];
if (typeof tval !== 'object' || Object.getPrototypeOf(tval) !== Object.prototype) {
tval = target[key] = {};
}
o.mergeDefaults(val, tval);
} else if (!(key in target) || (typeof val !== typeof target[key])) {
target[key] = val;
}
});
return target;
}