Detects the indentation type and amount from a string of code to maintain consistent formatting.
detect-indent is a Node.js library that analyzes a string of code to detect its indentation pattern, including type (spaces or tabs) and amount. It solves the problem of maintaining consistent formatting when programmatically editing files, such as JSON or source code, by ensuring new content matches the existing indentation style.
Developers working on tools that modify code or configuration files programmatically, such as build scripts, code generators, or editor plugins.
It provides a reliable, algorithm-driven solution for indentation detection that handles edge cases better than simple regex, helping avoid formatting inconsistencies in automated file modifications.
Detect the indentation of code
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Uses line difference analysis to ignore false positives from comment alignment or single-space changes, as shown in the CSS examples where it correctly identifies 2-space over 4-space based on change frequency.
Returns a default indentation string when detection fails, preventing crashes in automated scripts, as demonstrated in the JSON modification example where it falls back to four spaces.
Outputs an object with type, amount, and indent properties, making it easy to integrate into file manipulation code without complex parsing, as outlined in the API section.
Prioritizes reliability over simplicity, with algorithms that account for real-world code patterns, such as selecting the indentation with the most lines when usage counts are tied.
Only detects indentation and doesn't address other formatting aspects like newlines or brace placement, requiring additional tools for full formatting consistency, as hinted by the related projects list.
The README lacks details on speed or resource usage, which could be a concern for large files or high-throughput applications where indentation detection needs to be optimized.
For straightforward cases like uniformly indented files, the statistical approach might be more complex than a simple regex, adding unnecessary overhead for basic use cases.