2.1.0 • Published 6 months ago

@a11d/key-path v2.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

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,
}
*/
1.1.1

11 months ago

2.1.0

6 months ago

2.0.0

7 months ago

1.1.2

10 months ago

1.1.0

2 years ago

1.0.0

2 years ago