Skip to content

Accessing javascript properties in an uncertain world

License

Notifications You must be signed in to change notification settings

mgmeyers/prop-ops

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

prop

npm install prop-ops

prop-ops assists in performing CRUD operations on javascript objects and arrays. By default, prop-ops does not mutate objects, but offers the option of doing so.

Authors: Matthew Meyers, Sunyoung Kim
License: MIT

prop.get(obj, propString, [fallBack]) ⇒ Any

Safely access deeply nested properties of unstructured objects

Kind: static method of prop

Param Type Default Description
obj Object | Array the object or array to traverse
propString String the path to the desired property
[fallBack] Any a fall back value to return if the property is not found

Example

import * as prop from 'prop-ops'

const objA = { a: { b: 'c' } }
prop.get(objA, 'a.b')
// > 'c'

// Specify a value to return if a property is not found
prop.get(objA, 'a.nope')
// > null
prop.get(objA, 'a.nope', 24)
// > 24

// Traverse an array
const objB = { a: [{ b: 'c' }] }
prop.get(objB, 'a.[0].b')
// > 'c'

prop.set(obj, propString, value, [loose]) ⇒ Object | Array

Sets deeply nested object properties.

Kind: static method of prop
Returns: Object | Array - an updated version of obj

Param Type Default Description
obj Object | Array the object or array to traverse
propString String the path to the desired property
value Any the value to set
[loose] Boolean false create new objects / arrays along the path if undefined is encountered

Example

import * as prop from 'prop-ops'

const objA = { a: { b: 'c' } }
const updatedA = prop.set(objA, 'a.c', 'd')
// > objA     == { a: { b: 'c' } }
// > updatedA == { a: { b: 'c', c: 'd' } }

const constructedObj = prop.set({}, 'a.[0].b.c', 12, true)
// > constructedObj == { a: [{ b: { c: 12 } }] }

set.mutate(obj, propString, value, [loose])

Like set, but will modify the original object

Kind: static method of set

Param Type Default Description
obj Object | Array the object or array to traverse
propString String the path to the desired property
value Any the value to set
[loose] Boolean false create new objects / arrays along the path if undefined is encountered

Example

import * as prop from 'prop-ops'

const objA = { a: { b: 'c' } }
prop.set.mutate(objA, 'a.c', 'd')
// > objA == { a: { b: 'c', c: 'd' } }

const emptyObj = {}
prop.set.mutate(emptyObj, 'a.[0].b.c', 12, true)
// > emptyObj == { a: [{ b: { c: 12 } }] }

prop.merge(obj, propString, value, [loose]) ⇒ Object | Array

Merge deeply nested objects or arrays.

Kind: static method of prop
Returns: Object | Array - an updated version of obj

Param Type Default Description
obj Object | Array the object or array to traverse
propString String the path to the desired property
value Object | Array the object to merge
[loose] Boolean false create new objects / arrays along the path if undefined is encountered

Example

import * as prop from 'prop-ops'

const objA = { a: { b: 'c' } }
const updatedA = prop.merge(objA, 'a', { d: 'e', f: 'g' })
// > objA     == { a: { b: 'c' } }
// > updatedA == { a: { b: 'c', d: 'e', f: 'g' } }

const objB = { a: [0, 1, 2] }
const updatedB = prop.merge(objB, 'a', [, , 3, 4])
// > objB     == { a: [0, 1, 2] }
// > updatedB == { a: [0, 1, 3, 4] }

merge.mutate(obj, propString, value, [loose])

Like merge, but will modify the original object

Kind: static method of merge

Param Type Default Description
obj Object | Array the object or array to traverse
propString String the path to the desired property
value Object | Array the object to merge
[loose] Boolean false create new objects / arrays along the path if undefined is encountered

Example

import * as prop from 'prop-ops'

const objA = { a: { b: 'c' } }
prop.merge.mutate(objA, 'a', { d: 'e', f: 'g' })
// > objA == { a: { b: 'c', d: 'e', f: 'g' } }

const objB = { a: [0, 1, 2] }
prop.merge.mutate(objB, 'a', [, , 3, 4])
// > objB == { a: [0, 1, 3, 4] }

prop.has(obj, propString) ⇒ Boolean

Check if an object or array has a property

Kind: static method of prop

Param Type Description
obj Object object to traverse
propString String the path to the desired property

Example

import * as prop from 'prop-ops'

const objA = { a: [{ b: { c: 'd' } }] }
prop.has(objA, 'a.b')
// > false
prop.has(objA, 'a.[0].b.c')
// > true

prop.del(obj, propString) ⇒ Object | Array

Deletes deeply nested object properties

Kind: static method of prop

Param Type Description
obj Object | Array object to traverse
propString String the path to the desired property

Example

import * as prop from 'prop-ops'

const objA = { a: { b: { c: 'd' } } }
const updatedA = prop.del(objA, 'a.b')
// > objA     == { a: { b: { c: 'd' } } }
// > updatedA == { a: {} }

del.mutate(obj, propString)

Like del, but will modify the original object

Kind: static method of del

Param Type Description
obj Object | Array object to traverse
propString String the path to the desired property

Example

import * as prop from 'prop-ops'

const objA = { a: [{ b: { c: 'd' } }] }
prop.del.mutate(objA, 'a.b')
// noop
prop.del.mutate(objA, 'a.[0].b')
// objA == { a: [{}] }

About

Accessing javascript properties in an uncertain world

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published