mini-linq-js
Advanced tools
Comparing version 1.2.5 to 1.3.0
@@ -25,2 +25,2 @@ /* ! https://github.com/akopachov/mini-linq-js */ | ||
*/ | ||
!function(){var stringTrim=function(a){return"string"!=typeof a||null===a?a:String.prototype.trim?a.trim():a.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,"")},expressionCache={};LINQ={utils:{parseExpression:function(a){if("function"==typeof a)return a;if(null==a||"string"!=typeof a||a.indexOf("=>")<0)throw new SyntaxError('Expression "'+a+'" is invalid');if("function"==typeof expressionCache[a])return expressionCache[a];var b,c=a.split("=>"),d=stringTrim(c[0]).replace(/[\(\)\s]/gi,""),e=stringTrim(c[1]);try{e.indexOf("return")<0&&(e="return ("+e+")"),b=new Function(d,e)}catch(f){b=new Function(d,e)}if("function"!=typeof b)throw new SyntaxError('Expression "'+a+'" is invalid');return expressionCache[a]=b,b},getType:function(a){var b=typeof a;if("object"!==b)return b;if(null===a)return"null";var c=a.constructor,d="function"==typeof c&&c.name;return"string"==typeof d&&d.length>0?d:"object"},getDefaultValue:function(type){if("string"!=typeof type)throw new TypeError("Type must be a string.");switch(type){case"boolean":return!1;case"function":return function(){};case"null":return null;case"number":return 0;case"object":return{};case"string":return"";case"symbol":return Symbol();case"undefined":return}try{var ctor="function"==typeof this[type]?this[type]:eval(type);return new ctor}catch(e){return{}}},isArray:function(a){return Array.isArray?Array.isArray(a):"[object Array]"===Object.prototype.toString.call(a)}},methods:{any:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)return this.length>0;if("function"==typeof Array.prototype.some)return this.some(a);for(var b=0,c=this.length;c>b;b++)if(a(this[b],b,this))return!0;return!1},all:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Predicate is required");if("function"==typeof Array.prototype.every)return this.every(a);var b=function(){return!a.apply(this,arguments)};return!LINQ.methods.any.apply(this,[b])},where:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Predicate is required");if("function"==typeof Array.prototype.filter)return this.filter(a);for(var b=[],c=0,d=this.length;d>c;c++)a(this[c],c,this)&&b.push(this[c]);return b},select:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Selector is required");if("function"==typeof Array.prototype.map)return this.map(a);for(var b=[],c=0,d=this.length;d>c;c++)b.push(a(this[c],c,this));return b},selectMany:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Selector is required");for(var b=[],c=0,d=this.length;d>c;c++){var e=a(this[c],c,this);LINQ.utils.isArray(e)&&b.push(e)}return Array.prototype.concat.apply([],b)},count:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)return this.length;if("function"==typeof Array.prototype.filter)return this.filter(a).length;for(var b=0,c=0,d=this.length;d>c;c++)a(this[c],c,this)&&b++;return b},orderBy:function(a,b){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return b>a?-1:a>b?1:0}),this.slice(0).sort(function(c,d){var e=a(c),f=a(d);return b(e,f)})},orderByDescending:function(a,b){"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return b>a?-1:a>b?1:0});var c=function(){return-1*b.apply(this,arguments)};return LINQ.methods.orderBy.apply(this,[a,c])},groupBy:function(a,b){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Key selector is required");"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return{group:a,values:b}});for(var c={},d=0,e=this.length;e>d;d++){var f=a(this[d],d,this);c[f]||(c[f]=[]),c[f].push(this[d])}var g=[];for(var h in c)c.hasOwnProperty(h)&&g.push(b(h,c[h]));return g},distinct:function(a){"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a});for(var b={},c=[],d=0,e=this.length;e>d;d++){var f=a(this[d],d,this);b[f]||(b[f]=!0,c.push(this[d]))}return c},firstOrDefault:function(a){if("string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(){return!0}),"function"==typeof Array.prototype.find)return this.find(a)||null;for(var b=0,c=this.length;c>b;b++)if(a(this[b],b,this))return this[b];return null},lastOrDefault:function(a){"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(){return!0});for(var b=this.length-1;b>=0;b--)if(a(this[b],b,this))return this[b];return null},joinWith:function(a,b,c,d,e){if("string"==typeof b)b=LINQ.utils.parseExpression(b);else if("function"!=typeof b)throw new Error("Inner key selector is required");if("string"==typeof c)c=LINQ.utils.parseExpression(c);else if("function"!=typeof c)throw new Error("Outer key selector is required");if("string"==typeof d)d=LINQ.utils.parseExpression(d);else if("function"!=typeof d)throw new Error("Results selector is required");"string"==typeof e?e=LINQ.utils.parseExpression(e):"function"!=typeof e&&(e=function(a,b){return a===b});for(var f=[],g=0,h=this.length;h>g;g++)for(var i=c(this[g],g,this),j=0,k=a.length;k>j;j++){var l=b(a[j],j,a);e(l,i)&&f.push(d(a[j],this[g]))}return f},groupJoinWith:function(a,b,c,d,e){if("string"==typeof b)b=LINQ.utils.parseExpression(b);else if("function"!=typeof b)throw new Error("Inner key selector is required");if("string"==typeof c)c=LINQ.utils.parseExpression(c);else if("function"!=typeof c)throw new Error("Outer key selector is required");if("string"==typeof d)d=LINQ.utils.parseExpression(d);else if("function"!=typeof d)throw new Error("Results selector is required");"string"==typeof e?e=LINQ.utils.parseExpression(e):"function"!=typeof e&&(e=function(a,b){return a===b});for(var f=[],g=0,h=this.length;h>g;g++){var i=c(this[g],g,this),j=LINQ.methods.where.apply(a,[function(c,d){return innerKey=b(a[d],d,a),e(innerKey,i)}]);f.push(d(j,this[g]))}return f},contains:function(a,b){var c;if("string"==typeof b){var d=LINQ.utils.parseExpression(b);c=function(b){return d(b,a)}}else c="function"!=typeof b?function(b){return b===a}:function(c){return b(c,a)};return LINQ.methods.any.apply(this,[c])},aggregate:function(a,b){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Aggregator function is required");if(this.length<=0)return b;var c="undefined"==typeof b?LINQ.utils.getDefaultValue(LINQ.utils.getType(this[0])):b;if("function"==typeof Array.prototype.reduce)return this.reduce(a,c);for(var d=0,e=this.length;e>d;d++)c=a(c,this[d],d,this);return c},sum:function(a,b){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),b="undefined"==typeof b?LINQ.utils.getDefaultValue(LINQ.utils.getType(a(this[0],0,this))):b,LINQ.methods.aggregate.apply(this,[function(b,c,d,e){return b+a(c,d,e)},b])},min:function(a){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),this.length<=0?void 0:LINQ.methods.aggregate.apply(this,[function(b,c,d,e){var f=a(c,d,e);return b>f?f:b},a(this[0],0,this)])},max:function(a){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),this.length<=0?void 0:LINQ.methods.aggregate.apply(this,[function(b,c,d,e){var f=a(c,d,e);return f>b?f:b},a(this[0],0,this)])},skip:function(a){if("number"!=typeof a||0>a)throw new TypeError("Count is required and should be a positive number");return this.slice(a,this.length)},take:function(a){if("number"!=typeof a||0>a)throw new TypeError("Count is required and should be a positive number");return this.slice(0,a)},ofType:function(a){if("string"!=typeof a)throw new TypeError("Type is required.");return LINQ.methods.where.apply(this,[function(b){return typeof b===a}])},union:function(a){return this.concat(a)}}},LINQ.methods.firstOrDefault.finalize=!0,LINQ.methods.lastOrDefault.finalize=!0,LINQ.methods.count.finalize=!0,LINQ.methods.any.finalize=!0,LINQ.methods.contains.finalize=!0,LINQ.methods.all.finalize=!0,LINQ.methods.aggregate.finalize=!0,LINQ.methods.sum.finalize=!0,LINQ.methods.min.finalize=!0,LINQ.methods.max.finalize=!0;for(var key in LINQ.methods)LINQ.methods.hasOwnProperty(key)&&(Array.prototype[key]=LINQ.methods[key])}(),function(a){var b=[function(b,c){if(b.name===c.name&&"where"===c.name){var d=a.utils.parseExpression(b.args[0]),e=a.utils.parseExpression(c.args[0]);return function(c,d){return{name:b.name,args:[function(){return c.apply(this,arguments)&&d.apply(this,arguments)}],fn:a.methods[b.name]}}(d,e)}return!1},function(b,c){if(b.name===c.name&&"select"===c.name){var d=a.utils.parseExpression(b.args[0]),e=a.utils.parseExpression(c.args[0]);return function(c,d){return{name:b.name,args:[function(){return d.apply(this,[c.apply(this,arguments)])}],fn:a.methods[b.name]}}(d,e)}return!1},function(a,b){var c=["orderBy","orderByDescending"];return c.contains(a.name)&&c.contains(b.name)?b:!1},function(a,b){return"skip"==a.name&&"take"==b.name?function(a,b){return{name:"skiptake",args:[a,b],fn:function(a,b){return this.slice(a,a+b)}}}(a.args[0],b.args[0]):!1}],c=function(c){var d=this,e=c;d._modificatorQueue=[];var f=function(){for(var a=d._modificatorQueue.length,b=0;a>b;b++){var c=d._modificatorQueue[b];e=c.fn.apply(e,c.args)}return d._modificatorQueue.splice(0,a),a},g=function(){for(var a=0;a<d._modificatorQueue.length-1;){for(var c=d._modificatorQueue[a],e=d._modificatorQueue[a+1],f=!1,g=0,h=b.length;h>g;g++){var i=b[g],j=i(c,e);j&&(f=!0,d._modificatorQueue.splice(a+1,1),d._modificatorQueue[a]=j)}f||a++}};d.optimize=function(){return g(),d},d.toArray=function(){return e.length>100&&d._modificatorQueue.length>2&&d.optimize(),f(),a.utils.isArray(e)?e.slice(0):e}};Array.prototype.toLazy=function(){return new c(this)};for(var d in a.methods)a.methods.hasOwnProperty(d)&&(c.prototype[d]=function(b){return function(){var c=a.methods[b];return this._modificatorQueue.push({fn:a.methods[b],args:arguments,name:b}),c.finalize?this.toArray():this}}(d))}(LINQ),function(a){if("undefined"!=typeof ko&&ko.observableArray&&ko.observableArray.fn){Array.prototype.toObservableArray=function(){return ko.observableArray(this)},ko.observableArray.fn.toArray=function(){return this()};for(var b in a.methods)a.methods.hasOwnProperty(b)&&(ko.observableArray.fn[b]=function(b){return function(){return a.methods[b].apply(this(),arguments)}}(b))}}(LINQ); | ||
!function(){var stringTrim=function(a){return"string"!=typeof a||null===a?a:String.prototype.trim?a.trim():a.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,"")},expressionCache={};LINQ={utils:{parseExpression:function(a){if("function"==typeof a)return a;if(null==a||"string"!=typeof a||a.indexOf("=>")<0)throw new SyntaxError('Expression "'+a+'" is invalid');if("function"==typeof expressionCache[a])return expressionCache[a];var b,c=a.split("=>"),d=stringTrim(c[0]).replace(/[\(\)\s]/gi,""),e=stringTrim(c[1]);try{e.indexOf("return")<0&&(e="return ("+e+")"),b=new Function(d,e)}catch(f){b=new Function(d,e)}if("function"!=typeof b)throw new SyntaxError('Expression "'+a+'" is invalid');return expressionCache[a]=b,b},getType:function(a){var b=typeof a;if("object"!==b)return b;if(null===a)return"null";var c=a.constructor,d="function"==typeof c&&c.name;return"string"==typeof d&&d.length>0?d:"object"},getDefaultValue:function(type){if("string"!=typeof type)throw new TypeError("Type must be a string.");switch(type){case"boolean":return!1;case"function":return function(){};case"null":return null;case"number":return 0;case"object":return{};case"string":return"";case"symbol":return Symbol();case"undefined":return}try{var ctor="function"==typeof this[type]?this[type]:eval(type);return new ctor}catch(e){return{}}},isArray:function(a){return Array.isArray?Array.isArray(a):"[object Array]"===Object.prototype.toString.call(a)}},methods:{any:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)return this.length>0;if("function"==typeof Array.prototype.some)return this.some(a);for(var b=0,c=this.length;c>b;b++)if(a(this[b],b,this))return!0;return!1},all:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Predicate is required");if("function"==typeof Array.prototype.every)return this.every(a);var b=function(){return!a.apply(this,arguments)};return!LINQ.methods.any.apply(this,[b])},where:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Predicate is required");if("function"==typeof Array.prototype.filter)return this.filter(a);for(var b=[],c=0,d=this.length;d>c;c++)a(this[c],c,this)&&b.push(this[c]);return b},select:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Selector is required");if("function"==typeof Array.prototype.map)return this.map(a);for(var b=[],c=0,d=this.length;d>c;c++)b.push(a(this[c],c,this));return b},selectMany:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Selector is required");for(var b=[],c=0,d=this.length;d>c;c++){var e=a(this[c],c,this);LINQ.utils.isArray(e)&&b.push(e)}return Array.prototype.concat.apply([],b)},count:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)return this.length;if("function"==typeof Array.prototype.filter)return this.filter(a).length;for(var b=0,c=0,d=this.length;d>c;c++)a(this[c],c,this)&&b++;return b},orderBy:function(a,b){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return b>a?-1:a>b?1:0}),this.slice(0).sort(function(c,d){var e=a(c),f=a(d);return b(e,f)})},orderByDescending:function(a,b){"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return b>a?-1:a>b?1:0});var c=function(){return-1*b.apply(this,arguments)};return LINQ.methods.orderBy.apply(this,[a,c])},groupBy:function(a,b){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Key selector is required");"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return{group:a,values:b}});for(var c={},d=0,e=this.length;e>d;d++){var f=a(this[d],d,this);c[f]||(c[f]=[]),c[f].push(this[d])}var g=[];for(var h in c)c.hasOwnProperty(h)&&g.push(b(h,c[h]));return g},distinct:function(a,b){"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return a===b});for(var c=[],d={},e=0,f=this.length;f>e;e++){var g=a(this[e],e,this);if(d[g]){for(var h=!0,i=0,j=d[g].length;j>i;i++)if(b(d[g][i],g)){h=!1;break}h&&(c.push(this[e]),d[g].push(g))}else d[g]=[g],c.push(this[e])}return c},firstOrDefault:function(a){if("string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(){return!0}),"function"==typeof Array.prototype.find)return this.find(a)||null;for(var b=0,c=this.length;c>b;b++)if(a(this[b],b,this))return this[b];return null},lastOrDefault:function(a){"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(){return!0});for(var b=this.length-1;b>=0;b--)if(a(this[b],b,this))return this[b];return null},joinWith:function(a,b,c,d,e){if("string"==typeof b)b=LINQ.utils.parseExpression(b);else if("function"!=typeof b)throw new Error("Inner key selector is required");if("string"==typeof c)c=LINQ.utils.parseExpression(c);else if("function"!=typeof c)throw new Error("Outer key selector is required");if("string"==typeof d)d=LINQ.utils.parseExpression(d);else if("function"!=typeof d)throw new Error("Results selector is required");"string"==typeof e?e=LINQ.utils.parseExpression(e):"function"!=typeof e&&(e=function(a,b){return a===b});for(var f=[],g=0,h=this.length;h>g;g++)for(var i=c(this[g],g,this),j=0,k=a.length;k>j;j++){var l=b(a[j],j,a);e(l,i)&&f.push(d(a[j],this[g]))}return f},groupJoinWith:function(a,b,c,d,e){if("string"==typeof b)b=LINQ.utils.parseExpression(b);else if("function"!=typeof b)throw new Error("Inner key selector is required");if("string"==typeof c)c=LINQ.utils.parseExpression(c);else if("function"!=typeof c)throw new Error("Outer key selector is required");if("string"==typeof d)d=LINQ.utils.parseExpression(d);else if("function"!=typeof d)throw new Error("Results selector is required");"string"==typeof e?e=LINQ.utils.parseExpression(e):"function"!=typeof e&&(e=function(a,b){return a===b});for(var f=[],g=0,h=this.length;h>g;g++){var i=c(this[g],g,this),j=LINQ.methods.where.apply(a,[function(c,d){return innerKey=b(a[d],d,a),e(innerKey,i)}]);f.push(d(j,this[g]))}return f},contains:function(a,b){var c;if("string"==typeof b){var d=LINQ.utils.parseExpression(b);c=function(b){return d(b,a)}}else c="function"!=typeof b?function(b){return b===a}:function(c){return b(c,a)};return LINQ.methods.any.apply(this,[c])},aggregate:function(a,b){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Aggregator function is required");if(this.length<=0)return b;var c="undefined"==typeof b?LINQ.utils.getDefaultValue(LINQ.utils.getType(this[0])):b;if("function"==typeof Array.prototype.reduce)return this.reduce(a,c);for(var d=0,e=this.length;e>d;d++)c=a(c,this[d],d,this);return c},sum:function(a,b){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),b="undefined"==typeof b?LINQ.utils.getDefaultValue(LINQ.utils.getType(a(this[0],0,this))):b,LINQ.methods.aggregate.apply(this,[function(b,c,d,e){return b+a(c,d,e)},b])},min:function(a){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),this.length<=0?void 0:LINQ.methods.aggregate.apply(this,[function(b,c,d,e){var f=a(c,d,e);return b>f?f:b},a(this[0],0,this)])},max:function(a){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),this.length<=0?void 0:LINQ.methods.aggregate.apply(this,[function(b,c,d,e){var f=a(c,d,e);return f>b?f:b},a(this[0],0,this)])},skip:function(a){if("number"!=typeof a||0>a)throw new TypeError("Count is required and should be a positive number");return this.slice(a,this.length)},take:function(a){if("number"!=typeof a||0>a)throw new TypeError("Count is required and should be a positive number");return this.slice(0,a)},ofType:function(a){if("string"!=typeof a)throw new TypeError("Type is required.");return LINQ.methods.where.apply(this,[function(b){return typeof b===a}])},union:function(a,b){"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return a===b});for(var c=[],d=[].concat(this,a),e=0,f=d.length;f>e;e++){for(var g=!0,h=0,i=c.length;i>h;h++)if(b(d[e],c[h])){g=!1;break}g&&c.push(d[e])}return c},except:function(a,b){"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return a===b});for(var c=[],d=0,e=this.length;e>d;d++){for(var f=!0,g=0,h=a.length;h>g;g++)if(b(this[d],a[g])){f=!1;break}f&&c.push(this[d])}return c}}},LINQ.methods.firstOrDefault.finalize=!0,LINQ.methods.lastOrDefault.finalize=!0,LINQ.methods.count.finalize=!0,LINQ.methods.any.finalize=!0,LINQ.methods.contains.finalize=!0,LINQ.methods.all.finalize=!0,LINQ.methods.aggregate.finalize=!0,LINQ.methods.sum.finalize=!0,LINQ.methods.min.finalize=!0,LINQ.methods.max.finalize=!0;for(var key in LINQ.methods)LINQ.methods.hasOwnProperty(key)&&(Array.prototype[key]=LINQ.methods[key])}(),function(a){var b=[function(b,c){if(b.name===c.name&&"where"===c.name){var d=a.utils.parseExpression(b.args[0]),e=a.utils.parseExpression(c.args[0]);return function(c,d){return{name:b.name,args:[function(){return c.apply(this,arguments)&&d.apply(this,arguments)}],fn:a.methods[b.name]}}(d,e)}return!1},function(b,c){if(b.name===c.name&&"select"===c.name){var d=a.utils.parseExpression(b.args[0]),e=a.utils.parseExpression(c.args[0]);return function(c,d){return{name:b.name,args:[function(){return d.apply(this,[c.apply(this,arguments)])}],fn:a.methods[b.name]}}(d,e)}return!1},function(a,b){var c=["orderBy","orderByDescending"];return c.contains(a.name)&&c.contains(b.name)?b:!1},function(a,b){return"skip"==a.name&&"take"==b.name?function(a,b){return{name:"skiptake",args:[a,b],fn:function(a,b){return this.slice(a,a+b)}}}(a.args[0],b.args[0]):!1}],c=function(c){var d=this,e=c;d._modificatorQueue=[];var f=function(){for(var a=d._modificatorQueue.length,b=0;a>b;b++){var c=d._modificatorQueue[b];e=c.fn.apply(e,c.args)}return d._modificatorQueue.splice(0,a),a},g=function(){for(var a=0;a<d._modificatorQueue.length-1;){for(var c=d._modificatorQueue[a],e=d._modificatorQueue[a+1],f=!1,g=0,h=b.length;h>g;g++){var i=b[g],j=i(c,e);j&&(f=!0,d._modificatorQueue.splice(a+1,1),d._modificatorQueue[a]=j)}f||a++}};d.optimize=function(){return g(),d},d.toArray=function(){return e.length>100&&d._modificatorQueue.length>2&&d.optimize(),f(),a.utils.isArray(e)?e.slice(0):e}};Array.prototype.toLazy=function(){return new c(this)};for(var d in a.methods)a.methods.hasOwnProperty(d)&&(c.prototype[d]=function(b){return function(){var c=a.methods[b];return this._modificatorQueue.push({fn:a.methods[b],args:arguments,name:b}),c.finalize?this.toArray():this}}(d))}(LINQ),function(a){if("undefined"!=typeof ko&&ko.observableArray&&ko.observableArray.fn){Array.prototype.toObservableArray=function(){return ko.observableArray(this)},ko.observableArray.fn.toArray=function(){return this()};for(var b in a.methods)a.methods.hasOwnProperty(b)&&(ko.observableArray.fn[b]=function(b){return function(){return a.methods[b].apply(this(),arguments)}}(b))}}(LINQ); |
@@ -25,2 +25,2 @@ /* ! https://github.com/akopachov/mini-linq-js */ | ||
*/ | ||
!function(){var stringTrim=function(a){return"string"!=typeof a||null===a?a:String.prototype.trim?a.trim():a.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,"")},expressionCache={};LINQ={utils:{parseExpression:function(a){if("function"==typeof a)return a;if(null==a||"string"!=typeof a||a.indexOf("=>")<0)throw new SyntaxError('Expression "'+a+'" is invalid');if("function"==typeof expressionCache[a])return expressionCache[a];var b,c=a.split("=>"),d=stringTrim(c[0]).replace(/[\(\)\s]/gi,""),e=stringTrim(c[1]);try{e.indexOf("return")<0&&(e="return ("+e+")"),b=new Function(d,e)}catch(f){b=new Function(d,e)}if("function"!=typeof b)throw new SyntaxError('Expression "'+a+'" is invalid');return expressionCache[a]=b,b},getType:function(a){var b=typeof a;if("object"!==b)return b;if(null===a)return"null";var c=a.constructor,d="function"==typeof c&&c.name;return"string"==typeof d&&d.length>0?d:"object"},getDefaultValue:function(type){if("string"!=typeof type)throw new TypeError("Type must be a string.");switch(type){case"boolean":return!1;case"function":return function(){};case"null":return null;case"number":return 0;case"object":return{};case"string":return"";case"symbol":return Symbol();case"undefined":return}try{var ctor="function"==typeof this[type]?this[type]:eval(type);return new ctor}catch(e){return{}}},isArray:function(a){return Array.isArray?Array.isArray(a):"[object Array]"===Object.prototype.toString.call(a)}},methods:{any:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)return this.length>0;if("function"==typeof Array.prototype.some)return this.some(a);for(var b=0,c=this.length;c>b;b++)if(a(this[b],b,this))return!0;return!1},all:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Predicate is required");if("function"==typeof Array.prototype.every)return this.every(a);var b=function(){return!a.apply(this,arguments)};return!LINQ.methods.any.apply(this,[b])},where:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Predicate is required");if("function"==typeof Array.prototype.filter)return this.filter(a);for(var b=[],c=0,d=this.length;d>c;c++)a(this[c],c,this)&&b.push(this[c]);return b},select:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Selector is required");if("function"==typeof Array.prototype.map)return this.map(a);for(var b=[],c=0,d=this.length;d>c;c++)b.push(a(this[c],c,this));return b},selectMany:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Selector is required");for(var b=[],c=0,d=this.length;d>c;c++){var e=a(this[c],c,this);LINQ.utils.isArray(e)&&b.push(e)}return Array.prototype.concat.apply([],b)},count:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)return this.length;if("function"==typeof Array.prototype.filter)return this.filter(a).length;for(var b=0,c=0,d=this.length;d>c;c++)a(this[c],c,this)&&b++;return b},orderBy:function(a,b){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return b>a?-1:a>b?1:0}),this.slice(0).sort(function(c,d){var e=a(c),f=a(d);return b(e,f)})},orderByDescending:function(a,b){"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return b>a?-1:a>b?1:0});var c=function(){return-1*b.apply(this,arguments)};return LINQ.methods.orderBy.apply(this,[a,c])},groupBy:function(a,b){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Key selector is required");"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return{group:a,values:b}});for(var c={},d=0,e=this.length;e>d;d++){var f=a(this[d],d,this);c[f]||(c[f]=[]),c[f].push(this[d])}var g=[];for(var h in c)c.hasOwnProperty(h)&&g.push(b(h,c[h]));return g},distinct:function(a){"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a});for(var b={},c=[],d=0,e=this.length;e>d;d++){var f=a(this[d],d,this);b[f]||(b[f]=!0,c.push(this[d]))}return c},firstOrDefault:function(a){if("string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(){return!0}),"function"==typeof Array.prototype.find)return this.find(a)||null;for(var b=0,c=this.length;c>b;b++)if(a(this[b],b,this))return this[b];return null},lastOrDefault:function(a){"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(){return!0});for(var b=this.length-1;b>=0;b--)if(a(this[b],b,this))return this[b];return null},joinWith:function(a,b,c,d,e){if("string"==typeof b)b=LINQ.utils.parseExpression(b);else if("function"!=typeof b)throw new Error("Inner key selector is required");if("string"==typeof c)c=LINQ.utils.parseExpression(c);else if("function"!=typeof c)throw new Error("Outer key selector is required");if("string"==typeof d)d=LINQ.utils.parseExpression(d);else if("function"!=typeof d)throw new Error("Results selector is required");"string"==typeof e?e=LINQ.utils.parseExpression(e):"function"!=typeof e&&(e=function(a,b){return a===b});for(var f=[],g=0,h=this.length;h>g;g++)for(var i=c(this[g],g,this),j=0,k=a.length;k>j;j++){var l=b(a[j],j,a);e(l,i)&&f.push(d(a[j],this[g]))}return f},groupJoinWith:function(a,b,c,d,e){if("string"==typeof b)b=LINQ.utils.parseExpression(b);else if("function"!=typeof b)throw new Error("Inner key selector is required");if("string"==typeof c)c=LINQ.utils.parseExpression(c);else if("function"!=typeof c)throw new Error("Outer key selector is required");if("string"==typeof d)d=LINQ.utils.parseExpression(d);else if("function"!=typeof d)throw new Error("Results selector is required");"string"==typeof e?e=LINQ.utils.parseExpression(e):"function"!=typeof e&&(e=function(a,b){return a===b});for(var f=[],g=0,h=this.length;h>g;g++){var i=c(this[g],g,this),j=LINQ.methods.where.apply(a,[function(c,d){return innerKey=b(a[d],d,a),e(innerKey,i)}]);f.push(d(j,this[g]))}return f},contains:function(a,b){var c;if("string"==typeof b){var d=LINQ.utils.parseExpression(b);c=function(b){return d(b,a)}}else c="function"!=typeof b?function(b){return b===a}:function(c){return b(c,a)};return LINQ.methods.any.apply(this,[c])},aggregate:function(a,b){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Aggregator function is required");if(this.length<=0)return b;var c="undefined"==typeof b?LINQ.utils.getDefaultValue(LINQ.utils.getType(this[0])):b;if("function"==typeof Array.prototype.reduce)return this.reduce(a,c);for(var d=0,e=this.length;e>d;d++)c=a(c,this[d],d,this);return c},sum:function(a,b){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),b="undefined"==typeof b?LINQ.utils.getDefaultValue(LINQ.utils.getType(a(this[0],0,this))):b,LINQ.methods.aggregate.apply(this,[function(b,c,d,e){return b+a(c,d,e)},b])},min:function(a){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),this.length<=0?void 0:LINQ.methods.aggregate.apply(this,[function(b,c,d,e){var f=a(c,d,e);return b>f?f:b},a(this[0],0,this)])},max:function(a){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),this.length<=0?void 0:LINQ.methods.aggregate.apply(this,[function(b,c,d,e){var f=a(c,d,e);return f>b?f:b},a(this[0],0,this)])},skip:function(a){if("number"!=typeof a||0>a)throw new TypeError("Count is required and should be a positive number");return this.slice(a,this.length)},take:function(a){if("number"!=typeof a||0>a)throw new TypeError("Count is required and should be a positive number");return this.slice(0,a)},ofType:function(a){if("string"!=typeof a)throw new TypeError("Type is required.");return LINQ.methods.where.apply(this,[function(b){return typeof b===a}])},union:function(a){return this.concat(a)}}},LINQ.methods.firstOrDefault.finalize=!0,LINQ.methods.lastOrDefault.finalize=!0,LINQ.methods.count.finalize=!0,LINQ.methods.any.finalize=!0,LINQ.methods.contains.finalize=!0,LINQ.methods.all.finalize=!0,LINQ.methods.aggregate.finalize=!0,LINQ.methods.sum.finalize=!0,LINQ.methods.min.finalize=!0,LINQ.methods.max.finalize=!0;for(var key in LINQ.methods)LINQ.methods.hasOwnProperty(key)&&(Array.prototype[key]=LINQ.methods[key])}(); | ||
!function(){var stringTrim=function(a){return"string"!=typeof a||null===a?a:String.prototype.trim?a.trim():a.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,"")},expressionCache={};LINQ={utils:{parseExpression:function(a){if("function"==typeof a)return a;if(null==a||"string"!=typeof a||a.indexOf("=>")<0)throw new SyntaxError('Expression "'+a+'" is invalid');if("function"==typeof expressionCache[a])return expressionCache[a];var b,c=a.split("=>"),d=stringTrim(c[0]).replace(/[\(\)\s]/gi,""),e=stringTrim(c[1]);try{e.indexOf("return")<0&&(e="return ("+e+")"),b=new Function(d,e)}catch(f){b=new Function(d,e)}if("function"!=typeof b)throw new SyntaxError('Expression "'+a+'" is invalid');return expressionCache[a]=b,b},getType:function(a){var b=typeof a;if("object"!==b)return b;if(null===a)return"null";var c=a.constructor,d="function"==typeof c&&c.name;return"string"==typeof d&&d.length>0?d:"object"},getDefaultValue:function(type){if("string"!=typeof type)throw new TypeError("Type must be a string.");switch(type){case"boolean":return!1;case"function":return function(){};case"null":return null;case"number":return 0;case"object":return{};case"string":return"";case"symbol":return Symbol();case"undefined":return}try{var ctor="function"==typeof this[type]?this[type]:eval(type);return new ctor}catch(e){return{}}},isArray:function(a){return Array.isArray?Array.isArray(a):"[object Array]"===Object.prototype.toString.call(a)}},methods:{any:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)return this.length>0;if("function"==typeof Array.prototype.some)return this.some(a);for(var b=0,c=this.length;c>b;b++)if(a(this[b],b,this))return!0;return!1},all:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Predicate is required");if("function"==typeof Array.prototype.every)return this.every(a);var b=function(){return!a.apply(this,arguments)};return!LINQ.methods.any.apply(this,[b])},where:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Predicate is required");if("function"==typeof Array.prototype.filter)return this.filter(a);for(var b=[],c=0,d=this.length;d>c;c++)a(this[c],c,this)&&b.push(this[c]);return b},select:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Selector is required");if("function"==typeof Array.prototype.map)return this.map(a);for(var b=[],c=0,d=this.length;d>c;c++)b.push(a(this[c],c,this));return b},selectMany:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Selector is required");for(var b=[],c=0,d=this.length;d>c;c++){var e=a(this[c],c,this);LINQ.utils.isArray(e)&&b.push(e)}return Array.prototype.concat.apply([],b)},count:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)return this.length;if("function"==typeof Array.prototype.filter)return this.filter(a).length;for(var b=0,c=0,d=this.length;d>c;c++)a(this[c],c,this)&&b++;return b},orderBy:function(a,b){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return b>a?-1:a>b?1:0}),this.slice(0).sort(function(c,d){var e=a(c),f=a(d);return b(e,f)})},orderByDescending:function(a,b){"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return b>a?-1:a>b?1:0});var c=function(){return-1*b.apply(this,arguments)};return LINQ.methods.orderBy.apply(this,[a,c])},groupBy:function(a,b){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Key selector is required");"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return{group:a,values:b}});for(var c={},d=0,e=this.length;e>d;d++){var f=a(this[d],d,this);c[f]||(c[f]=[]),c[f].push(this[d])}var g=[];for(var h in c)c.hasOwnProperty(h)&&g.push(b(h,c[h]));return g},distinct:function(a,b){"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return a===b});for(var c=[],d={},e=0,f=this.length;f>e;e++){var g=a(this[e],e,this);if(d[g]){for(var h=!0,i=0,j=d[g].length;j>i;i++)if(b(d[g][i],g)){h=!1;break}h&&(c.push(this[e]),d[g].push(g))}else d[g]=[g],c.push(this[e])}return c},firstOrDefault:function(a){if("string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(){return!0}),"function"==typeof Array.prototype.find)return this.find(a)||null;for(var b=0,c=this.length;c>b;b++)if(a(this[b],b,this))return this[b];return null},lastOrDefault:function(a){"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(){return!0});for(var b=this.length-1;b>=0;b--)if(a(this[b],b,this))return this[b];return null},joinWith:function(a,b,c,d,e){if("string"==typeof b)b=LINQ.utils.parseExpression(b);else if("function"!=typeof b)throw new Error("Inner key selector is required");if("string"==typeof c)c=LINQ.utils.parseExpression(c);else if("function"!=typeof c)throw new Error("Outer key selector is required");if("string"==typeof d)d=LINQ.utils.parseExpression(d);else if("function"!=typeof d)throw new Error("Results selector is required");"string"==typeof e?e=LINQ.utils.parseExpression(e):"function"!=typeof e&&(e=function(a,b){return a===b});for(var f=[],g=0,h=this.length;h>g;g++)for(var i=c(this[g],g,this),j=0,k=a.length;k>j;j++){var l=b(a[j],j,a);e(l,i)&&f.push(d(a[j],this[g]))}return f},groupJoinWith:function(a,b,c,d,e){if("string"==typeof b)b=LINQ.utils.parseExpression(b);else if("function"!=typeof b)throw new Error("Inner key selector is required");if("string"==typeof c)c=LINQ.utils.parseExpression(c);else if("function"!=typeof c)throw new Error("Outer key selector is required");if("string"==typeof d)d=LINQ.utils.parseExpression(d);else if("function"!=typeof d)throw new Error("Results selector is required");"string"==typeof e?e=LINQ.utils.parseExpression(e):"function"!=typeof e&&(e=function(a,b){return a===b});for(var f=[],g=0,h=this.length;h>g;g++){var i=c(this[g],g,this),j=LINQ.methods.where.apply(a,[function(c,d){return innerKey=b(a[d],d,a),e(innerKey,i)}]);f.push(d(j,this[g]))}return f},contains:function(a,b){var c;if("string"==typeof b){var d=LINQ.utils.parseExpression(b);c=function(b){return d(b,a)}}else c="function"!=typeof b?function(b){return b===a}:function(c){return b(c,a)};return LINQ.methods.any.apply(this,[c])},aggregate:function(a,b){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Aggregator function is required");if(this.length<=0)return b;var c="undefined"==typeof b?LINQ.utils.getDefaultValue(LINQ.utils.getType(this[0])):b;if("function"==typeof Array.prototype.reduce)return this.reduce(a,c);for(var d=0,e=this.length;e>d;d++)c=a(c,this[d],d,this);return c},sum:function(a,b){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),b="undefined"==typeof b?LINQ.utils.getDefaultValue(LINQ.utils.getType(a(this[0],0,this))):b,LINQ.methods.aggregate.apply(this,[function(b,c,d,e){return b+a(c,d,e)},b])},min:function(a){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),this.length<=0?void 0:LINQ.methods.aggregate.apply(this,[function(b,c,d,e){var f=a(c,d,e);return b>f?f:b},a(this[0],0,this)])},max:function(a){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),this.length<=0?void 0:LINQ.methods.aggregate.apply(this,[function(b,c,d,e){var f=a(c,d,e);return f>b?f:b},a(this[0],0,this)])},skip:function(a){if("number"!=typeof a||0>a)throw new TypeError("Count is required and should be a positive number");return this.slice(a,this.length)},take:function(a){if("number"!=typeof a||0>a)throw new TypeError("Count is required and should be a positive number");return this.slice(0,a)},ofType:function(a){if("string"!=typeof a)throw new TypeError("Type is required.");return LINQ.methods.where.apply(this,[function(b){return typeof b===a}])},union:function(a,b){"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return a===b});for(var c=[],d=[].concat(this,a),e=0,f=d.length;f>e;e++){for(var g=!0,h=0,i=c.length;i>h;h++)if(b(d[e],c[h])){g=!1;break}g&&c.push(d[e])}return c},except:function(a,b){"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return a===b});for(var c=[],d=0,e=this.length;e>d;d++){for(var f=!0,g=0,h=a.length;h>g;g++)if(b(this[d],a[g])){f=!1;break}f&&c.push(this[d])}return c}}},LINQ.methods.firstOrDefault.finalize=!0,LINQ.methods.lastOrDefault.finalize=!0,LINQ.methods.count.finalize=!0,LINQ.methods.any.finalize=!0,LINQ.methods.contains.finalize=!0,LINQ.methods.all.finalize=!0,LINQ.methods.aggregate.finalize=!0,LINQ.methods.sum.finalize=!0,LINQ.methods.min.finalize=!0,LINQ.methods.max.finalize=!0;for(var key in LINQ.methods)LINQ.methods.hasOwnProperty(key)&&(Array.prototype[key]=LINQ.methods[key])}(); |
@@ -25,2 +25,2 @@ /* ! https://github.com/akopachov/mini-linq-js */ | ||
*/ | ||
!function(){var stringTrim=function(a){return"string"!=typeof a||null===a?a:String.prototype.trim?a.trim():a.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,"")},expressionCache={};LINQ={utils:{parseExpression:function(a){if("function"==typeof a)return a;if(null==a||"string"!=typeof a||a.indexOf("=>")<0)throw new SyntaxError('Expression "'+a+'" is invalid');if("function"==typeof expressionCache[a])return expressionCache[a];var b,c=a.split("=>"),d=stringTrim(c[0]).replace(/[\(\)\s]/gi,""),e=stringTrim(c[1]);try{e.indexOf("return")<0&&(e="return ("+e+")"),b=new Function(d,e)}catch(f){b=new Function(d,e)}if("function"!=typeof b)throw new SyntaxError('Expression "'+a+'" is invalid');return expressionCache[a]=b,b},getType:function(a){var b=typeof a;if("object"!==b)return b;if(null===a)return"null";var c=a.constructor,d="function"==typeof c&&c.name;return"string"==typeof d&&d.length>0?d:"object"},getDefaultValue:function(type){if("string"!=typeof type)throw new TypeError("Type must be a string.");switch(type){case"boolean":return!1;case"function":return function(){};case"null":return null;case"number":return 0;case"object":return{};case"string":return"";case"symbol":return Symbol();case"undefined":return}try{var ctor="function"==typeof this[type]?this[type]:eval(type);return new ctor}catch(e){return{}}},isArray:function(a){return Array.isArray?Array.isArray(a):"[object Array]"===Object.prototype.toString.call(a)}},methods:{any:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)return this.length>0;if("function"==typeof Array.prototype.some)return this.some(a);for(var b=0,c=this.length;c>b;b++)if(a(this[b],b,this))return!0;return!1},all:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Predicate is required");if("function"==typeof Array.prototype.every)return this.every(a);var b=function(){return!a.apply(this,arguments)};return!LINQ.methods.any.apply(this,[b])},where:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Predicate is required");if("function"==typeof Array.prototype.filter)return this.filter(a);for(var b=[],c=0,d=this.length;d>c;c++)a(this[c],c,this)&&b.push(this[c]);return b},select:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Selector is required");if("function"==typeof Array.prototype.map)return this.map(a);for(var b=[],c=0,d=this.length;d>c;c++)b.push(a(this[c],c,this));return b},selectMany:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Selector is required");for(var b=[],c=0,d=this.length;d>c;c++){var e=a(this[c],c,this);LINQ.utils.isArray(e)&&b.push(e)}return Array.prototype.concat.apply([],b)},count:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)return this.length;if("function"==typeof Array.prototype.filter)return this.filter(a).length;for(var b=0,c=0,d=this.length;d>c;c++)a(this[c],c,this)&&b++;return b},orderBy:function(a,b){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return b>a?-1:a>b?1:0}),this.slice(0).sort(function(c,d){var e=a(c),f=a(d);return b(e,f)})},orderByDescending:function(a,b){"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return b>a?-1:a>b?1:0});var c=function(){return-1*b.apply(this,arguments)};return LINQ.methods.orderBy.apply(this,[a,c])},groupBy:function(a,b){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Key selector is required");"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return{group:a,values:b}});for(var c={},d=0,e=this.length;e>d;d++){var f=a(this[d],d,this);c[f]||(c[f]=[]),c[f].push(this[d])}var g=[];for(var h in c)c.hasOwnProperty(h)&&g.push(b(h,c[h]));return g},distinct:function(a){"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a});for(var b={},c=[],d=0,e=this.length;e>d;d++){var f=a(this[d],d,this);b[f]||(b[f]=!0,c.push(this[d]))}return c},firstOrDefault:function(a){if("string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(){return!0}),"function"==typeof Array.prototype.find)return this.find(a)||null;for(var b=0,c=this.length;c>b;b++)if(a(this[b],b,this))return this[b];return null},lastOrDefault:function(a){"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(){return!0});for(var b=this.length-1;b>=0;b--)if(a(this[b],b,this))return this[b];return null},joinWith:function(a,b,c,d,e){if("string"==typeof b)b=LINQ.utils.parseExpression(b);else if("function"!=typeof b)throw new Error("Inner key selector is required");if("string"==typeof c)c=LINQ.utils.parseExpression(c);else if("function"!=typeof c)throw new Error("Outer key selector is required");if("string"==typeof d)d=LINQ.utils.parseExpression(d);else if("function"!=typeof d)throw new Error("Results selector is required");"string"==typeof e?e=LINQ.utils.parseExpression(e):"function"!=typeof e&&(e=function(a,b){return a===b});for(var f=[],g=0,h=this.length;h>g;g++)for(var i=c(this[g],g,this),j=0,k=a.length;k>j;j++){var l=b(a[j],j,a);e(l,i)&&f.push(d(a[j],this[g]))}return f},groupJoinWith:function(a,b,c,d,e){if("string"==typeof b)b=LINQ.utils.parseExpression(b);else if("function"!=typeof b)throw new Error("Inner key selector is required");if("string"==typeof c)c=LINQ.utils.parseExpression(c);else if("function"!=typeof c)throw new Error("Outer key selector is required");if("string"==typeof d)d=LINQ.utils.parseExpression(d);else if("function"!=typeof d)throw new Error("Results selector is required");"string"==typeof e?e=LINQ.utils.parseExpression(e):"function"!=typeof e&&(e=function(a,b){return a===b});for(var f=[],g=0,h=this.length;h>g;g++){var i=c(this[g],g,this),j=LINQ.methods.where.apply(a,[function(c,d){return innerKey=b(a[d],d,a),e(innerKey,i)}]);f.push(d(j,this[g]))}return f},contains:function(a,b){var c;if("string"==typeof b){var d=LINQ.utils.parseExpression(b);c=function(b){return d(b,a)}}else c="function"!=typeof b?function(b){return b===a}:function(c){return b(c,a)};return LINQ.methods.any.apply(this,[c])},aggregate:function(a,b){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Aggregator function is required");if(this.length<=0)return b;var c="undefined"==typeof b?LINQ.utils.getDefaultValue(LINQ.utils.getType(this[0])):b;if("function"==typeof Array.prototype.reduce)return this.reduce(a,c);for(var d=0,e=this.length;e>d;d++)c=a(c,this[d],d,this);return c},sum:function(a,b){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),b="undefined"==typeof b?LINQ.utils.getDefaultValue(LINQ.utils.getType(a(this[0],0,this))):b,LINQ.methods.aggregate.apply(this,[function(b,c,d,e){return b+a(c,d,e)},b])},min:function(a){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),this.length<=0?void 0:LINQ.methods.aggregate.apply(this,[function(b,c,d,e){var f=a(c,d,e);return b>f?f:b},a(this[0],0,this)])},max:function(a){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),this.length<=0?void 0:LINQ.methods.aggregate.apply(this,[function(b,c,d,e){var f=a(c,d,e);return f>b?f:b},a(this[0],0,this)])},skip:function(a){if("number"!=typeof a||0>a)throw new TypeError("Count is required and should be a positive number");return this.slice(a,this.length)},take:function(a){if("number"!=typeof a||0>a)throw new TypeError("Count is required and should be a positive number");return this.slice(0,a)},ofType:function(a){if("string"!=typeof a)throw new TypeError("Type is required.");return LINQ.methods.where.apply(this,[function(b){return typeof b===a}])},union:function(a){return this.concat(a)}}},LINQ.methods.firstOrDefault.finalize=!0,LINQ.methods.lastOrDefault.finalize=!0,LINQ.methods.count.finalize=!0,LINQ.methods.any.finalize=!0,LINQ.methods.contains.finalize=!0,LINQ.methods.all.finalize=!0,LINQ.methods.aggregate.finalize=!0,LINQ.methods.sum.finalize=!0,LINQ.methods.min.finalize=!0,LINQ.methods.max.finalize=!0;for(var key in LINQ.methods)LINQ.methods.hasOwnProperty(key)&&(Array.prototype[key]=LINQ.methods[key])}(),function(a){if("undefined"!=typeof ko&&ko.observableArray&&ko.observableArray.fn){Array.prototype.toObservableArray=function(){return ko.observableArray(this)},ko.observableArray.fn.toArray=function(){return this()};for(var b in a.methods)a.methods.hasOwnProperty(b)&&(ko.observableArray.fn[b]=function(b){return function(){return a.methods[b].apply(this(),arguments)}}(b))}}(LINQ); | ||
!function(){var stringTrim=function(a){return"string"!=typeof a||null===a?a:String.prototype.trim?a.trim():a.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,"")},expressionCache={};LINQ={utils:{parseExpression:function(a){if("function"==typeof a)return a;if(null==a||"string"!=typeof a||a.indexOf("=>")<0)throw new SyntaxError('Expression "'+a+'" is invalid');if("function"==typeof expressionCache[a])return expressionCache[a];var b,c=a.split("=>"),d=stringTrim(c[0]).replace(/[\(\)\s]/gi,""),e=stringTrim(c[1]);try{e.indexOf("return")<0&&(e="return ("+e+")"),b=new Function(d,e)}catch(f){b=new Function(d,e)}if("function"!=typeof b)throw new SyntaxError('Expression "'+a+'" is invalid');return expressionCache[a]=b,b},getType:function(a){var b=typeof a;if("object"!==b)return b;if(null===a)return"null";var c=a.constructor,d="function"==typeof c&&c.name;return"string"==typeof d&&d.length>0?d:"object"},getDefaultValue:function(type){if("string"!=typeof type)throw new TypeError("Type must be a string.");switch(type){case"boolean":return!1;case"function":return function(){};case"null":return null;case"number":return 0;case"object":return{};case"string":return"";case"symbol":return Symbol();case"undefined":return}try{var ctor="function"==typeof this[type]?this[type]:eval(type);return new ctor}catch(e){return{}}},isArray:function(a){return Array.isArray?Array.isArray(a):"[object Array]"===Object.prototype.toString.call(a)}},methods:{any:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)return this.length>0;if("function"==typeof Array.prototype.some)return this.some(a);for(var b=0,c=this.length;c>b;b++)if(a(this[b],b,this))return!0;return!1},all:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Predicate is required");if("function"==typeof Array.prototype.every)return this.every(a);var b=function(){return!a.apply(this,arguments)};return!LINQ.methods.any.apply(this,[b])},where:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Predicate is required");if("function"==typeof Array.prototype.filter)return this.filter(a);for(var b=[],c=0,d=this.length;d>c;c++)a(this[c],c,this)&&b.push(this[c]);return b},select:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Selector is required");if("function"==typeof Array.prototype.map)return this.map(a);for(var b=[],c=0,d=this.length;d>c;c++)b.push(a(this[c],c,this));return b},selectMany:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Selector is required");for(var b=[],c=0,d=this.length;d>c;c++){var e=a(this[c],c,this);LINQ.utils.isArray(e)&&b.push(e)}return Array.prototype.concat.apply([],b)},count:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)return this.length;if("function"==typeof Array.prototype.filter)return this.filter(a).length;for(var b=0,c=0,d=this.length;d>c;c++)a(this[c],c,this)&&b++;return b},orderBy:function(a,b){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return b>a?-1:a>b?1:0}),this.slice(0).sort(function(c,d){var e=a(c),f=a(d);return b(e,f)})},orderByDescending:function(a,b){"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return b>a?-1:a>b?1:0});var c=function(){return-1*b.apply(this,arguments)};return LINQ.methods.orderBy.apply(this,[a,c])},groupBy:function(a,b){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Key selector is required");"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return{group:a,values:b}});for(var c={},d=0,e=this.length;e>d;d++){var f=a(this[d],d,this);c[f]||(c[f]=[]),c[f].push(this[d])}var g=[];for(var h in c)c.hasOwnProperty(h)&&g.push(b(h,c[h]));return g},distinct:function(a,b){"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return a===b});for(var c=[],d={},e=0,f=this.length;f>e;e++){var g=a(this[e],e,this);if(d[g]){for(var h=!0,i=0,j=d[g].length;j>i;i++)if(b(d[g][i],g)){h=!1;break}h&&(c.push(this[e]),d[g].push(g))}else d[g]=[g],c.push(this[e])}return c},firstOrDefault:function(a){if("string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(){return!0}),"function"==typeof Array.prototype.find)return this.find(a)||null;for(var b=0,c=this.length;c>b;b++)if(a(this[b],b,this))return this[b];return null},lastOrDefault:function(a){"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(){return!0});for(var b=this.length-1;b>=0;b--)if(a(this[b],b,this))return this[b];return null},joinWith:function(a,b,c,d,e){if("string"==typeof b)b=LINQ.utils.parseExpression(b);else if("function"!=typeof b)throw new Error("Inner key selector is required");if("string"==typeof c)c=LINQ.utils.parseExpression(c);else if("function"!=typeof c)throw new Error("Outer key selector is required");if("string"==typeof d)d=LINQ.utils.parseExpression(d);else if("function"!=typeof d)throw new Error("Results selector is required");"string"==typeof e?e=LINQ.utils.parseExpression(e):"function"!=typeof e&&(e=function(a,b){return a===b});for(var f=[],g=0,h=this.length;h>g;g++)for(var i=c(this[g],g,this),j=0,k=a.length;k>j;j++){var l=b(a[j],j,a);e(l,i)&&f.push(d(a[j],this[g]))}return f},groupJoinWith:function(a,b,c,d,e){if("string"==typeof b)b=LINQ.utils.parseExpression(b);else if("function"!=typeof b)throw new Error("Inner key selector is required");if("string"==typeof c)c=LINQ.utils.parseExpression(c);else if("function"!=typeof c)throw new Error("Outer key selector is required");if("string"==typeof d)d=LINQ.utils.parseExpression(d);else if("function"!=typeof d)throw new Error("Results selector is required");"string"==typeof e?e=LINQ.utils.parseExpression(e):"function"!=typeof e&&(e=function(a,b){return a===b});for(var f=[],g=0,h=this.length;h>g;g++){var i=c(this[g],g,this),j=LINQ.methods.where.apply(a,[function(c,d){return innerKey=b(a[d],d,a),e(innerKey,i)}]);f.push(d(j,this[g]))}return f},contains:function(a,b){var c;if("string"==typeof b){var d=LINQ.utils.parseExpression(b);c=function(b){return d(b,a)}}else c="function"!=typeof b?function(b){return b===a}:function(c){return b(c,a)};return LINQ.methods.any.apply(this,[c])},aggregate:function(a,b){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Aggregator function is required");if(this.length<=0)return b;var c="undefined"==typeof b?LINQ.utils.getDefaultValue(LINQ.utils.getType(this[0])):b;if("function"==typeof Array.prototype.reduce)return this.reduce(a,c);for(var d=0,e=this.length;e>d;d++)c=a(c,this[d],d,this);return c},sum:function(a,b){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),b="undefined"==typeof b?LINQ.utils.getDefaultValue(LINQ.utils.getType(a(this[0],0,this))):b,LINQ.methods.aggregate.apply(this,[function(b,c,d,e){return b+a(c,d,e)},b])},min:function(a){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),this.length<=0?void 0:LINQ.methods.aggregate.apply(this,[function(b,c,d,e){var f=a(c,d,e);return b>f?f:b},a(this[0],0,this)])},max:function(a){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),this.length<=0?void 0:LINQ.methods.aggregate.apply(this,[function(b,c,d,e){var f=a(c,d,e);return f>b?f:b},a(this[0],0,this)])},skip:function(a){if("number"!=typeof a||0>a)throw new TypeError("Count is required and should be a positive number");return this.slice(a,this.length)},take:function(a){if("number"!=typeof a||0>a)throw new TypeError("Count is required and should be a positive number");return this.slice(0,a)},ofType:function(a){if("string"!=typeof a)throw new TypeError("Type is required.");return LINQ.methods.where.apply(this,[function(b){return typeof b===a}])},union:function(a,b){"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return a===b});for(var c=[],d=[].concat(this,a),e=0,f=d.length;f>e;e++){for(var g=!0,h=0,i=c.length;i>h;h++)if(b(d[e],c[h])){g=!1;break}g&&c.push(d[e])}return c},except:function(a,b){"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return a===b});for(var c=[],d=0,e=this.length;e>d;d++){for(var f=!0,g=0,h=a.length;h>g;g++)if(b(this[d],a[g])){f=!1;break}f&&c.push(this[d])}return c}}},LINQ.methods.firstOrDefault.finalize=!0,LINQ.methods.lastOrDefault.finalize=!0,LINQ.methods.count.finalize=!0,LINQ.methods.any.finalize=!0,LINQ.methods.contains.finalize=!0,LINQ.methods.all.finalize=!0,LINQ.methods.aggregate.finalize=!0,LINQ.methods.sum.finalize=!0,LINQ.methods.min.finalize=!0,LINQ.methods.max.finalize=!0;for(var key in LINQ.methods)LINQ.methods.hasOwnProperty(key)&&(Array.prototype[key]=LINQ.methods[key])}(),function(a){if("undefined"!=typeof ko&&ko.observableArray&&ko.observableArray.fn){Array.prototype.toObservableArray=function(){return ko.observableArray(this)},ko.observableArray.fn.toArray=function(){return this()};for(var b in a.methods)a.methods.hasOwnProperty(b)&&(ko.observableArray.fn[b]=function(b){return function(){return a.methods[b].apply(this(),arguments)}}(b))}}(LINQ); |
@@ -25,2 +25,2 @@ /* ! https://github.com/akopachov/mini-linq-js */ | ||
*/ | ||
!function(){var stringTrim=function(a){return"string"!=typeof a||null===a?a:String.prototype.trim?a.trim():a.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,"")},expressionCache={};LINQ={utils:{parseExpression:function(a){if("function"==typeof a)return a;if(null==a||"string"!=typeof a||a.indexOf("=>")<0)throw new SyntaxError('Expression "'+a+'" is invalid');if("function"==typeof expressionCache[a])return expressionCache[a];var b,c=a.split("=>"),d=stringTrim(c[0]).replace(/[\(\)\s]/gi,""),e=stringTrim(c[1]);try{e.indexOf("return")<0&&(e="return ("+e+")"),b=new Function(d,e)}catch(f){b=new Function(d,e)}if("function"!=typeof b)throw new SyntaxError('Expression "'+a+'" is invalid');return expressionCache[a]=b,b},getType:function(a){var b=typeof a;if("object"!==b)return b;if(null===a)return"null";var c=a.constructor,d="function"==typeof c&&c.name;return"string"==typeof d&&d.length>0?d:"object"},getDefaultValue:function(type){if("string"!=typeof type)throw new TypeError("Type must be a string.");switch(type){case"boolean":return!1;case"function":return function(){};case"null":return null;case"number":return 0;case"object":return{};case"string":return"";case"symbol":return Symbol();case"undefined":return}try{var ctor="function"==typeof this[type]?this[type]:eval(type);return new ctor}catch(e){return{}}},isArray:function(a){return Array.isArray?Array.isArray(a):"[object Array]"===Object.prototype.toString.call(a)}},methods:{any:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)return this.length>0;if("function"==typeof Array.prototype.some)return this.some(a);for(var b=0,c=this.length;c>b;b++)if(a(this[b],b,this))return!0;return!1},all:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Predicate is required");if("function"==typeof Array.prototype.every)return this.every(a);var b=function(){return!a.apply(this,arguments)};return!LINQ.methods.any.apply(this,[b])},where:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Predicate is required");if("function"==typeof Array.prototype.filter)return this.filter(a);for(var b=[],c=0,d=this.length;d>c;c++)a(this[c],c,this)&&b.push(this[c]);return b},select:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Selector is required");if("function"==typeof Array.prototype.map)return this.map(a);for(var b=[],c=0,d=this.length;d>c;c++)b.push(a(this[c],c,this));return b},selectMany:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Selector is required");for(var b=[],c=0,d=this.length;d>c;c++){var e=a(this[c],c,this);LINQ.utils.isArray(e)&&b.push(e)}return Array.prototype.concat.apply([],b)},count:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)return this.length;if("function"==typeof Array.prototype.filter)return this.filter(a).length;for(var b=0,c=0,d=this.length;d>c;c++)a(this[c],c,this)&&b++;return b},orderBy:function(a,b){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return b>a?-1:a>b?1:0}),this.slice(0).sort(function(c,d){var e=a(c),f=a(d);return b(e,f)})},orderByDescending:function(a,b){"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return b>a?-1:a>b?1:0});var c=function(){return-1*b.apply(this,arguments)};return LINQ.methods.orderBy.apply(this,[a,c])},groupBy:function(a,b){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Key selector is required");"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return{group:a,values:b}});for(var c={},d=0,e=this.length;e>d;d++){var f=a(this[d],d,this);c[f]||(c[f]=[]),c[f].push(this[d])}var g=[];for(var h in c)c.hasOwnProperty(h)&&g.push(b(h,c[h]));return g},distinct:function(a){"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a});for(var b={},c=[],d=0,e=this.length;e>d;d++){var f=a(this[d],d,this);b[f]||(b[f]=!0,c.push(this[d]))}return c},firstOrDefault:function(a){if("string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(){return!0}),"function"==typeof Array.prototype.find)return this.find(a)||null;for(var b=0,c=this.length;c>b;b++)if(a(this[b],b,this))return this[b];return null},lastOrDefault:function(a){"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(){return!0});for(var b=this.length-1;b>=0;b--)if(a(this[b],b,this))return this[b];return null},joinWith:function(a,b,c,d,e){if("string"==typeof b)b=LINQ.utils.parseExpression(b);else if("function"!=typeof b)throw new Error("Inner key selector is required");if("string"==typeof c)c=LINQ.utils.parseExpression(c);else if("function"!=typeof c)throw new Error("Outer key selector is required");if("string"==typeof d)d=LINQ.utils.parseExpression(d);else if("function"!=typeof d)throw new Error("Results selector is required");"string"==typeof e?e=LINQ.utils.parseExpression(e):"function"!=typeof e&&(e=function(a,b){return a===b});for(var f=[],g=0,h=this.length;h>g;g++)for(var i=c(this[g],g,this),j=0,k=a.length;k>j;j++){var l=b(a[j],j,a);e(l,i)&&f.push(d(a[j],this[g]))}return f},groupJoinWith:function(a,b,c,d,e){if("string"==typeof b)b=LINQ.utils.parseExpression(b);else if("function"!=typeof b)throw new Error("Inner key selector is required");if("string"==typeof c)c=LINQ.utils.parseExpression(c);else if("function"!=typeof c)throw new Error("Outer key selector is required");if("string"==typeof d)d=LINQ.utils.parseExpression(d);else if("function"!=typeof d)throw new Error("Results selector is required");"string"==typeof e?e=LINQ.utils.parseExpression(e):"function"!=typeof e&&(e=function(a,b){return a===b});for(var f=[],g=0,h=this.length;h>g;g++){var i=c(this[g],g,this),j=LINQ.methods.where.apply(a,[function(c,d){return innerKey=b(a[d],d,a),e(innerKey,i)}]);f.push(d(j,this[g]))}return f},contains:function(a,b){var c;if("string"==typeof b){var d=LINQ.utils.parseExpression(b);c=function(b){return d(b,a)}}else c="function"!=typeof b?function(b){return b===a}:function(c){return b(c,a)};return LINQ.methods.any.apply(this,[c])},aggregate:function(a,b){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Aggregator function is required");if(this.length<=0)return b;var c="undefined"==typeof b?LINQ.utils.getDefaultValue(LINQ.utils.getType(this[0])):b;if("function"==typeof Array.prototype.reduce)return this.reduce(a,c);for(var d=0,e=this.length;e>d;d++)c=a(c,this[d],d,this);return c},sum:function(a,b){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),b="undefined"==typeof b?LINQ.utils.getDefaultValue(LINQ.utils.getType(a(this[0],0,this))):b,LINQ.methods.aggregate.apply(this,[function(b,c,d,e){return b+a(c,d,e)},b])},min:function(a){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),this.length<=0?void 0:LINQ.methods.aggregate.apply(this,[function(b,c,d,e){var f=a(c,d,e);return b>f?f:b},a(this[0],0,this)])},max:function(a){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),this.length<=0?void 0:LINQ.methods.aggregate.apply(this,[function(b,c,d,e){var f=a(c,d,e);return f>b?f:b},a(this[0],0,this)])},skip:function(a){if("number"!=typeof a||0>a)throw new TypeError("Count is required and should be a positive number");return this.slice(a,this.length)},take:function(a){if("number"!=typeof a||0>a)throw new TypeError("Count is required and should be a positive number");return this.slice(0,a)},ofType:function(a){if("string"!=typeof a)throw new TypeError("Type is required.");return LINQ.methods.where.apply(this,[function(b){return typeof b===a}])},union:function(a){return this.concat(a)}}},LINQ.methods.firstOrDefault.finalize=!0,LINQ.methods.lastOrDefault.finalize=!0,LINQ.methods.count.finalize=!0,LINQ.methods.any.finalize=!0,LINQ.methods.contains.finalize=!0,LINQ.methods.all.finalize=!0,LINQ.methods.aggregate.finalize=!0,LINQ.methods.sum.finalize=!0,LINQ.methods.min.finalize=!0,LINQ.methods.max.finalize=!0;for(var key in LINQ.methods)LINQ.methods.hasOwnProperty(key)&&(Array.prototype[key]=LINQ.methods[key])}(),function(a){var b=[function(b,c){if(b.name===c.name&&"where"===c.name){var d=a.utils.parseExpression(b.args[0]),e=a.utils.parseExpression(c.args[0]);return function(c,d){return{name:b.name,args:[function(){return c.apply(this,arguments)&&d.apply(this,arguments)}],fn:a.methods[b.name]}}(d,e)}return!1},function(b,c){if(b.name===c.name&&"select"===c.name){var d=a.utils.parseExpression(b.args[0]),e=a.utils.parseExpression(c.args[0]);return function(c,d){return{name:b.name,args:[function(){return d.apply(this,[c.apply(this,arguments)])}],fn:a.methods[b.name]}}(d,e)}return!1},function(a,b){var c=["orderBy","orderByDescending"];return c.contains(a.name)&&c.contains(b.name)?b:!1},function(a,b){return"skip"==a.name&&"take"==b.name?function(a,b){return{name:"skiptake",args:[a,b],fn:function(a,b){return this.slice(a,a+b)}}}(a.args[0],b.args[0]):!1}],c=function(c){var d=this,e=c;d._modificatorQueue=[];var f=function(){for(var a=d._modificatorQueue.length,b=0;a>b;b++){var c=d._modificatorQueue[b];e=c.fn.apply(e,c.args)}return d._modificatorQueue.splice(0,a),a},g=function(){for(var a=0;a<d._modificatorQueue.length-1;){for(var c=d._modificatorQueue[a],e=d._modificatorQueue[a+1],f=!1,g=0,h=b.length;h>g;g++){var i=b[g],j=i(c,e);j&&(f=!0,d._modificatorQueue.splice(a+1,1),d._modificatorQueue[a]=j)}f||a++}};d.optimize=function(){return g(),d},d.toArray=function(){return e.length>100&&d._modificatorQueue.length>2&&d.optimize(),f(),a.utils.isArray(e)?e.slice(0):e}};Array.prototype.toLazy=function(){return new c(this)};for(var d in a.methods)a.methods.hasOwnProperty(d)&&(c.prototype[d]=function(b){return function(){var c=a.methods[b];return this._modificatorQueue.push({fn:a.methods[b],args:arguments,name:b}),c.finalize?this.toArray():this}}(d))}(LINQ); | ||
!function(){var stringTrim=function(a){return"string"!=typeof a||null===a?a:String.prototype.trim?a.trim():a.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g,"")},expressionCache={};LINQ={utils:{parseExpression:function(a){if("function"==typeof a)return a;if(null==a||"string"!=typeof a||a.indexOf("=>")<0)throw new SyntaxError('Expression "'+a+'" is invalid');if("function"==typeof expressionCache[a])return expressionCache[a];var b,c=a.split("=>"),d=stringTrim(c[0]).replace(/[\(\)\s]/gi,""),e=stringTrim(c[1]);try{e.indexOf("return")<0&&(e="return ("+e+")"),b=new Function(d,e)}catch(f){b=new Function(d,e)}if("function"!=typeof b)throw new SyntaxError('Expression "'+a+'" is invalid');return expressionCache[a]=b,b},getType:function(a){var b=typeof a;if("object"!==b)return b;if(null===a)return"null";var c=a.constructor,d="function"==typeof c&&c.name;return"string"==typeof d&&d.length>0?d:"object"},getDefaultValue:function(type){if("string"!=typeof type)throw new TypeError("Type must be a string.");switch(type){case"boolean":return!1;case"function":return function(){};case"null":return null;case"number":return 0;case"object":return{};case"string":return"";case"symbol":return Symbol();case"undefined":return}try{var ctor="function"==typeof this[type]?this[type]:eval(type);return new ctor}catch(e){return{}}},isArray:function(a){return Array.isArray?Array.isArray(a):"[object Array]"===Object.prototype.toString.call(a)}},methods:{any:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)return this.length>0;if("function"==typeof Array.prototype.some)return this.some(a);for(var b=0,c=this.length;c>b;b++)if(a(this[b],b,this))return!0;return!1},all:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Predicate is required");if("function"==typeof Array.prototype.every)return this.every(a);var b=function(){return!a.apply(this,arguments)};return!LINQ.methods.any.apply(this,[b])},where:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Predicate is required");if("function"==typeof Array.prototype.filter)return this.filter(a);for(var b=[],c=0,d=this.length;d>c;c++)a(this[c],c,this)&&b.push(this[c]);return b},select:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Selector is required");if("function"==typeof Array.prototype.map)return this.map(a);for(var b=[],c=0,d=this.length;d>c;c++)b.push(a(this[c],c,this));return b},selectMany:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Selector is required");for(var b=[],c=0,d=this.length;d>c;c++){var e=a(this[c],c,this);LINQ.utils.isArray(e)&&b.push(e)}return Array.prototype.concat.apply([],b)},count:function(a){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)return this.length;if("function"==typeof Array.prototype.filter)return this.filter(a).length;for(var b=0,c=0,d=this.length;d>c;c++)a(this[c],c,this)&&b++;return b},orderBy:function(a,b){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return b>a?-1:a>b?1:0}),this.slice(0).sort(function(c,d){var e=a(c),f=a(d);return b(e,f)})},orderByDescending:function(a,b){"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return b>a?-1:a>b?1:0});var c=function(){return-1*b.apply(this,arguments)};return LINQ.methods.orderBy.apply(this,[a,c])},groupBy:function(a,b){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Key selector is required");"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return{group:a,values:b}});for(var c={},d=0,e=this.length;e>d;d++){var f=a(this[d],d,this);c[f]||(c[f]=[]),c[f].push(this[d])}var g=[];for(var h in c)c.hasOwnProperty(h)&&g.push(b(h,c[h]));return g},distinct:function(a,b){"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return a===b});for(var c=[],d={},e=0,f=this.length;f>e;e++){var g=a(this[e],e,this);if(d[g]){for(var h=!0,i=0,j=d[g].length;j>i;i++)if(b(d[g][i],g)){h=!1;break}h&&(c.push(this[e]),d[g].push(g))}else d[g]=[g],c.push(this[e])}return c},firstOrDefault:function(a){if("string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(){return!0}),"function"==typeof Array.prototype.find)return this.find(a)||null;for(var b=0,c=this.length;c>b;b++)if(a(this[b],b,this))return this[b];return null},lastOrDefault:function(a){"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(){return!0});for(var b=this.length-1;b>=0;b--)if(a(this[b],b,this))return this[b];return null},joinWith:function(a,b,c,d,e){if("string"==typeof b)b=LINQ.utils.parseExpression(b);else if("function"!=typeof b)throw new Error("Inner key selector is required");if("string"==typeof c)c=LINQ.utils.parseExpression(c);else if("function"!=typeof c)throw new Error("Outer key selector is required");if("string"==typeof d)d=LINQ.utils.parseExpression(d);else if("function"!=typeof d)throw new Error("Results selector is required");"string"==typeof e?e=LINQ.utils.parseExpression(e):"function"!=typeof e&&(e=function(a,b){return a===b});for(var f=[],g=0,h=this.length;h>g;g++)for(var i=c(this[g],g,this),j=0,k=a.length;k>j;j++){var l=b(a[j],j,a);e(l,i)&&f.push(d(a[j],this[g]))}return f},groupJoinWith:function(a,b,c,d,e){if("string"==typeof b)b=LINQ.utils.parseExpression(b);else if("function"!=typeof b)throw new Error("Inner key selector is required");if("string"==typeof c)c=LINQ.utils.parseExpression(c);else if("function"!=typeof c)throw new Error("Outer key selector is required");if("string"==typeof d)d=LINQ.utils.parseExpression(d);else if("function"!=typeof d)throw new Error("Results selector is required");"string"==typeof e?e=LINQ.utils.parseExpression(e):"function"!=typeof e&&(e=function(a,b){return a===b});for(var f=[],g=0,h=this.length;h>g;g++){var i=c(this[g],g,this),j=LINQ.methods.where.apply(a,[function(c,d){return innerKey=b(a[d],d,a),e(innerKey,i)}]);f.push(d(j,this[g]))}return f},contains:function(a,b){var c;if("string"==typeof b){var d=LINQ.utils.parseExpression(b);c=function(b){return d(b,a)}}else c="function"!=typeof b?function(b){return b===a}:function(c){return b(c,a)};return LINQ.methods.any.apply(this,[c])},aggregate:function(a,b){if("string"==typeof a)a=LINQ.utils.parseExpression(a);else if("function"!=typeof a)throw new Error("Aggregator function is required");if(this.length<=0)return b;var c="undefined"==typeof b?LINQ.utils.getDefaultValue(LINQ.utils.getType(this[0])):b;if("function"==typeof Array.prototype.reduce)return this.reduce(a,c);for(var d=0,e=this.length;e>d;d++)c=a(c,this[d],d,this);return c},sum:function(a,b){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),b="undefined"==typeof b?LINQ.utils.getDefaultValue(LINQ.utils.getType(a(this[0],0,this))):b,LINQ.methods.aggregate.apply(this,[function(b,c,d,e){return b+a(c,d,e)},b])},min:function(a){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),this.length<=0?void 0:LINQ.methods.aggregate.apply(this,[function(b,c,d,e){var f=a(c,d,e);return b>f?f:b},a(this[0],0,this)])},max:function(a){return"string"==typeof a?a=LINQ.utils.parseExpression(a):"function"!=typeof a&&(a=function(a){return a}),this.length<=0?void 0:LINQ.methods.aggregate.apply(this,[function(b,c,d,e){var f=a(c,d,e);return f>b?f:b},a(this[0],0,this)])},skip:function(a){if("number"!=typeof a||0>a)throw new TypeError("Count is required and should be a positive number");return this.slice(a,this.length)},take:function(a){if("number"!=typeof a||0>a)throw new TypeError("Count is required and should be a positive number");return this.slice(0,a)},ofType:function(a){if("string"!=typeof a)throw new TypeError("Type is required.");return LINQ.methods.where.apply(this,[function(b){return typeof b===a}])},union:function(a,b){"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return a===b});for(var c=[],d=[].concat(this,a),e=0,f=d.length;f>e;e++){for(var g=!0,h=0,i=c.length;i>h;h++)if(b(d[e],c[h])){g=!1;break}g&&c.push(d[e])}return c},except:function(a,b){"string"==typeof b?b=LINQ.utils.parseExpression(b):"function"!=typeof b&&(b=function(a,b){return a===b});for(var c=[],d=0,e=this.length;e>d;d++){for(var f=!0,g=0,h=a.length;h>g;g++)if(b(this[d],a[g])){f=!1;break}f&&c.push(this[d])}return c}}},LINQ.methods.firstOrDefault.finalize=!0,LINQ.methods.lastOrDefault.finalize=!0,LINQ.methods.count.finalize=!0,LINQ.methods.any.finalize=!0,LINQ.methods.contains.finalize=!0,LINQ.methods.all.finalize=!0,LINQ.methods.aggregate.finalize=!0,LINQ.methods.sum.finalize=!0,LINQ.methods.min.finalize=!0,LINQ.methods.max.finalize=!0;for(var key in LINQ.methods)LINQ.methods.hasOwnProperty(key)&&(Array.prototype[key]=LINQ.methods[key])}(),function(a){var b=[function(b,c){if(b.name===c.name&&"where"===c.name){var d=a.utils.parseExpression(b.args[0]),e=a.utils.parseExpression(c.args[0]);return function(c,d){return{name:b.name,args:[function(){return c.apply(this,arguments)&&d.apply(this,arguments)}],fn:a.methods[b.name]}}(d,e)}return!1},function(b,c){if(b.name===c.name&&"select"===c.name){var d=a.utils.parseExpression(b.args[0]),e=a.utils.parseExpression(c.args[0]);return function(c,d){return{name:b.name,args:[function(){return d.apply(this,[c.apply(this,arguments)])}],fn:a.methods[b.name]}}(d,e)}return!1},function(a,b){var c=["orderBy","orderByDescending"];return c.contains(a.name)&&c.contains(b.name)?b:!1},function(a,b){return"skip"==a.name&&"take"==b.name?function(a,b){return{name:"skiptake",args:[a,b],fn:function(a,b){return this.slice(a,a+b)}}}(a.args[0],b.args[0]):!1}],c=function(c){var d=this,e=c;d._modificatorQueue=[];var f=function(){for(var a=d._modificatorQueue.length,b=0;a>b;b++){var c=d._modificatorQueue[b];e=c.fn.apply(e,c.args)}return d._modificatorQueue.splice(0,a),a},g=function(){for(var a=0;a<d._modificatorQueue.length-1;){for(var c=d._modificatorQueue[a],e=d._modificatorQueue[a+1],f=!1,g=0,h=b.length;h>g;g++){var i=b[g],j=i(c,e);j&&(f=!0,d._modificatorQueue.splice(a+1,1),d._modificatorQueue[a]=j)}f||a++}};d.optimize=function(){return g(),d},d.toArray=function(){return e.length>100&&d._modificatorQueue.length>2&&d.optimize(),f(),a.utils.isArray(e)?e.slice(0):e}};Array.prototype.toLazy=function(){return new c(this)};for(var d in a.methods)a.methods.hasOwnProperty(d)&&(c.prototype[d]=function(b){return function(){var c=a.methods[b];return this._modificatorQueue.push({fn:a.methods[b],args:arguments,name:b}),c.finalize?this.toArray():this}}(d))}(LINQ); |
{ | ||
"name": "mini-linq-js", | ||
"version": "1.2.5", | ||
"version": "1.3.0", | ||
"description": "LINQ for JavaScript library, which allows to work with arrays in a more easy way and focus on business logic.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/akopachov/mini-linq-js", |
@@ -49,2 +49,3 @@ # mini-linq.js | ||
* [union](#union) | ||
* [except](#except) | ||
@@ -200,5 +201,5 @@ ## Terms | ||
###### Description: | ||
`.distinct` selects distinct elements by using [selector](#selector) as a key or element if [selector](#selector) is not specified. | ||
`.distinct` selects distinct elements by using [selector](#selector) as a key or element if [selector](#selector) is not specified, and [comparator](#comparator) (optional) to compare equality of keys | ||
###### Arguments: | ||
`.distinct` may accept [selector](#selector). | ||
`.distinct` may accept [selector](#selector) as a first argument and [comparator](#comparator) as a second argument. | ||
###### Returns: | ||
@@ -209,3 +210,5 @@ Array of distinct elements. | ||
[2, 1, 2, 3, 1, 6, 7, 3, 2].distinct(); // will return [2, 1, 3, 6, 7] | ||
[2, 1, 2, 3, 1, 6, 7, 3, 2].distinct(d => d % 3) // will return [2, 1, 3] | ||
[2, 1, 2, 3, 1, 6, 7, 3, 2].distinct(d => d % 3); // will return [2, 1, 3] | ||
[1, 2, '2', '3', 3, 4, 5, 8, 5].distinct(); // will return [1, 2, '2', '3', 3, 4, 5, 8] (default comparator is "a === b"; | ||
[1, 2, '2', '3', 3, 4, 5, 8, 5].distinct('x => x', '(a, b) => a == b'); // will return [1, 2, '3', 4, 5, 8] (here we used custom comparator) | ||
``` | ||
@@ -415,15 +418,32 @@ --- | ||
###### Description: | ||
`.union` combines two arrays together into single array. | ||
`.union` produces the set union of arrays by using the [comparator](#comparator) (optional) to compare values. | ||
###### Arguments: | ||
`.union` accepts array to combine. | ||
`.union` accepts array to combine and may accept [comparator](#comparator) as a second argument. | ||
###### Returns: | ||
Array of elemnts from source arrays. | ||
Array of merged elements from source arrays. | ||
###### Example of usage: | ||
```javascript | ||
[1, 2, 3, 4].union([2, 3, 4, 5]); // will return [1, 2, 3, 4, 2, 3, 4, 5] | ||
[1, 2, 3, 4].union([2, 3, 4, 5]); // will return [1, 2, 3, 4, 5] | ||
[1, 2, 3, 4].union([]); // will return [1, 2, 3, 4] | ||
[].union([]); // will return [] | ||
[1, 2, 3, 4].union([2, '3', '4', 5], '(a, b) => a == b'); // will return [1, 2, 3, 4, 5]; | ||
``` | ||
--- | ||
### <a name="except">.except</a> | ||
###### Description: | ||
`.except` produces the set difference of two arrays by using the [comparator](#comparator) (optional) to compare values. | ||
###### Arguments: | ||
`.except` accepts array to compare and may accept [comparator](#comparator) as a second argument. | ||
###### Returns: | ||
Array of differences of source arrays. | ||
###### Example of usage: | ||
```javascript | ||
[1, 2, 3, 4].except([3, 4, 5]); // will return [1, 2] | ||
[1, 2, 3, 4].except([5, 6, 7]); // will return [1, 2, 3, 4] | ||
[1, 2, 3, 4].except([1, 2, 3, 4]); // will return [] | ||
[1, 2, 3, 4].except(['3', 4, '5'], '(a, b) => a == b'); // will return [1, 2] | ||
``` | ||
--- | ||
## <a name="lazyArrays">Lazy array</a> | ||
@@ -430,0 +450,0 @@ By default all mini-linq methods will execute their logic instantly and return result. But in some cases it may be useful to postpone execution, until some conditions take place. |
@@ -322,3 +322,3 @@ /* ! https://github.com/akopachov/mini-linq-js */ | ||
distinct: function (selector) { | ||
distinct: function (selector, comparator) { | ||
if (typeof (selector) === "string") { | ||
@@ -330,9 +330,28 @@ selector = LINQ.utils.parseExpression(selector); | ||
if (typeof (comparator) === "string") { | ||
comparator = LINQ.utils.parseExpression(comparator); | ||
} else if (typeof(comparator) !== 'function') { | ||
comparator = function(a, b) { return a === b; } | ||
} | ||
var unique = []; | ||
var uniqueMap = {}; | ||
var unique = []; | ||
for (var i = 0, l = this.length; i < l; i++) { | ||
var key = selector(this[i], i, this); | ||
if (!uniqueMap[key]) { | ||
uniqueMap[key] = true; | ||
uniqueMap[key] = [key]; | ||
unique.push(this[i]); | ||
} else { | ||
var isUnique = true; | ||
for (var j = 0, jl = uniqueMap[key].length; j < jl; j++) { | ||
if (comparator(uniqueMap[key][j], key)) { | ||
isUnique = false; | ||
break; | ||
} | ||
} | ||
if (isUnique) { | ||
unique.push(this[i]); | ||
uniqueMap[key].push(key); | ||
} | ||
} | ||
@@ -557,4 +576,51 @@ } | ||
union: function(anotherCollection) { | ||
return this.concat(anotherCollection); | ||
union: function(anotherCollection, comparator) { | ||
if (typeof (comparator) === "string") { | ||
comparator = LINQ.utils.parseExpression(comparator); | ||
} else if (typeof (comparator) !== "function") { | ||
comparator = function (a, b) { return a === b; } | ||
} | ||
var result = []; | ||
var allValues = [].concat(this, anotherCollection); | ||
for (var i = 0, l = allValues.length; i < l; i++) { | ||
var addToResult = true; | ||
for (var j = 0, rl = result.length; j < rl; j++) { | ||
if (comparator(allValues[i], result[j])) { | ||
addToResult = false; | ||
break; | ||
} | ||
} | ||
if (addToResult) { | ||
result.push(allValues[i]); | ||
} | ||
} | ||
return result; | ||
}, | ||
except: function(anotherCollection, comparator) { | ||
if (typeof (comparator) === "string") { | ||
comparator = LINQ.utils.parseExpression(comparator); | ||
} else if (typeof (comparator) !== "function") { | ||
comparator = function (a, b) { return a === b; } | ||
} | ||
var result = []; | ||
for (var i = 0, l = this.length; i < l; i++) { | ||
var addToResult = true; | ||
for (var j = 0, rl = anotherCollection.length; j < rl; j++) { | ||
if (comparator(this[i], anotherCollection[j])) { | ||
addToResult = false; | ||
break; | ||
} | ||
} | ||
if (addToResult) { | ||
result.push(this[i]); | ||
} | ||
} | ||
return result; | ||
} | ||
@@ -561,0 +627,0 @@ } |
@@ -176,2 +176,4 @@ if (typeof(require) !== 'undefined') { | ||
}); | ||
test.array([1, 2, '2', '3', 3, 4, 5, 8, 5].distinct()).is([1, 2, '2', '3', 3, 4, 5, 8]); | ||
test.array([1, 2, '2', '3', 3, 4, 5, 8, 5].distinct('x => x', '(a, b) => a == b')).is([1, 2, '3', 4, 5, 8]); | ||
}); | ||
@@ -289,7 +291,16 @@ | ||
it('.union', function() { | ||
test.array([1, 2, 3, 4].union([2, 3, 4, 5])).is([1, 2, 3, 4, 2, 3, 4, 5]); | ||
test.array([1, 2, 3, 4].union([2, 3, 4, 5])).is([1, 2, 3, 4, 5]); | ||
test.array([1, 2, 3, 4].union([5, 6, 7, 8])).is([1, 2, 3, 4, 5, 6, 7, 8]); | ||
test.array([1, 2, 3, 4].union([])).is([1, 2, 3, 4]); | ||
test.array([].union([])).is([]); | ||
test.array([1, 2, 3, 4].union([2, '3', '4', 5], '(a, b) => a == b')).is([1, 2, 3, 4, 5]); | ||
}); | ||
it('.except', function() { | ||
test.array([1, 2, 3, 4].except([3, 4, 5])).is([1, 2]); | ||
test.array([1, 2, 3, 4].except([5, 6, 7])).is([1, 2, 3, 4]); | ||
test.array([1, 2, 3, 4].except([1, 2, 3, 4])).is([]); | ||
test.array([1, 2, 3, 4].except(['3', 4, '5'], '(a, b) => a == b')).is([1, 2]); | ||
}); | ||
it('.selectMany', function() { | ||
@@ -296,0 +307,0 @@ var testArray2 = [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1862113
31396
463