Access API Reference
The library provides several helper functions to interact with nested access of data.
pathToParts
This function takes a string path in dot notation and returns an array of parts.
declare function pathToParts(path: string): Array<string | number>;
This is how it would be used:
import { pathToParts } from '@formsignals/form-core';
const parts = pathToParts('a.0.c');
console.log(parts); // ['a', 0, 'c']
INFO
Array indexes are converted to numbers.
getValueAtPath
This function takes an object and a path in dot notation and returns the value at that path. If the path does not exist, undefined
is returned.
declare function getValueAtPath<TValue, TPath extends Paths<TValue>>(
obj: TValue | undefined,
path: TPath,
): ValueAtPath<TValue, TPath> | undefined;
This is how it would be used:
import { getValueAtPath } from '@formsignals/form-core';
const obj = { a: { b: { c: 1 } } };
const value = getValueAtPath(obj, 'a.b.c');
console.log(value); // 1
removeValueAtPath
This function takes an object and a path in dot notation and returns the same object with the value at that path removed.
declare function removeValueAtPath<TValue, TPath extends Paths<TValue>>(
obj: TValue | undefined,
path: TPath,
): TValue | undefined;
This is how it would be used:
import { removeValueAtPath } from '@formsignals/form-core';
const obj = { a: { b: { c: 1 } } };
const newObj = removeValueAtPath(obj, 'a.b.c');
console.log(newObj); // { a: { b: {} } }
setValueAtPath
This function takes an object, a path in dot notation, and a value and returns the same object with the value set at that path.
declare function setValueAtPath<TValue, TPath extends Paths<TValue>>(
obj: TValue | undefined,
path: TPath,
value: ValueAtPath<TValue, TPath>,
): TValue | undefined;
This is how it would be used:
import { setValueAtPath } from '@formsignals/form-core';
const obj = { a: { b: { c: 1 } } };
const newObj = setValueAtPath(obj, 'a.b.c', 2);
console.log(newObj); // { a: { b: { c: 2 } } }
INFO
If the path does not exist, it will be created.