Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Detect the indentation of code

branch: master
Octocat-spinner-32 test Return `null` if there are no indentation September 26, 2013
Octocat-spinner-32 .editorconfig init August 11, 2013
Octocat-spinner-32 .gitattributes init August 11, 2013
Octocat-spinner-32 .gitignore init August 11, 2013
Octocat-spinner-32 .jshintrc init August 11, 2013
Octocat-spinner-32 .travis.yml init August 11, 2013
Octocat-spinner-32 bower.json Add keywords to bower.json December 14, 2013
Octocat-spinner-32 component.json 0.1.3 September 26, 2013
Octocat-spinner-32 detect-indent.js Add info banner December 14, 2013
Octocat-spinner-32 package.json Add keywords to bower.json December 14, 2013
Octocat-spinner-32 readme.md Update readme.md April 03, 2014
readme.md

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

Download manually or with a package-manager.

$ npm install --save detect-indent
$ bower install --save detect-indent
$ component install sindresorhus/detect-indent

API

Accepts a string and returns the indentation or null if it can't be detected.

Usage

Modify a JSON file while persisting the indentation in Node.js.

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) || '    ';
var json = JSON.parse(file);

json.ilove = 'unicorns';

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

License

MIT © Sindre Sorhus

Something went wrong with that request. Please try again.