# @blakek/deep 中文文档

规范地址：https://npmcn.com/package/%40blakek/deep/

版本：4\.0\.0
npm 元数据更新时间：2022-04-04T19:24:12.948Z
中文译文更新时间：2026-11-08T06:09:18.885Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40blakek/deep
原文对照：https://npmcn.com/package/%40blakek/deep/?lang=original
项目主页：https://github.com/blakek/deep
源码仓库：https://github.com/blakek/deep

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：88.9 kB，19 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：2 个
- 首次发布：2020-06-02T15:56:44.323Z
- 最新版本发布：2021-12-03T16:47:13.354Z
- 近 30 天下载量：145492（2026-10-09 至 2026-11-07，npm 官方下载统计）
- 已发布版本数：11（近 12 个月 0 个）

## 中文简介

获取、设置、去除和测试深层嵌入属性

## 安装

```sh
npm install @blakek/deep
```

也可以使用：`pnpm add @blakek/deep`、`yarn add @blakek/deep`、`bun add @blakek/deep`

## README

# 深处

> 获取、设置、去除和测试深层嵌入属性

帮助您安全工作 与嵌入属性.

## 安装

使用 [雅伦](https://yarnpkg.com/en/docs/)数字 :

```bash
$ yarn add @blakek/deep
```

...或使用 [无](https://npmjs.com/)数字 :

```bash
$ npm i --save @blakek/deep
```

## 使用量

```js
import {
  clone,
  createGetter,
  get,
  has,
  omit,
  pluck,
  remove,
  set
  // also available:
  // - createHas
  // - createOmit
  // - createPluck
  // - createRemove
  // - createSetter
  // - isObject
  // - traverseObject
} from '@blakek/deep';

const user = {
  id: 'abf87de',
  roles: ['alert:create', 'alert:read'],
  sites: {
    github: {
      username: 'blakek'
    }
  }
};

// Deeply clone most values
const userCopy = clone(user);
user === userCopy; //» false
user.id === userCopy.id; //» true
user.roles === userCopy.roles; //» false
user.roles[0] === userCopy.roles[0]; //» true

// Get a property value
get('sites.github.username', user); //» 'blakek'

// Get a property value with a fallback other than `undefined`
get('sites.facebook.username', user, 'no-account'); //» 'no-account'

// Create a function to get a property value later
const getUsername = createGetter('sites.github.username');
getUsername(user); //» 'blakek'

// Test for a property value
has('sites.github', user); //» true

// Clone an object and omit properties
omit(['roles', 'sites'], user); //» { id: 'abf87de' }

// Pluck a subset of properties
pluck(['id', 'roles'], user);
//» { id: 'abf87de', roles: [ 'alert:create', 'alert:read' ] }

// Remove a property value, modifying the current object
remove('a', { a: 42, b: 123 }); //» { b: 123 }

// Set a property value, modifying the current object
set(123, 'a.b.c', { a: 42 }); //» { a: { b: { c: 123 } } }
```

## API 密码

对于所有这些:

-   `path` 可以是点标记字符串,也可以是一系列路径部件

### `clone`

返回大多数值的深克隆 / 深复制: 原始值、 对象、 阵列、 地图、 设定、 日期等 .

```ts
function clone<T extends unknown>(value: T): T;
```

```js
const object = { value: 'yep' };
const cloned = clone(object);

cloned === object; //» false
cloned.value === object.value; //» true
```

### `get` 编号 : `createGetter`

获得带有可选倒计时值的给定路径值.

```ts
function get(path: Path, object: object, fallbackValue?: any): unknown;

function createGetter(
  path: Path,
  fallbackValue?: any
): (object: object) => unknown;
```

```js
const user = {
  id: 'abf87de',
  roles: ['alert:create', 'alert:read'],
  sites: {
    github: {
      username: 'blakek'
    }
  }
};

get('id', user); //» 'abf87de'
get('roles.0', user); //» 'alert:create'
get('roles[0]', user); //» 'alert:create'
get(['roles', 1], user); //» 'alert:read'
get('sites.github.username', user); //» 'blakek'
get('sites.github.avatar.src', user, 'default.png'); //» 'default.png'

const getID = get('id');
getID(user); //» 'abf87de'

const getRoles = createGetter('roles');
getRoles(user); //» ['alert:create', 'alert:read']
```

### `has` 编号 : `createHas`

回返 `true` 如果在给定路径中找到一个值或 `false` 如果什么都没有 发现.

```ts
function has(path: Path, object: any): boolean;

function createHas(path: Path): (object: any) => boolean;
```

```js
const product = {
  id: 'abf87de',
  name: 'Logo T-Shirt',
  attributes: {
    isCool: undefined,
    materials: ['cotton']
  }
};

has('attributes.materials', product); //» true
has(['avability', 'sizes'], product); //» false
has('attributes.isCool', product); //» true; property exists but is undefined

const hasMaterials = createHas('attributes.materials');
hasMaterials(product); //» true

// NOTE: `get()` should be used if you want to ensure a value is not `undefined`
get('attributes.isCool', product, false); //» false
```

### `omit` 编号 : `createOmit`

返回已删除属性列表的对象的克隆.

说明: `omit()` 返回带有删除属性的克隆。 如果你宁愿修改 ,考虑使用 `remove()`。 。 。 .

```ts
function omit(properties: Path[], object: any): any;

function createOmit(properties: Path[]): (object: any) => any;
```

```js
const user = {
  username: 'blakek',
  roles: ['alert:create', 'alert:read'],
  sites: {
    github: {
      username: 'blakek'
    }
  }
};

omit(['roles', 'sites'], user); //» { username: 'blakek' }
omit(['username', 'roles', 'sites.doesnt.exist'], user);
//» { sites: { github: { username: 'blakek' } } }

const omitExtra = createOmit(['roles, sites']);
omitExtra(user); //» { username: 'blakek' }
```

### `pluck` 编号 : `createPluck`

从对象中获取一个属性子集.

```ts
function pluck(properties: Path[], object: any): any;
```

```js
const user = {
  username: 'blakek',
  roles: ['alert:create', 'alert:read'],
  sites: {
    github: {
      username: 'blakek'
    }
  }
};

pluck(['username'], user); //» { username: 'blakek' }
pluck(['username', 'roles'], user);
//» { username: 'blakek', roles: [ 'alert:create', 'alert:read' ] }

const permissionInfo = pluck(['roles', 'username']);
permissionInfo(user); //» { roles: [ 'alert:create', 'alert:read' ], username: 'blakek' }
```

### `remove` 编号 : `createRemove`

删除路径中的值并返回对象.

说明: `remove()` 修改通过的对象而不是创建副本. 若为 您宁愿返回新对象 :

-   使用情况 `omit()`· ; `omit()` 返回带有删除属性列表的克隆
-   使用情况 `clone()` 在此之前 `remove()`
-   考虑另一种解决办法([不变](https://github.com/planttheidea/unchanged) 真的很不错)

```ts
function remove(path: Path, object: any): any;

function createRemove(path: Path): (object: any) => any;
```

```js
const user = {
  username: 'blakek',
  password: 'wouldntyouliketoknow'
};

remove('password', user); //» { username: 'blakek' }
remove('property.does.not.exist', user);
//» { username: 'blakek' } (same object from previous line)

const removePassword = createRemove('password');
removePassword({ username: 'bob', password: 'laskjfl' }); //» { username: 'bob' }
```

### `set` 编号 : `createSetter`

在路径上设置一个值并返回对象.

说明: `set()` 修改通过的对象而不是创建副本. 若为 您宁愿返回新对象 :

-   使用情况 `clone()` 在此之前 `set()`
-   考虑另一种解决办法([不变](https://github.com/planttheidea/unchanged) 真的很不错)

```ts
function set(value: any, path: Path, object: any): any;

function createSetter(path: Path, object: any): (value: any) => any;
```

```js
const user = {
  profile: {
    bgColor: '#639'
  }
};

set('tomato', 'profile.bgColor', user); //» { profile: { bgColor: 'tomato' } }

set('/images/user.png', 'profile.bgImage', user);
//» { profile: { bgColor: 'tomato', bgImage: '/images/user.png' } }

const logout = set(null, 'profile');
logout(user); //» { profile: null }

const setUsername = createSetter('username', user);
setUsername('blakek'); //»  { profile: { bgColor: 'tomato', bgImage: '/images/user.png' }, username: 'blakek' }
```

## 捐款

[节点.js](https://nodejs.org/) 财务报告和审定财务报表 [雅伦](https://yarnpkg.com/en/docs/) 要求与该项目合作.

要安装所有依赖, 运行 :

```bash
yarn
```

### 有用的命令

|  |  |
| --- | --- |
| `yarn build` | 将工程构建为 `./dist` |
| `yarn format` | 按照 Pretier 样式格式化源 |
| `yarn test` | 运行项目测试 |
| `yarn test --watch` | 运行工程测试, 监视文件更改 |

## 许可证

麻省理工学院

## 常见问题

### @blakek/deep 是什么？

获取、设置、去除和测试深层嵌入属性

### 如何安装 @blakek/deep？

在项目目录运行 npm install @blakek/deep，或使用 pnpm add / yarn add / bun add。

### @blakek/deep 支持 TypeScript 吗？

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

### @blakek/deep 支持 ESM 还是 CommonJS？

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

### @blakek/deep 可以商用吗？

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

### @blakek/deep 的下载量有多少？

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

### @blakek/deep 还在维护吗？

最新版本 4\.0\.0 发布于 2021/12/04（4 年前）；至今共发布 11 个版本，其中过去 12 个月发布了 0 个；首次发布于 2020/06/02。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

