@briza/illogical 1.7.1 → 1.7.3

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/lib/illogical.js CHANGED
@@ -94,15 +94,9 @@ function areAllResults(values) {
94
94
  * @param {Result[]} results results or evaluables
95
95
  * @returns {boolean} type guard
96
96
  */
97
- function areAllNumbers$1(results) {
97
+ function areAllNumbers(results) {
98
98
  return results.every(isNumber);
99
99
  }
100
- function isUndefined(value) {
101
- return value === undefined;
102
- }
103
- function isNull(value) {
104
- return value === null;
105
- }
106
100
 
107
101
  /**
108
102
  * Valid types for context members
@@ -160,7 +154,7 @@ class Arithmetic {
160
154
  if (presentValues.length !== results.length) {
161
155
  return false;
162
156
  }
163
- if (!areAllNumbers$1(presentValues)) {
157
+ if (!areAllNumbers(presentValues)) {
164
158
  throw new Error(`operands must be numbers for ${this.constructor.name}`);
165
159
  }
166
160
  return presentValues;
@@ -602,6 +596,9 @@ class Reference extends Operand {
602
596
  evaluate(ctx) {
603
597
  return this.toDataType(this.valueLookup(ctx));
604
598
  }
599
+ checkStrictAndOptional(key, strictKeys, optionalKeys) {
600
+ return strictKeys !== undefined && (Array.isArray(strictKeys) ? strictKeys.includes(key) : strictKeys.has(key)) || optionalKeys !== undefined && (Array.isArray(optionalKeys) ? !optionalKeys.includes(key) : !optionalKeys.has(key)) ? undefined : this;
601
+ }
605
602
 
606
603
  /**
607
604
  * {@link Evaluable.simplify}
@@ -615,7 +612,7 @@ class Reference extends Operand {
615
612
  if (!key || typeof key === 'number') {
616
613
  return this;
617
614
  }
618
- return strictKeys && strictKeys.includes(key) || optionalKeys && !optionalKeys.includes(key) ? undefined : this;
615
+ return this.checkStrictAndOptional(key, strictKeys, optionalKeys);
619
616
  }
620
617
 
621
618
  /**
@@ -1958,516 +1955,6 @@ class Parser {
1958
1955
  }
1959
1956
  }
1960
1957
 
1961
- const isTrueResult = value => value === true;
1962
- const isFalseResult = value => value === false;
1963
- const isNonFalseResult = operand => operand !== false && !isEvaluable(operand);
1964
- const isNonTrueResult = operand => operand !== true && !isEvaluable(operand);
1965
- const resultToInputInternal = value => {
1966
- if (isUndefined(value)) {
1967
- return undefined;
1968
- }
1969
- if (Array.isArray(value)) {
1970
- return undefined;
1971
- }
1972
- return value;
1973
- };
1974
- const resultToInput = value => {
1975
- if (isUndefined(value)) {
1976
- return undefined;
1977
- }
1978
- if (Array.isArray(value)) {
1979
- const definedValues = value.map(resultToInputInternal).filter(val => !isUndefined(val));
1980
- if (definedValues.length === 0) {
1981
- return undefined;
1982
- }
1983
- return definedValues;
1984
- }
1985
- return value;
1986
- };
1987
- const areAllNumbers = results => {
1988
- return results.every(isNumber);
1989
- };
1990
- const areAllInputs = values => values.every(value => !isEvaluable(value));
1991
- const getInputValues = results => {
1992
- const presentValues = results.filter(result => !isNull(result) && !isUndefined(result));
1993
- // If we have missing context values the result or we still have refences
1994
- // simplify to false.
1995
- if (presentValues.length !== results.length || !areAllNumbers(presentValues)) {
1996
- return false;
1997
- }
1998
- return presentValues;
1999
- };
2000
- const extractValues = input => {
2001
- const evaluable = isEvaluable(input);
2002
- if (!evaluable) {
2003
- return Array.isArray(input) ? input : [input];
2004
- }
2005
- if (input instanceof Collection) {
2006
- const results = input.getItems();
2007
- const values = results.map(item => item instanceof Value ? item.evaluate() : null).filter(value => !isNull(value));
2008
- return values;
2009
- }
2010
- return [];
2011
- };
2012
-
2013
- const simplifyAnd = simplifyInput => input => {
2014
- const [operator, ...operands] = input;
2015
- const simplifiedOperands = [];
2016
- for (const operand of operands) {
2017
- const simplification = simplifyInput(operand);
2018
- if (isUndefined(simplification) || isBoolean(simplification) && !isTrueResult(simplification)) {
2019
- // Short-circuit for AND
2020
- return false;
2021
- }
2022
- simplifiedOperands.push(simplification);
2023
- }
2024
-
2025
- // Remove false operands leaving only the Inputs
2026
- const simplified = simplifiedOperands.filter(isNonTrueResult);
2027
- if (simplified.length === 0) {
2028
- return true;
2029
- }
2030
- if (simplified.length === 1) {
2031
- return simplified[0];
2032
- }
2033
- return [operator, ...simplified];
2034
- };
2035
-
2036
- const simplify$3 = (simplifyInput, predicate) => input => {
2037
- const [, ...operands] = input;
2038
- const results = operands.map(simplifyInput);
2039
- if (areAllInputs(results)) {
2040
- const presentValues = getInputValues(results);
2041
- if (isFalseResult(presentValues)) {
2042
- return false;
2043
- }
2044
- return presentValues.reduce(predicate);
2045
- }
2046
- return input;
2047
- };
2048
- const simplifySum = simplifyInput => input => simplify$3(simplifyInput, operateWithExpectedDecimals('sum'))(input);
2049
- const simplifySubtract = simplifyInput => input => simplify$3(simplifyInput, operateWithExpectedDecimals('subtract'))(input);
2050
- const simplifyMultiply = simplifyInput => input => simplify$3(simplifyInput, operateWithExpectedDecimals('multiply'))(input);
2051
- const simplifyDivide = simplifyInput => input => simplify$3(simplifyInput, (acc, result) => acc / result)(input);
2052
-
2053
- const simplifyComparison = predicate => (opts, simplifyInput) => input => {
2054
- const [operator, ...operands] = input;
2055
- const [left, right] = operands;
2056
- const leftSimplified = simplifyInput(left);
2057
- const rightSimplified = simplifyInput(right);
2058
- const isLeftEvaluable = isEvaluable(leftSimplified);
2059
- const isRightEvaluable = isEvaluable(rightSimplified);
2060
- if (isLeftEvaluable || isRightEvaluable) {
2061
- // If either left or right is an array, we cannot simplify further
2062
- return [operator, isLeftEvaluable ? leftSimplified.serialize(opts) : left, isRightEvaluable ? rightSimplified.serialize(opts) : right];
2063
- }
2064
- if (isNumber(leftSimplified) && isNumber(rightSimplified)) {
2065
- return predicate(leftSimplified, rightSimplified);
2066
- }
2067
- const leftDate = toDateNumber(leftSimplified);
2068
- const rightDate = toDateNumber(rightSimplified);
2069
- if (leftDate && rightDate) {
2070
- return predicate(leftDate, rightDate);
2071
- }
2072
- if (Array.isArray(leftSimplified) || Array.isArray(rightSimplified)) {
2073
- return [operator, leftSimplified, rightSimplified];
2074
- }
2075
- return false;
2076
- };
2077
- const simplifyGt = (opts, simplifyInput) => input => simplifyComparison((left, right) => left > right)(opts, simplifyInput)(input);
2078
- const simplifyGe = (opts, simplifyInput) => input => simplifyComparison((left, right) => left >= right)(opts, simplifyInput)(input);
2079
- const simplifyLt = (opts, simplifyInput) => input => simplifyComparison((left, right) => left < right)(opts, simplifyInput)(input);
2080
- const simplifyLe = (opts, simplifyInput) => input => simplifyComparison((left, right) => left <= right)(opts, simplifyInput)(input);
2081
-
2082
- const simplifyEquality = predicate => (opts, simplifyInput) => input => {
2083
- const [operator, ...operands] = input;
2084
- const [left, right] = operands;
2085
- const leftSimplified = simplifyInput(left);
2086
- const rightSimplified = simplifyInput(right);
2087
- const isLeftEvaluable = isEvaluable(leftSimplified);
2088
- const isRightEvaluable = isEvaluable(rightSimplified);
2089
- if (isLeftEvaluable || isRightEvaluable) {
2090
- // If either left or right is an array, we cannot simplify further
2091
- return [operator, isLeftEvaluable ? leftSimplified.serialize(opts) : left, isRightEvaluable ? rightSimplified.serialize(opts) : right];
2092
- }
2093
-
2094
- // See Equal.comparison
2095
- return predicate(leftSimplified, rightSimplified);
2096
- };
2097
- const simplifyEq = (opts, simplifyInput) => input => simplifyEquality((left, right) => left === right)(opts, simplifyInput)(input);
2098
- const simplifyNe = (opts, simplifyInput) => input => simplifyEquality((left, right) => left !== right)(opts, simplifyInput)(input);
2099
-
2100
- const simplify$2 = (originalInput, nonArrayInput, arrayInput) => {
2101
- // If we don't have the non-array operand, there is nothing to do.
2102
- if (isEvaluable(nonArrayInput)) {
2103
- return originalInput;
2104
- }
2105
-
2106
- // If we don't have all the values, try with what we have for the
2107
- // positive case, but otherwise return the original input.
2108
- if (isEvaluable(arrayInput)) {
2109
- const leftValues = extractValues(arrayInput);
2110
- const isFound = leftValues.indexOf(nonArrayInput) > -1;
2111
- return isFound ? true : originalInput;
2112
- }
2113
-
2114
- // If we have all the values, we can check if the non-array operand is
2115
- // included in the array operand.
2116
- return arrayInput.indexOf(nonArrayInput) > -1;
2117
- };
2118
- const simplifyIn = simplifyInput => input => {
2119
- const [, ...operands] = input;
2120
- const [left, right] = operands;
2121
- if (isNull(left) || isUndefined(left) || isNull(right) || isUndefined(right)) {
2122
- return false;
2123
- }
2124
- const leftSimplified = simplifyInput(left);
2125
- const rightSimplified = simplifyInput(right);
2126
- const isLeftArray = Array.isArray(leftSimplified) || isEvaluable(leftSimplified);
2127
- const isRightArray = Array.isArray(rightSimplified) || isEvaluable(rightSimplified);
2128
- if (isLeftArray) {
2129
- return simplify$2(input, rightSimplified, leftSimplified);
2130
- }
2131
- if (isRightArray) {
2132
- return simplify$2(input, leftSimplified, rightSimplified);
2133
- }
2134
- return false;
2135
- };
2136
-
2137
- const simplifyNor = (opts, simplifyInput) => input => {
2138
- const [operator, ...operands] = input;
2139
- const simplifiedOperands = [];
2140
- for (const operand of operands) {
2141
- const simplification = simplifyInput(operand);
2142
- if (isUndefined(simplification) || isBoolean(simplification) && isTrueResult(simplification)) {
2143
- // Short-circuit for AND
2144
- return false;
2145
- }
2146
- simplifiedOperands.push(simplification);
2147
- }
2148
-
2149
- // Remove true operands leaving only the Inputs
2150
- const simplified = simplifiedOperands.filter(isNonFalseResult);
2151
- if (simplified.length === 0) {
2152
- return true;
2153
- }
2154
- if (simplified.length === 1) {
2155
- var _opts$operatorMapping;
2156
- return [(_opts$operatorMapping = opts.operatorMapping.get(OPERATOR$3)) !== null && _opts$operatorMapping !== void 0 ? _opts$operatorMapping : 'NOT', simplified[0]];
2157
- }
2158
- return [operator, ...simplified];
2159
- };
2160
-
2161
- const simplifyNot = simplifyInput => input => {
2162
- const [, ...operands] = input;
2163
- const simplification = simplifyInput(operands[0]);
2164
- if (isBoolean(simplification)) {
2165
- return !simplification;
2166
- }
2167
- return input;
2168
- };
2169
-
2170
- const simplifyNotIn = simplifyInput => input => {
2171
- const [, ...operands] = input;
2172
- const [left, right] = operands;
2173
- const leftArray = Array.isArray(left);
2174
- const rightArray = Array.isArray(right);
2175
- if (isNull(left) || isUndefined(left) || isNull(right) || isUndefined(right) || leftArray && rightArray || !leftArray && !rightArray) {
2176
- return true;
2177
- }
2178
- if (leftArray) {
2179
- // If any operand is still an Evaluable, we cannot simplify further
2180
- const rightSimplified = simplifyInput(right);
2181
- if (isEvaluable(rightSimplified)) {
2182
- return input;
2183
- }
2184
- const leftSimplified = left.map(simplifyInput);
2185
- if (leftSimplified.some(isEvaluable)) {
2186
- return input;
2187
- }
2188
- return leftSimplified.indexOf(rightSimplified) === -1;
2189
- }
2190
- if (rightArray) {
2191
- const leftSimplified = simplifyInput(left);
2192
- if (isEvaluable(leftSimplified)) {
2193
- return input;
2194
- }
2195
- const rightSimplified = right.map(simplifyInput);
2196
- if (rightSimplified.some(isEvaluable)) {
2197
- return input;
2198
- }
2199
- return rightSimplified.indexOf(leftSimplified) === -1;
2200
- }
2201
- return input;
2202
- };
2203
-
2204
- const simplifyOr = simplifyInput => input => {
2205
- const [operator, ...operands] = input;
2206
- const simplifiedOperands = [];
2207
- for (const operand of operands) {
2208
- const simplification = simplifyInput(operand);
2209
- if (isBoolean(simplification) && isTrueResult(simplification)) {
2210
- // Short-circuit for OR
2211
- return true;
2212
- } else if (simplification) {
2213
- simplifiedOperands.push(simplification);
2214
- }
2215
- }
2216
- const simplified = simplifiedOperands.filter(isNonFalseResult);
2217
- if (simplified.length === 0) {
2218
- return false;
2219
- }
2220
- if (simplified.length === 1) {
2221
- return simplified[0];
2222
- }
2223
- return [operator, ...simplified];
2224
- };
2225
-
2226
- const simplifyOverlap = simplifyInput => input => {
2227
- const [, ...operands] = input;
2228
- const [left, right] = operands;
2229
- const leftSimplified = simplifyInput(left);
2230
- const rightSimplified = simplifyInput(right);
2231
- const isLeftEvaluable = isEvaluable(leftSimplified);
2232
- const isRightEvaluable = isEvaluable(rightSimplified);
2233
-
2234
- // If we don't have all operands simplified, we can try the positive case
2235
- // of the values we have satisfying the OVERLAP. But if not, we need to
2236
- // return the original input.
2237
- if (isLeftEvaluable || isRightEvaluable) {
2238
- const leftValues = extractValues(leftSimplified);
2239
- const rightValues = extractValues(rightSimplified);
2240
- const rightSet = new Set(rightValues);
2241
- const res = leftValues.some(element => rightSet.has(element));
2242
- if (res) {
2243
- return true;
2244
- }
2245
- return input;
2246
- }
2247
-
2248
- // If simplified results are not arrays, it means we had a strictKey
2249
- // without values provided. Simplify to false.
2250
- if (!Array.isArray(leftSimplified) || !Array.isArray(rightSimplified)) {
2251
- return false;
2252
- }
2253
- const rightSet = new Set(rightSimplified);
2254
- const res = leftSimplified.some(element => rightSet.has(element));
2255
- if (res) {
2256
- return true;
2257
- }
2258
- return false;
2259
- };
2260
-
2261
- const simplify$1 = (opts, simplifyInput, startsWith = true) => input => {
2262
- const [operator, ...operands] = input;
2263
- const [left, right] = operands;
2264
- const leftSimplified = simplifyInput(left);
2265
- const rightSimplified = simplifyInput(right);
2266
- const isLeftEvaluable = isEvaluable(leftSimplified);
2267
- const isRightEvaluable = isEvaluable(rightSimplified);
2268
- if (isLeftEvaluable || isRightEvaluable) {
2269
- // If either left or right is an array, we cannot simplify further
2270
- return [operator, isLeftEvaluable ? leftSimplified.serialize(opts) : left, isRightEvaluable ? rightSimplified.serialize(opts) : right];
2271
- }
2272
- if (isString(leftSimplified) && isString(rightSimplified)) {
2273
- return startsWith ? rightSimplified.startsWith(leftSimplified) : leftSimplified.endsWith(rightSimplified);
2274
- }
2275
- return false;
2276
- };
2277
- const simplifyPrefix = (opts, simplifyInput) => input => simplify$1(opts, simplifyInput)(input);
2278
- const simplifySuffix = (opts, simplifyInput) => input => simplify$1(opts, simplifyInput, false)(input);
2279
-
2280
- const simplify = (simplifyInput, predicate, present = true) => input => {
2281
- const [, ...operands] = input;
2282
- const [operand] = operands;
2283
- const simplified = simplifyInput(operand);
2284
- const isOperandEvaluable = isEvaluable(simplified);
2285
- if (isOperandEvaluable) {
2286
- return input;
2287
- }
2288
-
2289
- // Operand simplifies to itself when it is included in strictKeys or
2290
- // optionalKeys, thus it was undefined.
2291
- if (operand === simplified) {
2292
- return !present;
2293
- }
2294
- return predicate(simplified);
2295
- };
2296
- const simplifyPresent = simplifyInput => input => simplify(simplifyInput, input => !isUndefined(input) && !isNull(input))(input);
2297
- const simplifyUndefined = simplifyInput => input => simplify(simplifyInput, isUndefined, false)(input);
2298
-
2299
- const simplifyXor = (opts, simplifyInput) => input => {
2300
- const [operator, ...operands] = input;
2301
- let trueCount = 0;
2302
- const simplifiedOperands = [];
2303
- for (const operand of operands) {
2304
- const simplification = simplifyInput(operand);
2305
- if (isBoolean(simplification) && isTrueResult(simplification)) {
2306
- trueCount++;
2307
- continue;
2308
- }
2309
- if (isBoolean(simplification) && isFalseResult(simplification)) {
2310
- continue;
2311
- }
2312
- if (!isEvaluable(simplification)) {
2313
- simplifiedOperands.push(simplification);
2314
- }
2315
- }
2316
- if (trueCount > 1) {
2317
- return false;
2318
- }
2319
- if (simplifiedOperands.length === 0) {
2320
- return trueCount === 1;
2321
- }
2322
- if (simplifiedOperands.length === 1) {
2323
- if (trueCount === 1) {
2324
- var _opts$operatorMapping;
2325
- return [(_opts$operatorMapping = opts.operatorMapping.get(OPERATOR$3)) !== null && _opts$operatorMapping !== void 0 ? _opts$operatorMapping : 'NOT', simplifiedOperands[0]];
2326
- }
2327
- return simplifiedOperands[0];
2328
- }
2329
- if (trueCount === 1) {
2330
- var _opts$operatorMapping2;
2331
- return [(_opts$operatorMapping2 = opts.operatorMapping.get(OPERATOR$2)) !== null && _opts$operatorMapping2 !== void 0 ? _opts$operatorMapping2 : 'NOR', ...simplifiedOperands];
2332
- }
2333
- return [operator, ...simplifiedOperands];
2334
- };
2335
-
2336
- const unsafeSimplify = (context, opts, strictKeys, optionalKeys) => {
2337
- const simplifyInput = input => {
2338
- // Value or Reference
2339
- if (!Array.isArray(input)) {
2340
- // Reference
2341
- if (isString(input) && opts.referencePredicate(input)) {
2342
- const result = new Reference(opts.referenceTransform(input)).simplify(context, strictKeys, optionalKeys);
2343
- if (isEvaluable(result)) {
2344
- return result;
2345
- }
2346
- const inputResult = resultToInput(result);
2347
- if (!isUndefined(inputResult)) {
2348
- return inputResult;
2349
- }
2350
- // It should have been a boolean or an Evaluable, but just in case
2351
- // fallback to returning the input.
2352
- }
2353
- // Value
2354
- return input;
2355
- }
2356
- const [operator] = input;
2357
- switch (operator) {
2358
- // Logical operators
2359
- case opts.operatorMapping.get(OPERATOR$4):
2360
- {
2361
- return simplifyAnd(simplifyInput)(input);
2362
- }
2363
- case opts.operatorMapping.get(OPERATOR$1):
2364
- {
2365
- return simplifyOr(simplifyInput)(input);
2366
- }
2367
- case opts.operatorMapping.get(OPERATOR$2):
2368
- {
2369
- return simplifyNor(opts, simplifyInput)(input);
2370
- }
2371
- case opts.operatorMapping.get(OPERATOR):
2372
- {
2373
- return simplifyXor(opts, simplifyInput)(input);
2374
- }
2375
- case opts.operatorMapping.get(OPERATOR$3):
2376
- {
2377
- return simplifyNot(simplifyInput)(input);
2378
- }
2379
- // Comparison operators
2380
- case opts.operatorMapping.get(OPERATOR$h):
2381
- {
2382
- return simplifyEq(opts, simplifyInput)(input);
2383
- }
2384
- case opts.operatorMapping.get(OPERATOR$b):
2385
- {
2386
- return simplifyNe(opts, simplifyInput)(input);
2387
- }
2388
- case opts.operatorMapping.get(OPERATOR$f):
2389
- {
2390
- return simplifyGt(opts, simplifyInput)(input);
2391
- }
2392
- case opts.operatorMapping.get(OPERATOR$g):
2393
- {
2394
- return simplifyGe(opts, simplifyInput)(input);
2395
- }
2396
- case opts.operatorMapping.get(OPERATOR$c):
2397
- {
2398
- return simplifyLt(opts, simplifyInput)(input);
2399
- }
2400
- case opts.operatorMapping.get(OPERATOR$d):
2401
- {
2402
- return simplifyLe(opts, simplifyInput)(input);
2403
- }
2404
- case opts.operatorMapping.get(OPERATOR$e):
2405
- {
2406
- return simplifyIn(simplifyInput)(input);
2407
- }
2408
- case opts.operatorMapping.get(OPERATOR$a):
2409
- {
2410
- return simplifyNotIn(simplifyInput)(input);
2411
- }
2412
- case opts.operatorMapping.get(OPERATOR$8):
2413
- {
2414
- return simplifyPrefix(opts, simplifyInput)(input);
2415
- }
2416
- case opts.operatorMapping.get(OPERATOR$6):
2417
- {
2418
- return simplifySuffix(opts, simplifyInput)(input);
2419
- }
2420
- case opts.operatorMapping.get(OPERATOR$9):
2421
- {
2422
- return simplifyOverlap(simplifyInput)(input);
2423
- }
2424
- case opts.operatorMapping.get(OPERATOR$5):
2425
- {
2426
- return simplifyUndefined(simplifyInput)(input);
2427
- }
2428
- case opts.operatorMapping.get(OPERATOR$7):
2429
- {
2430
- return simplifyPresent(simplifyInput)(input);
2431
- }
2432
- // Arithmetic operators
2433
- case opts.operatorMapping.get(OPERATOR$i):
2434
- {
2435
- return simplifySum(simplifyInput)(input);
2436
- }
2437
- case opts.operatorMapping.get(OPERATOR$j):
2438
- {
2439
- return simplifySubtract(simplifyInput)(input);
2440
- }
2441
- case opts.operatorMapping.get(OPERATOR$k):
2442
- {
2443
- return simplifyMultiply(simplifyInput)(input);
2444
- }
2445
- case opts.operatorMapping.get(OPERATOR$l):
2446
- {
2447
- return simplifyDivide(simplifyInput)(input);
2448
- }
2449
- default:
2450
- {
2451
- // Handle as an array of References / Values if no operator matches
2452
- const result = input.map(simplifyInput);
2453
- if (!areAllInputs(result)) {
2454
- return new Collection(result.map(item => {
2455
- if (item instanceof Reference) {
2456
- return item;
2457
- }
2458
- if (!isEvaluable(item)) {
2459
- return new Value(item);
2460
- }
2461
- throw new Error('Unexpected expression found within a collection of values/references');
2462
- }));
2463
- }
2464
- return result;
2465
- }
2466
- }
2467
- };
2468
- return simplifyInput;
2469
- };
2470
-
2471
1958
  const unexpectedResultError = 'non expression or boolean result should be returned';
2472
1959
 
2473
1960
  /**
@@ -2524,10 +2011,12 @@ class Engine {
2524
2011
  *
2525
2012
  * @param {ExpressionInput} exp Raw expression.
2526
2013
  * @param {Context} context Evaluation data context.
2527
- * @param {string[]} strictKeys keys to be considered present even if they are not present in the context
2528
- * @param {string[]} optionalKeys keys to be considered not present unless they are in the context or in
2014
+ * @param {string[] | Set<string>} strictKeys keys to be considered present even if they are not present in the
2015
+ * context. Passing as a Set is recommended for performance reasons.
2016
+ * @param {string[] | Set<string>} optionalKeys keys to be considered not present unless they are in the context or in
2529
2017
  * `strictKeys`; when `strictKeys` is `undefined` and `optionalKeys` is an array, every key that is not in
2530
- * `optionalKeys` is considered to be present and thus will be evaluated
2018
+ * `optionalKeys` is considered to be present and thus will be evaluated. Passing as a Set is recommended for
2019
+ * performance reasons.
2531
2020
  * @returns {Inpunt | boolean}
2532
2021
  */
2533
2022
  simplify(exp, context, strictKeys, optionalKeys) {
@@ -2540,18 +2029,6 @@ class Engine {
2540
2029
  }
2541
2030
  throw new Error(unexpectedResultError);
2542
2031
  }
2543
- unsafeSimplify(exp, context, strictKeys, optionalKeys) {
2544
- const result = unsafeSimplify(context, this.parser.options, strictKeys, optionalKeys)(exp);
2545
- if (isBoolean(result)) {
2546
- return result;
2547
- }
2548
-
2549
- // The unsafe implementation should not expose the Evaluable interface
2550
- if (isEvaluable(result)) {
2551
- throw new Error('Unexpected Evaluable not serialized result');
2552
- }
2553
- return result;
2554
- }
2555
2032
  }
2556
2033
 
2557
2034
  exports.OPERATOR_AND = OPERATOR$4;
package/package.json CHANGED
@@ -1,10 +1,17 @@
1
1
  {
2
2
  "name": "@briza/illogical",
3
- "version": "1.7.1",
3
+ "version": "1.7.3",
4
4
  "description": "A micro conditional javascript engine used to parse the raw logical and comparison expressions, evaluate the expression in the given data context, and provide access to a text form of the given expressions.",
5
- "main": "lib/illogical.js",
6
- "module": "lib/illogical.esm.js",
7
- "typings": "types/index.d.ts",
5
+ "main": "./lib/illogical.js",
6
+ "module": "./lib/illogical.esm.js",
7
+ "types": "./types/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./types/index.d.ts",
11
+ "require": "./lib/illogical.js",
12
+ "import": "./lib/illogical.esm.js"
13
+ }
14
+ },
8
15
  "files": [
9
16
  "/lib",
10
17
  "/types",
@@ -42,31 +49,31 @@
42
49
  "rules"
43
50
  ],
44
51
  "devDependencies": {
45
- "@babel/core": "^7.27.3",
52
+ "@babel/core": "^7.28.5",
46
53
  "@babel/plugin-proposal-class-properties": "^7.18.6",
47
- "@babel/preset-env": "^7.25.3",
48
- "@babel/preset-typescript": "^7.24.7",
49
- "@rollup/plugin-babel": "^6.0.4",
50
- "@rollup/plugin-commonjs": "^26.0.1",
51
- "@rollup/plugin-eslint": "^9.0.5",
52
- "@rollup/plugin-node-resolve": "^15.2.3",
53
- "@types/jest": "^29.5.12",
54
- "@typescript-eslint/eslint-plugin": "^8.0.1",
55
- "@typescript-eslint/parser": "^8.0.1",
56
- "eslint": "^8.57.0",
57
- "eslint-config-prettier": "^9.1.0",
58
- "eslint-import-resolver-typescript": "^3.6.1",
59
- "eslint-plugin-import": "^2.29.1",
54
+ "@babel/preset-env": "^7.28.5",
55
+ "@babel/preset-typescript": "^7.28.5",
56
+ "@rollup/plugin-babel": "^6.1.0",
57
+ "@rollup/plugin-commonjs": "^26.0.3",
58
+ "@rollup/plugin-eslint": "^9.1.0",
59
+ "@rollup/plugin-node-resolve": "^15.3.1",
60
+ "@types/jest": "^29.5.14",
61
+ "@typescript-eslint/eslint-plugin": "^8.46.2",
62
+ "@typescript-eslint/parser": "^8.46.2",
63
+ "eslint": "^8.57.1",
64
+ "eslint-config-prettier": "^9.1.2",
65
+ "eslint-import-resolver-typescript": "^3.10.1",
66
+ "eslint-plugin-import": "^2.32.0",
60
67
  "eslint-plugin-node": "^11.1.0",
61
- "eslint-plugin-prettier": "^5.2.1",
62
- "eslint-plugin-promise": "^7.1.0",
68
+ "eslint-plugin-prettier": "^5.5.4",
69
+ "eslint-plugin-promise": "^7.2.1",
63
70
  "eslint-plugin-simple-import-sort": "^12.1.1",
64
71
  "jest": "^29.7.0",
65
72
  "license-checker": "^25.0.1",
66
- "prettier": "^3.3.3",
67
- "rollup": "^4.41.1",
68
- "ts-jest": "^29.2.4",
69
- "typedoc": "^0.26.5",
70
- "typescript": "^5.5.4"
73
+ "prettier": "^3.6.2",
74
+ "rollup": "^4.52.5",
75
+ "ts-jest": "^29.4.5",
76
+ "typedoc": "^0.26.11",
77
+ "typescript": "^5.6.3"
71
78
  }
72
79
  }
package/readme.md CHANGED
@@ -222,19 +222,6 @@ engine.simplify(
222
222
  ) // ['==', '$b', 20]
223
223
  ```
224
224
 
225
- ### Unsafe Simplify
226
-
227
- Simplifies an expression with a given context, but without parsing and ensuring the condition is
228
- syntatically correct. This can be used in situations where you can decouple the parsing from
229
- the simplification and extra performance is needed in runtime.
230
-
231
- ```js
232
- engine.unsafeSimplify(
233
- ['OR', ['==', '$a', 10], ['==', '$b', 20], ['==', '$c', 20]],
234
- { a: 10 }
235
- ) // true due to $a. Expressions for $b and $c won't even be parsed.
236
- ```
237
-
238
225
  ## Working with Expressions
239
226
 
240
227
  ### Evaluation Data Context