From 8b4c13d69a76299f1d6c3126a86e8e673b7f7099 Mon Sep 17 00:00:00 2001 From: Francis Asante Date: Thu, 23 Apr 2020 10:42:21 +0200 Subject: [PATCH] Throw exception for unregistered operators --- CHANGELOG.md | 4 ++++ Makefile | 4 +++- README.md | 6 ++--- src/core.ts | 39 +++++++++++++++++++-------------- src/operators/pipeline/group.ts | 2 +- 5 files changed, 33 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5c49d69b..fcce6a6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 3.0.1 / 2020-04-xx + +- Throw exception for specifying unregistered operators + ## 3.0.0 / 2020-04-12 - Convert project to Typescript diff --git a/Makefile b/Makefile index 16f17cf7..ee975101 100644 --- a/Makefile +++ b/Makefile @@ -9,4 +9,6 @@ clean: @npm run clean test: - @npm run test \ No newline at end of file + @npm run test + +.PHONY: test \ No newline at end of file diff --git a/README.md b/README.md index 95fbd3bf..db70ce36 100644 --- a/README.md +++ b/README.md @@ -119,14 +119,14 @@ The following two examples are equivalent. ```js import { $unwind } from 'mingo/operators/pipeline' - ``` #### ES5 -```js -const $unwind = require('mingo/operators/pipeline').$unwind +Unlike the ES6 version, it is necessary to specify the operator module in the path to avoid loading any extras +```js +const $unwind = require('mingo/operators/pipeline/unwind').$unwind ``` ## Configuration diff --git a/src/core.ts b/src/core.ts index ca4453a9..c80a9046 100644 --- a/src/core.ts +++ b/src/core.ts @@ -216,25 +216,30 @@ export function computeValue(obj: object | any[], expr: any, operator: string, o options = options || { config: null } options.config = options.config || createConfig() - // if the field of the object is a valid operator - let call = getOperator(OperatorType.EXPRESSION, operator) - if (call) return call(obj, expr, options) - - // we also handle $group accumulator operators - call = getOperator(OperatorType.ACCUMULATOR, operator) - if (call) { - - // if object is not an array, first try to compute using the expression - if (!isArray(obj)) { - obj = computeValue(obj, expr, null, options) - expr = null - } + if (isOperator(operator)) { + // if the field of the object is a valid operator + let call = getOperator(OperatorType.EXPRESSION, operator) + if (call) return call(obj, expr, options) + + // we also handle $group accumulator operators + call = getOperator(OperatorType.ACCUMULATOR, operator) + if (call) { + + // if object is not an array, first try to compute using the expression + if (!isArray(obj)) { + obj = computeValue(obj, expr, null, options) + expr = null + } - // validate that we have an array - assert(isArray(obj), `${operator} target must be an array.`) + // validate that we have an array + assert(isArray(obj), `'${operator}' target must be an array.`) + + // we pass a null expression because all values have been resolved + return call(obj, expr, options) + } - // we pass a null expression because all values have been resolved - return call(obj, expr, options) + // operator was not found + throw new Error(`operator '${operator}' is not registered`) } // if expr is a variable for an object field diff --git a/src/operators/pipeline/group.ts b/src/operators/pipeline/group.ts index f7a818a0..489746c3 100644 --- a/src/operators/pipeline/group.ts +++ b/src/operators/pipeline/group.ts @@ -18,7 +18,7 @@ export function $group(collection: Iterator, expr: any, options: Options): Itera let id = expr[ID_KEY] return collection.transform(coll => { - let partitions = groupBy(coll, obj => computeValue(obj, id, id, options)) + let partitions = groupBy(coll, obj => computeValue(obj, id, null, options)) // remove the group key expr = into({}, expr)