get-indentation.js 635 B

1234567891011121314151617181920212223242526272829303132
  1. 'use strict';
  2. module.exports = indentation;
  3. /* Map of characters, and their column length,
  4. * which can be used as indentation. */
  5. var characters = {' ': 1, '\t': 4};
  6. /* Gets indentation information for a line. */
  7. function indentation(value) {
  8. var index = 0;
  9. var indent = 0;
  10. var character = value.charAt(index);
  11. var stops = {};
  12. var size;
  13. while (character in characters) {
  14. size = characters[character];
  15. indent += size;
  16. if (size > 1) {
  17. indent = Math.floor(indent / size) * size;
  18. }
  19. stops[indent] = index;
  20. character = value.charAt(++index);
  21. }
  22. return {indent: indent, stops: stops};
  23. }