@clickbar/dot-diver

中文文档
1.0.7发布于 2024/08/23MITTypeScriptESM + CJS零依赖构建来源已验证近 30 天下载 2.7万

Types and utilities to access object properties by dot notation.

原始使用文档
文档目录14 节
  1. 🌟 Features
  2. 📦 Installation
  3. 🚀 Usage
  4. 🔎 getByPath and 🔏 setByPath
  5. 🛣️ Path and 🔖 PathValue
  6. 🔄 Objects with cyclic dependency
  7. ⚙️ Customizing the Depth Limit
  8. ❓ FAQ
  9. ❗ Why are my paths truncated in a object with index signatu…
  10. ❗ Why are my paths truncated inside an array?
  11. ❗ I get the error "Type instantiation is excessively deep a…
  12. 👨‍💻 Contributing
  13. 🔒 Security Vulnerabilities
  14. 📄 License

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') // result is 'Hello world'

🌟 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'

// Define a sample object with nested properties
const object = {
  a: 'hello',
  b: {
    c: 42,
    d: {
      e: 'world',
    },
  },
  f: [{ g: 'array-item-1' }, { g: 'array-item-2' }],
}

// Example 1: Get a value by path
const value1 = getByPath(object, 'a') // Output: 'hello'
console.log(value1)

const value2 = getByPath(object, 'b.c') // Output: 42
console.log(value2)

const value3 = getByPath(object, 'b.d.e') // Output: 'world'
console.log(value3)

const value4 = getByPath(object, 'f.0') // Output: { g: 'array-item-1' }
console.log(value4)

const value5 = getByPath(object, 'f.1.g') // Output: 'array-item-2'
console.log(value5)

// Example 2: Set a value by path
setByPath(object, 'a', 'new hello')
console.log(object.a) // Output: 'new hello'

setByPath(object, 'b.c', 100)
console.log(object.b.c) // Output: 100

setByPath(object, 'b.d.e', 'new world')
console.log(object.b.d.e) // Output: 'new world'

setByPath(object, 'f.0', { g: 'new array-item-1' })
console.log(object.f[0]) // Output: { g: 'new array-item-1' }

setByPath(object, 'f.1.g', 'new array-item-2')
console.log(object.f[1].g) // Output: 'new array-item-2'


🛣️ Path and 🔖 PathValue


import type { Path, PathValue } from '@clickbar/dot-diver'

// Define a sample object type with nested properties
type MyObjectType = {
  a: string
  b: {
    c: number
    d: {
      e: boolean
    }
  }
  f: [{ g: string }, { g: string }]
}

// Example 1: Using the Path type
type MyObjectPaths = Path<MyObjectType>

// MyObjectPaths will be a union type representing all valid paths in dot notation:
// 'a' | 'b' | 'f' | 'b.c' | 'b.d' | 'b.d.e' | 'f.0' | 'f.1' | 'f.0.g' | 'f.1.g'

// Example 2: Using the PathValue type
type ValueAtPathA = PathValue<MyObjectType, 'a'> // Output: string
type ValueAtPathB_C = PathValue<MyObjectType, 'b.c'> // Output: number
type ValueAtPathB_D_E = PathValue<MyObjectType, 'b.d.e'> // Output: boolean
type ValueAtPathF_0 = PathValue<MyObjectType, 'f.0'> // Output: { g: string }
type ValueAtPathF_0_G = PathValue<MyObjectType, 'f.0.g'> // Output: string


🔄 Objects with cyclic dependency


import type { Path, PathValue } from '@clickbar/dot-diver'

// Define an object type with nested properties and a cyclic dependency
type Node = {
  id: number
  label: string
  parent: Node
  children: Node[]
}

// Example 1: Using the Path type with a Depth limit
type NodePathsDepth2 = Path<Node, 2> // Depth limit of 2

// NodePathsDepth2 will be a union type representing all valid paths in dot notation up to a depth of 3:
// 'id' | 'label' | 'parent' | 'children' | 'parent.id' | 'parent.label' | 'parent.parent' | 'parent.children' | `parent.parent.${any}` | `parent.children.${any}` | `children.${number}` | `children.${number}.${any}`

// Example 2: Using the PathValue type with a Depth limit
type ValueAtPathParent_Id = PathValue<Node, 'parent.id', 3> // Output: number
type ValueAtPathChildren_0_Label = PathValue<Node, 'children.0.label', 3> // Output: string | undefined
type ValueAtPathParent_Parent_Parent = PathValue<Node, 'parent.parent.parent.parent', 3> // Output: unknown (due to the depth limit)

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! 🌊

版本与发布记录

@clickbar/dot-diver 共发布 8 个版本,首次发布于 2023/03/27;过去 12 个月发布了 0 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.0.7latest2024/08/232 年前
2.0.02024/08/182 年前
1.0.62024/03/252 年前
1.0.52024/03/212 年前
1.0.42023/11/073 年前
1.0.32023/11/033 年前

关于 @clickbar/dot-diver 的常见问题

@clickbar/dot-diver 是什么?

Types and utilities to access object properties by dot notation.

如何安装 @clickbar/dot-diver?

在项目目录运行 npm install @clickbar/dot-diver,或使用 pnpm add / yarn add / bun add。

@clickbar/dot-diver 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@clickbar/dot-diver 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@clickbar/dot-diver 可以商用吗?

@clickbar/dot-diver 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@clickbar/dot-diver 的下载量有多少?

根据 npm 官方下载统计,@clickbar/dot-diver 在 2026/10/10 – 2026/11/08 这 30 天内被下载 26,863 次,日均约 895 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@clickbar/dot-diver 还在维护吗?

最新版本 1.0.7 发布于 2024/08/23(2 年前);至今共发布 8 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/03/27。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。