@briza/illogical 1.6.0 → 1.6.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/changelog.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # illogical changelog
2
2
 
3
+ ## 1.6.1
4
+
5
+ - Bugfix on Arithmetic evaluate. Returns false when ContextValue is not present in the Context.
6
+
3
7
  ## 1.6.0
4
8
 
5
9
  - Added support for Arithmetic expressions within other Comparison expressions. This allows for
@@ -135,6 +135,27 @@ class Arithmetic {
135
135
  _defineProperty(this, "type", EvaluableType.Expression);
136
136
  }
137
137
 
138
+ /**
139
+ * Helper function to assist with arithmetic evaluation. Ensures that all
140
+ * operands are present and are numbers. Throws error if any operand is not a
141
+ * number.
142
+ *
143
+ * @param {Result[]} results
144
+ * @returns {number[] | false} false if any operand is missing, otherwise the
145
+ * array of numbers
146
+ */
147
+ getResultValues(results) {
148
+ const presentValues = results.filter(result => result !== null && result !== undefined);
149
+ // If we have missing context values the result must be false
150
+ if (presentValues.length !== results.length) {
151
+ return false;
152
+ }
153
+ if (!areAllNumbers(presentValues)) {
154
+ throw new Error(`operands must be numbers for ${this.constructor.name}`);
155
+ }
156
+ return presentValues;
157
+ }
158
+
138
159
  /**
139
160
  * Performs the arithmetic operation on the operands evaluated values.
140
161
  * @param {Result[]} results Operand result values.
@@ -204,11 +225,11 @@ class Divide extends Arithmetic {
204
225
  super('/', OPERATOR$l, operands);
205
226
  }
206
227
  operate(results) {
207
- if (!areAllNumbers(results)) {
208
- throw new Error('operands must be numbers for divide');
228
+ const presentResults = this.getResultValues(results);
229
+ if (presentResults === false) {
230
+ return false;
209
231
  }
210
- const result = results.reduce((acc, result) => acc / result);
211
- return result;
232
+ return presentResults.reduce((acc, result) => acc / result);
212
233
  }
213
234
  }
214
235
 
@@ -247,10 +268,11 @@ class Multiply extends Arithmetic {
247
268
  super('*', OPERATOR$k, operands);
248
269
  }
249
270
  operate(results) {
250
- if (!areAllNumbers(results)) {
251
- throw new Error('operands must be numbers for multiply');
271
+ const presentResults = this.getResultValues(results);
272
+ if (presentResults === false) {
273
+ return false;
252
274
  }
253
- return results.reduce((acc, result) => multiplyWithExpectedDecimals(acc, result));
275
+ return presentResults.reduce((acc, result) => multiplyWithExpectedDecimals(acc, result));
254
276
  }
255
277
  }
256
278
 
@@ -278,10 +300,11 @@ class Subtract extends Arithmetic {
278
300
  super('-', OPERATOR$j, operands);
279
301
  }
280
302
  operate(results) {
281
- if (!areAllNumbers(results)) {
282
- throw new Error('operands must be numbers for subtract');
303
+ const presentResults = this.getResultValues(results);
304
+ if (presentResults === false) {
305
+ return false;
283
306
  }
284
- return results.reduce((acc, result) => subtractWithExpectedDecimals(acc, result));
307
+ return presentResults.reduce((acc, result) => subtractWithExpectedDecimals(acc, result));
285
308
  }
286
309
  }
287
310
 
@@ -309,10 +332,11 @@ class Sum extends Arithmetic {
309
332
  super('+', OPERATOR$i, operands);
310
333
  }
311
334
  operate(results) {
312
- if (!areAllNumbers(results)) {
313
- throw new Error('operands must be numbers for sum');
335
+ const presentResults = this.getResultValues(results);
336
+ if (presentResults === false) {
337
+ return false;
314
338
  }
315
- return results.reduce((acc, result) => addWithExpectedDecimals(acc, result));
339
+ return presentResults.reduce((acc, result) => addWithExpectedDecimals(acc, result));
316
340
  }
317
341
  }
318
342
 
package/lib/illogical.js CHANGED
@@ -139,6 +139,27 @@ class Arithmetic {
139
139
  _defineProperty(this, "type", EvaluableType.Expression);
140
140
  }
141
141
 
142
+ /**
143
+ * Helper function to assist with arithmetic evaluation. Ensures that all
144
+ * operands are present and are numbers. Throws error if any operand is not a
145
+ * number.
146
+ *
147
+ * @param {Result[]} results
148
+ * @returns {number[] | false} false if any operand is missing, otherwise the
149
+ * array of numbers
150
+ */
151
+ getResultValues(results) {
152
+ const presentValues = results.filter(result => result !== null && result !== undefined);
153
+ // If we have missing context values the result must be false
154
+ if (presentValues.length !== results.length) {
155
+ return false;
156
+ }
157
+ if (!areAllNumbers(presentValues)) {
158
+ throw new Error(`operands must be numbers for ${this.constructor.name}`);
159
+ }
160
+ return presentValues;
161
+ }
162
+
142
163
  /**
143
164
  * Performs the arithmetic operation on the operands evaluated values.
144
165
  * @param {Result[]} results Operand result values.
@@ -208,11 +229,11 @@ class Divide extends Arithmetic {
208
229
  super('/', OPERATOR$l, operands);
209
230
  }
210
231
  operate(results) {
211
- if (!areAllNumbers(results)) {
212
- throw new Error('operands must be numbers for divide');
232
+ const presentResults = this.getResultValues(results);
233
+ if (presentResults === false) {
234
+ return false;
213
235
  }
214
- const result = results.reduce((acc, result) => acc / result);
215
- return result;
236
+ return presentResults.reduce((acc, result) => acc / result);
216
237
  }
217
238
  }
218
239
 
@@ -251,10 +272,11 @@ class Multiply extends Arithmetic {
251
272
  super('*', OPERATOR$k, operands);
252
273
  }
253
274
  operate(results) {
254
- if (!areAllNumbers(results)) {
255
- throw new Error('operands must be numbers for multiply');
275
+ const presentResults = this.getResultValues(results);
276
+ if (presentResults === false) {
277
+ return false;
256
278
  }
257
- return results.reduce((acc, result) => multiplyWithExpectedDecimals(acc, result));
279
+ return presentResults.reduce((acc, result) => multiplyWithExpectedDecimals(acc, result));
258
280
  }
259
281
  }
260
282
 
@@ -282,10 +304,11 @@ class Subtract extends Arithmetic {
282
304
  super('-', OPERATOR$j, operands);
283
305
  }
284
306
  operate(results) {
285
- if (!areAllNumbers(results)) {
286
- throw new Error('operands must be numbers for subtract');
307
+ const presentResults = this.getResultValues(results);
308
+ if (presentResults === false) {
309
+ return false;
287
310
  }
288
- return results.reduce((acc, result) => subtractWithExpectedDecimals(acc, result));
311
+ return presentResults.reduce((acc, result) => subtractWithExpectedDecimals(acc, result));
289
312
  }
290
313
  }
291
314
 
@@ -313,10 +336,11 @@ class Sum extends Arithmetic {
313
336
  super('+', OPERATOR$i, operands);
314
337
  }
315
338
  operate(results) {
316
- if (!areAllNumbers(results)) {
317
- throw new Error('operands must be numbers for sum');
339
+ const presentResults = this.getResultValues(results);
340
+ if (presentResults === false) {
341
+ return false;
318
342
  }
319
- return results.reduce((acc, result) => addWithExpectedDecimals(acc, result));
343
+ return presentResults.reduce((acc, result) => addWithExpectedDecimals(acc, result));
320
344
  }
321
345
  }
322
346
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@briza/illogical",
3
- "version": "1.6.0",
3
+ "version": "1.6.1",
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
5
  "main": "lib/illogical.js",
6
6
  "module": "lib/illogical.esm.js",
@@ -29,7 +29,7 @@
29
29
  },
30
30
  "repository": {
31
31
  "type": "git",
32
- "url": "git@github.com:briza-insurance/illogical.git"
32
+ "url": "git+ssh://git@github.com/briza-insurance/illogical.git"
33
33
  },
34
34
  "bugs": {
35
35
  "url": "https://github.com/briza-insurance/illogical/issues"
@@ -17,6 +17,16 @@ export declare abstract class Arithmetic implements Evaluable {
17
17
  * @param {Operand[]} operands Operands.
18
18
  */
19
19
  constructor(operator: string, operatorSymbol: symbol, operands: Operand[]);
20
+ /**
21
+ * Helper function to assist with arithmetic evaluation. Ensures that all
22
+ * operands are present and are numbers. Throws error if any operand is not a
23
+ * number.
24
+ *
25
+ * @param {Result[]} results
26
+ * @returns {number[] | false} false if any operand is missing, otherwise the
27
+ * array of numbers
28
+ */
29
+ protected getResultValues(results: Result[]): number[] | false;
20
30
  /**
21
31
  * Performs the arithmetic operation on the operands evaluated values.
22
32
  * @param {Result[]} results Operand result values.