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