Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
object-tools
Advanced tools
Useful functions for working with objects
Example
var o = require("object-tools");
object
object
| array
object
boolean
boolean
object
object
object
object
*
object
Merge a list of objects, left to right, into one.
Kind: static method of object-tools
Param | Type | Description |
---|---|---|
...object | object | a sequence of object instances to be extended |
Example
> o.extend({ one: 1, three: 3 }, { one: "one", two: 2 }, { four: 4 });
{ one: 'one',
three: 3,
two: 2,
four: 4 }
object
| array
Clones an object or array
Kind: static method of object-tools
Param | Type | Description |
---|---|---|
input | object | array | the input to clone |
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
object
Deprecated
Returns a clone of the input object, minus the specified properties
Kind: static method of object-tools
Param | Type | Description |
---|---|---|
object | object | the object to clone |
toOmit | Array.<string> | an array of property names to omit from the clone |
Example
> o.omit({ one: 1, two: 2, three: 3, four: 4 }, [ "two", "four" ]);
{ one: 1, three: 3 }
boolean
Returns true if the supplied iterator function returns true for every property in the object
Kind: static method of object-tools
Param | Type | Description |
---|---|---|
object | object | the object to inspect |
iterator | function | the iterator function to run against each key/value pair, the args are (value, key) . |
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
Runs the iterator function against every key/value pair in the input object
Kind: static method of object-tools
Param | Type | Description |
---|---|---|
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
boolean
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.
Kind: static method of object-tools
Param | Type | Description |
---|---|---|
object | object | the object to examine |
query | object | the key/value pairs to look for |
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
> o.exists({ a: 1}, { a: function(n){ return n > 0; } })
true
> o.exists({ a: 1}, { a: function(n){ return n > 1; } })
false
object
Returns a clone of the object minus the specified properties. See also select.
Kind: static method of object-tools
Param | Type | Description |
---|---|---|
object | object | the input object |
toRemove | string | Array.<string> | a single property, or array of properties to omit |
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 }
object
Returns a new object containing the key/value pairs which satisfy the query
Kind: static method of object-tools
Since: 1.2.0
Param | Type | Description |
---|---|---|
object | object | The input object |
query | Array.<string> | function | Either an array of property names, or a function. The function is called with (value, key) and must return true to be included in the output. |
Example
> object = { a: 1, b: 0, c: 2 }
{ a: 1, b: 0, c: 2 }
> o.where(object, function(value, key){
return value > 0;
});
{ a: 1, c: 2 }
> o.where(object, [ "b" ]);
{ b: 0 }
> object
{ a: 1, b: 0, c: 2 }
object
identical to o.where(object, query)
with one exception - the found properties are removed from the input object
Kind: static method of object-tools
Since: 1.2.0
Param | Type | Description |
---|---|---|
object | object | The input object |
query | Array.<string> | function | Either an array of property names, or a function. The function is called with (value, key) and must return true to be included in the output. |
Example
> object = { a: 1, b: 0, c: 2 }
{ a: 1, b: 0, c: 2 }
> o.where(object, function(value, key){
return value > 0;
});
{ a: 1, c: 2 }
> object
{ b: 0 }
object
Returns a new object containing only the selected fields. See also without.
Kind: static method of object-tools
Param | Type | Description |
---|---|---|
object | object | the input object |
fields | array | a list of fields to return |
*
Returns the value at the given property.
Kind: static method of object-tools
Since: 1.4.0
Param | Type | Description |
---|---|---|
object | object | the input object |
expression | string | the property accessor expression |
© 2015 Lloyd Brookes <75pound@gmail.com>. Documented by jsdoc-to-markdown.
FAQs
Useful functions for working with objects
We found that object-tools demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.