You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
72 lines
1.9 KiB
72 lines
1.9 KiB
/*global navigator*/ |
|
'use strict'; |
|
|
|
const { |
|
REGEX_BACKSLASH, |
|
REGEX_REMOVE_BACKSLASH, |
|
REGEX_SPECIAL_CHARS, |
|
REGEX_SPECIAL_CHARS_GLOBAL |
|
} = require('./constants'); |
|
|
|
exports.isObject = val => val !== null && typeof val === 'object' && !Array.isArray(val); |
|
exports.hasRegexChars = str => REGEX_SPECIAL_CHARS.test(str); |
|
exports.isRegexChar = str => str.length === 1 && exports.hasRegexChars(str); |
|
exports.escapeRegex = str => str.replace(REGEX_SPECIAL_CHARS_GLOBAL, '\\$1'); |
|
exports.toPosixSlashes = str => str.replace(REGEX_BACKSLASH, '/'); |
|
|
|
exports.isWindows = () => { |
|
if (typeof navigator !== 'undefined' && navigator.platform) { |
|
const platform = navigator.platform.toLowerCase(); |
|
return platform === 'win32' || platform === 'windows'; |
|
} |
|
|
|
if (typeof process !== 'undefined' && process.platform) { |
|
return process.platform === 'win32'; |
|
} |
|
|
|
return false; |
|
}; |
|
|
|
exports.removeBackslashes = str => { |
|
return str.replace(REGEX_REMOVE_BACKSLASH, match => { |
|
return match === '\\' ? '' : match; |
|
}); |
|
}; |
|
|
|
exports.escapeLast = (input, char, lastIdx) => { |
|
const idx = input.lastIndexOf(char, lastIdx); |
|
if (idx === -1) return input; |
|
if (input[idx - 1] === '\\') return exports.escapeLast(input, char, idx - 1); |
|
return `${input.slice(0, idx)}\\${input.slice(idx)}`; |
|
}; |
|
|
|
exports.removePrefix = (input, state = {}) => { |
|
let output = input; |
|
if (output.startsWith('./')) { |
|
output = output.slice(2); |
|
state.prefix = './'; |
|
} |
|
return output; |
|
}; |
|
|
|
exports.wrapOutput = (input, state = {}, options = {}) => { |
|
const prepend = options.contains ? '' : '^'; |
|
const append = options.contains ? '' : '$'; |
|
|
|
let output = `${prepend}(?:${input})${append}`; |
|
if (state.negated === true) { |
|
output = `(?:^(?!${output}).*$)`; |
|
} |
|
return output; |
|
}; |
|
|
|
exports.basename = (path, { windows } = {}) => { |
|
const segs = path.split(windows ? /[\\/]/ : '/'); |
|
const last = segs[segs.length - 1]; |
|
|
|
if (last === '') { |
|
return segs[segs.length - 2]; |
|
} |
|
|
|
return last; |
|
};
|
|
|