New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

chainset

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

chainset

Set object values using property chaining syntax

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
1
Weekly downloads
 
Created
Source

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.

Keywords

FAQs

Package last updated on 21 Jul 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc