Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mingo

Package Overview
Dependencies
Maintainers
1
Versions
140
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mingo - npm Package Compare versions

Comparing version 0.2.4 to 0.2.5

2

mingo-min.js

@@ -1,1 +0,1 @@

(function(){"use strict";function normalize(expr){for(var i=0;i<primitives.length;i++)if(primitives[i](expr))return _.isRegExp(expr)?{$regex:expr}:{$eq:expr};if(_.isObject(expr)){var keys=_.keys(expr),notQuery=0===_.intersection(Ops.queryOperators,keys).length;if(notQuery)return{$eq:expr};if(_.contains(keys,"$regex")){var regex=expr.$regex,options=expr.$options||"",modifiers="";_.isString(regex)&&(modifiers+=regex.ignoreCase||options.indexOf("i")>=0?"i":"",modifiers+=regex.multiline||options.indexOf("m")>=0?"m":"",modifiers+=regex.global||options.indexOf("g")>=0?"g":"",regex=new RegExp(regex,modifiers)),expr.$regex=regex,delete expr.$options}}return expr}var previousMingo,_,root=this,Mingo={};null!=root&&(previousMingo=root.Mingo),Mingo.noConflict=function(){return root.Mingo=previousMingo,Mingo};var nodeEnabled="undefined"!=typeof exports&&"undefined"!=typeof module&&"undefined"!=typeof require&&"undefined"!=typeof process;"undefined"!=typeof exports?(exports="undefined"!=typeof module&&module.exports?module.exports=Mingo:Mingo,_=require("underscore")):(root.Mingo=Mingo,_=root._);var primitives=[_.isString,_.isBoolean,_.isNumber,_.isDate,_.isNull,_.isRegExp],settings={key:"_id"};if(Mingo.setup=function(options){_.extend(settings,options||{})},Mingo.Query=function(criteria,projection){return this instanceof Mingo.Query?(this._criteria=criteria,this._projection=projection,this._compiled=[],this._compile(),void 0):new Mingo.Query(criteria,projection)},Mingo.Query.prototype={_compile:function(){if(!_.isEmpty(this._criteria)){if(_.isArray(this._criteria)||_.isFunction(this._criteria)||!_.isObject(this._criteria))throw new Error("Invalid type for criteria");for(var field in this._criteria)if(this._criteria.hasOwnProperty(field)){var expr=this._criteria[field];if(_.contains(Ops.compoundOperators,field)){if(_.contains(["$not"],field))throw new Error("Invalid operator");this._processOperator(field,field,expr)}else{expr=normalize(expr);for(var op in expr)expr.hasOwnProperty(op)&&this._processOperator(field,op,expr[op])}}}},_processOperator:function(field,operator,value){var compiledSelector;if(_.contains(Ops.simpleOperators,operator))compiledSelector={test:function(obj){var actualValue=Mingo._resolve(obj,field);return simpleOperators[operator](actualValue,value)}};else{if(!_.contains(Ops.compoundOperators,operator))throw new Error("Invalid query operator '"+operator+"' detected");compiledSelector=compoundOperators[operator](field,value)}this._compiled.push(compiledSelector)},test:function(obj){for(var i=0;i<this._compiled.length;i++)if(!this._compiled[i].test(obj))return!1;return!0},find:function(collection,projection){return new Mingo.Cursor(collection,this,projection)},remove:function(collection){for(var arr=[],i=0;i<collection.length;i++)this.test(collection[i])||arr.push(collection[i]);return arr}},nodeEnabled){var Transform=require("stream").Transform,util=require("util");Mingo.Query.prototype.stream=function(options){return new Mingo.Stream(this,options)},Mingo.Stream=function(query,options){return this instanceof Mingo.Stream?(options=options||{},_.extend(options,{objectMode:!0}),Transform.call(this,options),this._query=query,void 0):new Mingo.Stream(query,options)},util.inherits(Mingo.Stream,Transform),Mingo.Stream.prototype._transform=function(chunk,encoding,done){if(_.isObject(chunk)&&this._query.test(chunk))if(_.isEmpty(this._query._projection))this.push(chunk);else{var cursor=new Mingo.Cursor([chunk],this._query);cursor.hasNext()&&this.push(cursor.next())}done()}}Mingo.Cursor=function(collection,query,projection){return this instanceof Mingo.Cursor?(this._query=query,this._collection=collection,this._projection=projection||query._projection,this._operators={},this._result=!1,this._position=0,void 0):new Mingo.Cursor(collection,query,projection)},Mingo.Cursor.prototype={_fetch:function(){var self=this;if(this._result!==!1)return this._result;if(_.isObject(this._projection)&&_.extend(this._operators,{$project:this._projection}),!_.isArray(this._collection))throw new Error("Input collection is not of a valid type.");this._result=_.filter(this._collection,this._query.test,this._query);var pipeline=[];if(_.each(["$sort","$skip","$limit","$project"],function(op){_.has(self._operators,op)&&pipeline.push(_.pick(self._operators,op))}),pipeline.length>0){var aggregator=new Mingo.Aggregator(pipeline);this._result=aggregator.run(this._result,this._query)}return this._result},all:function(){return this._fetch()},first:function(){return this.count()>0?this._fetch()[0]:null},last:function(){return this.count()>0?this._fetch()[this.count()-1]:null},count:function(){return this._fetch().length},skip:function(n){return _.extend(this._operators,{$skip:n}),this},limit:function(n){return _.extend(this._operators,{$limit:n}),this},sort:function(modifier){return _.extend(this._operators,{$sort:modifier}),this},next:function(){return this.hasNext()?this._fetch()[this._position++]:null},hasNext:function(){return this.count()>this._position},max:function(expr){return groupOperators.$max(this._fetch(),expr)},min:function(expr){return groupOperators.$min(this._fetch(),expr)},map:function(callback){return _.map(this._fetch(),callback)},forEach:function(callback){_.each(this._fetch(),callback)}},Mingo.Aggregator=function(operators){return this instanceof Mingo.Aggregator?(this._operators=operators,void 0):new Mingo.Aggregator(operators)},Mingo.Aggregator.prototype={run:function(collection,query){if(!_.isEmpty(this._operators))for(var i=0;i<this._operators.length;i++){var operator=this._operators[i];for(var key in operator)operator.hasOwnProperty(key)&&(collection=query?pipelineOperators[key].call(query,collection,operator[key]):pipelineOperators[key](collection,operator[key]))}return collection}},Mingo._get=function(obj,field){return _.result(obj,field)},Mingo._resolve=function(obj,field){if(!field)return void 0;for(var isText,names=field.split("."),value=obj,i=0;i<names.length;i++){if(isText=null===names[i].match(/^\d+$/),isText&&_.isArray(value)){var res=[];_.each(value,function(item){_.isObject(item)&&res.push(Mingo._resolve(item,names[i]))}),value=res}else value=Mingo._get(value,names[i]);if(void 0===value)break}return value},Mingo.compile=function(criteria,projection){return new Mingo.Query(criteria,projection)},Mingo.find=function(collection,criteria,projection){return new Mingo.Query(criteria).find(collection,projection)},Mingo.remove=function(collection,criteria){return new Mingo.Query(criteria).remove(collection)},Mingo.aggregate=function(collection,pipeline){if(!_.isArray(pipeline))throw new Error("Aggregation pipeline must be an array");return new Mingo.Aggregator(pipeline).run(collection)},Mingo.CollectionMixin={query:function(criteria,projection){return Mingo.find(this.toJSON(),criteria,projection)},aggregate:function(pipeline){var args=[this.toJSON(),pipeline];return Mingo.aggregate.apply(null,args)}};var pipelineOperators={$group:function(collection,expr){var idKey=expr[settings.key],indexes=[],groups=_.groupBy(collection,function(obj){var key=computeValue(obj,idKey,idKey);return indexes.push(key),key});indexes=_.uniq(indexes),expr=_.omit(expr,settings.key);var result=[];return _.each(indexes,function(index){var obj={};obj[settings.key]=index;for(var key in expr)expr.hasOwnProperty(key)&&(obj[key]=accumulate(groups[index],key,expr[key]));result.push(obj)}),result},$match:function(collection,expr){var query=new Mingo.Query(expr);return query.find(collection).all()},$project:function(collection,expr){if(_.isEmpty(expr))return collection;var projected=[],objKeys=_.keys(expr);if(_.contains(objKeys,settings.key)){var id=expr[settings.key];(0===id||id===!1)&&(objKeys=_.without(objKeys,settings.key))}else objKeys.push(settings.key);for(var i=0;i<collection.length;i++){var obj=collection[i],cloneObj={};_.each(objKeys,function(key){var newValue,subExpr=expr[key];if(key===settings.key&&_.isEmpty(subExpr))newValue=obj[key];else if(_.isString(subExpr))newValue=computeValue(obj,subExpr,key);else if(1===subExpr||subExpr===!0)newValue=_.result(obj,key);else if(_.isObject(subExpr)){var operator=_.keys(subExpr);if(operator=operator.length>1?!1:operator[0],operator!==!1&&_.contains(Ops.projectionOperators,operator)){var temp=projectionOperators[operator](obj,subExpr[operator],key);_.isUndefined(temp)||(newValue=temp)}else newValue=computeValue(obj,subExpr,key)}void 0!==newValue&&(cloneObj[key]=_.isObject(newValue)?_.clone(newValue):newValue)}),projected.push(cloneObj)}return projected},$limit:function(collection,value){return _.first(collection,value)},$skip:function(collection,value){return _.rest(collection,value)},$unwind:function(collection,expr){for(var result=[],field=expr.substr(1),i=0;i<collection.length;i++){var obj=collection[i],value=Mingo._get(obj,field);if(!_.isArray(value))throw new Error("Target field '"+field+"' is not of type Array.");_.each(value,function(item){var tmp=_.clone(obj);tmp[field]=item,result.push(tmp)})}return result},$sort:function(collection,sortKeys){if(!_.isEmpty(sortKeys)&&_.isObject(sortKeys)){var modifiers=_.keys(sortKeys);modifiers.reverse().forEach(function(key){var indexes=[],grouped=_.groupBy(collection,function(obj){var value=Mingo._resolve(obj,key);return indexes.push(value),value});indexes=_.sortBy(_.uniq(indexes),function(item){return item}),-1===sortKeys[key]&&indexes.reverse(),collection=[],_.each(indexes,function(item){Array.prototype.push.apply(collection,grouped[item])})})}return collection}},compoundOperators={$and:function(selector,value){if(!_.isArray(value))throw new Error("Invalid expression for $and criteria");var queries=[];return _.each(value,function(expr){queries.push(new Mingo.Query(expr))}),{test:function(obj){for(var i=0;i<queries.length;i++)if(!queries[i].test(obj))return!1;return!0}}},$or:function(selector,value){if(!_.isArray(value))throw new Error("Invalid expression for $or criteria");var queries=[];return _.each(value,function(expr){queries.push(new Mingo.Query(expr))}),{test:function(obj){for(var i=0;i<queries.length;i++)if(queries[i].test(obj))return!0;return!1}}},$nor:function(selector,value){if(!_.isArray(value))throw new Error("Invalid expression for $nor criteria");var query=this.$or("$or",value);return{test:function(obj){return!query.test(obj)}}},$not:function(selector,value){var criteria={};criteria[selector]=normalize(value);var query=new Mingo.Query(criteria);return{test:function(obj){return!query.test(obj)}}},$where:function(selector,value){return _.isFunction(value)||(value=new Function("return "+value+";")),{test:function(obj){return value.call(obj)===!0}}}},simpleOperators={$eq:function(a,b){return a=_.isArray(a)?a:[a],a=_.find(a,function(val){return _.isEqual(val,b)}),void 0!==a},$ne:function(a,b){return!this.$eq(a,b)},$in:function(a,b){return a=_.isArray(a)?a:[a],_.intersection(a,b).length>0},$nin:function(a,b){return _.isUndefined(a)||!this.$in(a,b)},$lt:function(a,b){return a=_.isArray(a)?a:[a],a=_.find(a,function(val){return b>val}),void 0!==a},$lte:function(a,b){return a=_.isArray(a)?a:[a],a=_.find(a,function(val){return b>=val}),void 0!==a},$gt:function(a,b){return a=_.isArray(a)?a:[a],a=_.find(a,function(val){return val>b}),void 0!==a},$gte:function(a,b){return a=_.isArray(a)?a:[a],a=_.find(a,function(val){return val>=b}),void 0!==a},$mod:function(a,b){return a=_.isArray(a)?a:[a],a=_.find(a,function(val){return _.isNumber(val)&&_.isArray(b)&&2===b.length&&val%b[0]===b[1]}),void 0!==a},$regex:function(a,b){return a=_.isArray(a)?a:[a],a=_.find(a,function(val){return _.isString(val)&&_.isRegExp(b)&&!!val.match(b)}),void 0!==a},$exists:function(a,b){return b===!1&&_.isUndefined(a)||b===!0&&!_.isUndefined(a)},$all:function(a,b){var self=this,matched=!1;if(_.isArray(a)&&_.isArray(b))for(var i=0;i<b.length;i++){if(!_.isObject(b[i])||!_.contains(_.keys(b[i]),"$elemMatch"))return _.intersection(b,a).length===b.length;matched=matched||self.$elemMatch(a,b[i].$elemMatch)}return matched},$size:function(a,b){return _.isArray(a)&&_.isNumber(b)&&a.length===b},$elemMatch:function(a,b){if(_.isArray(a)&&!_.isEmpty(a))for(var query=new Mingo.Query(b),i=0;i<a.length;i++)if(query.test(a[i]))return!0;return!1},$type:function(a,b){switch(b){case 1:return _.isNumeric(a)&&-1!==(a+"").indexOf(".");case 2:case 5:return _.isString(a);case 3:return _.isObject(a);case 4:return _.isArray(a);case 8:return _.isBoolean(a);case 9:return _.isDate(a);case 10:return _.isNull(a);case 11:return _.isRegExp(a);case 16:return _.isNumeric(a)&&2147483647>=a&&-1===(a+"").indexOf(".");case 18:return _.isNumeric(a)&&a>2147483647&&0x8000000000000000>=a&&-1===(a+"").indexOf(".");default:return!1}}},projectionOperators={$:function(){throw new Error("$ not implemented")},$elemMatch:function(obj,expr,field){var array=Mingo._resolve(obj,field),query=new Mingo.Query(expr);if(_.isUndefined(array)||!_.isArray(array))return void 0;for(var i=0;i<array.length;i++)if(query.test(array[i]))return array[i];return void 0},$slice:function(obj,expr,field){var array=Mingo._resolve(obj,field);return _.isUndefined(array)?void 0:(_.isArray(expr)||(expr=[expr]),Array.prototype.slice.apply(array,expr))}},groupOperators={$addToSet:function(collection,expr){var result=_.map(collection,function(obj){return computeValue(obj,expr)});return _.uniq(result)},$sum:function(collection,expr){return _.isNumber(expr)?collection.length*expr:_.reduce(collection,function(acc,obj){return acc+computeValue(obj,expr)},0)},$max:function(collection,expr){var obj=_.max(collection,function(obj){return computeValue(obj,expr)});return computeValue(obj,expr)},$min:function(collection,expr){var obj=_.min(collection,function(obj){return computeValue(obj,expr)});return computeValue(obj,expr)},$avg:function(collection,expr){return this.$sum(collection,expr)/collection.length},$push:function(collection,expr){return _.map(collection,function(obj){return computeValue(obj,expr)})},$first:function(collection,expr){return collection.length>0?computeValue(collection[0],expr):void 0},$last:function(collection,expr){return collection.length>0?computeValue(collection[collection.length-1],expr):void 0}},aggregateOperators={$add:function(obj,expr){var args=computeValue(obj,expr);return _.reduce(args,function(memo,num){return memo+num},0)},$subtract:function(obj,expr){var args=computeValue(obj,expr);return args[0]-args[1]},$divide:function(obj,expr){var args=computeValue(obj,expr);return args[0]/args[1]},$multiply:function(obj,expr){var args=computeValue(obj,expr);return _.reduce(args,function(memo,num){return memo*num},1)},$mod:function(obj,expr){var args=computeValue(obj,expr);return args[0]%args[1]},$cmp:function(obj,expr){var args=computeValue(obj,expr);return args[0]>args[1]?1:args[0]<args[1]?-1:0},$concat:function(obj,expr){var args=computeValue(obj,expr);return _.contains(args,null)||_.contains(args,void 0)?null:args.join("")},$strcasecmp:function(obj,expr){var args=computeValue(obj,expr);return args[0]=_.isEmpty(args[0])?"":args[0].toUpperCase(),args[1]=_.isEmpty(args[1])?"":args[1].toUpperCase(),args[0]>args[1]?1:args[0]<args[1]?-1:0},$substr:function(obj,expr){var args=computeValue(obj,expr);return _.isString(args[0])?args[1]<0?"":args[2]<0?args[0].substr(args[1]):args[0].substr(args[1],args[2]):""},$toLower:function(obj,expr){var value=computeValue(obj,expr);return _.isEmpty(value)?"":value.toLowerCase()},$toUpper:function(obj,expr){var value=computeValue(obj,expr);return _.isEmpty(value)?"":value.toUpperCase()}},setOperators={$setEquals:function(obj,expr){var args=computeValue(obj,expr),first=_.uniq(args[0]),second=_.uniq(args[1]);return first.length!==second.length?!1:0==_.difference(first,second).length},$setIntersection:function(obj,expr){var args=computeValue(obj,expr);return _.intersection(args[0],args[1])},$setDifference:function(obj,expr){var args=computeValue(obj,expr);return _.difference(args[0],args[1])},$setUnion:function(obj,expr){var args=computeValue(obj,expr);return _.union(args[0],args[1])},$setIsSubset:function(obj,expr){var args=computeValue(obj,expr);return _.intersection(args[0],args[1]).length===args[0].length},$anyElementTrue:function(obj,expr){for(var args=computeValue(obj,expr)[0],i=0;i<args.length;i++)if(args[i])return!0;return!1},$allElementsTrue:function(obj,expr){for(var args=computeValue(obj,expr)[0],i=0;i<args.length;i++)if(!args[i])return!1;return!0}},conditionalOperators={$cond:function(obj,expr){var ifExpr,thenExpr,elseExpr;if(_.isArray(expr)){if(3!=expr.length)throw new Error("Invalid arguments for $cond operator");ifExpr=expr[0],thenExpr=expr[1],elseExpr=expr[2]}else _.isObject(expr)&&(ifExpr=expr["if"],thenExpr=expr.then,elseExpr=expr["else"]);var condition=computeValue(obj,ifExpr);return condition?computeValue(obj,thenExpr):computeValue(obj,elseExpr)},$ifNull:function(obj,expr){if(!_.isArray(expr)||2!=expr.length)throw new Error("Invalid arguments for $ifNull operator");var args=computeValue(obj,expr);return null===args[0]||void 0===args[0]?args[1]:args[0]}};_.each(["$eq","$ne","$gt","$gte","$lt","$lte"],function(op){aggregateOperators[op]=function(obj,expr){var args=computeValue(obj,expr);return simpleOperators[op](args[0],args[1])}}),_.extend(aggregateOperators,setOperators,conditionalOperators);var Ops={simpleOperators:_.keys(simpleOperators),compoundOperators:_.keys(compoundOperators),setOperators:_.keys(setOperators),aggregateOperators:_.keys(aggregateOperators),groupOperators:_.keys(groupOperators),pipelineOperators:_.keys(pipelineOperators),projectionOperators:_.keys(projectionOperators)};Ops.queryOperators=_.union(Ops.simpleOperators,Ops.compoundOperators);var accumulate=function(collection,field,expr){if(_.contains(Ops.groupOperators,field))return groupOperators[field](collection,expr);if(_.isObject(expr)){var result={};for(var key in expr)if(expr.hasOwnProperty(key)&&(result[key]=accumulate(collection,key,expr[key]),_.contains(Ops.groupOperators,key))){if(result=result[key],_.keys(expr).length>1)throw new Error("Invalid $group expression '"+JSON.stringify(expr)+"'");break}return result}return void 0},computeValue=function(obj,expr,field){if(_.contains(Ops.aggregateOperators,field))return aggregateOperators[field](obj,expr);if(_.isString(expr)&&expr.length>0&&"$"===expr[0])return Mingo._resolve(obj,expr.slice(1));var result;if(_.isArray(expr)){result=[];for(var i=0;i<expr.length;i++)result.push(computeValue(obj,expr[i],null))}else if(_.isObject(expr)){result={};for(var key in expr)if(expr.hasOwnProperty(key)&&(result[key]=computeValue(obj,expr[key],key),_.contains(Ops.aggregateOperators,key))){if(result=result[key],_.keys(expr).length>1)throw new Error("Invalid aggregation expression '"+JSON.stringify(expr)+"'");break}}else for(var i=0;i<primitives.length;i++)if(primitives[i](expr))return expr;return result}}).call(this);
(function(){"use strict";function r(r){for(var t=0;t<s.length;t++)if(s[t](r))return e.isRegExp(r)?{$regex:r}:{$eq:r};if(e.isObject(r)){var n=e.keys(r),i=0===e.intersection($.queryOperators,n).length;if(i)return{$eq:r};if(e.contains(n,"$regex")){var o=r.$regex,u=r.$options||"",a="";e.isString(o)&&(a+=o.ignoreCase||u.indexOf("i")>=0?"i":"",a+=o.multiline||u.indexOf("m")>=0?"m":"",a+=o.global||u.indexOf("g")>=0?"g":"",o=new RegExp(o,a)),r.$regex=o,delete r.$options}}return r}var t,e,n=this,i={};null!=n&&(t=n.Mingo),i.noConflict=function(){return n.Mingo=t,i};var o="undefined"!=typeof exports&&"undefined"!=typeof module&&"undefined"!=typeof require&&"undefined"!=typeof process;"undefined"!=typeof exports?(exports="undefined"!=typeof module&&module.exports?module.exports=i:i,e=require("underscore")):(n.Mingo=i,e=n._);var s=[e.isString,e.isBoolean,e.isNumber,e.isDate,e.isNull,e.isRegExp],u={key:"_id"};if(i.setup=function(r){e.extend(u,r||{})},i.Query=function(r,t){return this instanceof i.Query?(this._criteria=r,this._projection=t,this._compiled=[],this._compile(),void 0):new i.Query(r,t)},i.Query.prototype={_compile:function(){if(!e.isEmpty(this._criteria)){if(e.isArray(this._criteria)||e.isFunction(this._criteria)||!e.isObject(this._criteria))throw new Error("Invalid type for criteria");for(var t in this._criteria)if(this._criteria.hasOwnProperty(t)){var n=this._criteria[t];if(e.contains($.compoundOperators,t)){if(e.contains(["$not"],t))throw new Error("Invalid operator");this._processOperator(t,t,n)}else{n=r(n);for(var i in n)n.hasOwnProperty(i)&&this._processOperator(t,i,n[i])}}}},_processOperator:function(r,t,n){var o;if(e.contains($.simpleOperators,t))o={test:function(e){var o=i._resolve(e,r);return l[t](o,n)}};else{if(!e.contains($.compoundOperators,t))throw new Error("Invalid query operator '"+t+"' detected");o=h[t](r,n)}this._compiled.push(o)},test:function(r){for(var t=0;t<this._compiled.length;t++)if(!this._compiled[t].test(r))return!1;return!0},find:function(r,t){return new i.Cursor(r,this,t)},remove:function(r){for(var t=[],e=0;e<r.length;e++)this.test(r[e])||t.push(r[e]);return t}},o){var a=require("stream").Transform,c=require("util");i.Query.prototype.stream=function(r){return new i.Stream(this,r)},i.Stream=function(r,t){return this instanceof i.Stream?(t=t||{},e.extend(t,{objectMode:!0}),a.call(this,t),this._query=r,void 0):new i.Stream(r,t)},c.inherits(i.Stream,a),i.Stream.prototype._transform=function(r,t,n){if(e.isObject(r)&&this._query.test(r))if(e.isEmpty(this._query._projection))this.push(r);else{var o=new i.Cursor([r],this._query);o.hasNext()&&this.push(o.next())}n()}}i.Cursor=function(r,t,e){return this instanceof i.Cursor?(this._query=t,this._collection=r,this._projection=e||t._projection,this._operators={},this._result=!1,this._position=0,void 0):new i.Cursor(r,t,e)},i.Cursor.prototype={_fetch:function(){var r=this;if(this._result!==!1)return this._result;if(e.isObject(this._projection)&&e.extend(this._operators,{$project:this._projection}),!e.isArray(this._collection))throw new Error("Input collection is not of a valid type.");this._result=e.filter(this._collection,this._query.test,this._query);var t=[];if(e.each(["$sort","$skip","$limit","$project"],function(n){e.has(r._operators,n)&&t.push(e.pick(r._operators,n))}),t.length>0){var n=new i.Aggregator(t);this._result=n.run(this._result,this._query)}return this._result},all:function(){return this._fetch()},first:function(){return this.count()>0?this._fetch()[0]:null},last:function(){return this.count()>0?this._fetch()[this.count()-1]:null},count:function(){return this._fetch().length},skip:function(r){return e.extend(this._operators,{$skip:r}),this},limit:function(r){return e.extend(this._operators,{$limit:r}),this},sort:function(r){return e.extend(this._operators,{$sort:r}),this},next:function(){return this.hasNext()?this._fetch()[this._position++]:null},hasNext:function(){return this.count()>this._position},max:function(r){return v.$max(this._fetch(),r)},min:function(r){return v.$min(this._fetch(),r)},map:function(r){return e.map(this._fetch(),r)},forEach:function(r){e.each(this._fetch(),r)}},i.Aggregator=function(r){return this instanceof i.Aggregator?(this._operators=r,void 0):new i.Aggregator(r)},i.Aggregator.prototype={run:function(r,t){if(!e.isEmpty(this._operators))for(var n=0;n<this._operators.length;n++){var i=this._operators[n];for(var o in i)i.hasOwnProperty(o)&&(r=t?f[o].call(t,r,i[o]):f[o](r,i[o]))}return r}},i._get=function(r,t){return e.result(r,t)},i._resolve=function(r,t){if(!t)return void 0;for(var n,o=t.split("."),s=r,u=0;u<o.length;u++){if(n=null===o[u].match(/^\d+$/),n&&e.isArray(s)){var a=[];e.each(s,function(r){e.isObject(r)&&a.push(i._resolve(r,o[u]))}),s=a}else s=i._get(s,o[u]);if(void 0===s)break}return s},i.compile=function(r,t){return new i.Query(r,t)},i.find=function(r,t,e){return new i.Query(t).find(r,e)},i.remove=function(r,t){return new i.Query(t).remove(r)},i.aggregate=function(r,t){if(!e.isArray(t))throw new Error("Aggregation pipeline must be an array");return new i.Aggregator(t).run(r)},i.CollectionMixin={query:function(r,t){return i.find(this.toJSON(),r,t)},aggregate:function(r){var t=[this.toJSON(),r];return i.aggregate.apply(null,t)}};var f={$group:function(r,t){var n=t[u.key],i=[],o=e.groupBy(r,function(r){var t=_(r,n,n);return i.push(t),t});i=e.uniq(i),t=e.omit(t,u.key);var s=[];return e.each(i,function(r){var e={};e[u.key]=r;for(var n in t)t.hasOwnProperty(n)&&(e[n]=m(o[r],n,t[n]));s.push(e)}),s},$match:function(r,t){var e=new i.Query(t);return e.find(r).all()},$project:function(r,t){if(e.isEmpty(t))return r;var n=[],i=e.keys(t);if(e.contains(i,u.key)){var o=t[u.key];(0===o||o===!1)&&(i=e.without(i,u.key))}else i.push(u.key);for(var s=0;s<r.length;s++){var a=r[s],c={};e.each(i,function(r){var n,i=t[r];if(r===u.key&&e.isEmpty(i))n=a[r];else if(e.isString(i))n=_(a,i,r);else if(1===i||i===!0)n=e.result(a,r);else if(e.isObject(i)){var o=e.keys(i);if(o=o.length>1?!1:o[0],o!==!1&&e.contains($.projectionOperators,o)){var s=p[o](a,i[o],r);e.isUndefined(s)||(n=s)}else n=_(a,i,r)}void 0!==n&&(c[r]=e.isObject(n)?e.clone(n):n)}),n.push(c)}return n},$limit:function(r,t){return e.first(r,t)},$skip:function(r,t){return e.rest(r,t)},$unwind:function(r,t){for(var n=[],o=t.substr(1),s=0;s<r.length;s++){var u=r[s],a=i._get(u,o);if(!e.isArray(a))throw new Error("Target field '"+o+"' is not of type Array.");e.each(a,function(r){var t=e.clone(u);t[o]=r,n.push(t)})}return n},$sort:function(r,t){if(!e.isEmpty(t)&&e.isObject(t)){var n=e.keys(t);n.reverse().forEach(function(n){var o=[],s=e.groupBy(r,function(r){var t=i._resolve(r,n);return o.push(t),t});o=e.sortBy(e.uniq(o),function(r){return r}),-1===t[n]&&o.reverse(),r=[],e.each(o,function(t){Array.prototype.push.apply(r,s[t])})})}return r}},h={$and:function(r,t){if(!e.isArray(t))throw new Error("Invalid expression for $and criteria");var n=[];return e.each(t,function(r){n.push(new i.Query(r))}),{test:function(r){for(var t=0;t<n.length;t++)if(!n[t].test(r))return!1;return!0}}},$or:function(r,t){if(!e.isArray(t))throw new Error("Invalid expression for $or criteria");var n=[];return e.each(t,function(r){n.push(new i.Query(r))}),{test:function(r){for(var t=0;t<n.length;t++)if(n[t].test(r))return!0;return!1}}},$nor:function(r,t){if(!e.isArray(t))throw new Error("Invalid expression for $nor criteria");var n=this.$or("$or",t);return{test:function(r){return!n.test(r)}}},$not:function(t,e){var n={};n[t]=r(e);var o=new i.Query(n);return{test:function(r){return!o.test(r)}}},$where:function(r,t){return e.isFunction(t)||(t=new Function("return "+t+";")),{test:function(r){return t.call(r)===!0}}}},l={$eq:function(r,t){return r=e.isArray(r)?r:[r],r=e.find(r,function(r){return e.isEqual(r,t)}),void 0!==r},$ne:function(r,t){return!this.$eq(r,t)},$in:function(r,t){return r=e.isArray(r)?r:[r],e.intersection(r,t).length>0},$nin:function(r,t){return e.isUndefined(r)||!this.$in(r,t)},$lt:function(r,t){return r=e.isArray(r)?r:[r],r=e.find(r,function(r){return t>r}),void 0!==r},$lte:function(r,t){return r=e.isArray(r)?r:[r],r=e.find(r,function(r){return t>=r}),void 0!==r},$gt:function(r,t){return r=e.isArray(r)?r:[r],r=e.find(r,function(r){return r>t}),void 0!==r},$gte:function(r,t){return r=e.isArray(r)?r:[r],r=e.find(r,function(r){return r>=t}),void 0!==r},$mod:function(r,t){return r=e.isArray(r)?r:[r],r=e.find(r,function(r){return e.isNumber(r)&&e.isArray(t)&&2===t.length&&r%t[0]===t[1]}),void 0!==r},$regex:function(r,t){return r=e.isArray(r)?r:[r],r=e.find(r,function(r){return e.isString(r)&&e.isRegExp(t)&&!!r.match(t)}),void 0!==r},$exists:function(r,t){return t===!1&&e.isUndefined(r)||t===!0&&!e.isUndefined(r)},$all:function(r,t){var n=this,i=!1;if(e.isArray(r)&&e.isArray(t))for(var o=0;o<t.length;o++){if(!e.isObject(t[o])||!e.contains(e.keys(t[o]),"$elemMatch"))return e.intersection(t,r).length===t.length;i=i||n.$elemMatch(r,t[o].$elemMatch)}return i},$size:function(r,t){return e.isArray(r)&&e.isNumber(t)&&r.length===t},$elemMatch:function(r,t){if(e.isArray(r)&&!e.isEmpty(r))for(var n=new i.Query(t),o=0;o<r.length;o++)if(n.test(r[o]))return!0;return!1},$type:function(r,t){switch(t){case 1:return e.isNumeric(r)&&-1!==(r+"").indexOf(".");case 2:case 5:return e.isString(r);case 3:return e.isObject(r);case 4:return e.isArray(r);case 8:return e.isBoolean(r);case 9:return e.isDate(r);case 10:return e.isNull(r);case 11:return e.isRegExp(r);case 16:return e.isNumeric(r)&&2147483647>=r&&-1===(r+"").indexOf(".");case 18:return e.isNumeric(r)&&r>2147483647&&0x8000000000000000>=r&&-1===(r+"").indexOf(".");default:return!1}}},p={$:function(){throw new Error("$ not implemented")},$elemMatch:function(r,t,n){var o=i._resolve(r,n),s=new i.Query(t);if(e.isUndefined(o)||!e.isArray(o))return void 0;for(var u=0;u<o.length;u++)if(s.test(o[u]))return o[u];return void 0},$slice:function(r,t,n){var o=i._resolve(r,n);return e.isUndefined(o)?void 0:(e.isArray(t)||(t=[t]),Array.prototype.slice.apply(o,t))}},v={$addToSet:function(r,t){var n=e.map(r,function(r){return _(r,t)});return e.uniq(n)},$sum:function(r,t){return e.isNumber(t)?r.length*t:e.reduce(r,function(r,e){return r+_(e,t)},0)},$max:function(r,t){var n=e.max(r,function(r){return _(r,t)});return _(n,t)},$min:function(r,t){var n=e.min(r,function(r){return _(r,t)});return _(n,t)},$avg:function(r,t){return this.$sum(r,t)/r.length},$push:function(r,t){return e.map(r,function(r){return _(r,t)})},$first:function(r,t){return r.length>0?_(r[0],t):void 0},$last:function(r,t){return r.length>0?_(r[r.length-1],t):void 0}},g={$add:function(r,t){var n=_(r,t);return e.reduce(n,function(r,t){return r+t},0)},$subtract:function(r,t){var e=_(r,t);return e[0]-e[1]},$divide:function(r,t){var e=_(r,t);return e[0]/e[1]},$multiply:function(r,t){var n=_(r,t);return e.reduce(n,function(r,t){return r*t},1)},$mod:function(r,t){var e=_(r,t);return e[0]%e[1]},$cmp:function(r,t){var e=_(r,t);return e[0]>e[1]?1:e[0]<e[1]?-1:0},$concat:function(r,t){var n=_(r,t);return e.contains(n,null)||e.contains(n,void 0)?null:n.join("")},$strcasecmp:function(r,t){var n=_(r,t);return n[0]=e.isEmpty(n[0])?"":n[0].toUpperCase(),n[1]=e.isEmpty(n[1])?"":n[1].toUpperCase(),n[0]>n[1]?1:n[0]<n[1]?-1:0},$substr:function(r,t){var n=_(r,t);return e.isString(n[0])?n[1]<0?"":n[2]<0?n[0].substr(n[1]):n[0].substr(n[1],n[2]):""},$toLower:function(r,t){var n=_(r,t);return e.isEmpty(n)?"":n.toLowerCase()},$toUpper:function(r,t){var n=_(r,t);return e.isEmpty(n)?"":n.toUpperCase()}},y={$setEquals:function(r,t){var n=_(r,t),i=e.uniq(n[0]),o=e.uniq(n[1]);return i.length!==o.length?!1:0==e.difference(i,o).length},$setIntersection:function(r,t){var n=_(r,t);return e.intersection(n[0],n[1])},$setDifference:function(r,t){var n=_(r,t);return e.difference(n[0],n[1])},$setUnion:function(r,t){var n=_(r,t);return e.union(n[0],n[1])},$setIsSubset:function(r,t){var n=_(r,t);return e.intersection(n[0],n[1]).length===n[0].length},$anyElementTrue:function(r,t){for(var e=_(r,t)[0],n=0;n<e.length;n++)if(e[n])return!0;return!1},$allElementsTrue:function(r,t){for(var e=_(r,t)[0],n=0;n<e.length;n++)if(!e[n])return!1;return!0}},d={$cond:function(r,t){var n,i,o;if(e.isArray(t)){if(3!=t.length)throw new Error("Invalid arguments for $cond operator");n=t[0],i=t[1],o=t[2]}else e.isObject(t)&&(n=t["if"],i=t.then,o=t["else"]);var s=_(r,n);return s?_(r,i):_(r,o)},$ifNull:function(r,t){if(!e.isArray(t)||2!=t.length)throw new Error("Invalid arguments for $ifNull operator");var n=_(r,t);return null===n[0]||void 0===n[0]?n[1]:n[0]}};e.each(["$eq","$ne","$gt","$gte","$lt","$lte"],function(r){g[r]=function(t,e){var n=_(t,e);return l[r](n[0],n[1])}}),e.extend(g,y,d);var $={simpleOperators:e.keys(l),compoundOperators:e.keys(h),setOperators:e.keys(y),aggregateOperators:e.keys(g),groupOperators:e.keys(v),pipelineOperators:e.keys(f),projectionOperators:e.keys(p)};$.queryOperators=e.union($.simpleOperators,$.compoundOperators);var m=function(r,t,n){if(e.contains($.groupOperators,t))return v[t](r,n);if(e.isObject(n)){var i={};for(var o in n)if(n.hasOwnProperty(o)&&(i[o]=m(r,o,n[o]),e.contains($.groupOperators,o))){if(i=i[o],e.keys(n).length>1)throw new Error("Invalid $group expression '"+JSON.stringify(n)+"'");break}return i}return void 0},_=function(r,t,n){if(e.contains($.aggregateOperators,n))return g[n](r,t);if(e.isString(t)&&t.length>0&&"$"===t[0])return i._resolve(r,t.slice(1));var o;if(e.isArray(t)){o=[];for(var u=0;u<t.length;u++)o.push(_(r,t[u],null))}else if(e.isObject(t)){o={};for(var a in t)if(t.hasOwnProperty(a)&&(o[a]=_(r,t[a],a),e.contains($.aggregateOperators,a))){if(o=o[a],e.keys(t).length>1)throw new Error("Invalid aggregation expression '"+JSON.stringify(t)+"'");break}}else for(var u=0;u<s.length;u++)if(s[u](t))return t;return o}}).call(this);
{
"name": "mingo",
"version": "0.2.4",
"version": "0.2.5",
"description": "JavaScript implementation of MongoDB query language",

@@ -11,3 +11,3 @@ "main": "mingo.js",

"test": "tape test/*.js",
"build": "uglifyjs mingo.js -c -o mingo-min.js"
"build": "uglifyjs mingo.js -c -m -o mingo-min.js"
},

@@ -14,0 +14,0 @@ "repository": {

@@ -6,6 +6,6 @@ # Mingo

# Dependencies
## Dependencies
[underscore](https://github.com/jashkenas/underscore)
# Installing
## Installing
```$ npm install mingo```

@@ -15,6 +15,8 @@

```html
<!-- DO NOT FORGET Underscore -->
<script type="text/javascript" src="./underscore-min.js"></script>
<script type="text/javascript" src="./mingo-min.js"></script>
```
# Features
## Features
- Comparisons Operators ($gt, $gte, $lt, $lte, $ne, $nin, $in)

@@ -32,5 +34,5 @@ - Logical Operators ($and, $or, $nor, $not)

- Projection Operators ($elemMatch, $slice)
- JSON stream filtering and projection. *NodeJS only*
- JSON stream filtering and projection. ***NodeJS only***
# Usage
## Usage
```javascript

@@ -81,6 +83,4 @@

// Filter non-matched objects
// Filter non-matched objects (
var result = query.remove(collection);
a.pipe(query).pipe(process.stdout);
```

@@ -159,3 +159,3 @@

# API
## API
### Mingo.Query(criteria, [projection])

@@ -205,7 +205,7 @@ Creates a ```Mingo.Query``` object with the given query criteria

# TODO
## TODO
- Geospatial Query Operators ($geoWithin, $geoIntersects, $near, $nearSphere)
- Geometry Specifiers ($geometry, $maxDistance, $center, $centerSphere, $box, $polygon)
# License
## License
MIT
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc