forked from dtao/lazy.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazy.min.js
21 lines (21 loc) · 10.3 KB
/
lazy.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
(function(h){function L(a,b,d){var c,f=0;for(b=eval(""+b+(!b.global?"g":""));c=b.exec(a);){if(!1===d(a.substring(f,c.index)))return;f=c.index+c[0].length}f<a.length&&d(a.substring(f))}function n(a){var b={};Lazy(a||[]).flatten().each(function(a){b[a]=!0});return b}function p(a,b,d){return"function"===typeof d?p(d(a),d(b)):a===b?0:a>b?1:-1}function q(a,b){for(var d=-1;++d<a.length;)if(a[d]instanceof Array){if(!1===q(a[d],b))return!1}else if(!1===b(a[d]))return!1}function M(a){return Array(a).join(" ")}
var c=function(a){this.parent=a};c.inherit=function(a){a.prototype=new c;return a};c.prototype.depth=function(){return this.parent?this.parent.depth()+1:0};c.prototype.log=function(a){console.log(M(this.depth())+a)};c.prototype.toArray=function(){var a=[];this.each(function(b){a.push(b)});return a};c.prototype.toObject=function(){var a={};this.each(function(b){a[b[0]]=b[1]});return a};c.prototype.map=function(a){return this.indexed?new r(this,a):new s(this,a)};c.prototype.pluck=function(a){return this.map(function(b){return b[a]})};
c.prototype.invoke=function(a){return this.map(function(b){return b[a]()})};c.prototype.select=c.prototype.filter=function(a){return this.indexed?new t(this,a):new u(this,a)};c.prototype.reject=function(a){return this.filter(function(b){return!a(b)})};c.prototype.where=function(a){return this.filter(function(b){for(var d in a)if(b[d]!==a[d])return!1;return!0})};c.prototype.reverse=function(){return this.indexed?new v(this):new w(this)};c.prototype.concat=function(){return new x(this,Array.prototype.slice.call(arguments,
0))};c.prototype.first=c.prototype.head=c.prototype.take=function(a){if("undefined"===typeof a){if(this.indexed)a=this.get(0);else{var b;this.each(function(a){b=a;return!1});a=b}return a}return this.indexed?new y(this,a):new z(this,a)};c.prototype.initial=function(a){"undefined"===typeof a&&(a=1);return this.take(this.length()-a)};c.prototype.last=function(a){return"undefined"===typeof a?this.reverse().first():this.reverse().take(a).reverse()};c.prototype.findWhere=function(a){return this.where(a).first()};
c.prototype.rest=c.prototype.tail=c.prototype.drop=function(a){return this.indexed?new l(this,a):new A(this,a)};c.prototype.sortBy=function(a){return new B(this,a)};c.prototype.groupBy=function(a){return new N(this,a)};c.prototype.countBy=function(a){return new O(this,a)};c.prototype.uniq=function(){return new C(this)};c.prototype.zip=function(){return new D(this,Array.prototype.slice.call(arguments,0))};c.prototype.shuffle=function(){return new E(this)};c.prototype.flatten=function(){return new F(this)};
c.prototype.compact=function(){return this.filter(function(a){return!!a})};c.prototype.without=c.prototype.difference=function(){return new G(this,Array.prototype.slice.call(arguments,0))};c.prototype.union=function(){return new H(this,Array.prototype.slice.call(arguments,0))};c.prototype.intersection=function(){return new I(this,Array.prototype.slice.call(arguments,0))};c.prototype.every=c.prototype.all=function(a){var b=!0;this.each(function(d){if(!a(d))return b=!1});return b};c.prototype.some=
c.prototype.any=function(a){a||(a=function(){return!0});var b=!1;this.each(function(d){if(a(d))return b=!0,!1});return b};c.prototype.isEmpty=function(){return!this.any()};c.prototype.indexOf=function(a){var b=0,d=-1;this.each(function(c){if(c===a)return d=b,!1;++b});return d};c.prototype.lastIndexOf=function(a){a=this.reverse().indexOf(a);-1!==a&&(a=this.length()-a-1);return a};c.prototype.sortedIndex=function(a){for(var b=0,d=this.length(),c;b<d;)c=b+d>>>1,this.get(c)<a?b=c+1:d=c;return b};c.prototype.contains=
function(a){return-1!==this.indexOf(a)};c.prototype.reduce=c.prototype.inject=c.prototype.foldl=function(a,b){this.each(function(d){b=a(b,d)});return b};c.prototype.reduceRight=c.prototype.foldr=function(a,b){return this.reverse().reduce(a,b)};c.prototype.find=c.prototype.detect=function(a){return this.filter(a).first()};c.prototype.min=function(){return this.reduce(function(a,b){return"undefined"===typeof a?b:b<a?b:a})};c.prototype.max=function(){return this.reduce(function(a,b){return"undefined"===
typeof a?b:b>a?b:a})};c.prototype.join=function(a){var b="";this.each(function(d){0<b.length&&(b+=a);b+=d});return b};c.prototype.getIterator=function(){return new m(this)};var m=function(a){this.sequence=a;this.index=-1};m.prototype.current=function(){return this.sequence.get(this.index)};m.prototype.moveNext=function(){if(this.index>=this.sequence.length()-1)return!1;++this.index;return!0};var j=c.inherit(function(a){this.source=a});j.prototype.indexed=!0;j.prototype.get=function(a){return this.source[a]};
j.prototype.length=function(){return this.source.length};j.prototype.each=function(a){for(var b=-1;++b<this.source.length&&!1!==a(this.source[b]););};j.prototype.toArray=function(){return this.source.slice(0)};var g=c.inherit(function(){});g.inherit=function(a){a.prototype=new g;return a};g.prototype.indexed=!0;g.prototype.get=function(a){return this.parent.get(a)};g.prototype.length=function(){return this.parent.length()};g.prototype.each=function(a){for(var b=this.length(),d=-1;++d<b&&!1!==a(this.get(d)););
};var e=c.inherit(function(){});e.inherit=function(a){a.prototype=new e;return a};e.prototype.indexed=!1;e.prototype.cache=function(){this.cached||(this.cached=this.toArray());return this.cached};e.prototype.get=function(a){return this.cache()[a]};e.prototype.length=function(){return this.cache().length};var s=c.inherit(function(a,b){this.parent=a;this.mapFn=b});s.prototype.each=function(a){var b=this.mapFn;this.parent.each(function(d){return a(b(d))})};var r=g.inherit(function(a,b){this.parent=a;
this.mapFn=b});r.prototype.get=function(a){return this.mapFn(this.parent.get(a))};var u=e.inherit(function(a,b){this.parent=a;this.filterFn=b});u.prototype.each=function(a){var b=this.filterFn;this.parent.each(function(d){if(b(d))return a(d)})};var t=e.inherit(function(a,b){this.parent=a;this.filterFn=b});t.prototype.each=function(a){for(var b=this.parent,d=this.filterFn,c=this.parent.length(),f=-1,e;++f<c&&!(e=b.get(f),d(e)&&!1===a(e)););};var w=e.inherit(function(a){this.parent=a});w.prototype.each=
function(a){for(var b=this.parent.toArray(),d=b.length;0<=--d&&!1!==a(b[d]););};var v=g.inherit(function(a){this.parent=a});v.prototype.get=function(a){return this.parent.get(this.length()-a-1)};var x=c.inherit(function(a,b){this.parent=a;this.arrays=b});x.prototype.each=function(a){var b=!1;this.parent.each(function(d){if(!1===a(d))return b=!0,!1});b||Lazy(this.arrays).flatten().each(a)};var z=e.inherit(function(a,b){this.parent=a;this.count=b});z.prototype.each=function(a){var b=this,d=0;b.parent.each(function(c){c=
a(c);return++d>=b.count?!1:c})};var y=g.inherit(function(a,b){this.parent=a;this.count=b});y.prototype.length=function(){var a=this.parent.length();return this.count<=a?this.count:a};var A=e.inherit(function(a,b){this.parent=a;this.count=b});A.prototype.each=function(a){var b=this,d=0;b.parent.each(function(c){if(!(d++<b.count))return a(c)})};var l=g.inherit(function(a,b){this.parent=a;this.count=b});l.prototype.get=function(a){return this.parent.get(this.count+a)};l.prototype.length=function(){var a=
this.parent.length();return this.count<=a?a-this.count:0};var B=e.inherit(function(a,b){this.parent=a;this.sortFn=b});B.prototype.each=function(a){var b=this.sortFn,d=this.parent.toArray(),c=-1;for(d.sort(function(a,d){return p(a,d,b)});++c<d.length&&!1!==a(d[c]););};var E=e.inherit(function(a){this.parent=a});E.prototype.each=function(a){for(var b=this.parent.toArray(),d=Math.floor,c=Math.random,f=b.length-1;0<f;--f){var e=b,g=d(c()*f)+1,h=e[f];e[f]=e[g];e[g]=h;if(!1===a(b[f]))return}a(b[0])};var N=
e.inherit(function(a,b){this.each=function(d){var c={};a.each(function(a){var d=b(a);c[d]?c[d].push(a):c[d]=[a]});for(var f in c)if(!1===d([f,c[f]]))break}}),O=e.inherit(function(a,b){this.each=function(d){var c={};a.each(function(a){a=b(a);c[a]=c[a]?c[a]+1:1});for(var f in c)d([f,c[f]])}}),C=e.inherit(function(a){this.parent=a});C.prototype.each=function(a){var b={};this.parent.each(function(c){if(!b[c])return b[c]=!0,a(c)})};var F=e.inherit(function(a){this.parent=a});F.prototype.each=function(a){this.parent.each(function(b){return b instanceof
Array?q(b,a):a(b)})};var G=e.inherit(function(a,b){this.parent=a;this.values=b});G.prototype.each=function(a){var b=n(this.values);this.parent.each(function(c){if(!b[c])return a(c)})};var H=e.inherit(function(a,b){this.parent=a;this.arrays=b});H.prototype.each=function(a){var b={},c=!1;this.parent.each(function(e){if(!b[e]&&(b[e]=!0,!1===a(e)))return c=!0,!1});c||Lazy(this.arrays).each(function(e){if(c)return!1;Lazy(e).each(function(e){if(!b[e]&&(b[e]=!0,!1===a(e)))return c=!0,!1})})};var I=e.inherit(function(a,
b){this.parent=a;this.arrays=b});I.prototype.each=function(a){var b=Lazy(this.arrays).map(function(a){return n(a)}).toArray();this.parent.each(function(c){for(var e=0;e<b.length;++e)if(!b[e][c])return;return a(c)})};var D=e.inherit(function(a,b){this.parent=a;this.arrays=b});D.prototype.each=function(a){var b=this.arrays,c=0;this.parent.each(function(e){e=[e];for(var f=0;f<b.length;++f)b[f].length>c&&e.push(b[f][c]);++c;return a(e)})};var k=c.inherit(function(a,b){this.get=a;this.fixedLength=b});
k.prototype.length=function(){return this.fixedLength};k.prototype.each=function(a){for(var b=this.get,c=this.fixedLength,e=0;("undefined"===typeof c||e<c)&&!1!==a(b(e++)););};var J=c.inherit(function(a,b){this.parent=a;this.interval=b||0});J.prototype.each=function(a){var b=this.parent.getIterator(),c=this.interval;b.moveNext()&&setTimeout(function f(){!1!==a(b.current())&&b.moveNext()&&setTimeout(f,c)},c)};var K=c.inherit(function(a,b){this.source=a;this.pattern=b});K.prototype.each=function(a){if(this.pattern instanceof
RegExp)L(this.source,this.pattern,a);else a:{for(var b=this.source,c=this.pattern,e=0,f=b.indexOf(c);-1!==f;){if(!1===a(b.substring(e,f)))break a;e=f+c.length;f=b.indexOf(c,e)}e<b.length&&a(b.substring(e))}};h.Lazy=function(a){return a instanceof Lazy.Sequence?a:new j(a)};h.Lazy.async=function(a,b){return new J(new j(a),b)};h.Lazy.split=function(a,b){return new K(a,b)};h.Lazy.generate=function(a){return new k(a)};h.Lazy.range=function(){var a=1<arguments.length?arguments[0]:0,b=1<arguments.length?
arguments[1]:arguments[0],c=2<arguments.length?arguments[2]:1;return this.generate(function(b){return a+c*b}).take(Math.floor((b-a)/c))};h.Lazy.Sequence=c;h.Lazy.IndexedSequence=g;h.Lazy.CachingSequence=e;h.Lazy.GeneratedSequence=k})("undefined"!==typeof exports?exports:window);