From f9fcede766a3492623685afc8e12cc34c73039e8 Mon Sep 17 00:00:00 2001 From: Grant Watters Date: Fri, 24 May 2013 04:02:22 -0700 Subject: [PATCH 1/2] Add delayDiskUpdates functionality to allow unsafe writes for better performance --- taffy.js | 34 +++++++++++++++++++++++++++++----- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/taffy.js b/taffy.js index 350fb47..61f76cd 100644 --- a/taffy.js +++ b/taffy.js @@ -1186,6 +1186,7 @@ var TAFFY, exports, T; onDBChange : false, storageName : false, forcePropertyCase : null, + delayDiskUpdates : 0, cacheSize : 100, name : '' }, @@ -1276,14 +1277,33 @@ var TAFFY, exports, T; settings.onDBChange.call( TOb ); }, 0 ); } - if ( settings.storageName ){ - setTimeout( function () { - localStorage.setItem( 'taffy_' + settings.storageName, - JSON.stringify( TOb ) ); - }); + if (settings.delayDiskUpdates === 0) + { + if ( settings.storageName ){ + setTimeout( function () { + localStorage.setItem( 'taffy_' + settings.storageName, + JSON.stringify( TOb ) ); + }); + } } return dm; }, + delayDiskUpdates: function ( ) { + settings.delayDiskUpdates++; + }, + flushDiskUpdates: function ( ) { + settings.delayDiskUpdates--; + + if (settings.delayDiskUpdates === 0) + { + if ( settings.storageName ){ + setTimeout( function () { + localStorage.setItem( 'taffy_' + settings.storageName, + JSON.stringify( TOb ) ); + }); + } + } + }, insert : function ( i, runEvent ) { // **************************************** // * @@ -1627,6 +1647,10 @@ var TAFFY, exports, T; root.insert = DBI.insert; + root.delayDiskUpdates = DBI.delayDiskUpdates; + + root.flushDiskUpdates = DBI.flushDiskUpdates; + root.merge = function ( i, key, runEvent ) { var search = {}, From dca57e388fb0b1139b930004867fb2b60d46ac46 Mon Sep 17 00:00:00 2001 From: Grant Watters Date: Fri, 24 May 2013 04:03:29 -0700 Subject: [PATCH 2/2] Update minified version --- taffy-min.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/taffy-min.js b/taffy-min.js index 1b5116e..8c7f3f9 100644 --- a/taffy-min.js +++ b/taffy-min.js @@ -1 +1 @@ -var TAFFY,exports,T;(function(){var f,q,p,t,d,b,n,m,r,e,c,u,w,v,h,g,j,o,i,l,a,s,k;if(!TAFFY){d="2.7";b=1;n="000000";m=1000;r={};e=function(x){if(TAFFY.isArray(x)||TAFFY.isObject(x)){return x}else{return JSON.parse(x)}};i=function(y,x){return l(y,function(z){return x.indexOf(z)>=0})};l=function(A,z,y){var x=[];if(A==null){return x}if(Array.prototype.filter&&A.filter===Array.prototype.filter){return A.filter(z,y)}c(A,function(D,B,C){if(z.call(y,D,B,C)){x[x.length]=D}});return x};k=function(x){return Object.prototype.toString.call(x)==="[object RegExp]"};s=function(z){var x=T.isArray(z)?[]:T.isObject(z)?{}:null;if(z===null){return z}for(var y in z){x[y]=k(z[y])?z[y].toString():T.isArray(z[y])||T.isObject(z[y])?s(z[y]):z[y]}return x};a=function(y){var x=JSON.stringify(y);if(x.match(/regex/)===null){return x}return JSON.stringify(s(y))};c=function(B,A,C){var E,D,z,F;if(B&&((T.isArray(B)&&B.length===1)||(!T.isArray(B)))){A((T.isArray(B))?B[0]:B,0)}else{for(E,D,z=0,B=(T.isArray(B))?B:[B],F=B.length;z4){c(A,function(B){if(h(z,B)){x=true}})}break}});return x};v=function(y){var x=[];if(T.isString(y)&&/[t][0-9]*[r][0-9]*/i.test(y)){y={___id:y}}if(T.isArray(y)){c(y,function(z){x.push(v(z))});y=function(){var A=this,z=false;c(x,function(B){if(h(A,B)){z=true}});return z};return y}if(T.isObject(y)){if(T.isObject(y)&&y.___id&&y.___s){y={___id:y.___id}}u(y,function(z,A){if(!T.isObject(z)){z={is:z}}u(z,function(B,C){var E=[],D;D=(C==="hasAll")?function(F,G){G(F)}:c;D(B,function(G){var F=true,H=false,I;I=function(){var N=this[A],M="==",O="!=",Q="===",R="<",L=">",S="<=",P=">=",K="!==",J;if(typeof N==="undefined"){return false}if((C.indexOf("!")===0)&&C!==O&&C!==K){F=false;C=C.substring(1,C.length)}J=((C==="regex")?(G.test(N)):(C==="lt"||C===R)?(NG):(C==="lte"||C===S)?(N<=G):(C==="gte"||C===P)?(N>=G):(C==="left")?(N.indexOf(G)===0):(C==="leftnocase")?(N.toLowerCase().indexOf(G.toLowerCase())===0):(C==="right")?(N.substring((N.length-G.length))===G):(C==="rightnocase")?(N.toLowerCase().substring((N.length-G.length))===G.toLowerCase()):(C==="like")?(N.indexOf(G)>=0):(C==="likenocase")?(N.toLowerCase().indexOf(G.toLowerCase())>=0):(C===Q||C==="is")?(N===G):(C===M)?(N==G):(C===K)?(N!==G):(C===O)?(N!=G):(C==="isnocase")?(N.toLowerCase?N.toLowerCase()===G.toLowerCase():N===G):(C==="has")?(T.has(N,G)):(C==="hasall")?(T.hasAll(N,G)):(C==="contains")?(TAFFY.isArray(N)&&N.indexOf(G)>-1):(C.indexOf("is")===-1&&!TAFFY.isNull(N)&&!TAFFY.isUndefined(N)&&!TAFFY.isObject(G)&&!TAFFY.isArray(G))?(G===N[C]):(T[C]&&T.isFunction(T[C])&&C.indexOf("is")===0)?T[C](N)===G:(T[C]&&T.isFunction(T[C]))?T[C](N,G):(false));J=(J&&!F)?false:(!J&&!F)?true:J;return J};E.push(I)});if(E.length===1){x.push(E[0])}else{x.push(function(){var G=this,F=false;c(E,function(H){if(H.apply(G)){F=true}});return F})}})});y=function(){var A=this,z=true;z=(x.length===1&&!x[0].apply(A))?false:(x.length===2&&(!x[0].apply(A)||!x[1].apply(A)))?false:(x.length===3&&(!x[0].apply(A)||!x[1].apply(A)||!x[2].apply(A)))?false:(x.length===4&&(!x[0].apply(A)||!x[1].apply(A)||!x[2].apply(A)||!x[3].apply(A)))?false:true;if(x.length>4){c(x,function(B){if(!h(A,B)){z=false}})}return z};return y}if(T.isFunction(y)){return y}};j=function(x,y){var z=function(B,A){var C=0;T.each(y,function(F){var H,E,D,I,G;H=F.split(" ");E=H[0];D=(H.length===1)?"logical":H[1];if(D==="logical"){I=g(B[E]);G=g(A[E]);T.each((I.length<=G.length)?I:G,function(J,K){if(I[K]G[K]){C=1;return TAFFY.EXIT}}})}else{if(D==="logicaldesc"){I=g(B[E]);G=g(A[E]);T.each((I.length<=G.length)?I:G,function(J,K){if(I[K]>G[K]){C=-1;return TAFFY.EXIT}else{if(I[K]A[E]){C=1;return T.EXIT}else{if(D==="desc"&&B[E]>A[E]){C=-1;return T.EXIT}else{if(D==="desc"&&B[E]G.length){C=1}else{if(C===0&&D==="logicaldesc"&&I.length>G.length){C=-1}else{if(C===0&&D==="logicaldesc"&&I.lengthm){x={};y=0}return x["_"+z]||(function(){var D=String(z),C=[],G="_",B="",A,E,F;for(A=0,E=D.length;A=48&&F<=57)||F===46){if(B!=="n"){B="n";C.push(G.toLowerCase());G=""}G=G+D.charAt(A)}else{if(B!=="s"){B="s";C.push(parseFloat(G));G=""}G=G+D.charAt(A)}}C.push((B==="n")?parseFloat(G):G.toLowerCase());C.shift();x["_"+z]=C;y++;return C}())}}());o=function(){this.context({results:this.getDBI().query(this.context())})};r.extend("filter",function(){var y=TAFFY.mergeObj(this.context(),{run:null}),x=[];c(y.q,function(z){x.push(z)});y.q=x;c(arguments,function(z){y.q.push(v(z));y.filterRaw.push(z)});return this.getroot(y)});r.extend("order",function(z){z=z.split(",");var y=[],A;c(z,function(x){y.push(x.replace(/^\s*/,"").replace(/\s*$/,""))});A=TAFFY.mergeObj(this.context(),{sort:null});A.order=y;return this.getroot(A)});r.extend("limit",function(z){var y=TAFFY.mergeObj(this.context(),{}),x;y.limit=z;if(y.run&&y.sort){x=[];c(y.results,function(B,A){if((A+1)>z){return TAFFY.EXIT}x.push(B)});y.results=x}return this.getroot(y)});r.extend("start",function(z){var y=TAFFY.mergeObj(this.context(),{}),x;y.start=z;if(y.run&&y.sort&&!y.limit){x=[];c(y.results,function(B,A){if((A+1)>z){x.push(B)}});y.results=x}else{y=TAFFY.mergeObj(this.context(),{run:null,start:z})}return this.getroot(y)});r.extend("update",function(A,z,x){var B=true,D={},y=arguments,C;if(TAFFY.isString(A)&&(arguments.length===2||arguments.length===3)){D[A]=z;if(arguments.length===3){B=x}}else{D=A;if(y.length===2){B=z}}C=this;o.call(this);c(this.context().results,function(E){var F=D;if(TAFFY.isFunction(F)){F=F.apply(TAFFY.mergeObj(E,{}))}else{if(T.isFunction(F)){F=F(TAFFY.mergeObj(E,{}))}}if(TAFFY.isObject(F)){C.getDBI().update(E.___id,F,B)}});if(this.context().results.length){this.context({run:null})}return this});r.extend("remove",function(x){var y=this,z=0;o.call(this);c(this.context().results,function(A){y.getDBI().remove(A.___id);z++});if(this.context().results.length){this.context({run:null});y.getDBI().removeCommit(x)}return z});r.extend("count",function(){o.call(this);return this.context().results.length});r.extend("callback",function(z,x){if(z){var y=this;setTimeout(function(){o.call(y);z.call(y.getroot(y.context()))},x||0)}return null});r.extend("get",function(){o.call(this);return this.context().results});r.extend("stringify",function(){return JSON.stringify(this.get())});r.extend("first",function(){o.call(this);return this.context().results[0]||false});r.extend("last",function(){o.call(this);return this.context().results[this.context().results.length-1]||false});r.extend("sum",function(){var y=0,x=this;o.call(x);c(arguments,function(z){c(x.context().results,function(A){y=y+(A[z]||0)})});return y});r.extend("min",function(y){var x=null;o.call(this);c(this.context().results,function(z){if(x===null||z[y]":return C>F;case"<=":return C<=F;case">=":return C>=F;case"==":return C==F;case"!=":return C!=F;default:throw String(H)+" is not supported"}};y=function(C,F){var B={},D,E;for(D in C){if(C.hasOwnProperty(D)){B[D]=C[D]}}for(D in F){if(F.hasOwnProperty(D)&&D!=="___id"&&D!=="___s"){E=!TAFFY.isUndefined(B[D])?"right_":"";B[E+String(D)]=F[D]}}return B};z=function(F){var B,D,C=arguments,E=C.length,G=[];if(typeof F.filter!=="function"){if(F.TAFFY){B=F()}else{throw"TAFFY DB or result not supplied"}}else{B=F}this.context({results:this.getDBI().query(this.context())});TAFFY.each(this.context().results,function(H){B.each(function(K){var I,J=true;CONDITION:for(D=1;Dx){x=z[y]}});return x});r.extend("select",function(){var y=[],x=arguments;o.call(this);if(arguments.length===1){c(this.context().results,function(z){y.push(z[x[0]])})}else{c(this.context().results,function(z){var A=[];c(x,function(B){A.push(z[B])});y.push(A)})}return y});r.extend("distinct",function(){var y=[],x=arguments;o.call(this);if(arguments.length===1){c(this.context().results,function(A){var z=A[x[0]],B=false;c(y,function(C){if(z===C){B=true;return TAFFY.EXIT}});if(!B){y.push(z)}})}else{c(this.context().results,function(z){var B=[],A=false;c(x,function(C){B.push(z[C])});c(y,function(D){var C=true;c(x,function(F,E){if(B[E]!==D[E]){C=false;return TAFFY.EXIT}});if(C){A=true;return TAFFY.EXIT}});if(!A){y.push(B)}})}return y});r.extend("supplant",function(y,x){var z=[];o.call(this);c(this.context().results,function(A){z.push(y.replace(/\{([^\{\}]*)\}/g,function(C,B){var D=A[B];return typeof D==="string"||typeof D==="number"?D:C}))});return(!x)?z.join(""):z});r.extend("each",function(x){o.call(this);c(this.context().results,x);return this});r.extend("map",function(x){var y=[];o.call(this);c(this.context().results,function(z){y.push(x(z))});return y});T=function(F){var C=[],G={},D=1,z={template:false,onInsert:false,onUpdate:false,onRemove:false,onDBChange:false,storageName:false,forcePropertyCase:null,cacheSize:100,name:""},B=new Date(),A=0,y=0,I={},E,x,H;x=function(L){var K=[],J=false;if(L.length===0){return C}c(L,function(M){if(T.isString(M)&&/[t][0-9]*[r][0-9]*/i.test(M)&&C[G[M]]){K.push(C[G[M]]);J=true}if(T.isObject(M)&&M.___id&&M.___s&&C[G[M.___id]]){K.push(C[G[M.___id]]);J=true}if(T.isArray(M)){c(M,function(N){c(x(N),function(O){K.push(O)})})}});if(J&&K.length>1){K=[]}return K};E={dm:function(J){if(J){B=J;I={};A=0;y=0}if(z.onDBChange){setTimeout(function(){z.onDBChange.call(C)},0)}if(z.storageName){setTimeout(function(){localStorage.setItem("taffy_"+z.storageName,JSON.stringify(C))})}return B},insert:function(M,N){var L=[],K=[],J=e(M);c(J,function(P,Q){var O,R;if(T.isArray(P)&&Q===0){c(P,function(S){L.push((z.forcePropertyCase==="lower")?S.toLowerCase():(z.forcePropertyCase==="upper")?S.toUpperCase():S)});return true}else{if(T.isArray(P)){O={};c(P,function(U,S){O[L[S]]=U});P=O}else{if(T.isObject(P)&&z.forcePropertyCase){R={};u(P,function(U,S){R[(z.forcePropertyCase==="lower")?S.toLowerCase():(z.forcePropertyCase==="upper")?S.toUpperCase():S]=P[S]});P=R}}}D++;P.___id="T"+String(n+b).slice(-6)+"R"+String(n+D).slice(-6);P.___s=true;K.push(P.___id);if(z.template){P=T.mergeObj(z.template,P)}C.push(P);G[P.___id]=C.length-1;if(z.onInsert&&(N||TAFFY.isUndefined(N))){z.onInsert.call(P)}E.dm(new Date())});return H(K)},sort:function(J){C=j(C,J.split(","));G={};c(C,function(L,K){G[L.___id]=K});E.dm(new Date());return true},update:function(Q,M,L){var P={},O,N,J,K;if(z.forcePropertyCase){u(M,function(R,S){P[(z.forcePropertyCase==="lower")?S.toLowerCase():(z.forcePropertyCase==="upper")?S.toUpperCase():S]=R});M=P}O=C[G[Q]];N=T.mergeObj(O,M);J={};K=false;u(N,function(R,S){if(TAFFY.isUndefined(O[S])||O[S]!==R){J[S]=R;K=true}});if(K){if(z.onUpdate&&(L||TAFFY.isUndefined(L))){z.onUpdate.call(N,C[G[Q]],J)}C[G[Q]]=N;E.dm(new Date())}},remove:function(J){C[G[J]].___s=false},removeCommit:function(K){var J;for(J=C.length-1;J>-1;J--){if(!C[J].___s){if(z.onRemove&&(K||TAFFY.isUndefined(K))){z.onRemove.call(C[J])}G[C[J].___id]=undefined;C.splice(J,1)}}G={};c(C,function(M,L){G[M.___id]=L});E.dm(new Date())},query:function(L){var O,P,K,N,M,J;if(z.cacheSize){P="";c(L.filterRaw,function(Q){if(T.isFunction(Q)){P="nocache";return TAFFY.EXIT}});if(P===""){P=a(T.mergeObj(L,{q:false,run:false,sort:false}))}}if(!L.results||!L.run||(L.run&&E.dm()>L.run)){K=[];if(z.cacheSize&&I[P]){I[P].i=A++;return I[P].results}else{if(L.q.length===0&&L.index.length===0){c(C,function(Q){K.push(Q)});O=K}else{N=x(L.index);c(N,function(Q){if(L.q.length===0||h(Q,L.q)){K.push(Q)}});O=K}}}else{O=L.results}if(L.order.length>0&&(!L.run||!L.sort)){O=j(O,L.order)}if(O.length&&((L.limit&&L.limit=L.start)){if(L.limit){J=(L.start)?(Q+1)-L.start:Q;if(JL.limit){return TAFFY.EXIT}}}else{M.push(R)}}});O=M}if(z.cacheSize&&P!=="nocache"){y++;setTimeout(function(){var Q,R;if(y>=z.cacheSize*2){y=0;Q=A-z.cacheSize;R={};u(function(U,S){if(U.i>=Q){R[S]=U}});I=R}},0);I[P]={i:A++,results:O}}return O}};H=function(){var K,J;K=TAFFY.mergeObj(TAFFY.mergeObj(r,{insert:undefined}),{getDBI:function(){return E},getroot:function(L){return H.call(L)},context:function(L){if(L){J=TAFFY.mergeObj(J,L.hasOwnProperty("results")?TAFFY.mergeObj(L,{run:new Date(),sort:new Date()}):L)}return J},extend:undefined});J=(this&&this.q)?this:{limit:false,start:false,q:[],filterRaw:[],index:[],order:[],results:false,run:null,sort:null,settings:z};c(arguments,function(L){if(w(L)){J.index.push(L)}else{J.q.push(v(L))}J.filterRaw.push(L)});return K};b++;if(F){E.insert(F)}H.insert=E.insert;H.merge=function(M,L,N){var K={},J=[],O={};N=N||false;L=L||"id";c(M,function(Q){var P;K[L]=Q[L];J.push(Q[L]);P=H(K).first();if(P){E.update(P.___id,Q,N)}else{E.insert(Q,N)}});O[L]=J;return H(O)};H.TAFFY=true;H.sort=E.sort;H.settings=function(J){if(J){z=TAFFY.mergeObj(z,J);if(J.template){H().update(J.template)}}return z};H.store=function(L){var K=false,J;if(localStorage){if(L){J=localStorage.getItem("taffy_"+L);if(J&&J.length>0){H.insert(J);K=true}if(C.length>0){setTimeout(function(){localStorage.setItem("taffy_"+z.storageName,JSON.stringify(C))})}}H.settings({storageName:L})}return H};return H};TAFFY=T;T.each=c;T.eachin=u;T.extend=r.extend;TAFFY.EXIT="TAFFYEXIT";TAFFY.mergeObj=function(z,x){var y={};u(z,function(A,B){y[B]=z[B]});u(x,function(A,B){y[B]=x[B]});return y};TAFFY.has=function(z,y){var x=false,A;if((z.TAFFY)){x=z(y);if(x.length>0){return true}else{return false}}else{switch(T.typeOf(z)){case"object":if(T.isObject(y)){u(y,function(B,C){if(x===true&&!T.isUndefined(z[C])&&z.hasOwnProperty(C)){x=T.has(z[C],y[C])}else{x=false;return TAFFY.EXIT}})}else{if(T.isArray(y)){c(y,function(B,C){x=T.has(z,y[C]);if(x){return TAFFY.EXIT}})}else{if(T.isString(y)){if(!TAFFY.isUndefined(z[y])){return true}else{return false}}}}return x;case"array":if(T.isObject(y)){c(z,function(B,C){x=T.has(z[C],y);if(x===true){return TAFFY.EXIT}})}else{if(T.isArray(y)){c(y,function(C,B){c(z,function(E,D){x=T.has(z[D],y[B]);if(x===true){return TAFFY.EXIT}});if(x===true){return TAFFY.EXIT}})}else{if(T.isString(y)||T.isNumber(y)){x=false;for(A=0;A=0})};b=function(e,t,n){var r=[];if(e==null)return r;if(Array.prototype.filter&&e.filter===Array.prototype.filter)return e.filter(t,n);l(e,function(e,i,s){if(t.call(n,e,i,s))r[r.length]=e});return r};S=function(e){return Object.prototype.toString.call(e)==="[object RegExp]"};E=function(e){var t=T.isArray(e)?[]:T.isObject(e)?{}:null;if(e===null)return e;for(var n in e){t[n]=S(e[n])?e[n].toString():T.isArray(e[n])||T.isObject(e[n])?E(e[n]):e[n]}return t};w=function(e){var t=JSON.stringify(e);if(t.match(/regex/)===null)return t;return JSON.stringify(E(e))};l=function(e,t,n){var r,i,s,o;if(e&&(T.isArray(e)&&e.length===1||!T.isArray(e))){t(T.isArray(e)?e[0]:e,0)}else{for(r,i,s=0,e=T.isArray(e)?e:[e],o=e.length;s4){l(t,function(t){if(d(e,t)){n=true}})}break}});return n};p=function(e){var t=[];if(T.isString(e)&&/[t][0-9]*[r][0-9]*/i.test(e)){e={___id:e}}if(T.isArray(e)){l(e,function(e){t.push(p(e))});e=function(){var e=this,n=false;l(t,function(t){if(d(e,t)){n=true}});return n};return e}if(T.isObject(e)){if(T.isObject(e)&&e.___id&&e.___s){e={___id:e.___id}}c(e,function(e,n){if(!T.isObject(e)){e={is:e}}c(e,function(e,r){var i=[],s;s=r==="hasAll"?function(e,t){t(e)}:l;s(e,function(e){var t=true,s=false,o;o=function(){var i=this[n],s="==",o="!=",u="===",a="<",f=">",l="<=",c=">=",h="!==",p;if(typeof i==="undefined"){return false}if(r.indexOf("!")===0&&r!==o&&r!==h){t=false;r=r.substring(1,r.length)}p=r==="regex"?e.test(i):r==="lt"||r===a?ie:r==="lte"||r===l?i<=e:r==="gte"||r===c?i>=e:r==="left"?i.indexOf(e)===0:r==="leftnocase"?i.toLowerCase().indexOf(e.toLowerCase())===0:r==="right"?i.substring(i.length-e.length)===e:r==="rightnocase"?i.toLowerCase().substring(i.length-e.length)===e.toLowerCase():r==="like"?i.indexOf(e)>=0:r==="likenocase"?i.toLowerCase().indexOf(e.toLowerCase())>=0:r===u||r==="is"?i===e:r===s?i==e:r===h?i!==e:r===o?i!=e:r==="isnocase"?i.toLowerCase?i.toLowerCase()===e.toLowerCase():i===e:r==="has"?T.has(i,e):r==="hasall"?T.hasAll(i,e):r==="contains"?TAFFY.isArray(i)&&i.indexOf(e)>-1:r.indexOf("is")===-1&&!TAFFY.isNull(i)&&!TAFFY.isUndefined(i)&&!TAFFY.isObject(e)&&!TAFFY.isArray(e)?e===i[r]:T[r]&&T.isFunction(T[r])&&r.indexOf("is")===0?T[r](i)===e:T[r]&&T.isFunction(T[r])?T[r](i,e):false;p=p&&!t?false:!p&&!t?true:p;return p};i.push(o)});if(i.length===1){t.push(i[0])}else{t.push(function(){var e=this,t=false;l(i,function(n){if(n.apply(e)){t=true}});return t})}})});e=function(){var e=this,n=true;n=t.length===1&&!t[0].apply(e)?false:t.length===2&&(!t[0].apply(e)||!t[1].apply(e))?false:t.length===3&&(!t[0].apply(e)||!t[1].apply(e)||!t[2].apply(e))?false:t.length===4&&(!t[0].apply(e)||!t[1].apply(e)||!t[2].apply(e)||!t[3].apply(e))?false:true;if(t.length>4){l(t,function(t){if(!d(e,t)){n=false}})}return n};return e}if(T.isFunction(e)){return e}};m=function(e,t){var n=function(e,n){var r=0;T.each(t,function(t){var i,s,o,u,a;i=t.split(" ");s=i[0];o=i.length===1?"logical":i[1];if(o==="logical"){u=v(e[s]);a=v(n[s]);T.each(u.length<=a.length?u:a,function(e,t){if(u[t]a[t]){r=1;return TAFFY.EXIT}})}else if(o==="logicaldesc"){u=v(e[s]);a=v(n[s]);T.each(u.length<=a.length?u:a,function(e,t){if(u[t]>a[t]){r=-1;return TAFFY.EXIT}else if(u[t]n[s]){r=1;return T.EXIT}else if(o==="desc"&&e[s]>n[s]){r=-1;return T.EXIT}else if(o==="desc"&&e[s]a.length){r=1}else if(r===0&&o==="logicaldesc"&&u.length>a.length){r=-1}else if(r===0&&o==="logicaldesc"&&u.lengthu){e={};t=0}return e["_"+n]||function(){var r=String(n),i=[],s="_",o="",u,a,f;for(u=0,a=r.length;u=48&&f<=57||f===46){if(o!=="n"){o="n";i.push(s.toLowerCase());s=""}s=s+r.charAt(u)}else{if(o!=="s"){o="s";i.push(parseFloat(s));s=""}s=s+r.charAt(u)}}i.push(o==="n"?parseFloat(s):s.toLowerCase());i.shift();e["_"+n]=i;t++;return i}()}})();g=function(){this.context({results:this.getDBI().query(this.context())})};a.extend("filter",function(){var e=TAFFY.mergeObj(this.context(),{run:null}),t=[];l(e.q,function(e){t.push(e)});e.q=t;l(arguments,function(t){e.q.push(p(t));e.filterRaw.push(t)});return this.getroot(e)});a.extend("order",function(e){e=e.split(",");var t=[],n;l(e,function(e){t.push(e.replace(/^\s*/,"").replace(/\s*$/,""))});n=TAFFY.mergeObj(this.context(),{sort:null});n.order=t;return this.getroot(n)});a.extend("limit",function(e){var t=TAFFY.mergeObj(this.context(),{}),n;t.limit=e;if(t.run&&t.sort){n=[];l(t.results,function(t,r){if(r+1>e){return TAFFY.EXIT}n.push(t)});t.results=n}return this.getroot(t)});a.extend("start",function(e){var t=TAFFY.mergeObj(this.context(),{}),n;t.start=e;if(t.run&&t.sort&&!t.limit){n=[];l(t.results,function(t,r){if(r+1>e){n.push(t)}});t.results=n}else{t=TAFFY.mergeObj(this.context(),{run:null,start:e})}return this.getroot(t)});a.extend("update",function(e,t,n){var r=true,i={},s=arguments,o;if(TAFFY.isString(e)&&(arguments.length===2||arguments.length===3)){i[e]=t;if(arguments.length===3){r=n}}else{i=e;if(s.length===2){r=t}}o=this;g.call(this);l(this.context().results,function(e){var t=i;if(TAFFY.isFunction(t)){t=t.apply(TAFFY.mergeObj(e,{}))}else{if(T.isFunction(t)){t=t(TAFFY.mergeObj(e,{}))}}if(TAFFY.isObject(t)){o.getDBI().update(e.___id,t,r)}});if(this.context().results.length){this.context({run:null})}return this});a.extend("remove",function(e){var t=this,n=0;g.call(this);l(this.context().results,function(e){t.getDBI().remove(e.___id);n++});if(this.context().results.length){this.context({run:null});t.getDBI().removeCommit(e)}return n});a.extend("count",function(){g.call(this);return this.context().results.length});a.extend("callback",function(e,t){if(e){var n=this;setTimeout(function(){g.call(n);e.call(n.getroot(n.context()))},t||0)}return null});a.extend("get",function(){g.call(this);return this.context().results});a.extend("stringify",function(){return JSON.stringify(this.get())});a.extend("first",function(){g.call(this);return this.context().results[0]||false});a.extend("last",function(){g.call(this);return this.context().results[this.context().results.length-1]||false});a.extend("sum",function(){var e=0,t=this;g.call(t);l(arguments,function(n){l(t.context().results,function(t){e=e+(t[n]||0)})});return e});a.extend("min",function(e){var t=null;g.call(this);l(this.context().results,function(n){if(t===null||n[e]":return r>i;case"<=":return r<=i;case">=":return r>=i;case"==":return r==i;case"!=":return r!=i;default:throw String(s)+" is not supported"}};t=function(e,t){var n={},r,i;for(r in e){if(e.hasOwnProperty(r)){n[r]=e[r]}}for(r in t){if(t.hasOwnProperty(r)&&r!=="___id"&&r!=="___s"){i=!TAFFY.isUndefined(n[r])?"right_":"";n[i+String(r)]=t[r]}}return n};n=function(n){var r,i,s=arguments,o=s.length,u=[];if(typeof n.filter!=="function"){if(n.TAFFY){r=n()}else{throw"TAFFY DB or result not supplied"}}else{r=n}this.context({results:this.getDBI().query(this.context())});TAFFY.each(this.context().results,function(n){r.each(function(r){var a,f=true;e:for(i=1;it){t=n[e]}});return t});a.extend("select",function(){var e=[],t=arguments;g.call(this);if(arguments.length===1){l(this.context().results,function(n){e.push(n[t[0]])})}else{l(this.context().results,function(n){var r=[];l(t,function(e){r.push(n[e])});e.push(r)})}return e});a.extend("distinct",function(){var e=[],t=arguments;g.call(this);if(arguments.length===1){l(this.context().results,function(n){var r=n[t[0]],i=false;l(e,function(e){if(r===e){i=true;return TAFFY.EXIT}});if(!i){e.push(r)}})}else{l(this.context().results,function(n){var r=[],i=false;l(t,function(e){r.push(n[e])});l(e,function(e){var n=true;l(t,function(t,i){if(r[i]!==e[i]){n=false;return TAFFY.EXIT}});if(n){i=true;return TAFFY.EXIT}});if(!i){e.push(r)}})}return e});a.extend("supplant",function(e,t){var n=[];g.call(this);l(this.context().results,function(t){n.push(e.replace(/\{([^\{\}]*)\}/g,function(e,n){var r=t[n];return typeof r==="string"||typeof r==="number"?r:e}))});return!t?n.join(""):n});a.extend("each",function(e){g.call(this);l(this.context().results,e);return this});a.extend("map",function(e){var t=[];g.call(this);l(this.context().results,function(n){t.push(e(n))});return t});T=function(e){var t=[],n={},r=1,i={template:false,onInsert:false,onUpdate:false,onRemove:false,onDBChange:false,storageName:false,forcePropertyCase:null,delayDiskUpdates:0,cacheSize:100,name:""},u=new Date,v=0,g=0,y={},b,E,S;E=function(e){var r=[],i=false;if(e.length===0){return t}l(e,function(e){if(T.isString(e)&&/[t][0-9]*[r][0-9]*/i.test(e)&&t[n[e]]){r.push(t[n[e]]);i=true}if(T.isObject(e)&&e.___id&&e.___s&&t[n[e.___id]]){r.push(t[n[e.___id]]);i=true}if(T.isArray(e)){l(e,function(e){l(E(e),function(e){r.push(e)})})}});if(i&&r.length>1){r=[]}return r};b={dm:function(e){if(e){u=e;y={};v=0;g=0}if(i.onDBChange){setTimeout(function(){i.onDBChange.call(t)},0)}if(i.delayDiskUpdates===0){if(i.storageName){setTimeout(function(){localStorage.setItem("taffy_"+i.storageName,JSON.stringify(t))})}}return u},delayDiskUpdates:function(){i.delayDiskUpdates++},flushDiskUpdates:function(){i.delayDiskUpdates--;if(i.delayDiskUpdates===0){if(i.storageName){setTimeout(function(){localStorage.setItem("taffy_"+i.storageName,JSON.stringify(t))})}}},insert:function(e,u){var a=[],h=[],p=f(e);l(p,function(e,f){var p,d;if(T.isArray(e)&&f===0){l(e,function(e){a.push(i.forcePropertyCase==="lower"?e.toLowerCase():i.forcePropertyCase==="upper"?e.toUpperCase():e)});return true}else if(T.isArray(e)){p={};l(e,function(e,t){p[a[t]]=e});e=p}else if(T.isObject(e)&&i.forcePropertyCase){d={};c(e,function(t,n){d[i.forcePropertyCase==="lower"?n.toLowerCase():i.forcePropertyCase==="upper"?n.toUpperCase():n]=e[n]});e=d}r++;e.___id="T"+String(o+s).slice(-6)+"R"+String(o+r).slice(-6);e.___s=true;h.push(e.___id);if(i.template){e=T.mergeObj(i.template,e)}t.push(e);n[e.___id]=t.length-1;if(i.onInsert&&(u||TAFFY.isUndefined(u))){i.onInsert.call(e)}b.dm(new Date)});return S(h)},sort:function(e){t=m(t,e.split(","));n={};l(t,function(e,t){n[e.___id]=t});b.dm(new Date);return true},update:function(e,r,s){var o={},u,a,f,l;if(i.forcePropertyCase){c(r,function(e,t){o[i.forcePropertyCase==="lower"?t.toLowerCase():i.forcePropertyCase==="upper"?t.toUpperCase():t]=e});r=o}u=t[n[e]];a=T.mergeObj(u,r);f={};l=false;c(a,function(e,t){if(TAFFY.isUndefined(u[t])||u[t]!==e){f[t]=e;l=true}});if(l){if(i.onUpdate&&(s||TAFFY.isUndefined(s))){i.onUpdate.call(a,t[n[e]],f)}t[n[e]]=a;b.dm(new Date)}},remove:function(e){t[n[e]].___s=false},removeCommit:function(e){var r;for(r=t.length-1;r>-1;r--){if(!t[r].___s){if(i.onRemove&&(e||TAFFY.isUndefined(e))){i.onRemove.call(t[r])}n[t[r].___id]=undefined;t.splice(r,1)}}n={};l(t,function(e,t){n[e.___id]=t});b.dm(new Date)},query:function(e){var n,r,s,o,u,a;if(i.cacheSize){r="";l(e.filterRaw,function(e){if(T.isFunction(e)){r="nocache";return TAFFY.EXIT}});if(r===""){r=w(T.mergeObj(e,{q:false,run:false,sort:false}))}}if(!e.results||!e.run||e.run&&b.dm()>e.run){s=[];if(i.cacheSize&&y[r]){y[r].i=v++;return y[r].results}else{if(e.q.length===0&&e.index.length===0){l(t,function(e){s.push(e)});n=s}else{o=E(e.index);l(o,function(t){if(e.q.length===0||d(t,e.q)){s.push(t)}});n=s}}}else{n=e.results}if(e.order.length>0&&(!e.run||!e.sort)){n=m(n,e.order)}if(n.length&&(e.limit&&e.limit=e.start){if(e.limit){a=e.start?n+1-e.start:n;if(ae.limit){return TAFFY.EXIT}}else{u.push(t)}}});n=u}if(i.cacheSize&&r!=="nocache"){g++;setTimeout(function(){var e,t;if(g>=i.cacheSize*2){g=0;e=v-i.cacheSize;t={};c(function(n,r){if(n.i>=e){t[r]=n}});y=t}},0);y[r]={i:v++,results:n}}return n}};S=function(){var e,t;e=TAFFY.mergeObj(TAFFY.mergeObj(a,{insert:undefined}),{getDBI:function(){return b},getroot:function(e){return S.call(e)},context:function(e){if(e){t=TAFFY.mergeObj(t,e.hasOwnProperty("results")?TAFFY.mergeObj(e,{run:new Date,sort:new Date}):e)}return t},extend:undefined});t=this&&this.q?this:{limit:false,start:false,q:[],filterRaw:[],index:[],order:[],results:false,run:null,sort:null,settings:i};l(arguments,function(e){if(h(e)){t.index.push(e)}else{t.q.push(p(e))}t.filterRaw.push(e)});return e};s++;if(e){b.insert(e)}S.insert=b.insert;S.delayDiskUpdates=b.delayDiskUpdates;S.flushDiskUpdates=b.flushDiskUpdates;S.merge=function(e,t,n){var r={},i=[],s={};n=n||false;t=t||"id";l(e,function(e){var s;r[t]=e[t];i.push(e[t]);s=S(r).first();if(s){b.update(s.___id,e,n)}else{b.insert(e,n)}});s[t]=i;return S(s)};S.TAFFY=true;S.sort=b.sort;S.settings=function(e){if(e){i=TAFFY.mergeObj(i,e);if(e.template){S().update(e.template)}}return i};S.store=function(e){var n=false,r;if(localStorage){if(e){r=localStorage.getItem("taffy_"+e);if(r&&r.length>0){S.insert(r);n=true}if(t.length>0){setTimeout(function(){localStorage.setItem("taffy_"+i.storageName,JSON.stringify(t))})}}S.settings({storageName:e})}return S};return S};TAFFY=T;T.each=l;T.eachin=c;T.extend=a.extend;TAFFY.EXIT="TAFFYEXIT";TAFFY.mergeObj=function(e,t){var n={};c(e,function(t,r){n[r]=e[r]});c(t,function(e,r){n[r]=t[r]});return n};TAFFY.has=function(e,t){var n=false,r;if(e.TAFFY){n=e(t);if(n.length>0){return true}else{return false}}else{switch(T.typeOf(e)){case"object":if(T.isObject(t)){c(t,function(r,i){if(n===true&&!T.isUndefined(e[i])&&e.hasOwnProperty(i)){n=T.has(e[i],t[i])}else{n=false;return TAFFY.EXIT}})}else if(T.isArray(t)){l(t,function(r,i){n=T.has(e,t[i]);if(n){return TAFFY.EXIT}})}else if(T.isString(t)){if(!TAFFY.isUndefined(e[t])){return true}else{return false}}return n;case"array":if(T.isObject(t)){l(e,function(r,i){n=T.has(e[i],t);if(n===true){return TAFFY.EXIT}})}else if(T.isArray(t)){l(t,function(r,i){l(e,function(r,s){n=T.has(e[s],t[i]);if(n===true){return TAFFY.EXIT}});if(n===true){return TAFFY.EXIT}})}else if(T.isString(t)||T.isNumber(t)){n=false;for(r=0;r