summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/filtered_search/filtered_search_tokenizer.js.es6
blob: 57c0e8fc359ca10d444e771a6d3edb40d6bfd871 (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
157
158
159
160
161
162
163
164
165
166
167
168
169
(() => {
  class FilteredSearchTokenizer {
    static parseToken(input) {
      const colonIndex = input.indexOf(':');
      let tokenKey;
      let tokenValue;
      let tokenSymbol;

      if (colonIndex !== -1) {
        tokenKey = input.slice(0, colonIndex).toLowerCase();
        tokenValue = input.slice(colonIndex + 1);
        tokenSymbol = tokenValue[0];
      }

      return {
        tokenKey,
        tokenValue,
        tokenSymbol,
      };
    }

    static getLastTokenObject(input) {
      const token = FilteredSearchTokenizer.getLastToken(input);
      const colonIndex = token.indexOf(':');

      const key = colonIndex !== -1 ? token.slice(0, colonIndex) : '';
      const value = colonIndex !== -1 ? token.slice(colonIndex) : token;

      return {
        key,
        value,
      };
    }

    static getLastToken(input) {
      let completeToken = false;
      let completeQuotation = true;
      let lastQuotation = '';
      let i = input.length;

      const doubleQuote = '"';
      const singleQuote = '\'';
      while (!completeToken && i >= 0) {
        const isDoubleQuote = input[i] === doubleQuote;
        const isSingleQuote = input[i] === singleQuote;

        // If the second quotation is found
        if ((lastQuotation === doubleQuote && isDoubleQuote) ||
          (lastQuotation === singleQuote && isSingleQuote)) {
          completeQuotation = true;
        }

        // Save the first quotation
        if ((isDoubleQuote && lastQuotation === '') ||
          (isSingleQuote && lastQuotation === '')) {
          lastQuotation = input[i];
          completeQuotation = false;
        }

        if (completeQuotation && input[i] === ' ') {
          completeToken = true;
        } else {
          i -= 1;
        }
      }

      // Adjust by 1 because of empty space
      return input.slice(i + 1);
    }

    static processTokens(input) {
      const tokens = [];
      let searchToken = '';
      let lastToken = '';

      const inputs = input.split(' ');
      let searchTerms = '';
      let lastQuotation = '';
      let incompleteToken = false;

      // Iterate through each word (broken up by spaces)
      inputs.forEach((i) => {
        if (incompleteToken) {
          // Continue previous token as it had an escaped
          // quote in the beginning
          const prevToken = tokens.last();
          prevToken.value += ` ${i}`;

          // Remove last quotation from the value
          const lastQuotationRegex = new RegExp(lastQuotation, 'g');
          prevToken.value = prevToken.value.replace(lastQuotationRegex, '');
          tokens[tokens.length - 1] = prevToken;

          // Check to see if this quotation completes the token value
          if (i.indexOf(lastQuotation) !== -1) {
            lastToken = tokens.last();
            incompleteToken = !incompleteToken;
          }

          return;
        }

        const colonIndex = i.indexOf(':');

        if (colonIndex !== -1) {
          const { tokenKey, tokenValue, tokenSymbol } = gl.FilteredSearchTokenizer.parseToken(i);

          const keyMatch = gl.FilteredSearchTokenKeys.searchByKey(tokenKey);
          const symbolMatch = gl.FilteredSearchTokenKeys.searchBySymbol(tokenSymbol);

          const doubleQuoteOccurrences = tokenValue.split('"').length - 1;
          const singleQuoteOccurrences = tokenValue.split('\'').length - 1;

          const doubleQuoteIndex = tokenValue.indexOf('"');
          const singleQuoteIndex = tokenValue.indexOf('\'');

          const doubleQuoteExist = doubleQuoteIndex !== -1;
          const singleQuoteExist = singleQuoteIndex !== -1;

          const doubleQuoteExistOnly = doubleQuoteExist && !singleQuoteExist;
          const doubleQuoteIsBeforeSingleQuote =
            doubleQuoteExist && singleQuoteExist && doubleQuoteIndex < singleQuoteIndex;

          const singleQuoteExistOnly = singleQuoteExist && !doubleQuoteExist;
          const singleQuoteIsBeforeDoubleQuote =
            doubleQuoteExist && singleQuoteExist && singleQuoteIndex < doubleQuoteIndex;

          if ((doubleQuoteExistOnly || doubleQuoteIsBeforeSingleQuote)
              && doubleQuoteOccurrences % 2 !== 0) {
            // " is found and is in front of ' (if any)
            lastQuotation = '"';
            incompleteToken = true;
          } else if ((singleQuoteExistOnly || singleQuoteIsBeforeDoubleQuote)
              && singleQuoteOccurrences % 2 !== 0) {
            // ' is found and is in front of " (if any)
            lastQuotation = '\'';
            incompleteToken = true;
          }

          if (keyMatch && tokenValue.length > 0) {
            tokens.push({
              key: keyMatch.key,
              value: tokenValue,
              wildcard: !symbolMatch,
            });
            lastToken = tokens.last();

            return;
          }
        }

        // Add space for next term
        searchTerms += `${i} `;
        lastToken = i;
      }, this);

      searchToken = searchTerms.trim();

      return {
        tokens,
        searchToken,
        lastToken,
      };
    }
  }

  window.gl = window.gl || {};
  gl.FilteredSearchTokenizer = FilteredSearchTokenizer;
})();