blob: c63b278c01079f60115dfa3d23276bf052613c74 [file] [log] [blame]
Jack Franklina5fd0a42022-12-09 11:30:241const { EOL } = require('os')
2
3const getFirstRegexpMatchOrDefault = (text, regexp, defaultValue) => {
4 regexp.lastIndex = 0 // https://stackoverflow.com/a/11477448/4536543
5 let match = regexp.exec(text)
Nikolay Vitkov10371982025-02-03 17:08:076 if (match !== null) {
7 return match[1]
8 } else {
9 return defaultValue
10 }
Jack Franklina5fd0a42022-12-09 11:30:2411}
12
13const DEFAULT_INDENT = ' '
14const INDENT_REGEXP = /^([ \t]+)[^\s]/m
15
16module.exports.detectIndent = text =>
17 getFirstRegexpMatchOrDefault(text, INDENT_REGEXP, DEFAULT_INDENT)
18module.exports.DEFAULT_INDENT = DEFAULT_INDENT
19
20const DEFAULT_EOL = EOL
21const EOL_REGEXP = /(\r\n|\n|\r)/g
22
23module.exports.detectEOL = text =>
24 getFirstRegexpMatchOrDefault(text, EOL_REGEXP, DEFAULT_EOL)
25module.exports.DEFAULT_EOL = DEFAULT_EOL