get-indentation.js 666 B

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict'
  2. module.exports = indentation
  3. var tab = '\t'
  4. var space = ' '
  5. var spaceSize = 1
  6. var tabSize = 4
  7. // Gets indentation information for a line.
  8. function indentation(value) {
  9. var index = 0
  10. var indent = 0
  11. var character = value.charAt(index)
  12. var stops = {}
  13. var size
  14. var lastIndent = 0
  15. while (character === tab || character === space) {
  16. size = character === tab ? tabSize : spaceSize
  17. indent += size
  18. if (size > 1) {
  19. indent = Math.floor(indent / size) * size
  20. }
  21. while (lastIndent < indent) {
  22. stops[++lastIndent] = index
  23. }
  24. character = value.charAt(++index)
  25. }
  26. return {indent: indent, stops: stops}
  27. }