dlv

中文文档
1.1.3发布于 2019/05/22MITESM + CJS零依赖近 30 天下载 1.6亿

Safely get a dot-notated property within an object.

原始使用文档
文档目录5 节
  1. Why?
  2. Installation
  3. Usage
  4. Setter Counterparts
  5. License

dlv(obj, keypath) NPM Build

Safely get a dot-notated path within a nested object, with ability to return a default if the full key path does not exist or the value is undefined

Why?

Smallest possible implementation: only 130 bytes.

You could write this yourself, but then you'd have to write tests.

Supports ES Modules, CommonJS and globals.

Installation

npm install --save dlv

Usage

delve(object, keypath, [default])

import delve from 'dlv';

let obj = {
    a: {
        b: {
            c: 1,
            d: undefined,
            e: null
        }
    }
};

//use string dot notation for keys
delve(obj, 'a.b.c') === 1;

//or use an array key
delve(obj, ['a', 'b', 'c']) === 1;

delve(obj, 'a.b') === obj.a.b;

//returns undefined if the full key path does not exist and no default is specified
delve(obj, 'a.b.f') === undefined;

//optional third parameter for default if the full key in path is missing
delve(obj, 'a.b.f', 'foo') === 'foo';

//or if the key exists but the value is undefined
delve(obj, 'a.b.d', 'foo') === 'foo';

//Non-truthy defined values are still returned if they exist at the full keypath
delve(obj, 'a.b.e', 'foo') === null;

//undefined obj or key returns undefined, unless a default is supplied
delve(undefined, 'a.b.c') === undefined;
delve(undefined, 'a.b.c', 'foo') === 'foo';
delve(obj, undefined, 'foo') === 'foo';

Setter Counterparts

  • dset by @lukeed is the spiritual "set" counterpart of dlv and very fast.
  • bury by @kalmbach does the opposite of dlv and is implemented in a very similar manner.

License

MIT

版本与发布记录

dlv 共发布 7 个版本,首次发布于 2016/08/04;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.3latest2019/05/227 年前
1.1.22018/06/078 年前
1.1.12018/01/148 年前
1.1.02017/02/019 年前
1.0.22016/08/0410 年前
1.0.12016/08/0410 年前

关于 dlv 的常见问题

dlv 是什么?

Safely get a dot-notated property within an object.

如何安装 dlv?

在项目目录运行 npm install dlv,或使用 pnpm add / yarn add / bun add。

dlv 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/dlv 是否存在。

dlv 支持 ESM 还是 CommonJS?

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

dlv 可以商用吗?

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

dlv 的下载量有多少?

根据 npm 官方下载统计,dlv 在 2026/10/07 – 2026/11/05 这 30 天内被下载 163,840,094 次,日均约 5,461,336 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

dlv 还在维护吗?

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