Skip to content

Commit

Permalink
Throw exception for unregistered operators
Browse files Browse the repository at this point in the history
  • Loading branch information
kofrasa committed Apr 23, 2020
1 parent b2d1f36 commit 8b4c13d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ clean:
@npm run clean

test:
@npm run test
@npm run test

.PHONY: test
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 22 additions & 17 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/operators/pipeline/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8b4c13d

Please sign in to comment.