@gwhitney/detect-indent

中文文档
7.0.2发布于 2025/12/09MITTypeScriptCommonJS零依赖近 30 天下载 380.9万

Detect the indentation of code (commonjs fork)

原始使用文档
文档目录6 节
  1. Use cases
  2. Install
  3. Usage
  4. API
  5. Algorithm
  6. Related

detect-indent

Detect the indentation of code (commonjs fork)

NOTE: This is a fork of sindresorhus/detect-indent modified solely to revert to providing CommonJS exports. It was created only to provide an upgrade path for detect-indent in the pnpm Node package manager. It is intended to be kept in sync with the upstream detect-indent and use identical version numbers. The creator of this fork neither endorses nor criticizes the convention decisions on the part of the maintainers/ authors of pnpm and detect-indent that led to the necessity of this fork.

Pass in a string of any kind of text and get the indentation.

Use cases

  • Persisting the indentation when modifying a file.
  • Have new content match the existing indentation.
  • Setting the right indentation in your editor.

Install

npm install detect-indent

Usage

Here we modify a JSON file while persisting the indentation:

const fs = require('node:fs');
const detectIndent = require('detect-indent');

/*
{
    "ilove": "pizza"
}
*/
const file = fs.readFileSync('foo.json', 'utf8');

// Tries to detect the indentation and falls back to a default if it can't
const indent = detectIndent(file).indent || '    ';

const json = JSON.parse(file);

json.ilove = 'unicorns';

fs.writeFileSync('foo.json', JSON.stringify(json, undefined, indent));
/*
{
    "ilove": "unicorns"
}
*/

API

Accepts a string and returns an object with stats about the indentation:

  • type {'tab' | 'space' | undefined} - The type of indentation. It is undefined if no indentation is detected.
  • amount {number} - The amount of indentation. For example, 2.
  • indent {string} - The actual indentation.

Algorithm

The current algorithm looks for the most common difference between two consecutive non-empty lines. Single-space indentations and changes are ignored by default to prevent common false positives from comment alignment.

In the following example, even if 4-space indentation appears 3 times while 2-space appears only 2 times, the 2-space indentation is detected because there are 4 indent changes of 2 spaces vs only 2 changes of 4 spaces:

html {
  box-sizing: border-box;
}

body {
  background: gray;
}

p {
    line-height: 1.3em;
    margin-top: 1em;
    text-indent: 2em;
}

Source.

Furthermore, if there are multiple indent differences with the same usage count, the indentation with the most lines is selected.

In the following example, the indentation is detected as 4-spaces:

body {
  background: gray;
}

p {
    line-height: 1.3em;
    margin-top: 1em;
    text-indent: 2em;
}

版本与发布记录

@gwhitney/detect-indent 共发布 2 个版本,首次发布于 2022/11/22;过去 12 个月发布了 1 个版本。

版本发布日期距今
7.0.2latest2025/12/0911 个月前
7.0.12022/11/223 年前

关于 @gwhitney/detect-indent 的常见问题

@gwhitney/detect-indent 是什么?

Detect the indentation of code (commonjs fork)

如何安装 @gwhitney/detect-indent?

在项目目录运行 npm install @gwhitney/detect-indent,或使用 pnpm add / yarn add / bun add。

@gwhitney/detect-indent 支持 TypeScript 吗?

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

@gwhitney/detect-indent 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@gwhitney/detect-indent 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=12.20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@gwhitney/detect-indent 可以商用吗?

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

@gwhitney/detect-indent 的下载量有多少?

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

@gwhitney/detect-indent 还在维护吗?

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