utils.js 712 B

12345678910111213141516171819202122232425
  1. const { EOL } = require('os')
  2. const getFirstRegexpMatchOrDefault = (text, regexp, defaultValue) => {
  3. regexp.lastIndex = 0 // https://stackoverflow.com/a/11477448/4536543
  4. let match = regexp.exec(text)
  5. if (match !== null) {
  6. return match[1]
  7. } else {
  8. return defaultValue
  9. }
  10. }
  11. const DEFAULT_INDENT = ' '
  12. const INDENT_REGEXP = /^([ \t]+)[^\s]/m
  13. module.exports.detectIndent = text =>
  14. getFirstRegexpMatchOrDefault(text, INDENT_REGEXP, DEFAULT_INDENT)
  15. module.exports.DEFAULT_INDENT = DEFAULT_INDENT
  16. const DEFAULT_EOL = EOL
  17. const EOL_REGEXP = /(\r\n|\n|\r)/g
  18. module.exports.detectEOL = text =>
  19. getFirstRegexpMatchOrDefault(text, EOL_REGEXP, DEFAULT_EOL)
  20. module.exports.DEFAULT_EOL = DEFAULT_EOL