UNPKG

@orchidjs/unicode-variants

Version:
66 lines 1.88 kB
/** * Convert array of strings to a regular expression * ex ['ab','a'] => (?:ab|a) * ex ['a','b'] => [ab] */ export const arrayToPattern = (chars) => { chars = chars.filter(Boolean); if (chars.length < 2) { return chars[0] || ''; } return (maxValueLength(chars) == 1) ? '[' + chars.join('') + ']' : '(?:' + chars.join('|') + ')'; }; export const sequencePattern = (array) => { if (!hasDuplicates(array)) { return array.join(''); } let pattern = ''; let prev_char_count = 0; const prev_pattern = () => { if (prev_char_count > 1) { pattern += '{' + prev_char_count + '}'; } }; array.forEach((char, i) => { if (char === array[i - 1]) { prev_char_count++; return; } prev_pattern(); pattern += char; prev_char_count = 1; }); prev_pattern(); return pattern; }; /** * Convert array of strings to a regular expression * ex ['ab','a'] => (?:ab|a) * ex ['a','b'] => [ab] */ export const setToPattern = (chars) => { let array = Array.from(chars); return arrayToPattern(array); }; /** * https://stackoverflow.com/questions/7376598/in-javascript-how-do-i-check-if-an-array-has-duplicate-values */ export const hasDuplicates = (array) => { return (new Set(array)).size !== array.length; }; /** * https://stackoverflow.com/questions/63006601/why-does-u-throw-an-invalid-escape-error */ export const escape_regex = (str) => { return (str + '').replace(/([\$\(\)\*\+\.\?\[\]\^\{\|\}\\])/gu, '\\$1'); }; /** * Return the max length of array values */ export const maxValueLength = (array) => { return array.reduce((longest, value) => Math.max(longest, unicodeLength(value)), 0); }; export const unicodeLength = (str) => { return Array.from(str).length; }; //# sourceMappingURL=regex.js.map