1 | 'use strict';
|
---|
2 |
|
---|
3 | const path = require('path');
|
---|
4 | const win32 = process.platform === 'win32';
|
---|
5 | const {
|
---|
6 | REGEX_BACKSLASH,
|
---|
7 | REGEX_REMOVE_BACKSLASH,
|
---|
8 | REGEX_SPECIAL_CHARS,
|
---|
9 | REGEX_SPECIAL_CHARS_GLOBAL
|
---|
10 | } = require('./constants');
|
---|
11 |
|
---|
12 | exports.isObject = val => val !== null && typeof val === 'object' && !Array.isArray(val);
|
---|
13 | exports.hasRegexChars = str => REGEX_SPECIAL_CHARS.test(str);
|
---|
14 | exports.isRegexChar = str => str.length === 1 && exports.hasRegexChars(str);
|
---|
15 | exports.escapeRegex = str => str.replace(REGEX_SPECIAL_CHARS_GLOBAL, '\\$1');
|
---|
16 | exports.toPosixSlashes = str => str.replace(REGEX_BACKSLASH, '/');
|
---|
17 |
|
---|
18 | exports.removeBackslashes = str => {
|
---|
19 | return str.replace(REGEX_REMOVE_BACKSLASH, match => {
|
---|
20 | return match === '\\' ? '' : match;
|
---|
21 | });
|
---|
22 | };
|
---|
23 |
|
---|
24 | exports.supportsLookbehinds = () => {
|
---|
25 | const segs = process.version.slice(1).split('.').map(Number);
|
---|
26 | if (segs.length === 3 && segs[0] >= 9 || (segs[0] === 8 && segs[1] >= 10)) {
|
---|
27 | return true;
|
---|
28 | }
|
---|
29 | return false;
|
---|
30 | };
|
---|
31 |
|
---|
32 | exports.isWindows = options => {
|
---|
33 | if (options && typeof options.windows === 'boolean') {
|
---|
34 | return options.windows;
|
---|
35 | }
|
---|
36 | return win32 === true || path.sep === '\\';
|
---|
37 | };
|
---|
38 |
|
---|
39 | exports.escapeLast = (input, char, lastIdx) => {
|
---|
40 | const idx = input.lastIndexOf(char, lastIdx);
|
---|
41 | if (idx === -1) return input;
|
---|
42 | if (input[idx - 1] === '\\') return exports.escapeLast(input, char, idx - 1);
|
---|
43 | return `${input.slice(0, idx)}\\${input.slice(idx)}`;
|
---|
44 | };
|
---|
45 |
|
---|
46 | exports.removePrefix = (input, state = {}) => {
|
---|
47 | let output = input;
|
---|
48 | if (output.startsWith('./')) {
|
---|
49 | output = output.slice(2);
|
---|
50 | state.prefix = './';
|
---|
51 | }
|
---|
52 | return output;
|
---|
53 | };
|
---|
54 |
|
---|
55 | exports.wrapOutput = (input, state = {}, options = {}) => {
|
---|
56 | const prepend = options.contains ? '' : '^';
|
---|
57 | const append = options.contains ? '' : '$';
|
---|
58 |
|
---|
59 | let output = `${prepend}(?:${input})${append}`;
|
---|
60 | if (state.negated === true) {
|
---|
61 | output = `(?:^(?!${output}).*$)`;
|
---|
62 | }
|
---|
63 | return output;
|
---|
64 | };
|
---|