npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

object-tools

v2.0.6

Published

Useful functions for working with objects

Downloads

15,751

Readme

view on npm npm module downloads Build Status Dependency Status Coverage Status js-standard-style

object-tools

Useful functions for working with objects

Example

var o = require('object-tools')

o.extend(...object) ⇒ object

Merge a list of objects, left to right, into one - to a maximum depth of 10.

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 }

o.clone(input) ⇒ 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

o.every(object, iterator) ⇒ 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; }
> o.every({ eggs: 12, carrots: 30, peas: 100 }, aboveTen)
true
> o.every({ eggs: 6, carrots: 30, peas: 100 }, aboveTen)
false

o.each(object, callback)

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
> function addToTotal(n){ total += n; }
> o.each({ eggs: 3, celery: 2, carrots: 1 }, addToTotal)
> total
6

o.exists(object, query) ⇒ 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

o.without(object, toRemove) ⇒ 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 }

o.where(object, query) ⇒ 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 }

o.extract(object, query) ⇒ 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 }

o.select(object, fields) ⇒ 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 | string | array | a list of fields to return |

o.get(object, expression) ⇒ *

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 |


© 2014-16 Lloyd Brookes <[email protected]>. Documented by jsdoc-to-markdown.