detect-indent
Advanced tools
Comparing version 6.0.0 to 6.1.0
126
index.js
@@ -6,23 +6,17 @@ 'use strict'; | ||
function getMostUsed(indents) { | ||
let result = 0; | ||
let maxUsed = 0; | ||
let maxWeight = 0; | ||
const INDENT_TYPE_SPACE = 'space'; | ||
const INDENT_TYPE_TAB = 'tab'; | ||
for (const [key, [usedCount, weight]] of indents) { | ||
if (usedCount > maxUsed || (usedCount === maxUsed && weight > maxWeight)) { | ||
maxUsed = usedCount; | ||
maxWeight = weight; | ||
result = key; | ||
} | ||
} | ||
// 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(); | ||
return result; | ||
} | ||
module.exports = string => { | ||
if (typeof string !== 'string') { | ||
throw new TypeError('Expected a string'); | ||
} | ||
// Remember the size of previous line's indentation | ||
@@ -35,13 +29,2 @@ let previousSize = 0; | ||
// Remember 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], | ||
// } | ||
const indents = new Map(); | ||
for (const line of string.split(/\n/g)) { | ||
@@ -66,7 +49,12 @@ if (!line) { | ||
if (matches[1]) { | ||
indentType = 's'; | ||
indentType = INDENT_TYPE_SPACE; | ||
} else { | ||
indentType = 't'; | ||
indentType = 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) { | ||
continue; | ||
} | ||
if (indentType !== previousIndentType) { | ||
@@ -88,3 +76,4 @@ previousSize = 0; | ||
} else { | ||
key = indentType + String(indentDifference > 0 ? indentDifference : -indentDifference); | ||
const absoluteIndentDifference = indentDifference > 0 ? indentDifference : -indentDifference; | ||
key = encodeIndentsKey(indentType, absoluteIndentDifference); | ||
} | ||
@@ -105,20 +94,67 @@ | ||
const result = getMostUsed(indents); | ||
return indents; | ||
} | ||
let amount = 0; | ||
let type; | ||
let indent = ''; | ||
// Encode the indent type and amount as a string (e.g. 's4') for use as a compound key in the indents Map. | ||
function encodeIndentsKey(indentType, indentAmount) { | ||
const typeCharacter = indentType === INDENT_TYPE_SPACE ? 's' : 't'; | ||
return typeCharacter + String(indentAmount); | ||
} | ||
if (result !== 0) { | ||
amount = Number(result.slice(1)); | ||
// Extract the indent type and amount from a key of the indents Map. | ||
function decodeIndentsKey(indentsKey) { | ||
const keyHasTypeSpace = indentsKey[0] === 's'; | ||
const type = keyHasTypeSpace ? INDENT_TYPE_SPACE : INDENT_TYPE_TAB; | ||
if (result[0] === 's') { | ||
type = 'space'; | ||
indent = ' '.repeat(amount); | ||
} else { | ||
type = 'tab'; | ||
indent = '\t'.repeat(amount); | ||
const amount = Number(indentsKey.slice(1)); | ||
return {type, amount}; | ||
} | ||
// Return the key (e.g. 's4') from the indents Map that represents the most common indent, | ||
// or return undefined if there are no indents. | ||
function getMostUsedKey(indents) { | ||
let result; | ||
let maxUsed = 0; | ||
let maxWeight = 0; | ||
for (const [key, [usedCount, weight]] of indents) { | ||
if (usedCount > maxUsed || (usedCount === maxUsed && weight > maxWeight)) { | ||
maxUsed = usedCount; | ||
maxWeight = weight; | ||
result = key; | ||
} | ||
} | ||
return result; | ||
} | ||
function makeIndentString(type, amount) { | ||
const indentCharacter = type === INDENT_TYPE_SPACE ? ' ' : '\t'; | ||
return indentCharacter.repeat(amount); | ||
} | ||
module.exports = string => { | ||
if (typeof string !== 'string') { | ||
throw new TypeError('Expected a string'); | ||
} | ||
// Identify indents while skipping single space indents to avoid common edge cases (e.g. code comments) | ||
// If no indents are identified, run again and include all indents for comprehensive detection | ||
let indents = makeIndentsMap(string, true); | ||
if (indents.size === 0) { | ||
indents = makeIndentsMap(string, false); | ||
} | ||
const keyOfMostUsedIndent = getMostUsedKey(indents); | ||
let type; | ||
let amount = 0; | ||
let indent = ''; | ||
if (keyOfMostUsedIndent !== undefined) { | ||
({type, amount} = decodeIndentsKey(keyOfMostUsedIndent)); | ||
indent = makeIndentString(type, amount); | ||
} | ||
return { | ||
@@ -125,0 +161,0 @@ amount, |
{ | ||
"name": "detect-indent", | ||
"version": "6.0.0", | ||
"version": "6.1.0", | ||
"description": "Detect the indentation of code", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -1,2 +0,2 @@ | ||
# detect-indent [![Build Status](https://travis-ci.org/sindresorhus/detect-indent.svg?branch=master)](https://travis-ci.org/sindresorhus/detect-indent) | ||
# detect-indent | ||
@@ -110,4 +110,12 @@ > Detect the indentation of code | ||
## License | ||
--- | ||
MIT © [Sindre Sorhus](https://sindresorhus.com) | ||
<div align="center"> | ||
<b> | ||
<a href="https://tidelift.com/subscription/pkg/npm-detect-indent?utm_source=npm-detect-indent&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a> | ||
</b> | ||
<br> | ||
<sub> | ||
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies. | ||
</sub> | ||
</div> |
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
9681
166
121