LUtils
A few important utilities which are implimented differently to libraries like underscore
. Each module is as small as it needs to be, which is ideal for use in the browser.
Include all or be selective.
{
clone, merge, typeOf, debounce, throttle
Transposer, Interpolator
} = require 'lance-utils'
typeOf = require 'lance-utils/typeOf'
Clone
clone( object, depth = 8 )
Clones an object.
Tries to JSON clone it first, then clone.merge it if that fails, while also merging in clone.findRegExp to preserve any RegExp.
clone.merge( object, depth = 10 )
Deep clones by recursively iterating over each object/array key, building a new one with each value.
clone.json( object )
Clones via JSON.stringify then JSON.parse
clone.findRegExp( object )
Recursively finds all RegExp in an object by creating a deep skeleton object containing only the RegExp values. Useful when you wish to preserve RegExp (which can't be done with JSON cloning).
Merge
merge( object1, object2, depth = 8, iterators = [ 'object' ] )
Merge the second object into the first recursively until depth is reached for each property, replacing object1's values with those in object2.
iterators
is an array of types that, when matched on a value, will be iterated over and merged in. This means you can merge a function's properties or an array's properties recursively, thus preserving pointer references to the first object's instance.
fn1 = -> return 'fn1' + fn1.prop.b
fn1.prop = { a: 1 }
fn2 = -> return 'fn2' + fn2.prop.a
fn2.prop = { b: 2 }
obj1 = { a: { b: { fn: fn1 } } }
obj2 = { a: { b: { fn: fn2 } } }
merge obj1, obj2
# >> { a: { b: { fn: [Function] } } }
obj1.a.b.fn.prop
# >> { a: 1, b: 2 }
obj1.a.b.fn()
# 'fn1!2'
merge.white( object1, object2, depth = 8, iterators = [ 'object' ] )
Whitelisted merge.
Merges properties into object1 from object only if the property exists in object1
merge.black( object1, object2, depth = 8, iterators = [ 'object' ] )
Blacklisted merge.
Merges properties into object1 from object only if the property doesnt exist in object1
merge.iterators = [ 'object' ]
Default iterator types array.
merge.depth = 8
Default depth.
TypeOf
typeOf( value )
Returns the primitive type of a value as a lowercase string, very reliable.
To be used in combination with instanceof
and object.constructor.name
when necessary.
typeOf 'a string' # >> 'object'
typeOf { an: { object: null } } # >> 'object'
typeOf null # >> 'null'
typeOf 0 # >> 'number'
Also has helper properties which return a boolean.
typeOf.RegExp 'not regex' # false
typeOf.Object null # false
typeOf.Array [] # true
###
Avaliable properties (Also in lowercase):
typeOf.Undefined
typeOf.Boolean
typeOf.String
typeOf.Function
typeOf.Array
typeOf.Object
typeOf.Null
typeOf.Number
typeOf.Date
typeOf.RegExp
typeOf.NaN
typeOf.Symbol
typeOf.Buffer
###
Debounce
debounce( options, callback )
# Default options
options = {
wait: 0 # Time in ms to accumulate callbacks before finishing
maxWait: Infinity # When reached will call the callback no matter what
immediate: false # Whether to immediately call the callback the first time (Currently broken)
}
options.wait = 1000
fn = debounce options, ->
console.log 'woo!'
setInterval fn, 100
# In 10 seconds, 'woo!' will be logged 10 times instead of 100 times