Feedback.esm.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import zxcvbnOptions from './Options.esm.js';
  2. import bruteforceMatcher from './matcher/bruteforce/feedback.esm.js';
  3. import dateMatcher from './matcher/date/feedback.esm.js';
  4. import dictionaryMatcher from './matcher/dictionary/feedback.esm.js';
  5. import regexMatcher from './matcher/regex/feedback.esm.js';
  6. import repeatMatcher from './matcher/repeat/feedback.esm.js';
  7. import sequenceMatcher from './matcher/sequence/feedback.esm.js';
  8. import spatialMatcher from './matcher/spatial/feedback.esm.js';
  9. const defaultFeedback = {
  10. warning: '',
  11. suggestions: []
  12. };
  13. /*
  14. * -------------------------------------------------------------------------------
  15. * Generate feedback ---------------------------------------------------------------
  16. * -------------------------------------------------------------------------------
  17. */
  18. class Feedback {
  19. constructor() {
  20. this.matchers = {
  21. bruteforce: bruteforceMatcher,
  22. date: dateMatcher,
  23. dictionary: dictionaryMatcher,
  24. regex: regexMatcher,
  25. repeat: repeatMatcher,
  26. sequence: sequenceMatcher,
  27. spatial: spatialMatcher
  28. };
  29. this.defaultFeedback = {
  30. warning: '',
  31. suggestions: []
  32. };
  33. this.setDefaultSuggestions();
  34. }
  35. setDefaultSuggestions() {
  36. this.defaultFeedback.suggestions.push(zxcvbnOptions.translations.suggestions.useWords, zxcvbnOptions.translations.suggestions.noNeed);
  37. }
  38. getFeedback(score, sequence) {
  39. if (sequence.length === 0) {
  40. return this.defaultFeedback;
  41. }
  42. if (score > 2) {
  43. return defaultFeedback;
  44. }
  45. const extraFeedback = zxcvbnOptions.translations.suggestions.anotherWord;
  46. const longestMatch = this.getLongestMatch(sequence);
  47. let feedback = this.getMatchFeedback(longestMatch, sequence.length === 1);
  48. if (feedback !== null && feedback !== undefined) {
  49. feedback.suggestions.unshift(extraFeedback);
  50. if (feedback.warning == null) {
  51. feedback.warning = '';
  52. }
  53. } else {
  54. feedback = {
  55. warning: '',
  56. suggestions: [extraFeedback]
  57. };
  58. }
  59. return feedback;
  60. }
  61. getLongestMatch(sequence) {
  62. let longestMatch = sequence[0];
  63. const slicedSequence = sequence.slice(1);
  64. slicedSequence.forEach(match => {
  65. if (match.token.length > longestMatch.token.length) {
  66. longestMatch = match;
  67. }
  68. });
  69. return longestMatch;
  70. }
  71. getMatchFeedback(match, isSoleMatch) {
  72. if (this.matchers[match.pattern]) {
  73. return this.matchers[match.pattern](match, isSoleMatch);
  74. }
  75. if (zxcvbnOptions.matchers[match.pattern] && 'feedback' in zxcvbnOptions.matchers[match.pattern]) {
  76. return zxcvbnOptions.matchers[match.pattern].feedback(match, isSoleMatch);
  77. }
  78. return defaultFeedback;
  79. }
  80. }
  81. export { Feedback as default };
  82. //# sourceMappingURL=Feedback.esm.js.map