Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame^] | 1 | 'use strict'; |
| 2 | |
| 3 | const path = require('path'); |
| 4 | const win32 = process.platform === 'win32'; |
| 5 | const { |
| 6 | REGEX_SPECIAL_CHARS, |
| 7 | REGEX_SPECIAL_CHARS_GLOBAL, |
| 8 | REGEX_REMOVE_BACKSLASH |
| 9 | } = require('./constants'); |
| 10 | |
| 11 | exports.isObject = val => val !== null && typeof val === 'object' && !Array.isArray(val); |
| 12 | exports.hasRegexChars = str => REGEX_SPECIAL_CHARS.test(str); |
| 13 | exports.isRegexChar = str => str.length === 1 && exports.hasRegexChars(str); |
| 14 | exports.escapeRegex = str => str.replace(REGEX_SPECIAL_CHARS_GLOBAL, '\\$1'); |
| 15 | exports.toPosixSlashes = str => str.replace(/\\/g, '/'); |
| 16 | |
| 17 | exports.removeBackslashes = str => { |
| 18 | return str.replace(REGEX_REMOVE_BACKSLASH, match => { |
| 19 | return match === '\\' ? '' : match; |
| 20 | }); |
| 21 | } |
| 22 | |
| 23 | exports.supportsLookbehinds = () => { |
| 24 | let segs = process.version.slice(1).split('.'); |
| 25 | if (segs.length === 3 && +segs[0] >= 9 || (+segs[0] === 8 && +segs[1] >= 10)) { |
| 26 | return true; |
| 27 | } |
| 28 | return false; |
| 29 | }; |
| 30 | |
| 31 | exports.isWindows = options => { |
| 32 | if (options && typeof options.windows === 'boolean') { |
| 33 | return options.windows; |
| 34 | } |
| 35 | return win32 === true || path.sep === '\\'; |
| 36 | }; |
| 37 | |
| 38 | exports.escapeLast = (input, char, lastIdx) => { |
| 39 | let idx = input.lastIndexOf(char, lastIdx); |
| 40 | if (idx === -1) return input; |
| 41 | if (input[idx - 1] === '\\') return exports.escapeLast(input, char, idx - 1); |
| 42 | return input.slice(0, idx) + '\\' + input.slice(idx); |
| 43 | }; |