7.0.1 • Published 2 years ago

detect-indent v7.0.1

Weekly downloads
8,001,763
License
MIT
Repository
github
Last release
2 years ago

detect-indent

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 detect-indent

Usage

Here we modify a JSON file while persisting the indentation:

import fs from 'node:fs';
import detectIndent from 'detect-indent';

/*
{
    "ilove": "pizza"
}
*/
const file = fs.readFileSync('foo.json', 'utf8');

// Tries to detect the indentation and falls back to a default if it can't
const indent = detectIndent(file).indent || '    ';

const json = JSON.parse(file);

json.ilove = 'unicorns';

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

API

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

  • amount {number} - Amount of indentation, for example 2
  • type {'tab' | 'space' | undefined} - Type of indentation. Possible values are 'tab', 'space' or undefined if no indentation is detected
  • indent {string} - Actual indentation

Algorithm

The current algorithm looks for the most common difference between two consecutive non-empty lines.

In the following example, even if the 4-space indentation is used 3 times whereas the 2-space one is used 2 times, it is detected as less used because there were only 2 differences with this value instead of 4 for the 2-space indentation:

html {
  box-sizing: border-box;
}

body {
  background: gray;
}

p {
    line-height: 1.3em;
    margin-top: 1em;
    text-indent: 2em;
}

Source.

Furthermore, if there are more than one most used difference, the indentation with the most lines is selected.

In the following example, the indentation is detected as 4-spaces:

body {
  background: gray;
}

p {
    line-height: 1.3em;
    margin-top: 1em;
    text-indent: 2em;
}

Related


babel-generatorwrite-json-file@joker777666/decaffeinatearchetype-librarybit-bin@graphql-codegen/clicordova-lib@teambit/legacykilli8n-react-native-fast-image@yannick243/rw-json-file@arisageha/react-lazyload@arisageha/react-lazyload-fixpicky-gherkin@cashremit/cr-streamline-icons@frxf/frxf@phil8795/pecuniarius-api@steven-torres/jsxrtest-multi-semantic-release@creditiq/set-project-referencesalex-frontend-sdk-stagingcthpb-plugin-socialgenerator-webpack-config@meicloud/clicogoportutilsuinz-notification@prodam/prodam-typesrenovate@everything-registry/sub-chunk-1471@explodingcamera/slate-edit-codeydw-babeldemo@mediakular/svelte-data-gridfahad-redux-axios-midlewarefastyarnfhir2files-explorefirebase-sign-in-with-ethereumfinboxio-yarnfixed_form_builderflow-shut-upfixjsonfixpackfixpectflint-babel-coreformat-package-jsonfreemambafrdrk-js-semaphorefscrfscriptsfyarnforminput-ui-libfork-versionfreshdepsfte.js-parserftl-renovateenveloperepns-frontend-sdk-stagingeslint-create-cliexpo-version-clievanutilsexclamo-versioninghuman-config-mergehubot-budahtml-versionip-designinline-criticaljamuskalimionic-5-versionjameslinenode.js1dt-appdts-bundledts-bundle-hot-fixeasy-web-app-buildereasyplayer-myebit-bineditorconfig-toolsel@empirica/meteor-deploy@emedvedev/renovategrid-component-react-ex-1gulp-flatgrunt-indentatorgrunt-version-bumpgrunt-ts@eliteswap/v2-coregraphql-code-generatorgraphql-codegen-cli-nuxt@epnsproject/frontend-sdk@epnsproject/frontend-sdk-staging@elsouza1985/react-org-chartgrunt-assetser@enirisdev/angular-google-chartsgulp-json-editgulp-json-editorgulp-json-fsmapgulp-json-extendergulp-codenamegrunt-simple-bumpgulp-progressive-cssgh-monoproject-cligogency
7.0.1

2 years ago

7.0.0

3 years ago

6.1.0

3 years ago

6.0.0

5 years ago

5.0.0

8 years ago

4.0.0

9 years ago

3.0.1

9 years ago

3.0.0

9 years ago

2.0.0

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago

0.2.0

10 years ago

0.1.4

10 years ago

0.1.3

11 years ago

0.1.2

11 years ago

0.1.1

11 years ago

0.1.0

11 years ago