Skip to content

Commit 429c145

Browse files
committedAug 8, 2021
Require Node.js 12.20 and move to ESM
1 parent 30044e4 commit 429c145

File tree

8 files changed

+73
-85
lines changed

8 files changed

+73
-85
lines changed
 

Diff for: ‎.github/workflows/main.yml

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@ jobs:
1010
fail-fast: false
1111
matrix:
1212
node-version:
13+
- 16
1314
- 14
1415
- 12
15-
- 10
16-
- 8
1716
steps:
1817
- uses: actions/checkout@v2
19-
- uses: actions/setup-node@v1
18+
- uses: actions/setup-node@v2
2019
with:
2120
node-version: ${{ matrix.node-version }}
2221
- run: npm install

Diff for: ‎index.d.ts

+21-23
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
declare namespace detectIndent {
2-
interface Indent {
3-
/**
4-
Type of indentation. Is `undefined` if no indentation is detected.
5-
*/
6-
type: 'tab' | 'space' | undefined;
7-
8-
/**
9-
Amount of indentation, for example `2`.
10-
*/
11-
amount: number;
12-
13-
/**
14-
Actual indentation.
15-
*/
16-
indent: string;
17-
}
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;
1818
}
1919

2020
/**
@@ -24,8 +24,8 @@ Detect the indentation of code.
2424
2525
@example
2626
```
27-
import * as fs from 'fs';
28-
import detectIndent = require('detect-indent');
27+
import fs from 'node:fs';
28+
import detectIndent from 'detect-indent';
2929
3030
// {
3131
// "ilove": "pizza"
@@ -39,12 +39,10 @@ const json = JSON.parse(file);
3939
4040
json.ilove = 'unicorns';
4141
42-
fs.writeFileSync('foo.json', JSON.stringify(json, null, indent));
42+
fs.writeFileSync('foo.json', JSON.stringify(json, undefined, indent));
4343
// {
4444
// "ilove": "unicorns"
4545
// }
4646
```
4747
*/
48-
declare function detectIndent(string: string): detectIndent.Indent;
49-
50-
export = detectIndent;
48+
export default function detectIndent(string: string): Indent;

Diff for: ‎index.js

+19-26
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
'use strict';
2-
31
// Detect either spaces or tabs but not both to properly handle tabs for indentation and spaces for alignment
42
const INDENT_REGEX = /^(?:( )+|\t+)/;
53

64
const INDENT_TYPE_SPACE = 'space';
75
const INDENT_TYPE_TAB = 'tab';
86

9-
// Make a Map that counts how many indents/unindents have occurred for a given size and how many lines follow a given indentation.
10-
// The key is a concatenation of the indentation type (s = space and t = tab) and the size of the indents/unindents.
11-
//
12-
// indents = {
13-
// t3: [1, 0],
14-
// t4: [1, 5],
15-
// s5: [1, 0],
16-
// s12: [1, 0],
17-
// }
7+
/**
8+
Make a Map that counts how many indents/unindents have occurred for a given size and how many lines follow a given indentation.
9+
10+
The key is a concatenation of the indentation type (s = space and t = tab) and the size of the indents/unindents.
11+
12+
```
13+
indents = {
14+
t3: [1, 0],
15+
t4: [1, 5],
16+
s5: [1, 0],
17+
s12: [1, 0],
18+
}
19+
```
20+
*/
1821
function makeIndentsMap(string, ignoreSingleSpaces) {
1922
const indents = new Map();
2023

@@ -42,12 +45,7 @@ function makeIndentsMap(string, ignoreSingleSpaces) {
4245
previousIndentType = '';
4346
} else {
4447
indent = matches[0].length;
45-
46-
if (matches[1]) {
47-
indentType = INDENT_TYPE_SPACE;
48-
} else {
49-
indentType = INDENT_TYPE_TAB;
50-
}
48+
indentType = matches[1] ? INDENT_TYPE_SPACE : INDENT_TYPE_TAB;
5149

5250
// Ignore single space unless it's the only indent detected to prevent common false positives
5351
if (ignoreSingleSpaces && indentType === INDENT_TYPE_SPACE && indent === 1) {
@@ -76,12 +74,7 @@ function makeIndentsMap(string, ignoreSingleSpaces) {
7674

7775
// Update the stats
7876
entry = indents.get(key);
79-
80-
if (entry === undefined) {
81-
entry = [1, 0]; // Init
82-
} else {
83-
entry = [++entry[0], entry[1] + weight];
84-
}
77+
entry = entry === undefined ? [1, 0] : [++entry[0], entry[1] + weight];
8578

8679
indents.set(key, entry);
8780
}
@@ -129,7 +122,7 @@ function makeIndentString(type, amount) {
129122
return indentCharacter.repeat(amount);
130123
}
131124

132-
module.exports = string => {
125+
export default function detectIndent(string) {
133126
if (typeof string !== 'string') {
134127
throw new TypeError('Expected a string');
135128
}
@@ -155,6 +148,6 @@ module.exports = string => {
155148
return {
156149
amount,
157150
type,
158-
indent
151+
indent,
159152
};
160-
};
153+
}

Diff for: ‎index.test-d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {expectType} from 'tsd';
2-
import detectIndent = require('.');
2+
import detectIndent, {Indent} from './index.js';
33

44
const indent = detectIndent('');
5-
expectType<detectIndent.Indent>(indent);
5+
expectType<Indent>(indent);
66
expectType<number>(indent.amount);
77
expectType<string>(indent.indent);
88
expectType<'space' | 'tab' | undefined>(indent.type);

Diff for: ‎license

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
3+
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
66

Diff for: ‎package.json

+8-5
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
"author": {
88
"name": "Sindre Sorhus",
99
"email": "sindresorhus@gmail.com",
10-
"url": "sindresorhus.com"
10+
"url": "https://sindresorhus.com"
1111
},
12+
"type": "module",
13+
"exports": "./index.js",
1214
"engines": {
13-
"node": ">=8"
15+
"node": ">=12.20"
1416
},
1517
"scripts": {
1618
"test": "xo && ava && tsd"
@@ -33,9 +35,10 @@
3335
"tab"
3436
],
3537
"devDependencies": {
36-
"ava": "^1.4.1",
37-
"tsd": "^0.7.2",
38-
"xo": "^0.24.0"
38+
"ava": "^3.15.0",
39+
"tsd": "^0.17.0",
40+
"typescript": "^4.3.5",
41+
"xo": "^0.44.0"
3942
},
4043
"xo": {
4144
"ignores": [

Diff for: ‎readme.md

+3-10
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,25 @@
44
55
Pass in a string of any kind of text and get the indentation.
66

7-
87
## Use cases
98

109
- Persisting the indentation when modifying a file.
1110
- Have new content match the existing indentation.
1211
- Setting the right indentation in your editor.
1312

14-
1513
## Install
1614

1715
```
1816
$ npm install detect-indent
1917
```
2018

21-
2219
## Usage
2320

2421
Here we modify a JSON file while persisting the indentation:
2522

2623
```js
27-
const fs = require('fs');
28-
const detectIndent = require('detect-indent');
24+
import fs from 'node:fs';
25+
import detectIndent from 'detect-indent';
2926

3027
/*
3128
{
@@ -41,15 +38,14 @@ const json = JSON.parse(file);
4138

4239
json.ilove = 'unicorns';
4340

44-
fs.writeFileSync('foo.json', JSON.stringify(json, null, indent));
41+
fs.writeFileSync('foo.json', JSON.stringify(json, undefined, indent));
4542
/*
4643
{
4744
"ilove": "unicorns"
4845
}
4946
*/
5047
```
5148

52-
5349
## API
5450

5551
Accepts a string and returns an object with stats about the indentation:
@@ -58,7 +54,6 @@ Accepts a string and returns an object with stats about the indentation:
5854
* `type` {'tab' | 'space' | undefined} - Type of indentation. Possible values are `'tab'`, `'space'` or `undefined` if no indentation is detected
5955
* `indent` {string} - Actual indentation
6056

61-
6257
## Algorithm
6358

6459
The current algorithm looks for the most common difference between two consecutive non-empty lines.
@@ -99,14 +94,12 @@ p {
9994
}
10095
```
10196

102-
10397
## Related
10498

10599
- [detect-indent-cli](https://github.com/sindresorhus/detect-indent-cli) - CLI for this module
106100
- [detect-newline](https://github.com/sindresorhus/detect-newline) - Detect the dominant newline character of a string
107101
- [detect-indent-rs](https://github.com/stefanpenner/detect-indent-rs) - Rust port
108102

109-
110103
---
111104

112105
<div align="center">

Diff for: ‎test.js

+17-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import fs from 'fs';
2-
import path from 'path';
1+
import fs from 'node:fs';
2+
import path from 'node:path';
3+
import {fileURLToPath} from 'node:url';
34
import test from 'ava';
4-
import detectIndent from '.';
5+
import detectIndent from './index.js';
56

7+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
68
const getFile = file => fs.readFileSync(path.join(__dirname, file), 'utf8');
79

810
test('detect the indent of a file with space indent', t => {
@@ -14,7 +16,7 @@ test('return indentation stats for spaces', t => {
1416
t.deepEqual(stats, {
1517
amount: 4,
1618
indent: ' ',
17-
type: 'space'
19+
type: 'space',
1820
});
1921
});
2022

@@ -23,7 +25,7 @@ test('return indentation stats for multiple tabs', t => {
2325
t.deepEqual(stats, {
2426
amount: 4,
2527
indent: '\t\t\t\t',
26-
type: 'tab'
28+
type: 'tab',
2729
});
2830
});
2931

@@ -36,7 +38,7 @@ test('return indentation stats for tabs', t => {
3638
t.deepEqual(stats, {
3739
amount: 1,
3840
indent: '\t',
39-
type: 'tab'
41+
type: 'tab',
4042
});
4143
});
4244

@@ -49,7 +51,7 @@ test('return indentation stats for equal tabs and spaces', t => {
4951
t.deepEqual(indent, {
5052
amount: 1,
5153
indent: '\t',
52-
type: 'tab'
54+
type: 'tab',
5355
});
5456
});
5557

@@ -63,7 +65,7 @@ test('return indentation stats for mostly spaces', t => {
6365
t.deepEqual(stats, {
6466
amount: 4,
6567
indent: ' ',
66-
type: 'space'
68+
type: 'space',
6769
});
6870
});
6971

@@ -77,7 +79,7 @@ test('return indentation stats for various spaces', t => {
7779
t.deepEqual(stats, {
7880
amount: 4,
7981
indent: ' ',
80-
type: 'space'
82+
type: 'space',
8183
});
8284
});
8385

@@ -90,7 +92,7 @@ test('return indentation stats for no indentation', t => {
9092
t.deepEqual(stats, {
9193
amount: 0,
9294
indent: '',
93-
type: undefined
95+
type: undefined,
9496
});
9597
});
9698

@@ -99,7 +101,7 @@ test('return indentation stats for fifty-fifty indented files with spaces first'
99101
t.deepEqual(stats, {
100102
amount: 4,
101103
indent: ' ',
102-
type: 'space'
104+
type: 'space',
103105
});
104106
});
105107

@@ -108,7 +110,7 @@ test('return indentation stats for fifty-fifty indented files with tabs first',
108110
t.deepEqual(stats, {
109111
amount: 1,
110112
indent: ' ',
111-
type: 'tab'
113+
type: 'tab',
112114
});
113115
});
114116

@@ -117,7 +119,7 @@ test('return indentation stats for indented files with spaces and tabs last', t
117119
t.deepEqual(stats, {
118120
amount: 1,
119121
indent: ' ',
120-
type: 'tab'
122+
type: 'tab',
121123
});
122124
});
123125

@@ -126,7 +128,7 @@ test('detect the indent of a file with single line comments', t => {
126128
t.deepEqual(stats, {
127129
amount: 4,
128130
indent: ' ',
129-
type: 'space'
131+
type: 'space',
130132
});
131133
});
132134

@@ -135,6 +137,6 @@ test('return indentations status for indented files with single spaces only', t
135137
t.deepEqual(stats, {
136138
amount: 1,
137139
indent: ' ',
138-
type: 'space'
140+
type: 'space',
139141
});
140142
});

0 commit comments

Comments
 (0)
Please sign in to comment.