index.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /**
  2. * The MIT License (MIT)
  3. * Copyright (c) 2017-present Dmitry Soshnikov <dmitry.soshnikov@gmail.com>
  4. */
  5. 'use strict';
  6. var NFA = require('./nfa/nfa');
  7. var DFA = require('./dfa/dfa');
  8. var nfaFromRegExp = require('./nfa/nfa-from-regexp');
  9. var builders = require('./nfa/builders');
  10. module.exports = {
  11. /**
  12. * Export NFA and DFA classes.
  13. */
  14. NFA: NFA,
  15. DFA: DFA,
  16. /**
  17. * Expose builders.
  18. */
  19. builders: builders,
  20. /**
  21. * Builds an NFA for the passed regexp.
  22. *
  23. * @param string | AST | RegExp:
  24. *
  25. * a regular expression in different representations: a string,
  26. * a RegExp object, or an AST.
  27. */
  28. toNFA: function toNFA(regexp) {
  29. return nfaFromRegExp.build(regexp);
  30. },
  31. /**
  32. * Builds DFA for the passed regexp.
  33. *
  34. * @param string | AST | RegExp:
  35. *
  36. * a regular expression in different representations: a string,
  37. * a RegExp object, or an AST.
  38. */
  39. toDFA: function toDFA(regexp) {
  40. return new DFA(this.toNFA(regexp));
  41. },
  42. /**
  43. * Returns true if regexp accepts the string.
  44. */
  45. test: function test(regexp, string) {
  46. return this.toDFA(regexp).matches(string);
  47. }
  48. };