1.0.0 • Published 2 years ago

chainset v1.0.0

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

chainset

Set object values using property chaining syntax.

Support this project by ⭐️ starring and sharing it. Follow me to see what other cool projects I'm working on! ❤️

Why?

Setting a value on an arbitrary nested path can be cumbersome to do correctly.

For example, someObject.propA.propB.propC = 'value' on a potentially empty someObject requires a guard for each nested property.

Without chainset

const someObject = {}

if (!Object.hasOwn(someObject, 'propA')) {
    someObject.propA = {}
}

if (!Object.hasOwn(someObject.propA, 'propB')) {
    someObject.propA.propB = {}
}

someObject.propA.propB.propC = 'value'

With chainset

const someObject = chainset()

someObject.propA.propB.propC = 'value'

Usage

Create a new object

import chainset from 'chainset'

const object = chainset() // => {}

// Automatically initializes 'propB' & 'propC' to objects
object.propA.propB.propC = 'value'

console.log(object)
/*
{
    propA: {
        propB: {
            propC: 'value'
        }
    }
}
*/

Use an existing object

const object = chainset({
    foo: {
        bar: {}
    }
})

// Automatically initializes 'propA' & 'propB' to objects
object.foo.bar.propA.propB = 'value'

console.log(object)
/*
{
    foo: {
        bar: {
            propA: {
                propB: 'value'
            }
        }
    }
}
*/

API

chainset(object, options)

object

Type: object

Default: Object.create(null) (a pure, prototype-less object)

The object to ehance with "chain-setting" support.

options

deep

Type: boolean | number

Default: true

How deep to add automatic object initialization support on access. Set to true to add support infinitely. Set a number to limit the depth of objects to add support to. Set to false to only add support to the immediate object.

allowedKeys

Type: RegExp | (string|RegExp)[] | (path, object) => boolean

A regular expression pattern, array of strings/patterns, or function that restrict what property names will be initialized on access.

defaultObject

Type: (key?: string) => object

Default: () => Object.create(null)

A function that creates a new object to use when a property is accessed.