@strikeentco/set

1.0.2 • Public • Published

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

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.0.2
    2,301
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.0.2
    2,301
  • 1.0.1
    52
  • 1.0.0
    172

Package Sidebar

Install

npm i @strikeentco/set

Weekly Downloads

2,004

Version

1.0.2

License

MIT

Unpacked Size

5.13 kB

Total Files

4

Last publish

Collaborators

  • strikeentco