UNPKG

2.84 kBMarkdownView Raw
1# detect-indent
2
3> Detect the indentation of code
4
5Pass in a string of any kind of text and get the indentation.
6
7## Use cases
8
9- Persisting the indentation when modifying a file.
10- Have new content match the existing indentation.
11- Setting the right indentation in your editor.
12
13## Install
14
15```
16$ npm install detect-indent
17```
18
19## Usage
20
21Here we modify a JSON file while persisting the indentation:
22
23```js
24import fs from 'node:fs';
25import detectIndent from 'detect-indent';
26
27/*
28{
29 "ilove": "pizza"
30}
31*/
32const file = fs.readFileSync('foo.json', 'utf8');
33
34// Tries to detect the indentation and falls back to a default if it can't
35const indent = detectIndent(file).indent || ' ';
36
37const json = JSON.parse(file);
38
39json.ilove = 'unicorns';
40
41fs.writeFileSync('foo.json', JSON.stringify(json, undefined, indent));
42/*
43{
44 "ilove": "unicorns"
45}
46*/
47```
48
49## API
50
51Accepts a string and returns an object with stats about the indentation:
52
53* `amount` {number} - Amount of indentation, for example `2`
54* `type` {'tab' | 'space' | undefined} - Type of indentation. Possible values are `'tab'`, `'space'` or `undefined` if no indentation is detected
55* `indent` {string} - Actual indentation
56
57## Algorithm
58
59The current algorithm looks for the most common difference between two consecutive non-empty lines.
60
61In the following example, even if the 4-space indentation is used 3 times whereas the 2-space one is used 2 times, it is detected as less used because there were only 2 differences with this value instead of 4 for the 2-space indentation:
62
63```css
64html {
65 box-sizing: border-box;
66}
67
68body {
69 background: gray;
70}
71
72p {
73 line-height: 1.3em;
74 margin-top: 1em;
75 text-indent: 2em;
76}
77```
78
79[Source.](https://medium.com/@heatherarthur/detecting-code-indentation-eff3ed0fb56b#3918)
80
81Furthermore, if there are more than one most used difference, the indentation with the most lines is selected.
82
83In the following example, the indentation is detected as 4-spaces:
84
85```css
86body {
87 background: gray;
88}
89
90p {
91 line-height: 1.3em;
92 margin-top: 1em;
93 text-indent: 2em;
94}
95```
96
97## Related
98
99- [detect-indent-cli](https://github.com/sindresorhus/detect-indent-cli) - CLI for this module
100- [detect-newline](https://github.com/sindresorhus/detect-newline) - Detect the dominant newline character of a string
101- [detect-indent-rs](https://github.com/stefanpenner/detect-indent-rs) - Rust port
102
103---
104
105<div align="center">
106 <b>
107 <a href="https://tidelift.com/subscription/pkg/npm-detect-indent?utm_source=npm-detect-indent&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
108 </b>
109 <br>
110 <sub>
111 Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
112 </sub>
113</div>