object-funcs

A very limited subset of object functions I use every day

Usage no npm install needed!

<script type="module">
  import objectFuncs from 'https://cdn.skypack.dev/object-funcs';
</script>

README

object-funcs

A very limited subset of object functions I use every day

Install

npm i object-funcs

Package on npm

API

defined([arg], [arg], [...])

Return the first defined arg

arg is considered not defined when

  • arg is null
  • arg is undefined
  • arg is NaN
const defined = require('object-funcs/defined')

var opts = {y:false, w:4}

// false
defined(opts.x, opts.y, 100)

// 'yes'
defined(null, undefined, NaN, 'yes')

merge([obj], [obj], [...])

Merge properties of a Plain Object or a serie of Plain Objects into a new one

const merge = require('object-funcs/merge')

var ref = {a:'foo'}

// {a:'foo', b:'bar'}
var obj = merge(ref, {b:'bar'})

// {a:'foo'}
ref

Always overwrite properties

const merge = require('object-funcs/merge')

// {a:'foo', b:'baz', c:'quux'}
merge({a:'foo'}, {b:'bar'}, {b:'baz'}, {c:'quux'})

No deep merge, only first level of properties are injected

const merge = require('object-funcs/merge')

var ref = {foo:{bar:'baz'}}

// {foo:{bar:'baz'}, a:1}
merge(ref, {a:1})

// {foo:{b:2}}
merge(ref, {foo:{b:2}})

only(obj, keys)

Return a new Plain Object with filtered properties

keys can be a String or an Array of targeted keys

keys can also be a RegExp

const only = require('object-funcs/only')

// {a:1, b:2}
only({a:1, b:2, c:3}, 'a b')

// {b:2, c:3}
only({a:1, b:2, c:3}, ['b', 'c'])

// {bar:1, baz:2}
only({foo:0, bar:1, baz:2, quux:3}, /^ba/)

similar(obj, search)

Partial comparison of two objects

Check if the keys / values in search are equals in obj

const similar = require('object-funcs/similar')

// true
similar({foo:'bar', baz:54}, {baz:54})

// false
similar({foo:'bar', baz:54}, {baz:54, a:1})

// true
similar({foo:{bar:{baz:'quux', c:3}, b:2}, a:1}, {foo:{bar:{baz:'quux'}}})

Thanks

Mainly forked / inspired on

License

MIT