utils.js 689 B

12345678910111213141516171819202122
  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) return match[1]
  6. return defaultValue
  7. }
  8. const DEFAULT_INDENT = ' '
  9. const INDENT_REGEXP = /^([ \t]+)[^\s]/m
  10. module.exports.detectIndent = text =>
  11. getFirstRegexpMatchOrDefault(text, INDENT_REGEXP, DEFAULT_INDENT)
  12. module.exports.DEFAULT_INDENT = DEFAULT_INDENT
  13. const DEFAULT_EOL = EOL
  14. const EOL_REGEXP = /(\r\n|\n|\r)/g
  15. module.exports.detectEOL = text =>
  16. getFirstRegexpMatchOrDefault(text, EOL_REGEXP, DEFAULT_EOL)
  17. module.exports.DEFAULT_EOL = DEFAULT_EOL