Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

index.js 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. function _sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
  2. function _slicedToArray(arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return _sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  5. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  6. var STOP = Symbol("STOP");
  7. function makeTransition(regex, nextState) {
  8. var _ref = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {},
  9. _ref$n = _ref.n,
  10. n = _ref$n === void 0 ? 1 : _ref$n,
  11. allowedSeparator = _ref.allowedSeparator;
  12. return function (instance) {
  13. if (allowedSeparator) {
  14. if (instance.input[instance.ptr] === allowedSeparator) {
  15. if (regex.test(instance.input.substring(instance.ptr - 1, instance.ptr))) {
  16. // Consume the separator and stay in current state
  17. return [instance.currentState, 1];
  18. } else {
  19. return [instance.terminatingState, 0];
  20. }
  21. }
  22. }
  23. if (regex.test(instance.input.substring(instance.ptr, instance.ptr + n))) {
  24. return [nextState, n];
  25. }
  26. return false;
  27. };
  28. }
  29. function combineTransitions(transitions) {
  30. return function () {
  31. var match = false;
  32. var currentTransitions = transitions[this.currentState] || [];
  33. for (var i = 0; i < currentTransitions.length; ++i) {
  34. match = currentTransitions[i](this);
  35. if (match !== false) {
  36. break;
  37. }
  38. }
  39. return match || [this.terminatingState, 0];
  40. };
  41. }
  42. var FSM =
  43. /*#__PURE__*/
  44. function () {
  45. function FSM(transitions, initialState) {
  46. var terminatingState = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : STOP;
  47. _classCallCheck(this, FSM);
  48. this.initialState = initialState;
  49. this.terminatingState = terminatingState;
  50. if (terminatingState === STOP || !transitions[terminatingState]) {
  51. transitions[terminatingState] = [];
  52. }
  53. this.transitionFunction = combineTransitions.call(this, transitions);
  54. }
  55. _createClass(FSM, [{
  56. key: "run",
  57. value: function run(input) {
  58. this.input = input;
  59. this.ptr = 0;
  60. this.currentState = this.initialState;
  61. var value = "";
  62. var eatLength, nextState;
  63. while (this.currentState !== this.terminatingState && this.ptr < this.input.length) {
  64. var _transitionFunction = this.transitionFunction();
  65. var _transitionFunction2 = _slicedToArray(_transitionFunction, 2);
  66. nextState = _transitionFunction2[0];
  67. eatLength = _transitionFunction2[1];
  68. value += this.input.substring(this.ptr, this.ptr += eatLength);
  69. this.currentState = nextState;
  70. }
  71. return value;
  72. }
  73. }]);
  74. return FSM;
  75. }();
  76. export { makeTransition, FSM };