UNPKG

890 BTypeScriptView Raw
1export interface Indent {
2 /**
3 The type of indentation.
4
5 It is `undefined` if no indentation is detected.
6 */
7 type: 'tab' | 'space' | undefined;
8
9 /**
10 The amount of indentation. For example, `2`.
11 */
12 amount: number;
13
14 /**
15 The actual indentation.
16 */
17 indent: string;
18}
19
20/**
21Detect the indentation of code.
22
23@param string - A string of any kind of text.
24
25@example
26```
27import fs from 'node:fs';
28import detectIndent from 'detect-indent';
29
30// {
31// "ilove": "pizza"
32// }
33const file = fs.readFileSync('foo.json', 'utf8');
34
35// Tries to detect the indentation and falls back to a default if it can't
36const indent = detectIndent(file).indent || ' ';
37
38const json = JSON.parse(file);
39
40json.ilove = 'unicorns';
41
42fs.writeFileSync('foo.json', JSON.stringify(json, undefined, indent));
43// {
44// "ilove": "unicorns"
45// }
46```
47*/
48export default function detectIndent(string: string): Indent;