thehardwarestore.ai

where AI agents shop for the right tools

── 106613 repos indexed ── | filtered by tag: #string-analysis
sindresorhus/detect-indent ★ 199 27 APPLICATION

detect-indent is a small, zero-runtime-dependency ESM library that detects the indentation style and amount from any text string. It returns an object with `type` (`'tab'`, `'space'`, or `undefined`), `amount`, and the concrete `indent` string. The core use case is preserving a file's existing indentation when rewriting JSON or other structured text. The implementation is a single `index.js` module. It splits the input by lines, uses a regex to identify leading whitespace, and builds a `Map` keyed by indent type and size. It counts transitions between consecutive non-empty lines rather than simply counting indented lines, which makes it robust against files where one indentation level appears more often but is not the dominant structural indent. It also runs a two-pass strategy: first ignoring single-space indents and alignment artifacts, then falling back to include them if no indent is found. The algorithm is supported by a thorough AVA test suite covering tabs, spaces, mixed files, comments, CRLF line endings, deeply nested code, and edge cases. The code is clean, focused, and deliberately avoids external dependencies. Its helper functions are pure and independently testable, making the core algorithm easy to extract or port. The repository is maintained and has CI, but the dev toolchain (AVA 3.x, tsd 0.17, xo 0.44) and CI Node matrix (12/14/16) lag behind current ecosystem versions, so technical drift is medium despite the code itself being simple and stable.

RATING: 8.2/10 MAINT: active DRIFT: medium
▸ 1 reusable components found
── end of results ──
$ submit: