summaryrefslogtreecommitdiff
path: root/node_modules/babel-plugin-minify-simplify/lib/pattern-match.js
blob: e5c34f6db511cbc341343693189beeb84f1cfe38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"use strict";

function _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

const LEAF_NODE = Symbol("LEAF_NODE");
module.exports = class PatternMatch {
  constructor(patterns) {
    this.decisionTree = this.makeDecisionTree(patterns);
  }

  handle(input, isMatch) {
    const result = this.match(input, isMatch);

    if (!result.match) {
      throw new Error("No Match Found for " + input.toString());
    }

    if (typeof result.value !== "function") {
      throw new Error("Expecting a function. Instead got - " + result.value.toString());
    }

    result.value.call(null, input, result.keys);
  }

  match(input, isMatch = (a, b) => a === b) {
    let current = this.decisionTree;
    const result = {
      match: false,
      value: void 0,
      keys: []
    }; // to handle falsy keys

    const NO_MATCH = Symbol("NO_MATCH");

    for (let i = 0; i < input.length; i++) {
      let matchedKey = NO_MATCH; // because map doesn't support custom key equal function

      var _iteratorNormalCompletion = true;
      var _didIteratorError = false;
      var _iteratorError = undefined;

      try {
        for (var _iterator = current.keys()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
          const key = _step.value;

          if (isMatch(key, input[i])) {
            matchedKey = key;
            result.keys.push(matchedKey);
            break;
          }
        }
      } catch (err) {
        _didIteratorError = true;
        _iteratorError = err;
      } finally {
        try {
          if (!_iteratorNormalCompletion && _iterator.return != null) {
            _iterator.return();
          }
        } finally {
          if (_didIteratorError) {
            throw _iteratorError;
          }
        }
      }

      if (matchedKey !== NO_MATCH) {
        current = current.get(matchedKey);

        if (i === input.length - 1) {
          if (current.has(LEAF_NODE)) {
            result.match = true;
            result.value = current.get(LEAF_NODE);
          }

          break;
        }
      } else {
        break;
      }
    }

    return result;
  }

  makeDecisionTree(patterns) {
    // order of keys in a Map is the order of insertion
    const root = new Map();
    var _iteratorNormalCompletion2 = true;
    var _didIteratorError2 = false;
    var _iteratorError2 = undefined;

    try {
      for (var _iterator2 = patterns[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
        const pattern = _step2.value;
        make(root, pattern);
      }
    } catch (err) {
      _didIteratorError2 = true;
      _iteratorError2 = err;
    } finally {
      try {
        if (!_iteratorNormalCompletion2 && _iterator2.return != null) {
          _iterator2.return();
        }
      } finally {
        if (_didIteratorError2) {
          throw _iteratorError2;
        }
      }
    }

    return root;

    function make(parent, pattern) {
      if (pattern.length < 2) {
        throw new Error("at least 2 elements required in a pattern");
      }

      if (pattern.length === 2) {
        if (parent.has(pattern[0])) {
          const pattern0 = parent.get(pattern[0]);

          if (!pattern0.has(LEAF_NODE)) {
            pattern0.set(LEAF_NODE, pattern[1]);
          } // here we don't handle duplicates
          // this pattern would have already been matched

        } else {
          parent.set(pattern[0], new Map([[LEAF_NODE, pattern[1]]]));
        }

        return parent;
      }

      const _pattern = _toArray(pattern),
            current = _pattern[0],
            rest = _pattern.slice(1);

      if (parent.has(current)) {
        make(parent.get(current), rest);
      } else {
        parent.set(current, make(new Map(), rest));
      }

      return parent;
    }
  }

};