Skip to content

Commit

Permalink
Fixes, new operators, code coverage, and deprecations
Browse files Browse the repository at this point in the history
* Add downloads badge
* Add $strLenBytes, $strLenCP, $substrCP, $substrBytes
* Fix $indexOfBytes
* Fix $stdDevSamp
* Fix $in to handle arguments correctly and use aggregation semantics
* Fix string operators.
* Add code coverage tool
* Remove max and min cursor methods
* Restrict custom query operator type `OP_QUERY` to return boolean only
* Remove contributors file
* Add istanbul plugin to babelrc
  • Loading branch information
kofrasa committed Aug 10, 2017
1 parent 824f372 commit 86b13fd
Show file tree
Hide file tree
Showing 59 changed files with 2,243 additions and 2,847 deletions.
7 changes: 6 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@
],
"plugins": [
"external-helpers"
]
],
"env": {
"test": {
"plugins": ["istanbul"]
}
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.*
coverage/
dist/
node_modules
npm-debug.log
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.*
node_modules/
coverage/
coverage.lcov
coverage
2 changes: 0 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ Changelog
## 1.3.3 / 2017-08-02
- Fix `computeValue` not overriding group operator keys after resolving expression
- Added `$in`, `$objectToArray`, and `$arrayToObject` array aggregation operators
- Minor refactoring

## 1.3.2 / 2017-07-28
- Restore `setup` function. https://github.com/kofrasa/mingo/issues/56
Expand All @@ -30,7 +29,6 @@ Changelog
## v1.1.2 / 2017-03-30
- Optimize `$lookup` implementation
- Avoid reversing original input to `$reverseArray`
- Refactor some methods

## v1.1.1 / 2017-03-12
- Fix incorrect method call for ObjectProto
Expand Down
2 changes: 0 additions & 2 deletions CONTRIBUTORS.md

This file was deleted.

15 changes: 5 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,7 @@ MODULE = mingo
VERSION = $(shell cat VERSION)
YEAR = $(shell date +%Y)
BANNER = templates/header.txt

# tools
NODE_MODULES = ./node_modules
ROLLUP = ${NODE_MODULES}/rollup/bin/rollup
UGLIFY = ${NODE_MODULES}/uglify-js/bin/uglifyjs

TEST_FILES = $(shell find test -name "*.js")

all: clean test build

Expand All @@ -19,7 +14,7 @@ build: prepare build.es6 compress bower.json package.json


build.es6:
@${ROLLUP} -c config/rollup.es.js
@node_modules/.bin/rollup -c config/rollup.es.js


prepare:
Expand All @@ -28,7 +23,7 @@ prepare:

compress: mingo.js
@cat ${BANNER} | sed "s/@VERSION/${VERSION}/" | sed "s/@YEAR/${YEAR}/" > dist/${MODULE}.min.js
@${UGLIFY} dist/${MODULE}.js --compress --mangle --source-map dist/${MODULE}.map >> dist/${MODULE}.min.js
@node_modules/.bin/uglifyjs dist/${MODULE}.js --compress --mangle --source-map dist/${MODULE}.min.map >> dist/${MODULE}.min.js
@gzip -kf dist/${MODULE}.min.js


Expand All @@ -39,11 +34,11 @@ clean:


mingo.js:
@${ROLLUP} -c config/rollup.umd.js
@node_modules/.bin/rollup -c config/rollup.umd.js


test: mingo.js
@tape test/*.js
@node_modules/.bin/nyc --reporter=lcov --reporter=text node_modules/.bin/tape ${TEST_FILES}


%.json: templates/%.json.txt
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ JavaScript implementation of MongoDB query language

[![version](https://img.shields.io/npm/v/mingo.svg)](https://www.npmjs.org/package/mingo)
[![build status](https://secure.travis-ci.org/kofrasa/mingo.png)](http://travis-ci.org/kofrasa/mingo)
[![npm](https://img.shields.io/npm/dt/mingo.svg)]()

## Install
```$ npm install mingo```
Expand Down Expand Up @@ -51,7 +52,7 @@ var mingo = require('mingo')
```

For the browser
```
```html
// minified UMD module
<script type="text/javascript" src="./dist/mingo.min.js"></script>

Expand All @@ -65,9 +66,10 @@ Tiny configuration if needed
mingo.setup({
key: '_id' // default
});

```

## Using query object to test objects
```js
// create a query with criteria
// find all grades for homework with score >= 50
let query = new mingo.Query({
Expand Down
2 changes: 1 addition & 1 deletion lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ export const OP_AGGREGATE = 'aggregate'
export const OP_GROUP = 'group'
export const OP_PIPELINE = 'pipeline'
export const OP_PROJECTION = 'projection'
export const OP_QUERY = 'query'
export const OP_QUERY = 'query'
18 changes: 0 additions & 18 deletions lib/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,24 +133,6 @@ export class Cursor {
return this.count() > this.__position
}

/**
* Specifies the exclusive upper bound for a specific field
* @param expr
* @returns {Number}
*/
max (expr) {
return groupOperators.$max(this._fetch(), expr)
}

/**
* Specifies the inclusive lower bound for a specific field
* @param expr
* @returns {Number}
*/
min (expr) {
return groupOperators.$min(this._fetch(), expr)
}

/**
* Applies a function to each document in a cursor and collects the return values in an array.
* @param callback
Expand Down
1 change: 0 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import './polyfill'
import { OP_AGGREGATE, OP_GROUP, OP_PIPELINE, OP_PROJECTION, OP_QUERY } from './constants'
import { _internal, setup } from './internal'
import { Query, find, remove } from './query'
Expand Down
21 changes: 10 additions & 11 deletions lib/internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,6 @@ export function resolve (obj, selector, deepFlag = false) {
* @param selector {String} dot separated path to field
*/
export function resolveObj (obj, selector) {
if (isNil(obj)) return

let names = selector.split('.')
let key = names[0]
// get the next part of the selector
Expand Down Expand Up @@ -430,7 +428,7 @@ export function slice (xs, skip, limit = null) {
assert(limit > 0, 'Invalid argument value for $slice operator. Limit must be a positive number')
limit += skip
}
return Array.prototype.slice.apply(xs, [skip, limit])
return xs.slice(skip, limit)
}

/**
Expand All @@ -441,9 +439,9 @@ export function slice (xs, skip, limit = null) {
export function stddev (ctx) {
let sum = reduce(ctx.data, (acc, n) => acc + n, 0)
let N = ctx.data.length || 1
let correction = ctx.sampled === true ? 1 : 0
let avg = sum / (N - correction)
return Math.sqrt(reduce(ctx.data, (acc, n) => acc + Math.pow(n - avg, 2), 0) / N)
let correction = (ctx.sampled && 1) || 0
let avg = sum / N
return Math.sqrt(reduce(ctx.data, (acc, n) => acc + Math.pow(n - avg, 2), 0) / (N - correction))
}

/**
Expand All @@ -467,16 +465,15 @@ export function redactObj (obj, expr, opt = {}) {
*/
export function _internal () {
return {
computeValue,
idKey,
ops,
resolve,
assert,
computeValue,
clone,
each,
err,
getType,
has,
idKey,
includes: inArray.bind(null),
isArray,
isBoolean,
isDate,
Expand All @@ -491,6 +488,8 @@ export function _internal () {
isString,
isUndefined,
keys,
map
map,
ops,
resolve
}
}
25 changes: 9 additions & 16 deletions lib/operators/aggregation/arithmetic.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const arithmeticOperators = {
/**
* Returns the absolute value of a number.
* https://docs.mongodb.com/manual/reference/operator/aggregation/abs/#exp._S_abs
*
* @param obj
* @param expr
* @return {Number|null|NaN}
Expand Down Expand Up @@ -36,9 +37,8 @@ export const arithmeticOperators = {
*/
$ceil (obj, expr) {
let arg = computeValue(obj, expr)
if (isNaN(arg)) return NaN
if (isNil(arg)) return null
assert(isNumber(arg), '$ceil must be a valid expression that resolves to a number.')
assert(isNumber(arg) || isNaN(arg), '$ceil must be a valid expression that resolves to a number.')
return Math.ceil(arg)
},

Expand All @@ -63,9 +63,8 @@ export const arithmeticOperators = {
*/
$exp (obj, expr) {
let arg = computeValue(obj, expr)
if (isNaN(arg)) return NaN
if (isNil(arg)) return null
assert(isNumber(arg), '$exp must be a valid expression that resolves to a number.')
assert(isNumber(arg) || isNaN(arg), '$exp must be a valid expression that resolves to a number.')
return Math.exp(arg)
},

Expand All @@ -78,9 +77,8 @@ export const arithmeticOperators = {
*/
$floor (obj, expr) {
let arg = computeValue(obj, expr)
if (isNaN(arg)) return NaN
if (isNil(arg)) return null
assert(isNumber(arg), '$floor must be a valid expression that resolves to a number.')
assert(isNumber(arg) || isNaN(arg), '$floor must be a valid expression that resolves to a number.')
return Math.floor(arg)
},

Expand All @@ -93,9 +91,8 @@ export const arithmeticOperators = {
*/
$ln (obj, expr) {
let arg = computeValue(obj, expr)
if (isNaN(arg)) return NaN
if (isNil(arg)) return null
assert(isNumber(arg), '$ln must be a valid expression that resolves to a number.')
assert(isNumber(arg) || isNaN(arg), '$ln must be a valid expression that resolves to a number.')
return Math.log(arg)
},

Expand All @@ -109,9 +106,8 @@ export const arithmeticOperators = {
$log (obj, expr) {
let args = computeValue(obj, expr)
assert(isArray(args) && args.length === 2, '$log must be a valid expression that resolves to an array of 2 items')
if (args.some(isNaN)) return NaN
if (args.some(isNil)) return null
assert(args.every(isNumber), '$log expression must resolve to array of 2 numbers')
assert(args.some(isNaN) || args.every(isNumber), '$log expression must resolve to array of 2 numbers')
return Math.log10(args[0]) / Math.log10(args[1])
},

Expand All @@ -124,9 +120,8 @@ export const arithmeticOperators = {
*/
$log10 (obj, expr) {
let arg = computeValue(obj, expr)
if (isNaN(arg)) return NaN
if (isNil(arg)) return null
assert(isNumber(arg), '$log10 must be a valid expression that resolves to a number.')
assert(isNumber(arg) || isNaN(arg), '$log10 must be a valid expression that resolves to a number.')
return Math.log10(arg)
},

Expand Down Expand Up @@ -179,9 +174,8 @@ export const arithmeticOperators = {
*/
$sqrt (obj, expr) {
let n = computeValue(obj, expr)
if (isNaN(n)) return NaN
if (isNil(n)) return null
assert(isNumber(n) && n > 0, '$sqrt expression must resolve to non-negative number.')
assert(isNumber(n) && n > 0 || isNaN(n), '$sqrt expression must resolve to non-negative number.')
return Math.sqrt(n)
},

Expand All @@ -206,9 +200,8 @@ export const arithmeticOperators = {
*/
$trunc (obj, expr) {
let n = computeValue(obj, expr)
if (isNaN(n)) return NaN
if (isNil(n)) return null
assert(isNumber(n) && n > 0, '$trunc must be a valid expression that resolves to a non-negative number.')
assert(isNumber(n) || isNaN(n), '$trunc expression must resolve to a number.')
return Math.trunc(n)
}
}
Loading

0 comments on commit 86b13fd

Please sign in to comment.