detect-indent
Advanced tools
Comparing version 6.1.0 to 7.0.0
@@ -1,18 +0,18 @@ | ||
declare namespace detectIndent { | ||
interface Indent { | ||
/** | ||
Type of indentation. Is `undefined` if no indentation is detected. | ||
*/ | ||
type: 'tab' | 'space' | undefined; | ||
export interface Indent { | ||
/** | ||
The type of indentation. | ||
/** | ||
Amount of indentation, for example `2`. | ||
*/ | ||
amount: number; | ||
It is `undefined` if no indentation is detected. | ||
*/ | ||
type: 'tab' | 'space' | undefined; | ||
/** | ||
Actual indentation. | ||
*/ | ||
indent: string; | ||
} | ||
/** | ||
The amount of indentation. For example, `2`. | ||
*/ | ||
amount: number; | ||
/** | ||
The actual indentation. | ||
*/ | ||
indent: string; | ||
} | ||
@@ -27,4 +27,4 @@ | ||
``` | ||
import * as fs from 'fs'; | ||
import detectIndent = require('detect-indent'); | ||
import fs from 'node:fs'; | ||
import detectIndent from 'detect-indent'; | ||
@@ -43,3 +43,3 @@ // { | ||
fs.writeFileSync('foo.json', JSON.stringify(json, null, indent)); | ||
fs.writeFileSync('foo.json', JSON.stringify(json, undefined, indent)); | ||
// { | ||
@@ -50,4 +50,2 @@ // "ilove": "unicorns" | ||
*/ | ||
declare function detectIndent(string: string): detectIndent.Indent; | ||
export = detectIndent; | ||
export default function detectIndent(string: string): Indent; |
45
index.js
@@ -1,3 +0,1 @@ | ||
'use strict'; | ||
// Detect either spaces or tabs but not both to properly handle tabs for indentation and spaces for alignment | ||
@@ -9,11 +7,16 @@ const INDENT_REGEX = /^(?:( )+|\t+)/; | ||
// 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) { | ||
@@ -46,9 +49,4 @@ const indents = new Map(); | ||
indent = matches[0].length; | ||
indentType = matches[1] ? INDENT_TYPE_SPACE : INDENT_TYPE_TAB; | ||
if (matches[1]) { | ||
indentType = INDENT_TYPE_SPACE; | ||
} else { | ||
indentType = INDENT_TYPE_TAB; | ||
} | ||
// Ignore single space unless it's the only indent detected to prevent common false positives | ||
@@ -81,9 +79,4 @@ if (ignoreSingleSpaces && indentType === INDENT_TYPE_SPACE && indent === 1) { | ||
entry = indents.get(key); | ||
entry = entry === undefined ? [1, 0] : [++entry[0], entry[1] + weight]; | ||
if (entry === undefined) { | ||
entry = [1, 0]; // Init | ||
} else { | ||
entry = [++entry[0], entry[1] + weight]; | ||
} | ||
indents.set(key, entry); | ||
@@ -135,3 +128,3 @@ } | ||
module.exports = string => { | ||
export default function detectIndent(string) { | ||
if (typeof string !== 'string') { | ||
@@ -162,4 +155,4 @@ throw new TypeError('Expected a string'); | ||
type, | ||
indent | ||
indent, | ||
}; | ||
}; | ||
} |
{ | ||
"name": "detect-indent", | ||
"version": "6.1.0", | ||
"version": "7.0.0", | ||
"description": "Detect the indentation of code", | ||
@@ -10,6 +10,8 @@ "license": "MIT", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
"url": "https://sindresorhus.com" | ||
}, | ||
"type": "module", | ||
"exports": "./index.js", | ||
"engines": { | ||
"node": ">=8" | ||
"node": ">=12.20" | ||
}, | ||
@@ -37,5 +39,6 @@ "scripts": { | ||
"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" | ||
}, | ||
@@ -42,0 +45,0 @@ "xo": { |
@@ -7,3 +7,2 @@ # detect-indent | ||
## Use cases | ||
@@ -15,3 +14,2 @@ | ||
## Install | ||
@@ -23,3 +21,2 @@ | ||
## Usage | ||
@@ -30,4 +27,4 @@ | ||
```js | ||
const fs = require('fs'); | ||
const detectIndent = require('detect-indent'); | ||
import fs from 'node:fs'; | ||
import detectIndent from 'detect-indent'; | ||
@@ -48,3 +45,3 @@ /* | ||
fs.writeFileSync('foo.json', JSON.stringify(json, null, indent)); | ||
fs.writeFileSync('foo.json', JSON.stringify(json, undefined, indent)); | ||
/* | ||
@@ -57,3 +54,2 @@ { | ||
## API | ||
@@ -67,3 +63,2 @@ | ||
## Algorithm | ||
@@ -109,3 +104,2 @@ | ||
## Related | ||
@@ -117,3 +111,2 @@ | ||
--- | ||
@@ -120,0 +113,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Yes
9609
4
158
114