import bruteforceMatcher from './matcher/bruteforce/feedback.mjs'; import dateMatcher from './matcher/date/feedback.mjs'; import dictionaryMatcher from './matcher/dictionary/feedback.mjs'; import regexMatcher from './matcher/regex/feedback.mjs'; import repeatMatcher from './matcher/repeat/feedback.mjs'; import sequenceMatcher from './matcher/sequence/feedback.mjs'; import spatialMatcher from './matcher/spatial/feedback.mjs'; import separatorMatcher from './matcher/separator/feedback.mjs'; import wordSequenceMatcher from './matcher/wordSequence/feedback.mjs'; const defaultFeedback = { warning: null, suggestions: [] }; /* * ------------------------------------------------------------------------------- * Generate feedback --------------------------------------------------------------- * ------------------------------------------------------------------------------- */ class Feedback { constructor(options) { this.options = options; this.matchers = {}; this.defaultFeedback = { warning: null, suggestions: [] }; this.setDefaultSuggestions(); this.matchers = { bruteforce: bruteforceMatcher, date: dateMatcher, dictionary: dictionaryMatcher, regex: regexMatcher, repeat: repeatMatcher, sequence: sequenceMatcher, spatial: spatialMatcher, separator: separatorMatcher, wordSequence: wordSequenceMatcher }; Object.entries(this.options.matchers).forEach(([key, matcher]) => { if (matcher.feedback) { this.matchers[key] = matcher.feedback; } }); } setDefaultSuggestions() { this.defaultFeedback.suggestions.push(this.options.translations.suggestions.useWords, this.options.translations.suggestions.noNeed); } getFeedback(score, sequence) { if (sequence.length === 0) { return this.defaultFeedback; } if (score > 2) { return defaultFeedback; } const extraFeedback = this.options.translations.suggestions.anotherWord; const longestMatch = this.getLongestMatch(sequence); let feedback = this.getMatchFeedback(longestMatch, sequence.length === 1); if (feedback !== null && feedback !== undefined) { feedback.suggestions.unshift(extraFeedback); } else { feedback = { warning: null, suggestions: [extraFeedback] }; } return feedback; } getLongestMatch(sequence) { let longestMatch = sequence[0]; // ignore first entry for (let i = 1; i < sequence.length; i += 1) { const match = sequence[i]; if (match.token.length > longestMatch.token.length) { longestMatch = match; } } return longestMatch; } getMatchFeedback(match, isSoleMatch) { if (this.matchers[match.pattern]) { return this.matchers[match.pattern](this.options, match, isSoleMatch); } return defaultFeedback; } } export { Feedback as default }; //# sourceMappingURL=Feedback.mjs.map