{"id":13454579,"url":"https://github.com/sindresorhus/detect-indent","last_synced_at":"2025-05-15T14:07:29.089Z","repository":{"id":9996388,"uuid":"12028961","full_name":"sindresorhus/detect-indent","owner":"sindresorhus","description":"Detect the indentation of code","archived":false,"fork":false,"pushed_at":"2023-10-08T15:00:39.000Z","size":64,"stargazers_count":196,"open_issues_count":2,"forks_count":27,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-05-06T07:26:06.358Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sindresorhus.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":".github/funding.yml","license":"license","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/security.md","support":null,"governance":null},"funding":{"github":"sindresorhus","open_collective":"sindresorhus","custom":"https://sindresorhus.com/donate"}},"created_at":"2013-08-11T00:24:41.000Z","updated_at":"2025-01-07T20:10:48.000Z","dependencies_parsed_at":"2022-08-07T05:15:21.523Z","dependency_job_id":null,"html_url":"https://github.com/sindresorhus/detect-indent","commit_stats":{"total_commits":70,"total_committers":13,"mean_commits":5.384615384615385,"dds":0.2142857142857143,"last_synced_commit":"8271e8a9d886d1281d373cb09b43577f10be83ce"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fdetect-indent","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fdetect-indent/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fdetect-indent/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sindresorhus%2Fdetect-indent/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sindresorhus","download_url":"https://codeload.github.com/sindresorhus/detect-indent/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254262584,"owners_count":22041448,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T08:00:55.577Z","updated_at":"2025-05-15T14:07:29.018Z","avatar_url":"https://github.com/sindresorhus.png","language":"JavaScript","funding_links":["https://github.com/sponsors/sindresorhus","https://opencollective.com/sindresorhus","https://sindresorhus.com/donate","https://tidelift.com/subscription/pkg/npm-detect-indent?utm_source=npm-detect-indent\u0026utm_medium=referral\u0026utm_campaign=readme"],"categories":["Packages","JavaScript","包","Repository","目录","Text"],"sub_categories":["Text","文本","Text/String","文本处理"],"readme":"# detect-indent\n\n\u003e Detect the indentation of code\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- [detect-indent-py](https://github.com/Ethan-Vanderheijden/detect-indent-py) - Python port\n\n---\n\n\u003cdiv align=\"center\"\u003e\n  \u003cb\u003e\n    \u003ca href=\"https://tidelift.com/subscription/pkg/npm-detect-indent?utm_source=npm-detect-indent\u0026utm_medium=referral\u0026utm_campaign=readme\"\u003eGet professional support for this package with a Tidelift subscription\u003c/a\u003e\n  \u003c/b\u003e\n  \u003cbr\u003e\n  \u003csub\u003e\n    Tidelift helps make open source sustainable for maintainers while giving companies\u003cbr\u003eassurances about security, maintenance, and licensing for their dependencies.\n  \u003c/sub\u003e\n\u003c/div\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2Fdetect-indent","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsindresorhus%2Fdetect-indent","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsindresorhus%2Fdetect-indent/lists"}