$ show sindresorhus/detect-indent
This is a small, focused npm library that detects the indentation style (spaces vs tabs) and amount from a string of code. The core algorithm analyzes consecutive non-empty lines, counts indent changes, and uses a heuristic to ignore single-space indents initially to avoid false positives from alignment. It returns the detected type, amount, and the actual indent string. The implementation is clean, well-tested, and has zero runtime dependencies. It is a classic example of a single-purpose utility library with careful edge-case handling (e.g., mixed indentation, vendor-prefixed CSS, comments, CRLF line endings). The code is self-contained and could be easily extracted or reused in other projects.
The LLM identified 1 files worth highlighting for reuse:
The entire library is contained in this single file. It exports a default function `detectIndent` that implements a robust heuristic for detecting indentation. The algorithm uses a regex to capture leading whitespace, builds a map of indent differences (encoded as type+amount keys), and selects the most common indent by count, breaking ties by weight. It intelligently ignores single-space indents on first pass to avoid false positives from alignment, then falls back to including them if no indent is detected. The helper functions (`makeIndentsMap`, `getMostUsedKey`, `encodeIndentsKey`, `decodeIndentsKey`) are well-isolated and could be reused independently. The code is clean, well-commented, and demonstrates non-obvious skill in handling edge cases like mixed indentation, comments, and alignment.