forked from af83/nodetk
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
65 lines (59 loc) · 1.59 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
/* Some every-day useful functions.
*/
exports.extend = function(target, obj1, obj2) {
/* Merge the contents of two or more objects together into the first object.
* Returns the target.
*
* Arguments:
* - target: where to merge
* - obj1, obj2...: what to merge.
*/
target = target || {};
var objs = Array.prototype.slice.apply(arguments, [1]);
objs.forEach(function(obj) {
for(var attr in obj) {
target[attr] = obj[attr];
}
});
return target;
};
var deep_extend = exports.deep_extend = function(target, obj1, obj2) {
/* Deeply merge obj1 and obj2 (and any other given obj) in target
* Array are not considered as objects, so the last arriving will
* override any already defined array.
*/
target = target || {};
var objs = Array.prototype.slice.apply(arguments, [1]);
objs.forEach(function(obj) {
for(var attr in obj) {
var val = obj[attr];
if(typeof val == "object" && !isArray(val)) {
target[attr] = deep_extend(target[attr] || {}, val);
}
else {
target[attr] = val;
}
}
});
return target;
};
exports.count_properties = function(obj) {
/* Counts and returns the number of properties in obj.
*/
var count = 0;
for(property in obj) count++;
return count;
}
var isArray = exports.isArray = function(obj) {
/** Returns True if obj is an Array.
*/
return obj.constructor == Array;
};
exports.each = function(obj, callback) {
/** Call callback(attr_name, attr_value) for each attr of obj.
* Only works on Objects.
*/
for(var attr in obj) {
callback(attr, obj[attr]);
}
};