0.1.4 • Published 7 months ago

@3mo/key-path v0.1.4

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

KeyPath

A set of type-safe utilities for working with objects and their properties with support for array members.

  • getKeyPath - A type-safe function to get a key-path.
  • getValueByKeyPath - A function to extract the value by a given key-path.
  • setValueByKeyPath - A function to set the value by a given key-path.
const customer = {
	name: 'John Doe',
	addresses: [
		{ street: '123 Main St', city: 'Anytown' },
		{ street: '456 Elm St', city: 'Anytown' },
	] as const,
}

const keyPath = getKeyPath<typeof customer>('addresses.0.street') // 'addresses.0.street'
const value = getValueByKeyPath(customer, 'addresses.0.street') // '123 Main St'
setValueByKeyPath(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,
}
*/
0.1.4

7 months ago

0.1.2

12 months ago

0.1.3

12 months ago

0.1.1

1 year ago

0.1.0

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago