# @darkobits/mask-string 中文文档

规范地址：https://npmcn.com/package/%40darkobits/mask-string/

版本：3\.0\.0
npm 元数据更新时间：2024-11-27T09:01:48.203Z
中文译文更新时间：2026-11-11T11:56:10.581Z
软件包许可：Hippocratic

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

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

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

- 包体积（解压后）：26.3 kB，12 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- 运行依赖：0 个
- 首次发布：2019-07-15T02:03:37.636Z
- 最新版本发布：2024-11-27T09:01:48.034Z
- 近 30 天下载量：5900（2026-10-12 至 2026-11-10，npm 官方下载统计）
- 已发布版本数：7（近 12 个月 0 个）

## 中文简介

面具标志在一个字符串.

## 安装

```sh
npm install @darkobits/mask-string
```

也可以使用：`pnpm add @darkobits/mask-string`、`yarn add @darkobits/mask-string`、`bun add @darkobits/mask-string`

## README

![](https://github.com/darkobits/mask-string/assets/441546/392e18ff-2a0c-46db-a2d3-5167d03bcb2b)

[![](https://img.shields.io/npm/v/@darkobits/mask-string.svg?style=flat-square)](https://www.npmjs.com/package/@darkobits/mask-string) [![](https://img.shields.io/github/actions/workflow/status/darkobits/mask-string/ci.yml?style=flat-square)](https://github.com/darkobits/mask-string/actions?query=workflow%3Aci) [![](https://img.shields.io/depfu/darkobits/mask-string?style=flat-square)](https://depfu.com/repos/github/darkobits/mask-string) [![](https://img.shields.io/static/v1?label=commits&message=conventional&style=flat-square&color=398AFB)](https://conventionalcommits.org/) [![](https://img.shields.io/static/v1?label=license&message=hippocratic&style=flat-square&color=753065)](https://firstdonoharm.dev/)

## 安装

```
npm i @darkobits/mask-string
```

## 使用

此包的默认导出是以下签名的函数:

```ts
mask(pattern: string | RegExp | Array<string | RegExp>, str: string, maskChar = '*'): string
```

**示例**

```ts
import mask from '@darkobits/mask-string';

const str = 'Colorless green ideas sleep furiously.';
const masked = mask('green', str);
// => 'Colorless ***** ideas sleep furiously.'
```

```ts
import mask from '@darkobits/mask-string';

const str = 'Twas bryllyg, and ye slythy toves did gyre and gymble in ye wabe.';
const masked = mask([/and/g, /gy/g], str, '#');
// => 'Twas bryllyg, ### ye slythy toves did ##re ### ##mble in ye wabe.'
```

### `maskAll`

此套件额外导出一个函数 `maskAll` 其签名如下:

```ts
maskAll(str: string, maskChar = '*'): string
```

此函数接受一个字符串并返回一个完全由所提供的口罩字符组成的相同长度的字符串.

**示例**

```ts
import { maskAll } from '@darkobits/mask-string';

maskAll('foo') // => '***'
maskAll('bar', '#') // => '###'
```

  
[![](https://user-images.githubusercontent.com/441546/102322726-5e6d4200-3f34-11eb-89f2-c31624ab7488.png)](https://npmcn.com/package/%40darkobits/mask-string/#readme-top)

## 常见问题

### @darkobits/mask-string 是什么？

面具标志在一个字符串\.

### 如何安装 @darkobits/mask-string？

在项目目录运行 npm install @darkobits/mask-string，或使用 pnpm add / yarn add / bun add。

### @darkobits/mask-string 支持 TypeScript 吗？

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

### @darkobits/mask-string 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### @darkobits/mask-string 可以商用吗？

@darkobits/mask-string 使用 Hippocratic 许可。声明的许可为 Hippocratic，请阅读许可原文确认使用条件。以上为概括，不构成法律意见，请以许可原文为准。

### @darkobits/mask-string 的下载量有多少？

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

### @darkobits/mask-string 还在维护吗？

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

