1 | export 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 | /**
|
21 | Detect the indentation of code.
|
22 |
|
23 | @param string - A string of any kind of text.
|
24 |
|
25 | @example
|
26 | ```
|
27 | import fs from 'node:fs';
|
28 | import detectIndent from 'detect-indent';
|
29 |
|
30 | // {
|
31 | // "ilove": "pizza"
|
32 | // }
|
33 | const file = fs.readFileSync('foo.json', 'utf8');
|
34 |
|
35 | // Tries to detect the indentation and falls back to a default if it can't
|
36 | const indent = detectIndent(file).indent || ' ';
|
37 |
|
38 | const json = JSON.parse(file);
|
39 |
|
40 | json.ilove = 'unicorns';
|
41 |
|
42 | fs.writeFileSync('foo.json', JSON.stringify(json, undefined, indent));
|
43 | // {
|
44 | // "ilove": "unicorns"
|
45 | // }
|
46 | ```
|
47 | */
|
48 | export default function detectIndent(string: string): Indent;
|