nixfilter
Version:
Simplify the development of (UNIX) "Filters"
62 lines (54 loc) • 1.99 kB
JavaScript
;
var extend, first_valid_value, is_valid_value, mixin_factory, mixin_own;
// Creates and returns mixin function (<object>, <mixin_objects...>) -> that mixins all properties for which <key_filter_function>(<key>, <mixin_object>, <object>) returns true into <object>
mixin_factory = function(key_filter_function) {
return function(object, ...mixin_objects) {
var i, key, len, mixin_object;
for (i = 0, len = mixin_objects.length; i < len; i++) {
mixin_object = mixin_objects[i];
if (mixin_object) {
for (key in mixin_object) {
if (key_filter_function(key, mixin_object, object)) {
object[key] = mixin_object[key];
}
}
}
}
return object;
};
};
// (<object>, <mixin_objects...>) -> Mixin all OWN properties of <mixin_objects...> into <object>
mixin_own = mixin_factory(function(key, mixin_object) {
return mixin_object.hasOwnProperty(key);
});
extend = function(parent, properties, static_properties) {
var child;
child = ((properties != null ? properties.hasOwnProperty('constructor') : void 0) ? properties.constructor : (function() {
return parent.apply(this, arguments);
}));
child.prototype = Object.create(parent.prototype);
mixin_own(child.prototype, properties);
child.prototype.constructor = child;
return mixin_own(child, parent, static_properties);
};
// Returns true if the passed argument <value> is neither null nor undefined
is_valid_value = function(value) {
return (value !== void 0) && (value !== null);
};
// Returns the first value in <values...> that is neither null nor undefined
first_valid_value = function(...values) {
var i, len, value;
for (i = 0, len = values.length; i < len; i++) {
value = values[i];
if (is_valid_value(value)) {
return value;
}
}
};
// What this module exports
module.exports = {
extend: extend,
first_valid_value: first_valid_value,
mixin_own: mixin_own
};
//# sourceMappingURL=utils.js.map