STDOBJ
Fast, small, dependency-free lib for common work with object literals.
Installation
yarn add stdobj
# ...or...
npm i stdobj
Usage
stdobj
provides a number of methods which can be destructured and used
in code. Below is a list of the supported methods:
isObj
- Checks if parameter passed is an object literalkeys
- Retruns array of keys from objecttoPairs
- Converts object to array of key-value pair arraysfromPairs
- Converts array of key-value pairs to objectget
- Gets a value using standard or dot-notated keyset
- Sets a value using standard or dot-notated keyflatten
- Flattens object to dot-notated key-value objectexpand
- Expands a flattened, dot-notated object to nested objectredact
- Redacts keys or values on an object literalmerge
- Performs a deep merge on object parameter(s)
isObj
isObj(<*>)
- Checks if parameter passed is an object literal
const obj = foo: 'bar' const notObj = 42 // -> true // -> false
keys
keys(<Object>)
- Retruns array of keys from object
const obj = foo: 'bar' fizz: 'buzz' // -> [ 'foo', 'fizz' ]
toPairs
toPairs(<Object>)
- Converts object to array of key-value pair arrays
const obj = foo: 'bar' fizz: 'buzz' // -> [ [ 'foo', 'bar' ], [ 'fizz', 'buzz' ] ]
fromPairs
fromPairs(<Array>)
- Converts array of key-value pairs to object
const pairs = 'foo' 'bar' 'fizz' 'buzz' // -> { foo: 'bar', fizz: 'buzz' }
get
get(<String>)
- Gets a value using standard or dot-notated key
const obj = foo: 'bar' fizz: buzz: 'bizz' // -> 'bar' // -> 'bizz'
set
set(Obj<Object>, Key<String>, Value<*>)
- Sets a value using standard or dot-notated key
const obj = {} // obj = { foo: { bar: 'fizz' } }
flatten
flatten(<Object>)
- Flattens object to dot-notated key-value object
const obj = foo: 'bar' fizz: buzz: 'bizz' // -> { foo: 'bar', 'fizz.buzz': 'bizz' }
expand
expand(<Object>)
- Expands a flattened, dot-notated object to nested object
const flatObj = foo: 'bar' 'fizz.buzz': 'bizz' // -> { foo: 'bar', fizz: { buzz: 'bizz' } }
redact
redact(Obj<Object>, <Object{keys<Array>, values<Array>}>
- Redacts keys or values on an object literal
const obj = foo: 'bar' quz: 'baz' fizz: buzz: 'fizz' bizz: 'fuzz' // -> { foo: 'bar', fizz: { buzz: 'fizz' } }
Note: for arrays, you can specify the index explicity: foo.bar[1]...
or specify
that all keys be affected during redaction with n
; foo.bar[n]...
. This is useful for
picking off properties of object nested in arrays
merge
merge(...<Object>)
- Performs a deep merge on object parameter(s)
const objOne = foo: 'bar' const objTwo = fizz: buzz: 'fizz' // -> { foo: 'bar', fizz: { buzz: 'fizz' } }