Skip to content

Commit 429c145

Browse files
committed
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

.github/workflows/main.yml

Lines changed: 2 additions & 3 deletions
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

index.d.ts

Lines changed: 21 additions & 23 deletions
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;

index.js

Lines changed: 19 additions & 26 deletions
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+
}

index.test-d.ts

Lines changed: 2 additions & 2 deletions
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);

license

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
3+
Copyright (c) Sindre Sorhus <[email protected]> (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

package.json

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
"author": {
88
"name": "Sindre Sorhus",
99
"email": "[email protected]",
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": [

readme.md

Lines changed: 3 additions & 10 deletions
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">

0 commit comments

Comments
 (0)