{"name":"@gwhitney/detect-indent","version":"7.0.2","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","types":"./index.d.ts","sideEffects":false,"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","xo":"^0.44.0"},"xo":{"ignores":["fixture"],"rules":{"unicorn/prefer-module":"off"}},"gitHead":"bdbf0593684e3ac666a7a1bb8485eb69540ba2ba","_id":"@gwhitney/detect-indent@7.0.2","bugs":{"url":"https://github.com/gwhitney/detect-indent/issues"},"homepage":"https://github.com/gwhitney/detect-indent#readme","_nodeVersion":"22.15.1","_npmVersion":"11.6.4","dist":{"integrity":"sha512-8TggvKn5pKu1Yt7BkcLEmxeDU0CmbBCaHVgDqs0RPbbOFIi3pJEX6Qv+H/v6V21D/hvBPcR0b+1SnDbK0xZnEg==","shasum":"00286252010f899b6a30b8f69791e25be52e1f60","tarball":"http://registry-npm.ywwl.com/@gwhitney/detect-indent/download/@gwhitney/detect-indent-7.0.2.tgz","fileCount":5,"unpackedSize":10529,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEQCIEMY/62j/qKHybnEZj/pS2ibyjkoL5wjESnnmzSMDtdfAiBdalcTXpTyb4KVjtBMjmAOVZ2dtDSLCH0uPV8kr7GWyQ=="}],"size":4277,"noattachment":false,"key":"/@gwhitney/detect-indent/-/@gwhitney/detect-indent-7.0.2.tgz"},"_npmUser":{"name":"gwhitney","email":"glen@studioinfinity.org"},"directories":{},"maintainers":[{"name":"gwhitney","email":"glen@studioinfinity.org"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/detect-indent_7.0.2_1765225900990_0.7981507743160015"},"_hasShrinkwrap":false,"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```sh\nnpm install detect-indent\n```\n\n## Usage\n\nHere we modify a JSON file while persisting the indentation:\n\n```js\nconst fs = require('node:fs');\nconst detectIndent = require('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- `type` {'tab' | 'space' | undefined} - The type of indentation. It is `undefined` if no indentation is detected.\n- `amount` {number} - The amount of indentation. For example, `2`.\n- `indent` {string} - The actual indentation.\n\n## Algorithm\n\nThe current algorithm looks for the most common difference between two consecutive non-empty lines. Single-space indentations and changes are ignored by default to prevent common false positives from comment alignment.\n\nIn the following example, even if 4-space indentation appears 3 times while 2-space appears only 2 times, the 2-space indentation is detected because there are 4 indent changes of 2 spaces vs only 2 changes of 4 spaces:\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 multiple indent differences with the same usage count, 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- [detect-indent-py](https://github.com/Ethan-Vanderheijden/detect-indent-py) - Python port\n","publish_time":1765225901189,"_cnpm_publish_time":1765225901189,"dist-tags":{"latest":"7.0.2"}}