3bcf38d04fec218b9bf9ab5359c6552f7418787d1e18b90dbf60bf9523882f1ce53f7e56aec0be91518fb9b92915e24c75aff5d89462070ac8331b53291c18 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # detect-indent [![Build Status](https://travis-ci.org/sindresorhus/detect-indent.svg?branch=master)](https://travis-ci.org/sindresorhus/detect-indent)
  2. > Detect the indentation of code
  3. Pass in a string of any kind of text and get the indentation.
  4. ## Use cases
  5. - Persisting the indentation when modifying a file.
  6. - Have new content match the existing indentation.
  7. - Setting the right indentation in your editor.
  8. ## Install
  9. ```
  10. $ npm install --save detect-indent
  11. ```
  12. ## Usage
  13. Here we modify a JSON file while persisting the indentation:
  14. ```js
  15. var fs = require('fs');
  16. var detectIndent = require('detect-indent');
  17. /*
  18. {
  19. "ilove": "pizza"
  20. }
  21. */
  22. var file = fs.readFileSync('foo.json', 'utf8');
  23. // tries to detect the indentation and falls back to a default if it can't
  24. var indent = detectIndent(file).indent || ' ';
  25. var json = JSON.parse(file);
  26. json.ilove = 'unicorns';
  27. fs.writeFileSync('foo.json', JSON.stringify(json, null, indent));
  28. /*
  29. {
  30. "ilove": "unicorns"
  31. }
  32. */
  33. ```
  34. ## API
  35. Accepts a string and returns an object with stats about the indentation:
  36. * `amount` {number} - Amount of indentation, e.g. `2`
  37. * `type` {string|null} - Type of indentation. Possible values are `tab`, `space` or `null` if no indentation is detected
  38. * `indent` {string} - Actual indentation
  39. ## Algorithm
  40. The current algorithm looks for the most common difference between two consecutive non-empty lines.
  41. In 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:
  42. ```css
  43. html {
  44. box-sizing: border-box;
  45. }
  46. body {
  47. background: gray;
  48. }
  49. p {
  50. line-height: 1.3em;
  51. margin-top: 1em;
  52. text-indent: 2em;
  53. }
  54. ```
  55. [Source.](https://medium.com/@heatherarthur/detecting-code-indentation-eff3ed0fb56b#3918)
  56. Furthermore, if there are more than one most used difference, the indentation with the most lines is selected.
  57. In the following example, the indentation is detected as 4-spaces:
  58. ```css
  59. body {
  60. background: gray;
  61. }
  62. p {
  63. line-height: 1.3em;
  64. margin-top: 1em;
  65. text-indent: 2em;
  66. }
  67. ```
  68. ## Related
  69. - [detect-indent-cli](https://github.com/sindresorhus/detect-indent-cli) - CLI for this module
  70. ## License
  71. MIT © [Sindre Sorhus](http://sindresorhus.com)