Matching.js 1.8 KB

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