detect-indent
Advanced tools
+59
-26
| 'use strict'; | ||
| var repeating = require('repeating'); | ||
@@ -7,4 +8,22 @@ // detect either spaces or tabs but not both to properly handle tabs | ||
| var repeating = require('repeating'); | ||
| function getMostUsed(indents) { | ||
| var result = 0; | ||
| var maxUsed = 0; | ||
| var maxWeight = 0; | ||
| for (var n in indents) { | ||
| var indent = indents[n]; | ||
| var u = indent[0]; | ||
| var w = indent[1]; | ||
| if (u > maxUsed || u === maxUsed && w > maxWeight) { | ||
| maxUsed = u; | ||
| maxWeight = w; | ||
| result = +n; | ||
| } | ||
| } | ||
| return result; | ||
| } | ||
| module.exports = function (str) { | ||
@@ -15,12 +34,26 @@ if (typeof str !== 'string') { | ||
| // used to if tabs or spaces are the most used | ||
| var t = 0; | ||
| var s = 0; | ||
| // used to see if tabs or spaces are the most used | ||
| var tabs = 0; | ||
| var spaces = 0; | ||
| // remember the indentation used for the previous line | ||
| // remember the size of previous line's indentation | ||
| var prev = 0; | ||
| // remember how much a given indentation size was used | ||
| // remember how many indents/unindents as occurred for a given size | ||
| // and how much lines follow a given indentation | ||
| // | ||
| // indents = { | ||
| // 3: [1, 0], | ||
| // 4: [1, 5], | ||
| // 5: [1, 0], | ||
| // 12: [1, 0], | ||
| // } | ||
| var indents = {}; | ||
| // pointer to the array of last used indent | ||
| var current; | ||
| // whether the last action was an indent (opposed to an unindent) | ||
| var isIndent; | ||
| str.split(/\n/g).forEach(function (line) { | ||
@@ -32,5 +65,5 @@ if (!line) { | ||
| var indent; | ||
| var matches = line.match(INDENT_RE); | ||
| var indent; | ||
| if (!matches) { | ||
@@ -42,30 +75,30 @@ indent = 0; | ||
| if (matches[1]) { | ||
| // spaces were used | ||
| s++; | ||
| spaces++; | ||
| } else { | ||
| // tabs were used | ||
| t++; | ||
| tabs++; | ||
| } | ||
| } | ||
| var diff = Math.abs(indent - prev); | ||
| var diff = indent - prev; | ||
| prev = indent; | ||
| if (diff) { | ||
| // an indent or deindent has been detected | ||
| indents[diff] = (indents[diff] || 0) + 1; | ||
| // an indent or unindent has been detected | ||
| isIndent = diff > 0; | ||
| current = indents[isIndent ? diff : -diff]; | ||
| if (current) { | ||
| current[0]++; | ||
| } else { | ||
| current = indents[diff] = [1, 0]; | ||
| } | ||
| } else if (current) { | ||
| // if the last action was an indent, increment the weight | ||
| current[1] += +isIndent; | ||
| } | ||
| }); | ||
| // find most frequent indentation | ||
| var amount = 0; | ||
| var max = 0; | ||
| var n; | ||
| for (var diff in indents) { | ||
| n = indents[diff]; | ||
| if (n > max) { | ||
| max = n; | ||
| amount = +diff; | ||
| } | ||
| } | ||
| var amount = getMostUsed(indents); | ||
@@ -77,3 +110,3 @@ var type; | ||
| actual = ''; | ||
| } else if (s >= t) { | ||
| } else if (spaces >= tabs) { | ||
| type = 'space'; | ||
@@ -80,0 +113,0 @@ actual = repeating(' ', amount); |
+1
-1
| { | ||
| "name": "detect-indent", | ||
| "version": "2.0.0", | ||
| "version": "3.0.0", | ||
| "description": "Detect the indentation of code", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
5710
13.47%144
21.01%3
50%