match.js 907 B

12345678910111213141516171819202122232425262728293031323334
  1. import { __values, __read } from './_virtual/_tslib.js';
  2. import { State } from './State.js';
  3. function matchState(state, patterns, defaultValue) {
  4. var e_1, _a;
  5. var resolvedState = State.from(state, state instanceof State ? state.context : undefined);
  6. try {
  7. for (var patterns_1 = __values(patterns), patterns_1_1 = patterns_1.next(); !patterns_1_1.done; patterns_1_1 = patterns_1.next()) {
  8. var _b = __read(patterns_1_1.value, 2),
  9. stateValue = _b[0],
  10. getValue = _b[1];
  11. if (resolvedState.matches(stateValue)) {
  12. return getValue(resolvedState);
  13. }
  14. }
  15. } catch (e_1_1) {
  16. e_1 = {
  17. error: e_1_1
  18. };
  19. } finally {
  20. try {
  21. if (patterns_1_1 && !patterns_1_1.done && (_a = patterns_1.return)) _a.call(patterns_1);
  22. } finally {
  23. if (e_1) throw e_1.error;
  24. }
  25. }
  26. return defaultValue(resolvedState);
  27. }
  28. export { matchState };