Dot Diver 🌊🔍
A lightweight, powerful, and dependency-free TypeScript utility library that provides types and functions to work with object paths in dot notation. Dive into your objects with ease, while maintaining comprehensive type safety! 🎉
Dot notation is a popular and convenient way to access deeply nested properties in objects. With Dot Diver, you can safely work with object paths in TypeScript projects, ensuring type correctness and productivity!
Example:
import { getByPath } from '@clickbar/dot-diver'
const object = {
a: 'Hello world',
}
const result = getByPath(object, 'a')
🌟 Features
- 🎯 Works with arrays, tuples, and objects
- 🛡️ Works with readonly properties
- ✅ Tests included
- 🌀 Works with cyclic dependencies in types
- 🚫 No dependencies
- 🪶 Tiny footprint
📦 Installation
Install the package using your favorite package manager:
npm
npm install -D @clickbar/dot-diver
yarn
yarn add -D @clickbar/dot-diver
pnpm
pnpm install -D @clickbar/dot-diver
🚀 Usage
🔎 getByPath
and 🔏 setByPath
import { getByPath, setByPath } from '@clickbar/dot-diver'
const object = {
a: 'hello',
b: {
c: 42,
d: {
e: 'world',
},
},
f: [{ g: 'array-item-1' }, { g: 'array-item-2' }],
}
const value1 = getByPath(object, 'a')
console.log(value1)
const value2 = getByPath(object, 'b.c')
console.log(value2)
const value3 = getByPath(object, 'b.d.e')
console.log(value3)
const value4 = getByPath(object, 'f.0')
console.log(value4)
const value5 = getByPath(object, 'f.1.g')
console.log(value5)
setByPath(object, 'a', 'new hello')
console.log(object.a)
setByPath(object, 'b.c', 100)
console.log(object.b.c)
setByPath(object, 'b.d.e', 'new world')
console.log(object.b.d.e)
setByPath(object, 'f.0', { g: 'new array-item-1' })
console.log(object.f[0])
setByPath(object, 'f.1.g', 'new array-item-2')
console.log(object.f[1].g)
🛣️ Path and 🔖 PathValue
import type { Path, PathValue } from '@clickbar/dot-diver'
type MyObjectType = {
a: string
b: {
c: number
d: {
e: boolean
}
}
f: [{ g: string }, { g: string }]
}
type MyObjectPaths = Path<MyObjectType>
type ValueAtPathA = PathValue<MyObjectType, 'a'>
type ValueAtPathB_C = PathValue<MyObjectType, 'b.c'>
type ValueAtPathB_D_E = PathValue<MyObjectType, 'b.d.e'>
type ValueAtPathF_0 = PathValue<MyObjectType, 'f.0'>
type ValueAtPathF_0_G = PathValue<MyObjectType, 'f.0.g'>
🔄 Objects with cyclic dependency
import type { Path, PathValue } from '@clickbar/dot-diver'
type Node = {
id: number
label: string
parent: Node
children: Node[]
}
type NodePathsDepth2 = Path<Node, 2>
type ValueAtPathParent_Id = PathValue<Node, 'parent.id', 3>
type ValueAtPathChildren_0_Label = PathValue<Node, 'children.0.label', 3>
type ValueAtPathParent_Parent_Parent = PathValue<Node, 'parent.parent.parent.parent', 3>
The default depth is currently 10.
At the moment, it is not possible to customize it when using the provided getByPath
and setByPath
functions.
This is further explained in this issue.
⚙️ Customizing the Depth Limit
You can still customize it, by implementing your own functions, which just calls ours.
Example:
import { getByPath, setByPath } from '@clickbar/dot-diver'
import type { Path, SearchableObject, PathValue } from '@clickbar/dot-diver'
function getByPathDepth5<T extends SearchableObject, P extends Path<T, 5> & string>(
object: T,
path: P,
): PathValue<T, P, 5> {
return getByPath(object, path) as PathValue<T, P, 5>
}
function setByPathDepth5<
T extends SearchableObject,
P extends Path<T, 5> & string,
V extends PathValue<T, P, 5>,
>(object: T, path: P, value: V): void {
setByPath(object, path, value as PathValue<T, P>)
}
export { getByPathDepth5 as getByPath, setByPathDepth5 as setByPath }
The intersection between Path<T, 5>
and string
is necessary for TypeScript to successfully narrow down the type of P
based on the user-provided path
input.
Without the intersection, the path
would just be of type Path<T, 5>
and PathValueEntry
would be a union of all possible return types.
By using the intersection, TypeScript is forced to apply the Path
constraints and infer the type from the provided user input.
❓ FAQ
❗ Why are my paths truncated in a object with index signature?
See this issue.
❗ Why are my paths truncated inside an array?
Your paths are not truncated. Typescript will still validate them.
Some IDEs have problems with displaying children.${number}
paths.
If you can, define the array as an tuple. This will include all paths in the auto completion.
❗ I get the error "Type instantiation is excessively deep and possibly infinite.ts(2589)"
This happens if typescript reaches its maximum depth limit. This library should prevent this, but it can still happen if a object has a lot of cyclic dependencies.
For example:
type TestType = {
a: TestType
b: [TestType]
c: TestType[]
d: {
e: TestType
}
f: TestType2
}
You can try to decrease the depth limit of the auto completion by reimplementing the getByPath
and setByPath
functions.
See this section for customizing the depth limit.
👨💻 Contributing
If you would like to contribute to Dot Diver, feel free to fork the repository, make changes, and submit a pull request. We appreciate any help and feedback.
See CONTRIBUTING.md for more information.
🔒 Security Vulnerabilities
Please see SECURITY for details.
📄 License
Dot Diver is licensed under the MIT License.
🎉 Happy diving! 🌊