Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 8, 2021
1 parent 30044e4 commit 429c145
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 85 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 14
- 12
- 10
- 8
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
44 changes: 21 additions & 23 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
declare namespace detectIndent {
interface Indent {
/**
Type of indentation. Is `undefined` if no indentation is detected.
*/
type: 'tab' | 'space' | undefined;

/**
Amount of indentation, for example `2`.
*/
amount: number;

/**
Actual indentation.
*/
indent: string;
}
export interface Indent {
/**
The type of indentation.
It is `undefined` if no indentation is detected.
*/
type: 'tab' | 'space' | undefined;

/**
The amount of indentation. For example, `2`.
*/
amount: number;

/**
The actual indentation.
*/
indent: string;
}

/**
Expand All @@ -24,8 +24,8 @@ Detect the indentation of code.
@example
```
import * as fs from 'fs';
import detectIndent = require('detect-indent');
import fs from 'node:fs';
import detectIndent from 'detect-indent';
// {
// "ilove": "pizza"
Expand All @@ -39,12 +39,10 @@ const json = JSON.parse(file);
json.ilove = 'unicorns';
fs.writeFileSync('foo.json', JSON.stringify(json, null, indent));
fs.writeFileSync('foo.json', JSON.stringify(json, undefined, indent));
// {
// "ilove": "unicorns"
// }
```
*/
declare function detectIndent(string: string): detectIndent.Indent;

export = detectIndent;
export default function detectIndent(string: string): Indent;
45 changes: 19 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
'use strict';

// Detect either spaces or tabs but not both to properly handle tabs for indentation and spaces for alignment
const INDENT_REGEX = /^(?:( )+|\t+)/;

const INDENT_TYPE_SPACE = 'space';
const INDENT_TYPE_TAB = 'tab';

// Make a Map that counts how many indents/unindents have occurred for a given size and how many lines follow a given indentation.
// The key is a concatenation of the indentation type (s = space and t = tab) and the size of the indents/unindents.
//
// indents = {
// t3: [1, 0],
// t4: [1, 5],
// s5: [1, 0],
// s12: [1, 0],
// }
/**
Make a Map that counts how many indents/unindents have occurred for a given size and how many lines follow a given indentation.
The key is a concatenation of the indentation type (s = space and t = tab) and the size of the indents/unindents.
```
indents = {
t3: [1, 0],
t4: [1, 5],
s5: [1, 0],
s12: [1, 0],
}
```
*/
function makeIndentsMap(string, ignoreSingleSpaces) {
const indents = new Map();

Expand Down Expand Up @@ -42,12 +45,7 @@ function makeIndentsMap(string, ignoreSingleSpaces) {
previousIndentType = '';
} else {
indent = matches[0].length;

if (matches[1]) {
indentType = INDENT_TYPE_SPACE;
} else {
indentType = INDENT_TYPE_TAB;
}
indentType = matches[1] ? INDENT_TYPE_SPACE : INDENT_TYPE_TAB;

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

// Update the stats
entry = indents.get(key);

if (entry === undefined) {
entry = [1, 0]; // Init
} else {
entry = [++entry[0], entry[1] + weight];
}
entry = entry === undefined ? [1, 0] : [++entry[0], entry[1] + weight];

indents.set(key, entry);
}
Expand Down Expand Up @@ -129,7 +122,7 @@ function makeIndentString(type, amount) {
return indentCharacter.repeat(amount);
}

module.exports = string => {
export default function detectIndent(string) {
if (typeof string !== 'string') {
throw new TypeError('Expected a string');
}
Expand All @@ -155,6 +148,6 @@ module.exports = string => {
return {
amount,
type,
indent
indent,
};
};
}
4 changes: 2 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {expectType} from 'tsd';
import detectIndent = require('.');
import detectIndent, {Indent} from './index.js';

const indent = detectIndent('');
expectType<detectIndent.Indent>(indent);
expectType<Indent>(indent);
expectType<number>(indent.amount);
expectType<string>(indent.indent);
expectType<'space' | 'tab' | undefined>(indent.type);
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com)

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:

Expand Down
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": ">=12.20"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -33,9 +35,10 @@
"tab"
],
"devDependencies": {
"ava": "^1.4.1",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"ava": "^3.15.0",
"tsd": "^0.17.0",
"typescript": "^4.3.5",
"xo": "^0.44.0"
},
"xo": {
"ignores": [
Expand Down
13 changes: 3 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,25 @@
Pass in a string of any kind of text and get the indentation.


## Use cases

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


## Install

```
$ npm install detect-indent
```


## Usage

Here we modify a JSON file while persisting the indentation:

```js
const fs = require('fs');
const detectIndent = require('detect-indent');
import fs from 'node:fs';
import detectIndent from 'detect-indent';

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

json.ilove = 'unicorns';

fs.writeFileSync('foo.json', JSON.stringify(json, null, indent));
fs.writeFileSync('foo.json', JSON.stringify(json, undefined, indent));
/*
{
"ilove": "unicorns"
}
*/
```


## API

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


## Algorithm

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


## Related

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


---

<div align="center">
Expand Down
Loading

0 comments on commit 429c145

Please sign in to comment.