You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

detect-indent

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

detect-indent - npm Package Compare versions

Comparing version

to
2.0.0

80

index.js
'use strict';
var strip = require('strip-comments');
function gcd(a, b) {
return b ? gcd(b, a % b) : a;
}
// detect either spaces or tabs but not both to properly handle tabs
// for indentation and spaces for alignment
var INDENT_RE = /^(?:( )+|\t+)/;
var repeating = require('repeating');
module.exports = function (str) {

@@ -13,32 +14,67 @@ if (typeof str !== 'string') {

var re = /^([ \t]*)/;
var result = [];
// used to if tabs or spaces are the most used
var t = 0;
var s = 0;
// strip multi-line comments and normalize
str = strip.block(str).replace(/\r/g, '');
// remember the indentation used for the previous line
var prev = 0;
// remember how much a given indentation size was used
var indents = {};
str.split(/\n/g).forEach(function (line) {
/^\t/.test(line) ? t++ : s++;
if (!line) {
// ignore empty lines
return;
}
var len = line.match(re)[0].length;
var matches = line.match(INDENT_RE);
// convert odd numbers to even numbers
if (len % 2 === 1) {
len += 1;
var indent;
if (!matches) {
indent = 0;
} else {
indent = matches[0].length;
if (matches[1]) {
// spaces were used
s++;
} else {
// tabs were used
t++;
}
}
result.push(len);
var diff = Math.abs(indent - prev);
prev = indent;
if (diff) {
// an indent or deindent has been detected
indents[diff] = (indents[diff] || 0) + 1;
}
});
// greatest common divisor is most likely the indent size
var indent = result.reduce(gcd);
// 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 = indent === 0 ? 0 : (s === t ? indent / 2 : (t >= s ? indent / 2 : indent));
var type = indent === 0 ? null : (s >= t ? 'space' : 'tab');
var actual = indent === 0 ? '' : (s >= t ? ' ' : '\t');
if (amount > 0) {
actual = new Array(amount + 1).join(actual);
var type;
var actual;
if (!amount) {
type = null;
actual = '';
} else if (s >= t) {
type = 'space';
actual = repeating(' ', amount);
} else {
type = 'tab';
actual = repeating('\t', amount);
}

@@ -45,0 +81,0 @@

{
"name": "detect-indent",
"version": "1.0.1",
"version": "2.0.0",
"description": "Detect the indentation of code",

@@ -43,3 +43,3 @@ "license": "MIT",

"minimist": "^1.1.0",
"strip-comments": "^0.3.2"
"repeating": "^1.1.0"
},

@@ -46,0 +46,0 @@ "devDependencies": {

@@ -79,7 +79,10 @@ # detect-indent [![Build Status](https://travis-ci.org/sindresorhus/detect-indent.svg?branch=master)](https://travis-ci.org/sindresorhus/detect-indent)

It first strips off multi-line comments. Then goes through each line counting whether it's tabs or spaces and the indent size coerced to an even number. The resulting indent size is the GCD (Greates Common Divisor) of the gathered line indent sizes and the type is the most used type.
Look for the most common difference between two consecutive non-empty
lines.
[Source](https://medium.com/@heatherarthur/detecting-code-indentation-eff3ed0fb56b#3918).
## License
MIT © [Sindre Sorhus](http://sindresorhus.com)