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