-
Notifications
You must be signed in to change notification settings - Fork 0
/
linq.min.js
1 lines (1 loc) · 4.24 KB
/
linq.min.js
1
var clone=function(n){var t,i,u,r;if(!n||typeof n!="object")return n;if(n instanceof Array){for(t=[],i=0,u=n.length;i<u;i++)t[i]=clone(n[i]);return t}if(n instanceof Date)return t=new Date,t.setTime(n.getTime()),t;if(n instanceof Object){t={};for(r in n)n.hasOwnProperty(r)&&(t[r]=clone(n[r]));return t}throw new Error("Unable to copy obj! Its type isn't supported.");},generateHash=function(n){var t,i;if(n instanceof Array)return n.reduce(function(n,t){return n+generateHash(t)},"");if(n instanceof Date)return n.toISOString();if(n instanceof Object){t="";for(i in n)n.hasOwnProperty(i)&&(t+=generateHash(n[i]));return t}return typeof n=="string"?n:n?n.toString():""};Array.prototype.aggregate=function(n,t,i){var r,u;if(!this.length)throw new Error("Sequence contains no elements");if(!n)throw new Error("Argument cannot be null. Parameter name: func");for(r=t||clone(this[0]),u=t?0:1;u<this.length;u++)r=n(r,this[u]);return i?i(r):r};Array.prototype.all=function(n){return n?this.every(n):!0};Array.prototype.any=function(n){return n?this.some(n):!!this.length};Array.prototype.average=function(n){if(!this.length)throw new Error("Sequence contains no elements");return this.sum(n)/this.length};Array.prototype.contains=function(n){return this.indexOf(n)!==-1};Array.prototype.distinct=function(n){var t=[],i=[];return this.forEach(function(r){var u=n?n(r):r;i.contains(r)||(i.push(u),t.push(r))}),t};Array.prototype.first=function(n){if(!this.length)throw new Error("Sequence contains no elements");var t=n?this.where(n):this;return t.length?t[0]:null};Array.prototype.groupBy=function(n,t,i){if(!n)throw new Error("Argument cannot be null. Parameter name: keySelector");var r=[],u={};return this.forEach(function(i){var e=n(i),o=generateHash(e),f=u[o];f||(r.push(u[o]=f=[]),f.key=e);f.push(t?t(i):i)}),i&&r.forEach(function(n,t){r[t]=i(n.key,n)}),r};Array.prototype.last=function(n){if(!this.length)throw new Error("Sequence contains no elements");var t=n?this.where(n):this;return t.length?t[t.length-1]:null};Array.prototype.max=function(n){if(!this.length)throw new Error("Sequence contains no elements");var t=Number.MIN_VALUE;return(n?this.select(n):this).forEach(function(n){n>t&&(t=n)}),t};Array.prototype.min=function(n){if(!this.length)throw new Error("Sequence contains no elements");var t=Number.MAX_VALUE;return(n?this.select(n):this).forEach(function(n){n<t&&(t=n)}),t};Array.prototype.orderBy=function(n){if(!n)throw new Error("Argument cannot be null. Parameter name: selector");var t=[].concat(this);return t.sort(function(t,i){var r,u;for(t=generateHash(n(t)),i=generateHash(n(i)),r=0;r<t.length&&r<i.length;r++)if(u=t.charCodeAt(r)-i.charCodeAt(r),u!==0)return u;return Number(t)-Number(i)}),t};Array.prototype.orderByDescending=function(n){if(!n)throw new Error("Argument cannot be null. Parameter name: selector");var t=[].concat(this);return t.sort(function(t,i){var r,u;for(t=generateHash(n(t)),i=generateHash(n(i)),r=0;r<t.length&&r<i.length;r++)if(u=i.charCodeAt(r)-t.charCodeAt(r),u!==0)return u;return Number(i)-Number(t)}),t};Array.prototype.select=function(n){if(!n)throw new Error("Argument cannot be null. Parameter name: selector");var t=[];return this.forEach(function(i,r){return t.push(n(i,r))}),t};Array.prototype.selectMany=function(n,t){if(!n)throw new Error("Argument cannot be null. Parameter name: collectionSelector");var i=[];return this.forEach(function(r,u){var f=n(r,u);t?i.push(t(r,f)):f.forEach(function(n){return i.push(n)})}),i};Array.prototype.skip=function(n){return this.slice(n)};Array.prototype.skipWhile=function(n){var i,t,r;if(!n)throw new Error("Argument cannot be null. Parameter name: predicate");for(i=[],t=0;t<this.length;t++)(r=this[t],n(r,t))||i.push(r);return i};Array.prototype.sum=function(n){if(!this.length)throw new Error("Sequence contains no elements");return(n?this.select(n):this).reduce(function(n,t){return n+t},0)};Array.prototype.take=function(n){return this.slice(0,n)};Array.prototype.takeWhile=function(n){var i,t,r;if(!n)throw new Error("Argument cannot be null. Parameter name: predicate");for(i=[],t=0;t<this.length;t++){if(r=this[t],!n(r,t))break;i.push(r)}return i};Array.prototype.where=function(n){if(!n)throw new Error("Argument cannot be null. Parameter name: predicate");var t=[];return this.forEach(function(i,r){n(i,r)&&t.push(i)}),t};