UNPKG

just-safe-set

Version:

set value at property, create intermediate properties if necessary

40 lines (29 loc) 951 B
<!-- DO NOT EDIT THIS FILE! THIS FILE WAS AUTOGENERATED BY TEMPLATE-MATE --> <!-- SEE https://github.com/angus-c/just/blob/master/CONTRIBUTING.md#readme-template --> ## just-safe-set Part of a [library](https://anguscroll.com/just) of zero-dependency npm modules that do just do one thing. Guilt-free utilities for every occasion. [`🍦 Try it`](https://anguscroll.com/just/just-safe-set) ```shell npm install just-safe-set ``` ```shell yarn add just-safe-set ``` Set value at property, create intermediate properties if necessary ```js import set from 'just-safe-set'; const obj1 = {}; set(obj1, 'a.aa.aaa', 4); // true obj1; // {a: {aa: {aaa: 4}}} const obj2 = {}; set(obj2, ['a', 'aa', 'aaa'], 4); // true obj2; // {a: {aa: {aaa: 4}}} const obj3 = {a: {aa: {aaa: 2}}}; set(obj3, 'a.aa.aaa', 3); // true obj3; // {a: {aa: {aaa: 3}}} const obj5 = {a: {}}; const sym = Symbol(); set(obj5.a, sym, 7); // true obj5; // {a: {Symbol(): 7}} ```