stdobj

0.6.2 • Public • Published

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 literal
  • keys - Retruns array of keys from object
  • toPairs - Converts object to array of key-value pair arrays
  • fromPairs - Converts array of key-value pairs to object
  • get - Gets a value using standard or dot-notated key
  • set - Sets a value using standard or dot-notated key
  • flatten - Flattens object to dot-notated key-value object
  • expand - Expands a flattened, dot-notated object to nested object
  • redact - Redacts keys or values on an object literal
  • merge - 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
 
isObj(obj) // -> true
isObj(notObj) // -> false

keys

keys(<Object>) - Retruns array of keys from object

const obj = { foo: 'bar', fizz: 'buzz' }
 
keys(obj) // -> [ 'foo', 'fizz' ]

toPairs

toPairs(<Object>) - Converts object to array of key-value pair arrays

const obj = { foo: 'bar', fizz: 'buzz' }
 
toPairs(obj) // -> [ [ 'foo', 'bar' ], [ 'fizz', 'buzz' ] ]

fromPairs

fromPairs(<Array>) - Converts array of key-value pairs to object

const pairs = [ [ 'foo', 'bar' ], [ 'fizz', 'buzz' ] ]
 
fromPairs(pairs) // -> { foo: 'bar', fizz: 'buzz' }

get

get(<String>) - Gets a value using standard or dot-notated key

const obj = {
  foo: 'bar',
  fizz: {
    buzz: 'bizz'
  }
}
 
get('foo') // -> 'bar'
get('fizz.buzz') // -> 'bizz'

set

set(Obj<Object>, Key<String>, Value<*>) - Sets a value using standard or dot-notated key

const obj = {}
 
set(obj, 'foo.bar', 'fizz') // obj = { foo: { bar: 'fizz' } }

flatten

flatten(<Object>) - Flattens object to dot-notated key-value object

const obj = {
  foo: 'bar',
  fizz: {
    buzz: 'bizz'
  }
}
 
flatten(obj) // -> { foo: 'bar', 'fizz.buzz': 'bizz' }

expand

expand(<Object>) - Expands a flattened, dot-notated object to nested object

const flatObj = { foo: 'bar', 'fizz.buzz': 'bizz' }
 
expand(flatObj) // -> { 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'
  }
}
 
redact(obj, { keys: [ 'fizz.bizz' ], values: [ 'baz' ] }) // -> { 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' } }
 
merge(objOne, objTwo) // -> { foo: 'bar', fizz: { buzz: 'fizz' } }

Readme

Keywords

none

Package Sidebar

Install

npm i stdobj

Weekly Downloads

1

Version

0.6.2

License

ISC

Unpacked Size

25.5 kB

Total Files

15

Last publish

Collaborators

  • fluidbyte