Package Exports
- object-tools
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (object-tools) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
#object-tools Useful functions for working with objects
####Example
var o = require("object-tools");
Contents
- extend(...object)
- clone(input)
- omit(object, toOmit)
- every(object, iterator)
- each(object, callback)
- exists(object, query)
- without(object, toRemove)
###extend(...object) Merge a list of objects, left to right, into one.
- ...object
Object
a sequence of Object instances to be extended
Returns: Object
####Example
> o.extend({ one: 1, three: 3 }, { one: "one", two: 2 }, { four: 4 });
{ one: 'one',
three: 3,
two: 2,
four: 4 }
###clone(input) Clones an object or array
- input
Object | Array
the input to clone
Returns: Object | Array
####Example
> date = new Date()
Fri May 09 2014 13:54:34 GMT+0200 (CEST)
> o.clone(date)
{} // a Date instance doesn't own any properties
> date.clive = "hater"
'hater'
> o.clone(date)
{ clive: 'hater' }
> array = [1,2,3]
[ 1, 2, 3 ]
> newArray = o.clone(array)
[ 1, 2, 3 ]
> array === newArray
false
###omit(object, toOmit) Returns a clone of the input object, minus the specified properties
- object
Object
the object to clone - toOmit
Array.<string>
an array of property names to omit from the clone
Returns: Object
####Example
> o.omit({ one: 1, two: 2, three: 3, four: 4 }, [ "two", "four" ]);
{ one: 1, three: 3 }
###every(object, iterator) Returns true if the supplied iterator function returns true for every property in the object
- object
Object
the object to inspect - iterator
function
the iterator function to run against each key/value pair, the args are(value, key)
.
Returns: Boolean
####Example
> function aboveTen(input){ return input > 10; }
undefined
> o.every({ eggs: 12, carrots: 30, peas: 100 }, aboveTen)
true
> o.every({ eggs: 6, carrots: 30, peas: 100 }, aboveTen)
false
###each(object, callback) Runs the iterator function against every key/value pair in the input object
- object
Object
the object to iterate - callback
function
the iterator function to run against each key/value pair, the args are(value, key)
.
####Example
> var total = 0;
undefined
> function addToTotal(n){ total += n; }
undefined
> o.each({ eggs: 3, celery: 2, carrots: 1 }, addToTotal)
undefined
> total
6
###exists(object, query)
returns true if the key/value pairs in query
also exist identically in object
.
Also supports RegExp values in query
. If the query
property begins with !
then test is negated.
- object
Object
the object to examine - query
Object
the key/value pairs to look for
Returns: boolean
####Example
> o.exists({ a: 1, b: 2}, {a: 0})
false
> o.exists({ a: 1, b: 2}, {a: 1})
true
> o.exists({ a: 1, b: 2}, {"!a": 1})
false
> o.exists({ name: "clive hater" }, { name: /clive/ })
true
> o.exists({ name: "clive hater" }, { "!name": /ian/ })
true
###without(object, toRemove) returns a clone of the object minus the specified properties.
- object
Object
the input object - toRemove
string | Array.<string>
a single property, or array of properties to omit
Returns: Object
####Example
> o.without({ a: 1, b: 2, c: 3}, "b")
{ a: 1, c: 3 }
> o.without({ a: 1, b: 2, c: 3}, ["b", "a"])
{ c: 3 }