mirror of
https://github.com/datahub-project/datahub.git
synced 2025-10-11 00:48:39 +00:00
77 lines
1.7 KiB
JavaScript
77 lines
1.7 KiB
JavaScript
/*
|
|
* 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;
|
|
};
|
|
};
|