Skip to content

Latest commit

 

History

History
70 lines (49 loc) · 1.37 KB

toSet.mdx

File metadata and controls

70 lines (49 loc) · 1.37 KB

import { Callout } from "nextra/components"; import REPL from "../../components/REPL";

Sets the value to an object at the given path.

Immutable: This does not mutate the original object.

Related

Syntax

import { toSet } from '@opentf/std';

toSet<T>(
  obj: T,
  path: string | unknown[],
  value: unknown | ((val: unknown) => unknown)
): T
The value param can be either any `value` or `callback` function. The `callback` fn can be called with the property path value if it exist.

Examples

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] }

Try

<REPL code={`const { toSet } = require('@opentf/std');

log(toSet({}, 'a.b', 25));

toSet({ a: 1 }, 'a', (val) => val + 1) `} />