2.1.0 • Published 6 months ago
@a11d/key-path v2.1.0
KeyPath
A set of type-safe utilities for working with objects and their properties with support for array members.
KeyPath.of
- A type-safe way to create a key-path.KeyPath.get
- A function to get the value by a given key-path.KeyPath.set
- A function to set the value by a given key-path.KeyPath.isWritable
- Check if a key-path is writable.
const customer = {
name: 'John Doe',
addresses: [
{ street: '123 Main St', city: 'Anytown' },
{ street: '456 Elm St', city: 'Anytown' },
] as const,
}
const keyPath = KeyPath.of<typeof customer>('addresses.0.street') // 'addresses.0.street'
const value = KeyPath.get(customer, 'addresses.0.street') // '123 Main St'
KeyPath.set(customer, 'addresses.0.street', '180 Azadi St')
/*
{
name: 'John Doe',
addresses: [
{ street: '180 Azadi St', city: 'Anytown' },
{ street: '456 Elm St', city: 'Anytown' },
] as const,
}
*/