1.1.6 • Published 5 years ago

prop-ops v1.1.6

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

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

ParamTypeDefaultDescription
objObject | Arraythe object or array to traverse
propStringStringthe path to the desired property
fallBackAnya 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

ParamTypeDefaultDescription
objObject | Arraythe object or array to traverse
propStringStringthe path to the desired property
valueAnythe value to set
looseBooleanfalsecreate 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

ParamTypeDefaultDescription
objObject | Arraythe object or array to traverse
propStringStringthe path to the desired property
valueAnythe value to set
looseBooleanfalsecreate 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

ParamTypeDefaultDescription
objObject | Arraythe object or array to traverse
propStringStringthe path to the desired property
valueObject | Arraythe object to merge
looseBooleanfalsecreate 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

ParamTypeDefaultDescription
objObject | Arraythe object or array to traverse
propStringStringthe path to the desired property
valueObject | Arraythe object to merge
looseBooleanfalsecreate 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

ParamTypeDescription
objObjectobject to traverse
propStringStringthe 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

ParamTypeDescription
objObject | Arrayobject to traverse
propStringStringthe 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

ParamTypeDescription
objObject | Arrayobject to traverse
propStringStringthe 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: [{}] }
1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago