Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
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 4.0.0 to 5.0.0

80

index.js

@@ -1,23 +0,24 @@

/* eslint-disable guard-for-in */
'use strict';
var repeating = require('repeating');
// detect either spaces or tabs but not both to properly handle tabs
// for indentation and spaces for alignment
var INDENT_RE = /^(?:( )+|\t+)/;
const INDENT_RE = /^(?:( )+|\t+)/;
function getMostUsed(indents) {
var result = 0;
var maxUsed = 0;
var maxWeight = 0;
let result = 0;
let maxUsed = 0;
let maxWeight = 0;
for (var n in indents) {
var indent = indents[n];
var u = indent[0];
var w = indent[1];
for (const entry of indents) {
// TODO: use destructuring when targeting Node.js 6
const key = entry[0];
const val = entry[1];
if (u > maxUsed || u === maxUsed && w > maxWeight) {
const u = val[0];
const w = val[1];
if (u > maxUsed || (u === maxUsed && w > maxWeight)) {
maxUsed = u;
maxWeight = w;
result = Number(n);
result = Number(key);
}

@@ -29,3 +30,3 @@ }

module.exports = function (str) {
module.exports = str => {
if (typeof str !== 'string') {

@@ -36,7 +37,7 @@ throw new TypeError('Expected a string');

// used to see if tabs or spaces are the most used
var tabs = 0;
var spaces = 0;
let tabs = 0;
let spaces = 0;
// remember the size of previous line's indentation
var prev = 0;
let prev = 0;

@@ -52,22 +53,20 @@ // remember how many indents/unindents as occurred for a given size

// }
var indents = {};
const indents = new Map();
// pointer to the array of last used indent
var current;
let current;
// whether the last action was an indent (opposed to an unindent)
var isIndent;
let isIndent;
str.split(/\n/g).forEach(function (line) {
for (const line of str.split(/\n/g)) {
if (!line) {
// ignore empty lines
return;
continue;
}
var indent;
var matches = line.match(INDENT_RE);
let indent;
const matches = line.match(INDENT_RE);
if (!matches) {
indent = 0;
} else {
if (matches) {
indent = matches[0].length;

@@ -80,5 +79,7 @@

}
} else {
indent = 0;
}
var diff = indent - prev;
const diff = indent - prev;
prev = indent;

@@ -91,3 +92,3 @@

current = indents[isIndent ? diff : -diff];
current = indents.get(isIndent ? diff : -diff);

@@ -97,3 +98,4 @@ if (current) {

} else {
current = indents[diff] = [1, 0];
current = [1, 0];
indents.set(diff, current);
}

@@ -104,24 +106,24 @@ } else if (current) {

}
});
}
var amount = getMostUsed(indents);
const amount = getMostUsed(indents);
var type;
var actual;
let type;
let indent;
if (!amount) {
type = null;
actual = '';
indent = '';
} else if (spaces >= tabs) {
type = 'space';
actual = repeating(' ', amount);
indent = ' '.repeat(amount);
} else {
type = 'tab';
actual = repeating('\t', amount);
indent = '\t'.repeat(amount);
}
return {
amount: amount,
type: type,
indent: actual
amount,
type,
indent
};
};
{
"name": "detect-indent",
"version": "4.0.0",
"version": "5.0.0",
"description": "Detect the indentation of code",

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

"engines": {
"node": ">=0.10.0"
"node": ">=4"
},

@@ -35,5 +35,2 @@ "scripts": {

],
"dependencies": {
"repeating": "^2.0.0"
},
"devDependencies": {

@@ -44,6 +41,4 @@ "ava": "*",

"xo": {
"ignores": [
"fixture/**"
]
"esnext": true
}
}

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

```js
var fs = require('fs');
var detectIndent = require('detect-indent');
const fs = require('fs');
const detectIndent = require('detect-indent');

@@ -36,8 +36,8 @@ /*

*/
var file = fs.readFileSync('foo.json', 'utf8');
const file = fs.readFileSync('foo.json', 'utf8');
// tries to detect the indentation and falls back to a default if it can't
var indent = detectIndent(file).indent || ' ';
const indent = detectIndent(file).indent || ' ';
var json = JSON.parse(file);
const json = JSON.parse(file);

@@ -57,7 +57,7 @@ json.ilove = 'unicorns';

Accepts a string and returns an object with stats about the indentation:
Accepts a string and returns an object with stats about the indentation:
* `amount` {number} - Amount of indentation, e.g. `2`
* `type` {string|null} - Type of indentation. Possible values are `tab`, `space` or `null` if no indentation is detected
* `indent` {string} - Actual indentation
* `amount` {number} - Amount of indentation, for example `2`
* `type` {string|null} - Type of indentation. Possible values are `tab`, `space` or `null` if no indentation is detected
* `indent` {string} - Actual indentation

@@ -109,2 +109,3 @@

- [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

@@ -114,2 +115,2 @@

MIT © [Sindre Sorhus](http://sindresorhus.com)
MIT © [Sindre Sorhus](https://sindresorhus.com)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc