{"_id":"@gwhitney/detect-indent","_rev":"3287584","name":"@gwhitney/detect-indent","description":"Detect the indentation of code (commonjs fork)","dist-tags":{"latest":"7.0.1"},"maintainers":[{"name":"gwhitney","email":""}],"time":{"modified":"2024-05-23T06:35:42.000Z","created":"2022-11-22T14:07:22.341Z","7.0.1":"2022-11-22T14:07:22.341Z"},"users":{},"author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"},"repository":{"type":"git","url":"git+https://github.com/gwhitney/detect-indent.git"},"versions":{"7.0.1":{"name":"@gwhitney/detect-indent","version":"7.0.1","description":"Detect the indentation of code (commonjs fork)","license":"MIT","repository":{"type":"git","url":"git+https://github.com/gwhitney/detect-indent.git"},"author":{"name":"Sindre Sorhus","email":"sindresorhus@gmail.com","url":"https://sindresorhus.com"},"exports":"./index.js","engines":{"node":">=12.20"},"scripts":{"test":"xo && ava && tsd"},"keywords":["indent","indentation","detect","infer","identify","code","string","text","source","space","tab"],"devDependencies":{"ava":"^3.15.0","tsd":"^0.17.0","typescript":"^4.3.5","xo":"^0.44.0"},"xo":{"ignores":["fixture"],"rules":{"unicorn/prefer-module":"off"}},"types":"./index.d.ts","gitHead":"9a9832f0c96f8d3c70f000c11d9da5a8ef15c1bf","bugs":{"url":"https://github.com/gwhitney/detect-indent/issues"},"homepage":"https://github.com/gwhitney/detect-indent#readme","_id":"@gwhitney/detect-indent@7.0.1","_nodeVersion":"19.1.0","_npmVersion":"8.19.3","dist":{"shasum":"db16d7fe6d13b26dc792442e5156677b44cc428e","size":4346,"noattachment":false,"key":"/@gwhitney/detect-indent/-/@gwhitney/detect-indent-7.0.1.tgz","tarball":"http://registry.cnpm.dingdandao.com/@gwhitney/detect-indent/download/@gwhitney/detect-indent-7.0.1.tgz"},"_npmUser":{"name":"gwhitney","email":"glen@studioinfinity.org"},"directories":{},"maintainers":[{"name":"gwhitney","email":""}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/detect-indent_7.0.1_1669126042187_0.8578880263789046"},"_hasShrinkwrap":false,"_cnpmcore_publish_time":"2022-11-22T14:36:45.804Z","publish_time":1669126042341,"_cnpm_publish_time":1669126042341}},"readme":"# detect-indent\n\n> Detect the indentation of code (commonjs fork)\n\n> **NOTE:** This is a fork of sindresorhus/detect-indent modified solely\n> to revert to providing CommonJS exports. It was created only to provide\n> an upgrade path for detect-indent in the `pnpm` Node package manager.\n> It is intended to be kept in sync with the upstream `detect-indent` and\n> use identical version numbers. The creator of this fork neither endorses\n> nor criticizes the convention decisions on the part of the maintainers/\n> authors of `pnpm` and `detect-indent` that led to the necessity of this\n> fork.\n\nPass in a string of any kind of text and get the indentation.\n\n## Use cases\n\n- Persisting the indentation when modifying a file.\n- Have new content match the existing indentation.\n- Setting the right indentation in your editor.\n\n## Install\n\n```\n$ npm install detect-indent\n```\n\n## Usage\n\nHere we modify a JSON file while persisting the indentation:\n\n```js\nimport fs from 'node:fs';\nimport detectIndent from 'detect-indent';\n\n/*\n{\n    \"ilove\": \"pizza\"\n}\n*/\nconst file = fs.readFileSync('foo.json', 'utf8');\n\n// Tries to detect the indentation and falls back to a default if it can't\nconst indent = detectIndent(file).indent || '    ';\n\nconst json = JSON.parse(file);\n\njson.ilove = 'unicorns';\n\nfs.writeFileSync('foo.json', JSON.stringify(json, undefined, indent));\n/*\n{\n    \"ilove\": \"unicorns\"\n}\n*/\n```\n\n## API\n\nAccepts a string and returns an object with stats about the indentation:\n\n* `amount` {number} - Amount of indentation, for example `2`\n* `type` {'tab' | 'space' | undefined} - Type of indentation. Possible values are `'tab'`, `'space'` or `undefined` if no indentation is detected\n* `indent` {string} - Actual indentation\n\n## Algorithm\n\nThe current algorithm looks for the most common difference between two consecutive non-empty lines.\n\nIn 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:\n\n```css\nhtml {\n  box-sizing: border-box;\n}\n\nbody {\n  background: gray;\n}\n\np {\n    line-height: 1.3em;\n    margin-top: 1em;\n    text-indent: 2em;\n}\n```\n\n[Source.](https://medium.com/@heatherarthur/detecting-code-indentation-eff3ed0fb56b#3918)\n\nFurthermore, if there are more than one most used difference, the indentation with the most lines is selected.\n\nIn the following example, the indentation is detected as 4-spaces:\n\n```css\nbody {\n  background: gray;\n}\n\np {\n    line-height: 1.3em;\n    margin-top: 1em;\n    text-indent: 2em;\n}\n```\n\n## Related\n\n- [detect-indent-cli](https://github.com/sindresorhus/detect-indent-cli) - CLI for this module\n- [detect-newline](https://github.com/sindresorhus/detect-newline) - Detect the dominant newline character of a string\n- [detect-indent-rs](https://github.com/stefanpenner/detect-indent-rs) - Rust port\n\n---\n\n<div align=\"center\">\n  <b>\n    <a href=\"https://tidelift.com/subscription/pkg/npm-detect-indent?utm_source=npm-detect-indent&utm_medium=referral&utm_campaign=readme\">Get professional support for this package with a Tidelift subscription</a>\n  </b>\n  <br>\n  <sub>\n    Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.\n  </sub>\n</div>\n","_attachments":{},"homepage":"https://github.com/gwhitney/detect-indent#readme","bugs":{"url":"https://github.com/gwhitney/detect-indent/issues"},"license":"MIT"}