import { ESCAPE_REGEX, SEPARATOR_CHARS } from '../../data/const' import { MatcherBaseClass, MatchOptions, SeparatorMatch } from '../../types' type SeparatorMatchOptions = Pick // a set instead of a regex character class, so no separator ('-', '\\', ']', '^') // can turn into a range or an escape depending on its position in the list const separatorChars = new Set(SEPARATOR_CHARS) /* *------------------------------------------------------------------------------- * separators (any semi-repeated special character) ----------------------------- *------------------------------------------------------------------------------- */ class MatchSeparator extends MatcherBaseClass { static getMostUsedSeparatorChar(password: string): string | undefined { const counts = new Map() for (const char of password) { if (separatorChars.has(char)) { counts.set(char, (counts.get(char) || 0) + 1) } } let maxChar: string | undefined let maxCount = 0 for (const [char, count] of counts.entries()) { if (count > maxCount) { maxCount = count maxChar = char } } // If the special character is only used once, don't treat it like a separator if (maxCount < 2) { return undefined } return maxChar } static getSeparatorRegex(separator: string): RegExp { const escaped = separator.replace(ESCAPE_REGEX, '\\$&') return new RegExp(`([^${escaped}\n])(${escaped})(?!${escaped})`, 'g') // negative lookbehind can be added again in a few years when it is more supported by the browsers (currently 2023) // https://github.com/zxcvbn-ts/zxcvbn/issues/202 // return new RegExp(`(?