@witchcraft/expressit 0.2.0 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/Lexer.js CHANGED
@@ -1,9 +1,6 @@
1
1
  var __defProp = Object.defineProperty;
2
2
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => {
4
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
- return value;
6
- };
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
4
  import { isBlank } from "@alanscodelog/utils/isBlank.js";
8
5
  import { pushIfNotIn } from "@alanscodelog/utils/pushIfNotIn.js";
9
6
  import { checkParserOpts } from "./internal/checkParserOpts.js";
@@ -76,8 +73,7 @@ function matchWhileCharNotEqualToUnescaped(char) {
76
73
  end++;
77
74
  c = input[end];
78
75
  }
79
- if (start === end)
80
- return false;
76
+ if (start === end) return false;
81
77
  return input.slice(start, end);
82
78
  };
83
79
  }
@@ -119,12 +115,9 @@ class Lexer {
119
115
  const syms = [...symOrs, ...symAnds, ...symNots];
120
116
  const customPropertyOperators = opts.customPropertyOperators ?? [];
121
117
  const expandedPropertySeparator = opts.expandedPropertySeparator ?? "";
122
- if (expandedPropertySeparator)
123
- syms.push(expandedPropertySeparator);
124
- if (customPropertyOperators.length > 0)
125
- pushIfNotIn(syms, customPropertyOperators);
126
- if (opts.regexValues)
127
- syms.push("\\/");
118
+ if (expandedPropertySeparator) syms.push(expandedPropertySeparator);
119
+ if (customPropertyOperators.length > 0) pushIfNotIn(syms, customPropertyOperators);
120
+ if (opts.regexValues) syms.push("\\/");
128
121
  if (opts.arrayValues) {
129
122
  syms.push("\\[");
130
123
  }
@@ -169,8 +162,7 @@ class Lexer {
169
162
  end++;
170
163
  c = input[end];
171
164
  }
172
- if (start === end)
173
- return false;
165
+ if (start === end) return false;
174
166
  return input.slice(start, end);
175
167
  }
176
168
  }),
@@ -196,8 +188,7 @@ class Lexer {
196
188
  end += match.input.length;
197
189
  }
198
190
  return input.slice(start, end);
199
- } else
200
- return false;
191
+ } else return false;
201
192
  }
202
193
  }),
203
194
  [
@@ -210,10 +201,8 @@ class Lexer {
210
201
  let inGroup = 0;
211
202
  let prevEscaped = false;
212
203
  while (c !== void 0 && (c !== "/" || inGroup > 0 || prevEscaped)) {
213
- if (c === "[")
214
- inGroup++;
215
- if (c === "]" && inGroup > 0)
216
- inGroup--;
204
+ if (c === "[") inGroup++;
205
+ if (c === "]" && inGroup > 0) inGroup--;
217
206
  if (c === "\\") {
218
207
  if (!prevEscaped) {
219
208
  prevEscaped = true;
@@ -226,8 +215,7 @@ class Lexer {
226
215
  end++;
227
216
  c = input[end];
228
217
  }
229
- if (start === end)
230
- return false;
218
+ if (start === end) return false;
231
219
  return input.slice(start, end);
232
220
  }
233
221
  }),
@@ -253,8 +241,7 @@ class Lexer {
253
241
  case "MAIN":
254
242
  return "NOT_SINGLE";
255
243
  default:
256
- if (mode.startsWith(BRACKET_PREFIX))
257
- return "BRACKET_MAIN";
244
+ if (mode.startsWith(BRACKET_PREFIX)) return "BRACKET_MAIN";
258
245
  return "MAIN";
259
246
  }
260
247
  },
@@ -267,8 +254,7 @@ class Lexer {
267
254
  push: (mode, tokens) => {
268
255
  const previous = tokens[tokens.length - 2];
269
256
  if ((previous == null ? void 0 : previous.type) === "VALUE_NOT_DOUBLE" || (previous == null ? void 0 : previous.type) === "VALUE_UNQUOTED") {
270
- if (mode.startsWith(BRACKET_PREFIX))
271
- return "BRACKET_MAIN";
257
+ if (mode.startsWith(BRACKET_PREFIX)) return "BRACKET_MAIN";
272
258
  return "MAIN";
273
259
  }
274
260
  switch (mode) {
@@ -277,8 +263,7 @@ class Lexer {
277
263
  case "MAIN":
278
264
  return "NOT_DOUBLE";
279
265
  default:
280
- if (mode.startsWith(BRACKET_PREFIX))
281
- return "BRACKET_MAIN";
266
+ if (mode.startsWith(BRACKET_PREFIX)) return "BRACKET_MAIN";
282
267
  return "MAIN";
283
268
  }
284
269
  },
@@ -299,8 +284,7 @@ class Lexer {
299
284
  case "MAIN":
300
285
  return "NOT_BACKTICK";
301
286
  default:
302
- if (mode.startsWith(BRACKET_PREFIX))
303
- return "BRACKET_MAIN";
287
+ if (mode.startsWith(BRACKET_PREFIX)) return "BRACKET_MAIN";
304
288
  return "MAIN";
305
289
  }
306
290
  },
@@ -350,8 +334,7 @@ class Lexer {
350
334
  break;
351
335
  }
352
336
  }
353
- if (found)
354
- break;
337
+ if (found) break;
355
338
  }
356
339
  if (c === " " || c === " " || c === "(" || c === ")" || c === "'" || c === '"' || c === "`" || c === "\\" || mode === "BRACKET_MAIN" && c === "]") {
357
340
  break;
@@ -359,8 +342,7 @@ class Lexer {
359
342
  end++;
360
343
  c = input[end];
361
344
  }
362
- if (start === end)
363
- return false;
345
+ if (start === end) return false;
364
346
  return input.slice(start, end);
365
347
  }
366
348
  }),
@@ -424,8 +406,7 @@ class Lexer {
424
406
  matches: (_c, input, start) => {
425
407
  for (const op of opts.expandedPropertySeparator) {
426
408
  const chars = input.slice(start, start + op.length);
427
- if (chars === op)
428
- return op;
409
+ if (chars === op) return op;
429
410
  }
430
411
  return false;
431
412
  }
@@ -439,8 +420,7 @@ class Lexer {
439
420
  matches: (_c, input, start) => {
440
421
  for (const op of opts.customPropertyOperators ?? []) {
441
422
  const chars = input.slice(start, start + op.length);
442
- if (chars === op)
443
- return op;
423
+ if (chars === op) return op;
444
424
  }
445
425
  return false;
446
426
  }
@@ -930,8 +910,7 @@ class Lexer {
930
910
  const newIndex = index + matchLength;
931
911
  const val = match === true ? c : match;
932
912
  const token = createToken(type, val, index, newIndex - 1);
933
- if (!t.skip)
934
- tokens.push(token);
913
+ if (!t.skip) tokens.push(token);
935
914
  if (t.push) {
936
915
  mode = typeof t.push === "function" ? t.push(mode, tokens) : t.push;
937
916
  branch = branches[mode];
package/dist/Parser.js CHANGED
@@ -1,9 +1,6 @@
1
1
  var __defProp = Object.defineProperty;
2
2
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => {
4
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
- return value;
6
- };
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
4
  import { get } from "@alanscodelog/utils/get.js";
8
5
  import { insert } from "@alanscodelog/utils/insert.js";
9
6
  import { isArray } from "@alanscodelog/utils/isArray.js";
@@ -37,11 +34,9 @@ const OPPOSITE = {
37
34
  [TOKEN_TYPE.OR]: TOKEN_TYPE.AND
38
35
  };
39
36
  function isEqualSet(setA, setB) {
40
- if (setA.size !== setB.size)
41
- return false;
37
+ if (setA.size !== setB.size) return false;
42
38
  for (const key of setA) {
43
- if (!setB.has(key))
44
- return false;
39
+ if (!setB.has(key)) return false;
45
40
  }
46
41
  return true;
47
42
  }
@@ -58,8 +53,7 @@ const createDefaultRequires = (partial = {}) => ({
58
53
  prefix: partial.prefix ?? false
59
54
  });
60
55
  const tokenRequiresWhitespace = (valid, whitespace, wordOps) => {
61
- if (whitespace || valid === void 0)
62
- return false;
56
+ if (whitespace || valid === void 0) return false;
63
57
  return valid.type === TOKEN_TYPE.VALUE || [TOKEN_TYPE.AND, TOKEN_TYPE.OR, TOKEN_TYPE.NOT].includes(valid.type) && wordOps.find((_) => _.value === valid.value) !== void 0;
64
58
  };
65
59
  const tokenVariable = [TOKEN_TYPE.BACKTICK, TOKEN_TYPE.DOUBLEQUOTE, TOKEN_TYPE.SINGLEQUOTE, TOKEN_TYPE.VALUE, TOKEN_TYPE.REGEX];
@@ -180,13 +174,10 @@ class Parser {
180
174
  return false;
181
175
  }
182
176
  isType(token2, type) {
183
- if (token2 === void 0)
184
- return false;
185
- if (token2.type === type)
186
- return true;
177
+ if (token2 === void 0) return false;
178
+ if (token2.type === type) return true;
187
179
  const tokenType = this.getTokenType(token2.type);
188
- if ((tokenType == null ? void 0 : tokenType.type) === type)
189
- return true;
180
+ if ((tokenType == null ? void 0 : tokenType.type) === type) return true;
190
181
  const category = this.$categories[type];
191
182
  if ((category == null ? void 0 : category.entries[token2.type]) !== void 0) {
192
183
  return true;
@@ -327,13 +318,11 @@ class Parser {
327
318
  }
328
319
  this.restoreState(state);
329
320
  }
330
- if (type === "AND" && pairs.length === 0)
331
- return void 0;
321
+ if (type === "AND" && pairs.length === 0) return void 0;
332
322
  let res = pairs[pairs.length - 1][0];
333
323
  for (let i = pairs.length - 1; i > 0; i--) {
334
324
  const before = pairs[i - 1];
335
- if (type === "OR" && res === void 0 && before === void 0)
336
- return void 0;
325
+ if (type === "OR" && res === void 0 && before === void 0) return void 0;
337
326
  res = expression(before[0], before[1], res);
338
327
  }
339
328
  return res;
@@ -371,8 +360,7 @@ class Parser {
371
360
  }
372
361
  return group(not, value, ...group$1);
373
362
  }
374
- if ([not, property, value].every((_) => _ === void 0))
375
- return void 0;
363
+ if ([not, property, value].every((_) => _ === void 0)) return void 0;
376
364
  return condition(not, property == null ? void 0 : property.prop, property == null ? void 0 : property.rest, value);
377
365
  }
378
366
  ruleConditionValue(property, { convertRegexValues = false, convertArrayValues = false } = {}) {
@@ -383,23 +371,19 @@ class Parser {
383
371
  if (this.options.prefixableGroups && property === void 0 && (next == null ? void 0 : next.type) !== $T.PAREN_L && (this.isType(next, $C.VALUE) && (this.isType(next2, $T.PAREN_L) || this.isType(next2, $C.QUOTE_ANY) && this.isType(next3, $T.PAREN_L)) || this.isType(next, $C.QUOTE_ANY) && (this.isType(next2, $T.PAREN_L) || this.isType(next2, $C.VALUE) && (this.isType(next3, $T.PAREN_L) || // "a(
384
372
  this.isType(next3, $C.QUOTE_ANY) && this.isType(next4, $T.PAREN_L))))) {
385
373
  const res = this.ruleVariable({ unprefixed: true });
386
- if (res)
387
- return res;
374
+ if (res) return res;
388
375
  }
389
376
  if (!this.isType(next, $T.PAREN_L)) {
390
377
  const res = this.ruleVariable({ unprefixed: false });
391
- if (res)
392
- return res;
378
+ if (res) return res;
393
379
  }
394
380
  if (this.isType(next, $T.PAREN_L)) {
395
381
  const res = this.rulePlainGroup({ onlyValues: property !== void 0, convertRegexValues, convertArrayValues });
396
- if (res)
397
- return res;
382
+ if (res) return res;
398
383
  }
399
384
  if (this.isType(next, $T.BRACKET_L)) {
400
385
  const res = this.rulePlainBracketGroup({ convertArrayValues });
401
- if (res)
402
- return res;
386
+ if (res) return res;
403
387
  }
404
388
  return void 0;
405
389
  }
@@ -457,8 +441,7 @@ class Parser {
457
441
  }
458
442
  }
459
443
  const bracketR = this.isType(this.peek(1), $T.BRACKET_R) ? this.ruleBracketR() : void 0;
460
- if (bracketL === void 0)
461
- throw new Error("bracketL is undefined, peek before using rule.");
444
+ if (bracketL === void 0) throw new Error("bracketL is undefined, peek before using rule.");
462
445
  if (!convertArrayValues) {
463
446
  return array(bracketL, values, bracketR);
464
447
  }
@@ -640,8 +623,7 @@ class Parser {
640
623
  if (!unprefixed && this.options.prefixableStrings !== void 0 && this.isType(next2, $C.QUOTE_ANY) && next2 && this.isType(next4, next2.type) && next && this.options.prefixableStrings.includes(next.value)) {
641
624
  return this.ruleValueUnquoted({ onlyToken });
642
625
  }
643
- if (onlyToken)
644
- return void 0;
626
+ if (onlyToken) return void 0;
645
627
  return token.value(...this.processToken());
646
628
  }
647
629
  ruleValueUnquoted({
@@ -860,8 +842,7 @@ class Parser {
860
842
  };
861
843
  for (const error of surroundingErrors) {
862
844
  for (const type of error.expected) {
863
- if (errorTypesHandled.includes(type))
864
- continue;
845
+ if (errorTypesHandled.includes(type)) continue;
865
846
  errorTypesHandled.push(type);
866
847
  switch (type) {
867
848
  case TOKEN_TYPE.DOUBLEQUOTE:
@@ -903,10 +884,8 @@ class Parser {
903
884
  }),
904
885
  range: pos({ start: index }, { fill: true })
905
886
  });
906
- if (type === TOKEN_TYPE.AND)
907
- errorTypesHandled.push(TOKEN_TYPE.OR);
908
- if (type === TOKEN_TYPE.OR)
909
- errorTypesHandled.push(TOKEN_TYPE.AND);
887
+ if (type === TOKEN_TYPE.AND) errorTypesHandled.push(TOKEN_TYPE.OR);
888
+ if (type === TOKEN_TYPE.OR) errorTypesHandled.push(TOKEN_TYPE.AND);
910
889
  break;
911
890
  case TOKEN_TYPE.PARENL:
912
891
  case TOKEN_TYPE.PARENR:
@@ -1177,16 +1156,14 @@ class Parser {
1177
1156
  getBestIndexes(indexes, existing, sortIndex = "") {
1178
1157
  indexes = indexes.filter((set) => {
1179
1158
  for (const key of set) {
1180
- if (!existing.has(key))
1181
- return false;
1159
+ if (!existing.has(key)) return false;
1182
1160
  }
1183
1161
  return true;
1184
1162
  });
1185
1163
  let finalIndexes = indexes;
1186
1164
  if (existing.has(sortIndex)) {
1187
1165
  const indexesWithSortIndex = indexes.filter((set) => set.has(sortIndex));
1188
- if (indexesWithSortIndex.length > 0)
1189
- finalIndexes = indexesWithSortIndex;
1166
+ if (indexesWithSortIndex.length > 0) finalIndexes = indexesWithSortIndex;
1190
1167
  }
1191
1168
  let smallest = Infinity;
1192
1169
  if (existing instanceof Map) {
@@ -1230,8 +1207,7 @@ class Parser {
1230
1207
  const allKeys = /* @__PURE__ */ new Set();
1231
1208
  for (const leftSet of left) {
1232
1209
  const exists = sets.find((set) => isEqualSet(set, leftSet));
1233
- if (exists)
1234
- continue;
1210
+ if (exists) continue;
1235
1211
  sets.push(leftSet);
1236
1212
  for (const key of leftSet) {
1237
1213
  allKeys.add(key);
@@ -1239,22 +1215,19 @@ class Parser {
1239
1215
  }
1240
1216
  for (const rightSet of right) {
1241
1217
  const exists = sets.find((set) => isEqualSet(set, rightSet));
1242
- if (exists)
1243
- continue;
1218
+ if (exists) continue;
1244
1219
  sets.push(rightSet);
1245
1220
  for (const key of rightSet) {
1246
1221
  allKeys.add(key);
1247
1222
  }
1248
1223
  }
1249
1224
  const commonKeys = /* @__PURE__ */ new Set();
1250
- outerCheck:
1251
- for (const key of allKeys) {
1252
- for (const set of sets) {
1253
- if (!set.has(key))
1254
- continue outerCheck;
1255
- }
1256
- commonKeys.add(key);
1225
+ outerCheck: for (const key of allKeys) {
1226
+ for (const set of sets) {
1227
+ if (!set.has(key)) continue outerCheck;
1257
1228
  }
1229
+ commonKeys.add(key);
1230
+ }
1258
1231
  if (commonKeys.size > 0) {
1259
1232
  return [commonKeys, allKeys];
1260
1233
  } else {
@@ -1424,11 +1397,9 @@ class Parser {
1424
1397
  condition: ast
1425
1398
  };
1426
1399
  const res = opts.valueValidator(contextValue, query, context);
1427
- if (res && !isArray(res))
1428
- throw new Error("The valueValidator must return an array or nothing/undefined");
1400
+ if (res && !isArray(res)) throw new Error("The valueValidator must return an array or nothing/undefined");
1429
1401
  if (res) {
1430
- for (const entry of res)
1431
- results.push(entry);
1402
+ for (const entry of res) results.push(entry);
1432
1403
  }
1433
1404
  } else {
1434
1405
  let name = unescape(ast.property.value.value);
@@ -1436,16 +1407,14 @@ class Parser {
1436
1407
  name = applyPrefix(prefix, name, opts.prefixApplier);
1437
1408
  }
1438
1409
  const boolValue = applyBoolean(groupValue, ast.operator === void 0);
1439
- if (ast.property)
1440
- prefixes.push(ast.property);
1410
+ if (ast.property) prefixes.push(ast.property);
1441
1411
  const operator22 = ast.propertyOperator;
1442
1412
  self_.validate(ast.value, context, name, boolValue, results, prefixes, operator22);
1443
1413
  }
1444
1414
  }
1445
1415
  if (ast.type === AST_TYPE.GROUP) {
1446
1416
  const _prefix = ((_j = ast.prefix) == null ? void 0 : _j.type) === AST_TYPE.CONDITION && ast.prefix.value.type === AST_TYPE.VARIABLE ? ast.prefix.value : void 0;
1447
- if (_prefix)
1448
- prefixes.push(_prefix);
1417
+ if (_prefix) prefixes.push(_prefix);
1449
1418
  const _groupValue = ((_k = ast.prefix) == null ? void 0 : _k.type) === AST_TYPE.CONDITION ? ast.prefix.operator === void 0 : !(((_l = ast.prefix) == null ? void 0 : _l.valid) === true);
1450
1419
  self_.validate(ast.expression, context, applyPrefix(prefix, (_prefix == null ? void 0 : _prefix.value.value) ?? "", opts.prefixApplier), applyBoolean(groupValue, _groupValue), results, prefixes, operator2);
1451
1420
  }
@@ -15,10 +15,8 @@ function condition(value, not = true, property, propertyOperator, { right, left
15
15
  }
16
16
  if (right || left) {
17
17
  node.sep = {};
18
- if (right)
19
- node.sep.right = right;
20
- if (left)
21
- node.sep.left = left;
18
+ if (right) node.sep.right = right;
19
+ if (left) node.sep.left = left;
22
20
  }
23
21
  return createConditionNode(node);
24
22
  }
@@ -2,10 +2,8 @@ import { type } from "./type.js";
2
2
  import { TOKEN_TYPE } from "../../types/ast.js";
3
3
  function delim(left = false, right = false) {
4
4
  let quoteType;
5
- if (typeof left === "string")
6
- quoteType = type(left);
7
- else if (typeof right === "string")
8
- quoteType = type(right);
5
+ if (typeof left === "string") quoteType = type(left);
6
+ else if (typeof right === "string") quoteType = type(right);
9
7
  const opts = {
10
8
  left: left === true || typeof left === "string" && left !== void 0,
11
9
  right: right === true || typeof right === "string" && right !== void 0
@@ -12,8 +12,7 @@ function expression(left, operator, right) {
12
12
  let op;
13
13
  if (operator === void 0) {
14
14
  operator = token([TOKEN_TYPE.AND, TOKEN_TYPE.OR], void 0, { start: left.end });
15
- } else
16
- op = operator;
15
+ } else op = operator;
17
16
  if (right === void 0) {
18
17
  right = token(TOKEN_TYPE.VALUE, void 0, { start: op.end });
19
18
  }
@@ -4,8 +4,7 @@ function pos(start, end) {
4
4
  return { start, end };
5
5
  } else {
6
6
  const item = start;
7
- if (item === void 0)
8
- return {};
7
+ if (item === void 0) return {};
9
8
  const fill = typeof end === "object" ? end.fill : false;
10
9
  if (isFullPos(item)) {
11
10
  return { start: item.start, end: item.end };
@@ -13,10 +12,8 @@ function pos(start, end) {
13
12
  if (fill) {
14
13
  let start2 = item.start;
15
14
  let end2 = item.end;
16
- if (start2 !== void 0 && end2 === void 0)
17
- end2 = start2;
18
- if (end2 !== void 0 && start2 === void 0)
19
- start2 = end2;
15
+ if (start2 !== void 0 && end2 === void 0) end2 = start2;
16
+ if (end2 !== void 0 && start2 === void 0) start2 = end2;
20
17
  return { start: start2, end: end2 };
21
18
  }
22
19
  return {};
@@ -16,16 +16,14 @@ function variable(prefix, value, quote, position) {
16
16
  node.quote = {};
17
17
  const quoteLeftPos = (position == null ? void 0 : position.start) !== void 0 ? pos({ start: position.start }, { fill: true }) : void 0;
18
18
  if (quote.left) {
19
- if (quoteLeftPos)
20
- quoteLeftPos.start -= 1;
19
+ if (quoteLeftPos) quoteLeftPos.start -= 1;
21
20
  node.quote.left = token(quote.type, quoteFromType(quote.type), quoteLeftPos);
22
21
  } else {
23
22
  node.quote.left = token(quote.type, void 0, quoteLeftPos);
24
23
  }
25
24
  const quoteRightPos = (position == null ? void 0 : position.end) !== void 0 ? pos({ end: position.end }, { fill: true }) : void 0;
26
25
  if (quote.right) {
27
- if (quoteRightPos)
28
- quoteRightPos.end += 1;
26
+ if (quoteRightPos) quoteRightPos.end += 1;
29
27
  node.quote.right = token(quote.type, quoteFromType(quote.type), quoteRightPos);
30
28
  } else {
31
29
  node.quote.right = token(quote.type, void 0, quoteRightPos);
package/dist/ast/error.js CHANGED
@@ -1,7 +1,6 @@
1
1
  import { createToken } from "./createToken.js";
2
2
  function error(pos, expected) {
3
- if (pos === void 0)
4
- throw new Error("should never happen, passed undefined position for error token");
3
+ if (pos === void 0) throw new Error("should never happen, passed undefined position for error token");
5
4
  return createToken({ expected, start: pos, end: pos });
6
5
  }
7
6
  export {
@@ -6,8 +6,7 @@ import { createToken } from "./createToken.js";
6
6
  import { createVariableNode } from "./createVariableNode.js";
7
7
  import { TOKEN_TYPE, AST_TYPE } from "../types/ast.js";
8
8
  function error(pos, expected) {
9
- if (pos === void 0)
10
- throw new Error("should never happen, passed undefined position for error token");
9
+ if (pos === void 0) throw new Error("should never happen, passed undefined position for error token");
11
10
  return createToken({ expected, start: pos, end: pos });
12
11
  }
13
12
  const operators = (type) => (value, pos) => createToken({ value, type, ...pos });
@@ -75,10 +74,8 @@ function condition(not, property, { propertyOperator, sepL, sepR } = {}, value)
75
74
  if (not) {
76
75
  node.operator = not;
77
76
  }
78
- if (property)
79
- node.property = property;
80
- if (propertyOperator)
81
- node.propertyOperator = propertyOperator;
77
+ if (property) node.property = property;
78
+ if (propertyOperator) node.propertyOperator = propertyOperator;
82
79
  if (sepL || sepR) {
83
80
  node.sep = {};
84
81
  if (sepL) {
@@ -86,8 +83,7 @@ function condition(not, property, { propertyOperator, sepL, sepR } = {}, value)
86
83
  node.property || (node.property = error(sepL.start, [TOKEN_TYPE.VALUE]));
87
84
  node.propertyOperator || (node.propertyOperator = error((sepL == null ? void 0 : sepL.end) ?? (sepR == null ? void 0 : sepR.start), [TOKEN_TYPE.VALUE]));
88
85
  }
89
- if (sepR)
90
- node.sep.right = sepR;
86
+ if (sepR) node.sep.right = sepR;
91
87
  else if (!node.value || node.value.type === AST_TYPE.VARIABLE) {
92
88
  node.sep.right = error(((_a = node.value) == null ? void 0 : _a.start) ?? end, [TOKEN_TYPE.OP_EXPANDED_SEP]);
93
89
  }
@@ -1,6 +1,5 @@
1
1
  const defaultKeyParser = function defaultKeyParser2(value) {
2
- if (value === void 0)
3
- return [];
2
+ if (value === void 0) return [];
4
3
  return [value];
5
4
  };
6
5
  export {
@@ -1,9 +1,6 @@
1
1
  var __defProp = Object.defineProperty;
2
2
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => {
4
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
- return value;
6
- };
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
4
  import { Parser } from "../Parser.js";
8
5
  class ShortcutContextParser extends Parser {
9
6
  constructor(dummyContext, validRegexFlags = ["i", "u", "m"]) {
@@ -67,16 +64,14 @@ class ShortcutContextParser extends Parser {
67
64
  }
68
65
  }
69
66
  }
70
- if (tokens.length > 0)
71
- return tokens;
67
+ if (tokens.length > 0) return tokens;
72
68
  },
73
69
  conditionNormalizer({ operator, value, regexFlags, isRegex, isNegated, isQuoted }) {
74
70
  let finalValue = value;
75
71
  let finalOperator = operator;
76
72
  if (typeof value === "string" && !isQuoted) {
77
73
  const asNum = parseInt(value, 2);
78
- if (!isNaN(asNum))
79
- finalValue = asNum;
74
+ if (!isNaN(asNum)) finalValue = asNum;
80
75
  if (["true", "false"].includes(value)) {
81
76
  finalValue = value === "true";
82
77
  }
@@ -118,8 +113,7 @@ class ShortcutContextParser extends Parser {
118
113
  this.regexablekeys.push(prev ? `${prev}.${key}` : key);
119
114
  }
120
115
  } else {
121
- if (typeof context[key] !== "object")
122
- throw new Error("A dummy context value must be a boolean or an object.");
116
+ if (typeof context[key] !== "object") throw new Error("A dummy context value must be a boolean or an object.");
123
117
  this._extractKeysFromContext(context[key], prev ? `${prev}.${key}` : key);
124
118
  }
125
119
  }
@@ -1,9 +1,6 @@
1
1
  var __defProp = Object.defineProperty;
2
2
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
- var __publicField = (obj, key, value) => {
4
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
5
- return value;
6
- };
3
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
7
4
  import { crop } from "@alanscodelog/utils/crop.js";
8
5
  import { indent } from "@alanscodelog/utils/indent.js";
9
6
  import { pretty } from "@alanscodelog/utils/pretty.js";
@@ -16,10 +16,8 @@ function checkParserOpts(opts, evaluatorChecks = false, validatorChecks = false)
16
16
  ...opts.regexValues ? ["/"] : []
17
17
  ];
18
18
  const extra = [];
19
- if (opts.expandedPropertySeparator)
20
- extra.push(opts.expandedPropertySeparator);
21
- if (opts.customPropertyOperators)
22
- pushIfNotIn(extra, opts.customPropertyOperators);
19
+ if (opts.expandedPropertySeparator) extra.push(opts.expandedPropertySeparator);
20
+ if (opts.customPropertyOperators) pushIfNotIn(extra, opts.customPropertyOperators);
23
21
  if (opts.expandedPropertySeparator && isBlank(opts.expandedPropertySeparator)) {
24
22
  throw new ExpressitError(
25
23
  ERROR_CODES.PARSER_CONFLICTING_OPTIONS_ERROR,
@@ -84,14 +82,10 @@ function checkParserOpts(opts, evaluatorChecks = false, validatorChecks = false)
84
82
  }
85
83
  if (evaluatorChecks) {
86
84
  const requireCustomNormalizer = [];
87
- if ((((_g = opts.prefixableStrings) == null ? void 0 : _g.length) ?? 0) > 0)
88
- requireCustomNormalizer.push("prefixableStrings");
89
- if ((((_h = opts.customPropertyOperators) == null ? void 0 : _h.length) ?? 0) > 0)
90
- requireCustomNormalizer.push("customPropertyOperators");
91
- if ((((_i = opts.expandedPropertySeparator) == null ? void 0 : _i.length) ?? 0) > 0)
92
- requireCustomNormalizer.push("expandedPropertySeparator");
93
- if (opts.regexValues)
94
- requireCustomNormalizer.push("regexValues");
85
+ if ((((_g = opts.prefixableStrings) == null ? void 0 : _g.length) ?? 0) > 0) requireCustomNormalizer.push("prefixableStrings");
86
+ if ((((_h = opts.customPropertyOperators) == null ? void 0 : _h.length) ?? 0) > 0) requireCustomNormalizer.push("customPropertyOperators");
87
+ if ((((_i = opts.expandedPropertySeparator) == null ? void 0 : _i.length) ?? 0) > 0) requireCustomNormalizer.push("expandedPropertySeparator");
88
+ if (opts.regexValues) requireCustomNormalizer.push("regexValues");
95
89
  if (requireCustomNormalizer.length > 0 && opts.conditionNormalizer === defaultConditionNormalizer) {
96
90
  throw new ExpressitError(
97
91
  ERROR_CODES.PARSER_OPTION_REQUIRED_ERROR,
@@ -100,10 +94,8 @@ function checkParserOpts(opts, evaluatorChecks = false, validatorChecks = false)
100
94
  );
101
95
  }
102
96
  const requireCustomComparer = [];
103
- if (opts.regexValues)
104
- requireCustomComparer.push("regexValues");
105
- if (opts.regexValues)
106
- requireCustomComparer.push("arrayValues");
97
+ if (opts.regexValues) requireCustomComparer.push("regexValues");
98
+ if (opts.regexValues) requireCustomComparer.push("arrayValues");
107
99
  if (requireCustomComparer.length > 0 && opts.valueComparer === defaultValueComparer) {
108
100
  throw new ExpressitError(
109
101
  ERROR_CODES.PARSER_OPTION_REQUIRED_ERROR,
@@ -2,16 +2,14 @@ import { multisplice } from "@alanscodelog/utils/multisplice.js";
2
2
  function escapeVariableOrPrefix(variable, preferredQuote) {
3
3
  let doQuote = false;
4
4
  for (const quoteType of ['"', "'", "`"]) {
5
- if (!variable.includes(quoteType) && !variable.includes(" "))
6
- continue;
5
+ if (!variable.includes(quoteType) && !variable.includes(" ")) continue;
7
6
  if (variable.startsWith(quoteType) && variable.endsWith(quoteType)) {
8
7
  break;
9
8
  }
10
9
  const indexes = [];
11
10
  for (let i = 0; i < variable.length; i++) {
12
11
  const char = variable[i];
13
- if (char === void 0)
14
- break;
12
+ if (char === void 0) break;
15
13
  if (char === "\\") {
16
14
  i += 2;
17
15
  continue;
@@ -19,11 +17,9 @@ function escapeVariableOrPrefix(variable, preferredQuote) {
19
17
  if (char === " ") {
20
18
  doQuote = true;
21
19
  }
22
- if (char === quoteType)
23
- indexes.push(i);
20
+ if (char === quoteType) indexes.push(i);
24
21
  }
25
- if (indexes.length === 0)
26
- break;
22
+ if (indexes.length === 0) break;
27
23
  const newVal = multisplice(variable.split(""), indexes, 0, "\\").array.join("");
28
24
  variable = newVal;
29
25
  break;
@@ -1,6 +1,6 @@
1
1
  const name = "@witchcraft/expressit";
2
2
  const description = "A blazing fast, customizable, error-tolerant expression parser that creates safe to eval expressions + a few other goodies like autocomplete.";
3
- const version = "0.2.0";
3
+ const version = "0.2.1";
4
4
  const types = "./dist/index.d.ts";
5
5
  const type = "module";
6
6
  const module = "./dist/index.js";
@@ -7,63 +7,43 @@ function generateParentsMap(ast) {
7
7
  const map = recursiveMap ?? /* @__PURE__ */ new Map();
8
8
  const self = generateParentsMap;
9
9
  if (ast.type === AST_TYPE.VARIABLE) {
10
- if (ast.prefix)
11
- map.set(ast.prefix, ast);
12
- if ((_a = ast.quote) == null ? void 0 : _a.left)
13
- map.set(ast.quote.left, ast);
14
- if ((_b = ast.quote) == null ? void 0 : _b.right)
15
- map.set(ast.quote.right, ast);
16
- if ((_c = ast.quote) == null ? void 0 : _c.flags)
17
- map.set(ast.quote.flags, ast);
10
+ if (ast.prefix) map.set(ast.prefix, ast);
11
+ if ((_a = ast.quote) == null ? void 0 : _a.left) map.set(ast.quote.left, ast);
12
+ if ((_b = ast.quote) == null ? void 0 : _b.right) map.set(ast.quote.right, ast);
13
+ if ((_c = ast.quote) == null ? void 0 : _c.flags) map.set(ast.quote.flags, ast);
18
14
  map.set(ast.value, ast);
19
15
  } else if (ast.type === AST_TYPE.CONDITION) {
20
- if (ast.operator)
21
- map.set(ast.operator, ast);
16
+ if (ast.operator) map.set(ast.operator, ast);
22
17
  if (ast.property) {
23
18
  map.set(ast.property, ast);
24
- if (isNode(ast.property))
25
- self(ast.property, map);
19
+ if (isNode(ast.property)) self(ast.property, map);
26
20
  }
27
- if (ast.propertyOperator)
28
- map.set(ast.propertyOperator, ast);
29
- if ((_d = ast.sep) == null ? void 0 : _d.left)
30
- map.set(ast.sep.left, ast);
31
- if ((_e = ast.sep) == null ? void 0 : _e.right)
32
- map.set(ast.sep.right, ast);
21
+ if (ast.propertyOperator) map.set(ast.propertyOperator, ast);
22
+ if ((_d = ast.sep) == null ? void 0 : _d.left) map.set(ast.sep.left, ast);
23
+ if ((_e = ast.sep) == null ? void 0 : _e.right) map.set(ast.sep.right, ast);
33
24
  map.set(ast.value, ast);
34
- if (isNode(ast.value))
35
- self(ast.value, map);
25
+ if (isNode(ast.value)) self(ast.value, map);
36
26
  } else if (ast.type === AST_TYPE.EXPRESSION) {
37
- if (ast.operator)
38
- map.set(ast.operator, ast);
27
+ if (ast.operator) map.set(ast.operator, ast);
39
28
  map.set(ast.right, ast);
40
- if (isNode(ast.right))
41
- self(ast.right, map);
29
+ if (isNode(ast.right)) self(ast.right, map);
42
30
  map.set(ast.left, ast);
43
- if (isNode(ast.left))
44
- self(ast.left, map);
31
+ if (isNode(ast.left)) self(ast.left, map);
45
32
  } else if (ast.type === AST_TYPE.GROUP) {
46
33
  if (ast.prefix) {
47
34
  map.set(ast.prefix, ast);
48
- if (isNode(ast.prefix))
49
- self(ast.prefix, map);
35
+ if (isNode(ast.prefix)) self(ast.prefix, map);
50
36
  }
51
- if ((_f = ast.paren) == null ? void 0 : _f.left)
52
- map.set(ast.paren.left, ast);
53
- if ((_g = ast.paren) == null ? void 0 : _g.right)
54
- map.set(ast.paren.right, ast);
37
+ if ((_f = ast.paren) == null ? void 0 : _f.left) map.set(ast.paren.left, ast);
38
+ if ((_g = ast.paren) == null ? void 0 : _g.right) map.set(ast.paren.right, ast);
55
39
  map.set(ast.expression, ast);
56
- if (isNode(ast.expression))
57
- self(ast.expression, map);
40
+ if (isNode(ast.expression)) self(ast.expression, map);
58
41
  } else if (ast.type === AST_TYPE.ARRAY) {
59
- if (ast.bracket.left)
60
- map.set(ast.bracket.left, ast);
61
- if (ast.bracket.right)
62
- map.set(ast.bracket.right, ast);
42
+ if (ast.bracket.left) map.set(ast.bracket.left, ast);
43
+ if (ast.bracket.right) map.set(ast.bracket.right, ast);
63
44
  for (const val of ast.values) {
64
45
  map.set(val, ast);
65
- if (isNode(val))
66
- self(val, map);
46
+ if (isNode(val)) self(val, map);
67
47
  }
68
48
  }
69
49
  return map;
@@ -55,28 +55,20 @@ function setWhitespaceBetween(input, index, side, info) {
55
55
  if (info.at) {
56
56
  if (info.valid[side]) {
57
57
  const nextTokStart = info.valid[side][oppositePos];
58
- if (info.at[pos] !== nextTokStart)
59
- info.whitespace[side] = true;
60
- else
61
- return;
58
+ if (info.at[pos] !== nextTokStart) info.whitespace[side] = true;
59
+ else return;
62
60
  } else {
63
- if (info.at[pos] !== limit)
64
- info.whitespace[side] = true;
65
- else
66
- return;
61
+ if (info.at[pos] !== limit) info.whitespace[side] = true;
62
+ else return;
67
63
  }
68
64
  } else {
69
65
  if (info.valid[side]) {
70
66
  const nextTokStart = info.valid[side][oppositePos];
71
- if (index !== nextTokStart)
72
- info.whitespace[side] = true;
73
- else
74
- return;
67
+ if (index !== nextTokStart) info.whitespace[side] = true;
68
+ else return;
75
69
  } else {
76
- if (index !== limit)
77
- info.whitespace[side] = true;
78
- else
79
- return;
70
+ if (index !== limit) info.whitespace[side] = true;
71
+ else return;
80
72
  }
81
73
  }
82
74
  }
@@ -6,8 +6,7 @@ import { isQuote } from "./isQuote.js";
6
6
  import { TOKEN_TYPE } from "../types/ast.js";
7
7
  function getOppositeDelimiter(token, parentsMap) {
8
8
  const parent = parentsMap.get(token);
9
- if (!isDelimiter(token))
10
- throw new Error("Token is not a delimiter type.");
9
+ if (!isDelimiter(token)) throw new Error("Token is not a delimiter type.");
11
10
  if (isParen(token)) {
12
11
  const paren = parent.paren;
13
12
  const opposite = paren.left === token ? "right" : "left";
@@ -18,14 +17,12 @@ function getOppositeDelimiter(token, parentsMap) {
18
17
  return bracket[opposite];
19
18
  } else if (isQuote(token)) {
20
19
  const quotes = parent.quote;
21
- if (quotes === void 0)
22
- unreachable();
20
+ if (quotes === void 0) unreachable();
23
21
  const opposite = quotes.left === token ? "right" : "left";
24
22
  return quotes[opposite];
25
23
  } else if (token.type === TOKEN_TYPE.OP_EXPANDED_SEP) {
26
24
  const sep = parent.sep;
27
- if (sep === void 0)
28
- unreachable();
25
+ if (sep === void 0) unreachable();
29
26
  const opposite = sep.left === token ? "right" : "left";
30
27
  return sep[opposite];
31
28
  }
@@ -28,8 +28,7 @@ function prettyAst(ast, { indent = " ", children = "├╴", last = "└╴",
28
28
  const _ = indent;
29
29
  const __ = arguments[3] ?? "";
30
30
  let extra = arguments[4] ?? "";
31
- if (!isBlank(extra))
32
- extra = ` ${c.hint}${extra}${c.reset}`;
31
+ if (!isBlank(extra)) extra = ` ${c.hint}${extra}${c.reset}`;
33
32
  const ___ = __ + _ + branch;
34
33
  const __L = __ + _ + indent[0];
35
34
  const prettyAst_ = prettyAst;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@witchcraft/expressit",
3
3
  "description": "A blazing fast, customizable, error-tolerant expression parser that creates safe to eval expressions + a few other goodies like autocomplete.",
4
- "version": "0.2.0",
4
+ "version": "0.2.1",
5
5
  "types": "./dist/index.d.ts",
6
6
  "type": "module",
7
7
  "module": "./dist/index.js",