nixfilter
Version:
Simplify the development of (UNIX) "Filters"
40 lines (33 loc) • 1.45 kB
text/coffeescript
'use strict'
# 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 = (key_filter_function) ->
(object, mixin_objects...) ->
for mixin_object in mixin_objects
if mixin_object
for key of mixin_object
if key_filter_function(key, mixin_object, object)
object[key] = mixin_object[key]
object
# (<object>, <mixin_objects...>) -> Mixin all OWN properties of <mixin_objects...> into <object>
mixin_own = mixin_factory (key, mixin_object) ->
mixin_object.hasOwnProperty(key)
extend = (parent, properties, static_properties) ->
child = (if properties?.hasOwnProperty('constructor') then properties.constructor else (-> parent.apply(@, arguments)))
child.prototype = Object.create(parent.prototype)
mixin_own(child.prototype, properties)
child.prototype.constructor = child
mixin_own(child, parent, static_properties)
# Returns true if the passed argument <value> is neither null nor undefined
is_valid_value = (value) ->
((value isnt undefined) and (value isnt null))
# Returns the first value in <values...> that is neither null nor undefined
first_valid_value = (values...) ->
for value in values
if is_valid_value(value)
return value
return
# What this module exports
module.exports =
extend: extend
first_valid_value: first_valid_value
mixin_own: mixin_own