Socket
Socket
Sign inDemoInstall

detect-indent

Package Overview
Dependencies
3
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

detect-indent


Version published
Weekly downloads
13M
decreased by-5.94%
Maintainers
1
Install size
61.3 kB
Created
Weekly downloads
 

Package description

What is detect-indent?

The detect-indent npm package is designed to analyze a given piece of text and determine the indentation style used. This can be particularly useful in scenarios where maintaining consistent code formatting is crucial, such as in collaborative coding projects. It can detect both spaces and tabs used for indentation and provide information about the most used indentation type and size.

What are detect-indent's main functionalities?

Detecting indentation from a string

This feature allows you to pass a string of text to the `detectIndent` function, which analyzes the indentation used in the text. It returns an object containing the amount of indentation, the type of indentation (spaces or tabs), and the actual indentation string.

const detectIndent = require('detect-indent');
const code = '  const x = 1;\n  const y = 2;';
const indent = detectIndent(code);
console.log(indent); // {amount: 2, type: 'space', indent: '  '}

Other packages similar to detect-indent

Readme

Source

detect-indent Build Status

Detect the indentation of code

Pass in a string of any kind of text and get the indentation.

Use cases

  • Persisting the indentation when modifying a file.
  • Have new content match the existing indentation.
  • Setting the right indentation in your editor.

Install

$ npm install --save detect-indent

Usage

// modify a JSON file while persisting the indentation in Node
var fs = require('fs');
var detectIndent = require('detect-indent');
/*
{
    "ilove": "pizza"
}
*/
var 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 || '    ';
var json = JSON.parse(file);

json.ilove = 'unicorns';

fs.writeFileSync('foo.json', JSON.stringify(json, null, indent));
/*
{
    "ilove": "unicorns"
}
*/

API

Accepts a string and returns an object with stats about the indentation:

  • amount: {Number} the amount of indentation, e.g. 2
  • type: {String|Null} the type of indentation. Possible values are tab, space or null if no indentation is detected
  • indent: {String} the actual indentation

CLI

$ npm install --global detect-indent
$ detect-indent --help

  Usage
    detect-indent <file>
    echo <string> | detect-indent

  Example
    echo '  foo\n  bar' | detect-indent | wc --chars
    2

Algorithm

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.

License

MIT © Sindre Sorhus

Keywords

FAQs

Last updated on 03 Oct 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc