Matching.esm.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { extend, sorted } from './helper.esm.js';
  2. import MatchDate from './matcher/date/matching.esm.js';
  3. import MatchDictionary from './matcher/dictionary/matching.esm.js';
  4. import MatchRegex from './matcher/regex/matching.esm.js';
  5. import MatchRepeat from './matcher/repeat/matching.esm.js';
  6. import MatchSequence from './matcher/sequence/matching.esm.js';
  7. import MatchSpatial from './matcher/spatial/matching.esm.js';
  8. import zxcvbnOptions from './Options.esm.js';
  9. class Matching {
  10. constructor() {
  11. this.matchers = {
  12. date: MatchDate,
  13. dictionary: MatchDictionary,
  14. regex: MatchRegex,
  15. // @ts-ignore => TODO resolve this type issue. This is because it is possible to be async
  16. repeat: MatchRepeat,
  17. sequence: MatchSequence,
  18. spatial: MatchSpatial
  19. };
  20. }
  21. match(password) {
  22. const matches = [];
  23. const promises = [];
  24. const matchers = [...Object.keys(this.matchers), ...Object.keys(zxcvbnOptions.matchers)];
  25. matchers.forEach(key => {
  26. if (!this.matchers[key] && !zxcvbnOptions.matchers[key]) {
  27. return;
  28. }
  29. const Matcher = this.matchers[key] ? this.matchers[key] : zxcvbnOptions.matchers[key].Matching;
  30. const usedMatcher = new Matcher();
  31. const result = usedMatcher.match({
  32. password,
  33. omniMatch: this
  34. });
  35. if (result instanceof Promise) {
  36. result.then(response => {
  37. extend(matches, response);
  38. });
  39. promises.push(result);
  40. } else {
  41. extend(matches, result);
  42. }
  43. });
  44. if (promises.length > 0) {
  45. return new Promise(resolve => {
  46. Promise.all(promises).then(() => {
  47. resolve(sorted(matches));
  48. });
  49. });
  50. }
  51. return sorted(matches);
  52. }
  53. }
  54. export { Matching as default };
  55. //# sourceMappingURL=Matching.esm.js.map