@hacknlove/deepobject
v1.1.6
Published
Operations in objects with dotted keys, that returns a new object or updates in place
Downloads
13
Readme
deepObject
Because sometimes you would like to access/update an object using 'deeply.dotted.keys'
Install
npm:
npm i hacknlove/deepobject
CDN
<script src="https://cdn.jsdelivr.net/npm/@hacknlove/[email protected]/dist/deepObject.umd.min.js"></script>
API
getValue(object, key) => any
Returns the value.
import { getValue } from '@hacknlove/deepobject'
const state = {
foo: {
bar: {
buz: 42
}
}
}
getValue(state, 'foo.bar.buz') // 42
setValue(oldState, key, value) => object
returns a newState
object whose value at key
is value
It does not modify oldState
import { setValue } from '@hacknlove/deepobject'
const oldState = {
foo: {
bar: {
buz: 42
}
}
}
const newState = setValue(oldState, 'foo.bar.some.new', 'new value')
/*
{
foo: {
bar: {
buz: 42m
some: {
new: 'new value'
}
}
}
}
*/
setValueUpdate(state, key, value) => undefined
Updates a state
to make the value at key
be value
import { setValueUpdate } from '@hacknlove/deepobject'
const state = {
foo: {
bar: {
buz: 42
}
}
}
setValue(state, 'foo.bar.some.new', 'new value') // undefined
/* state
{
foo: {
bar: {
buz: 42m
some: {
new: 'new value'
}
}
}
}
*/
deleteValue(oldState, key) => object
returns a newState
object with the key
deleted
It does not modify oldState
import { deleteValue } from '@hacknlove/deepobject'
const oldState = {
foo: {
bar: {
buz: 42
}
}
}
const newState = deleteValue(oldState, 'foo.bar')
/*
{
"foo": {
}
}
*/
deleteValueUpdate(state, key) => undefined
deletes key
in state
import { deleteValueUpdate } from '@hacknlove/deepobject'
const state = {
foo: {
bar: {
buz: 42
}
}
}
deleteValue(oldState, 'foo.bar') // undefined
/* state
{
"foo": {
}
}
*/