检测缩进
检测代码缩进( commonjs fork)
注: 这是一个叉子 辛德瑞索胡斯/ 检测-缩进 仅修改 恢复提供共同JS出口。 创建它只是为了提供 用于检测缩进的升级路径
pnpm节点包管理器 . 打算与上游保持同步detect-indent财务报告和审定财务报表 使用相同的版本编号。 这个叉子的创造者不赞成 也不批评维护者/ 作者pnpm财务报告和审定财务报表detect-indent这导致有必要 叉来.
传入任意一种文字的字符串并获得缩进.
使用大小写
- 修改文件时坚持缩进 .
- 有与现有缩进相匹配的新内容 .
- 在编辑器中设置正确的缩进 .
安装
npm install detect-indent
使用量
在此修改 JSON 文件, 同时坚持缩进 :
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 密码
接受字符串并返回带有缩进数据的对象 :
type-=YTET -伊甸园字幕组=- 翻译: 缩入之类. 没错undefined如果没有检测到缩进.amount- 缩进量。 譬如说,2。 。 。 .indent- 实际缩入.
算法
目前的算法寻找两个相接而来的非空行之间最常见的区别. 单空缩进和变化默认被忽略,以防止常见的假正态对齐.
在下个例子中,即使4-空格缩进出现3次而2-空格只出现2次,也检测出2-空格缩进,因为2个空格有4个缩进变化而4个空格只有2个变化:
html {
box-sizing: border-box;
}
body {
background: gray;
}
p {
line-height: 1.3em;
margin-top: 1em;
text-indent: 2em;
}
此外,如果使用数相同,有多个缩进差异,则选择多行缩进.
在以下例子中,缩进被检测出为4-空格:
body {
background: gray;
}
p {
line-height: 1.3em;
margin-top: 1em;
text-indent: 2em;
}
相关
- 检测-缩进- cli - 本单元的CLI
- 探测-新线 - 检测字符串的主要新行字符
- 检测缩进器 - 锈口口
- 检测-缩进-平 - Python 口号