Socket
Socket
Sign inDemoInstall

@strikeentco/set

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @strikeentco/set

Set nested values on an object using a dot path or custom separator


Version published
Maintainers
1
Install size
5.01 kB
Created

Readme

Source

set License npm

Build Status node Test Coverage

One of the smallest (31 sloc) and most effective implementations of setting a nested value on an object.

Usage

$ npm install @strikeentco/set --save
const set = require('@strikeentco/set');

set({ a: { b: 'c' } }, 'a.b', 'd');
//=> { a: { b: 'd' } }

set({ a: { b: ['c', 'd'] } }, 'a.b.1', 'e');
//=> { a: { b: ['c', 'e'] } }

set({ a: { b: ['c', 'd'] } }, ['a', 'b'], 'c');
//=> { a: { b: 'c' } }

set({ a: { b: 'c' } }, 'a.b.c.d', 'e');
//=> { a: { b: { c: { d: 'e' } } } }

set({ a: { b: 'c' } }, 'a:b', 'd', ':');
//=> { a: { b: 'd' } }

API

set(obj, path, val, [separator])

Params:
  • obj (Object) - Source object.
  • path (String|Array) - String or array with path.
  • val (Any) - Value to set.
  • [separator] (String) - . by default.

License

The MIT License (MIT)
Copyright (c) 2018-present Alexey Bystrov

Keywords

FAQs

Last updated on 27 Jan 2022

Did you know?

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc