forked from dagrejs/dagre-d3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
76 lines (66 loc) · 1.66 KB
/
util.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
/*
* Returns the smallest value in the array.
*/
exports.min = function(values) {
return Math.min.apply(null, values);
};
/*
* Returns the largest value in the array.
*/
exports.max = function(values) {
return Math.max.apply(null, values);
};
/*
* Returns `true` only if `f(x)` is `true` for all `x` in `xs`. Otherwise
* returns `false`. This function will return immediately if it finds a
* case where `f(x)` does not hold.
*/
exports.all = function(xs, f) {
for (var i = 0; i < xs.length; ++i) {
if (!f(xs[i])) {
return false;
}
}
return true;
};
/*
* Accumulates the sum of elements in the given array using the `+` operator.
*/
exports.sum = function(values) {
return values.reduce(function(acc, x) { return acc + x; }, 0);
};
/*
* Returns an array of all values in the given object.
*/
exports.values = function(obj) {
return Object.keys(obj).map(function(k) { return obj[k]; });
};
exports.createTimer = function(enabled) {
var self = {};
// Default to disabled
enabled = enabled || false;
self.enabled = function(x) {
if (!arguments.length) return enabled;
enabled = x;
return self;
};
self.wrap = function(name, func) {
return function() {
var start = enabled ? new Date().getTime() : null;
try {
return func.apply(null, arguments);
} finally {
if (start) console.log(name + " time: " + (new Date().getTime() - start) + "ms");
}
};
};
return self;
};
exports.propertyAccessor = function(self, config, field, setHook) {
return function(x) {
if (!arguments.length) return config[field];
config[field] = x;
if (setHook) setHook(x);
return self;
};
};