Sets the value to an object at the given path.
Info
Immutable: This does not mutate the original object.
Related
Syntax
TypeScript
import { toSet } from '@opentf/std'; toSet<T>( obj: T, path: string | unknown[], value: unknown | ((val: unknown) => unknown) ): T
Info
The value param can be either any value or callback function.
Info
The callback fn can be called with the property path value if it exist.
Info
Missing intermediate branches are created automatically. Existing non-object intermediates such as 0, false, '', and null are preserved, and deep writes through them are ignored.
Examples
TypeScript
toSet({}, 'a', null) //=> { a: null } toSet({}, 'a', 1) //=> { a: 1 } toSet({}, 'a.b', 25) //=> { a: { b: 25 } } toSet({}, 'user.email', 'user@example.com') //=> // { // user: { email: 'user@example.com' } // } toSet({}, '0', 'Apple') //=> { '0': 'Apple' } toSet({}, 'fruits[0]', 'Apple') //=> { fruits: ['Apple'] } toSet({ a: 1 }, 'a', (val) => val + 1) //=> { a: 2 } const fn = () => render('My Component') toSet({ subscribeFns: [] }, 'subscribeFns[0]', () => fn) //=> { subscribeFns: [fn] } toSet({ a: false }, 'a.b', 1) //=> { a: false }