From 4a03ba38747bc2c6c11fd3231a59c7a3248e07e8 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Fri, 5 Apr 2013 22:14:44 -0700 Subject: [PATCH] Add more `_.debounce` and `_.throttle` tests. Former-commit-id: db3cb30a8eac82850a056f85d0c363e752d10027 --- dist/lodash.compat.js | 10 +-- dist/lodash.compat.min.js | 4 +- dist/lodash.js | 10 +-- dist/lodash.min.js | 75 +++++++++--------- dist/lodash.underscore.js | 10 +-- dist/lodash.underscore.min.js | 19 ++--- lodash.js | 10 +-- test/test.js | 138 ++++++++++++++++++++++++++++++---- 8 files changed, 192 insertions(+), 84 deletions(-) diff --git a/dist/lodash.compat.js b/dist/lodash.compat.js index 5694c0ec1..09dbd76f2 100644 --- a/dist/lodash.compat.js +++ b/dist/lodash.compat.js @@ -4466,9 +4466,9 @@ if (options === true) { var leading = true; trailing = false; - } else if (options) { + } else if (options && objectTypes[typeof options]) { leading = options.leading; - trailing = options.trailing; + trailing = 'trailing' in options ? options.trailing : trailing; } return function() { var isLeading = leading && !timeoutId; @@ -4689,9 +4689,9 @@ } if (options === false) { leading = false; - } else if (options) { - leading = options.leading; - trailing = options.trailing; + } else if (options && objectTypes[typeof options]) { + leading = 'leading' in options ? options.leading : leading; + trailing = 'trailing' in options ? options.trailing : trailing; } return function() { var now = new Date; diff --git a/dist/lodash.compat.min.js b/dist/lodash.compat.min.js index e76237199..5ad6e7f93 100644 --- a/dist/lodash.compat.min.js +++ b/dist/lodash.compat.min.js @@ -27,13 +27,13 @@ try{fe.nodeClass=!(Wt.call(document)==S&&!({toString:0}+""))}catch(u){fe.nodeCla Z(/x/)&&(Z=function(n){return n instanceof It||Wt.call(n)==O});var be=Vt?function(n){if(!n||Wt.call(n)!=S||!fe.argsClass&&J(n))return!1;var t=n.valueOf,e=typeof t=="function"&&(e=Vt(t))&&Vt(e);return e?n==e||Vt(n)==e:V(n)}:V;return oe&&u&&typeof Jt=="function"&&(jt=Ct(Jt,r)),Jt=8==ue("08")?ue:function(n,t){return ue(et(n)?n.replace(y,""):n,t||0)},a.after=function(n,t){return 1>n?t():function(){return 1>--n?t.apply(this,arguments):void 0}},a.assign=he,a.at=function(n){var t=-1,e=Mt.apply(Rt,G(arguments,1)),r=e.length,u=Et(r); for(fe.unindexedChars&&et(n)&&(n=n.split(""));++tmt(c,l)){o&&c.push(l);for(var s=e;--s;)if(!(r[s]||(r[s]=B(t[s],0,100)))(l))continue n;i.push(l)}}return i},a.invert=X,a.invoke=function(n,t){var e=G(arguments,2),r=-1,u=typeof t=="function",a=n?n.length:0,o=Et(typeof a=="number"?a:0);return ct(n,function(n){o[++r]=(u?t:n[t]).apply(n,e)}),o},a.keys=pe,a.map=lt,a.max=pt,a.memoize=function(n,t){var e={};return function(){var r=f+(t?t.apply(this,arguments):arguments[0]); return Gt.call(e,r)?e[r]:e[r]=n.apply(this,arguments)}},a.merge=rt,a.min=function(n,t,e){var r=1/0,u=r;if(!t&&ce(n)){e=-1;for(var o=n.length;++emt(o,e,1))&&(u[e]=n)}),u},a.once=function(n){var t,e;return function(){return t?e:(t=!0,e=n.apply(this,arguments),n=null,e) }},a.pairs=function(n){for(var t=-1,e=pe(n),r=e.length,u=Et(r);++t=e;if(u){var a={};for(e=t-1;++et||typeof n=="undefined")return 1;if(ne?0:e);++re?ue(0,u+e):e)||0,typeof u=="number"?o=-1<(rt(n)?n.indexOf(t,e):dt(n,t,e)):me(n,function(n){return++ru&&(u=o) -}}else t=!t&&rt(n)?D:R.createCallback(t,e),lt(n,function(n,e,a){e=t(n,e,a),e>r&&(r=e,u=n)});return u}function vt(n,t){var e=-1,r=n?n.length:0;if(typeof r=="number")for(var u=It(r);++earguments.length;t=R.createCallback(t,r,4);var o=-1,i=n.length;if(typeof i=="number")for(u&&(e=n[++o]);++oarguments.length; -if(typeof u!="number")var i=pe(n),u=i.length;return t=R.createCallback(t,r,4),lt(n,function(r,f,c){f=i?i[--u]:--u,e=o?(o=a,n[f]):t(e,n[f],f,c)}),e}function yt(n,t,e){var r;t=R.createCallback(t,e),e=-1;var u=n?n.length:0;if(typeof u=="number")for(;++ee?ue(0,u+e):e||0)-1;else if(e)return r=kt(n,t),n[r]===t?r:-1;for(;++r>>1,e(n[r])dt(c,v))&&((e||l)&&c.push(v),f.push(r))}return f}function jt(n,t){for(var e=-1,r=n?n.length:0,u={};++e/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:b,variable:"",imports:{_:R}},St={a:"q,w,g",h:"var a=arguments,b=0,c=typeof g=='number'?2:a.length;while(++b":">",'"':""","'":"'"},ve=Y(se),ge=K(St,{h:St.h.replace(";",";if(c>3&&typeof a[c-2]=='function'){var d=p.createCallback(a[--c-1],a[c--],2);}else if(c>2&&typeof a[c-1]=='function'){d=a[--c];}"),f:"u[i]=d?d(u[i],m[i]):m[i]"}),he=K(St),ye=K(Bt,Ft,{i:a}),me=K(Bt,Ft);return qt&&i&&typeof Wt=="function"&&(xt=Ct(Wt,o)),Wt=8==oe("08")?oe:function(n,t){return oe(rt(n)?n.replace(d,""):n,t||0) -},R.after=function(n,t){return 1>n?t():function(){return 1>--n?t.apply(this,arguments):void 0}},R.assign=ge,R.at=function(n){for(var t=-1,e=Gt.apply(zt,J(arguments,1)),r=e.length,u=It(r);++tdt(f,c)){o&&f.push(c);for(var s=e;--s;)if(!(r[s]||(r[s]=T(t[s],0,100)))(c))continue n;i.push(c)}}return i},R.invert=Y,R.invoke=function(n,t){var e=J(arguments,2),r=-1,u=typeof t=="function",a=n?n.length:0,o=It(typeof a=="number"?a:0);return lt(n,function(n){o[++r]=(u?t:n[t]).apply(n,e)}),o},R.keys=pe,R.map=pt,R.max=st,R.memoize=function(n,t){var e={};return function(){var r=p+(t?t.apply(this,arguments):arguments[0]); -return Lt.call(e,r)?e[r]:e[r]=n.apply(this,arguments)}},R.merge=ut,R.min=function(n,t,e){var r=1/0,u=r;if(!t&&le(n)){e=-1;for(var a=n.length;++edt(a,e,1))&&(u[e]=n)}),u},R.once=function(n){var t,e;return function(){return t?e:(t=r,e=n.apply(this,arguments),n=u,e) -}},R.pairs=function(n){for(var t=-1,e=pe(n),r=e.length,u=It(r);++te?ue(0,r+e):ae(e,r-1))+1);r--;)if(n[r]===t)return r;return-1},R.mixin=Nt,R.noConflict=function(){return o._=Kt,this},R.parseInt=Wt,R.random=function(n,t){return n==u&&t==u&&(t=1),n=+n||0,t==u&&(t=n,n=0),n+Ht(ie()*((+t||0)-n+1)) -},R.reduce=gt,R.reduceRight=ht,R.result=function(n,t){var r=n?n[t]:e;return nt(r)?n[t]():r},R.runInContext=t,R.size=function(n){var t=n?n.length:0;return typeof t=="number"?t:pe(n).length},R.some=yt,R.sortedIndex=kt,R.template=function(n,t,u){var a=R.templateSettings;n||(n=""),u=he({},u,a);var o,i=he({},u.imports,a.imports),a=pe(i),i=at(i),f=0,c=u.interpolate||_,l="__p+='",c=Rt((u.escape||_).source+"|"+c.source+"|"+(c===b?y:_).source+"|"+(u.evaluate||_).source+"|$","g");n.replace(c,function(t,e,u,a,i,c){return u||(u=a),l+=n.slice(f,c).replace(w,M),e&&(l+="'+__e("+e+")+'"),i&&(o=r,l+="';"+i+";__p+='"),u&&(l+="'+((__t=("+u+"))==null?'':__t)+'"),f=c+t.length,t -}),l+="';\n",c=u=u.variable,c||(u="obj",l="with("+u+"){"+l+"}"),l=(o?l.replace(s,""):l).replace(v,"$1").replace(g,"$1;"),l="function("+u+"){"+(c?"":u+"||("+u+"={});")+"var __t,__p='',__e=_.escape"+(o?",__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}":";")+l+"return __p}";try{var p=$t(a,"return "+l).apply(e,i)}catch(h){throw h.source=l,h}return t?p(t):(p.source=l,p)},R.unescape=function(n){return n==u?"":Tt(n).replace(h,L)},R.uniqueId=function(n){var t=++c;return Tt(n==u?"":n)+t -},R.all=it,R.any=yt,R.detect=ct,R.foldl=gt,R.foldr=ht,R.include=ot,R.inject=gt,me(R,function(n,t){R.prototype[t]||(R.prototype[t]=function(){var t=[this.__wrapped__];return Qt.apply(t,arguments),n.apply(R,t)})}),R.first=mt,R.last=function(n,t,e){if(n){var r=0,a=n.length;if(typeof t!="number"&&t!=u){var o=a;for(t=R.createCallback(t,e);o--&&t(n[o],o,n);)r++}else if(r=t,r==u||e)return n[a-1];return J(n,ue(0,a-r))}},R.take=mt,R.head=mt,me(R,function(n,t){R.prototype[t]||(R.prototype[t]=function(t,e){var r=n(this.__wrapped__,t,e); -return t==u||e&&typeof t!="function"?r:new V(r)})}),R.VERSION="1.1.1",R.prototype.toString=function(){return Tt(this.__wrapped__)},R.prototype.value=Et,R.prototype.valueOf=Et,lt(["join","pop","shift"],function(n){var t=zt[n];R.prototype[n]=function(){return t.apply(this.__wrapped__,arguments)}}),lt(["push","reverse","sort","unshift"],function(n){var t=zt[n];R.prototype[n]=function(){return t.apply(this.__wrapped__,arguments),this}}),lt(["concat","slice","splice"],function(n){var t=zt[n];R.prototype[n]=function(){return new V(t.apply(this.__wrapped__,arguments)) -}}),R}var e,r=!0,u=null,a=!1,o=typeof exports=="object"&&exports,i=typeof module=="object"&&module&&module.exports==o&&module,f=typeof global=="object"&&global;(f.global===f||f.window===f)&&(n=f);var c=0,l={},p=+new Date+"",s=/\b__p\+='';/g,v=/\b(__p\+=)''\+/g,g=/(__e\(.*?\)|\b__t\))\+'';/g,h=/&(?:amp|lt|gt|quot|#39);/g,y=/\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g,m=/\w*$/,b=/<%=([\s\S]+?)%>/g,d=/^0+(?=.$)/,_=/($^)/,k=/[&<>"']/g,w=/['\n\r\t\u2028\u2029\\]/g,j="Array Boolean Date Function Math Number Object RegExp String _ attachEvent clearTimeout isFinite isNaN parseInt setImmediate setTimeout".split(" "),C="[object Arguments]",x="[object Array]",O="[object Boolean]",N="[object Date]",E="[object Number]",I="[object Object]",S="[object RegExp]",A="[object String]",$={"[object Function]":a}; -$[C]=$[x]=$[O]=$[N]=$[E]=$[I]=$[S]=$[A]=r;var q={"boolean":a,"function":r,object:r,number:a,string:a,undefined:a},B={"\\":"\\","'":"'","\n":"n","\r":"r"," ":"t","\u2028":"u2028","\u2029":"u2029"},F=t();typeof define=="function"&&typeof define.amd=="object"&&define.amd?(n._=F,define(function(){return F})):o&&!o.nodeType?i?(i.exports=F)._=F:o._=F:n._=F})(this); \ No newline at end of file +;(function(n){function t(r){function a(n){return n&&typeof n=="object"&&!ie(n)&&Vt.call(n,"__wrapped__")?n:new P(n)}function q(n,t,e){var r=n.length,u=r-t>=e;if(u){var a={};for(e=t-1;++et||typeof n=="undefined")return 1;if(ne?0:e);++re?ne(0,u+e):e)||0,typeof u=="number"?a=-1<(Z(n)?n.indexOf(t,e):ht(n,t,e)):ve(n,function(n){return++ru&&(u=o)}}else t=!t&&Z(n)?B:a.createCallback(t,e),it(n,function(n,e,a){e=t(n,e,a),e>r&&(r=e,u=n)});return u}function ct(n,t){var e=-1,r=n?n.length:0;if(typeof r=="number")for(var u=xt(r);++earguments.length;t=a.createCallback(t,r,4);var i=-1,o=n.length;if(typeof o=="number")for(u&&(e=n[++i]);++iarguments.length;if(typeof u!="number")var o=oe(n),u=o.length;return t=a.createCallback(t,r,4),it(n,function(r,a,f){a=o?o[--u]:--u,e=i?(i=!1,n[a]):t(e,n[a],a,f)}),e}function st(n,t,e){var r;t=a.createCallback(t,e),e=-1;var u=n?n.length:0; +if(typeof u=="number")for(;++ee?ne(0,u+e):e||0)-1;else if(e)return r=mt(n,t),n[r]===t?r:-1;for(;++r>>1,e(n[r])ht(c,s))&&((e||l)&&c.push(s),o.push(r))}return o}function dt(n,t){for(var e=-1,r=n?n.length:0,u={};++e/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:h,variable:"",imports:{_:a}},Ot={a:"q,w,g",h:"var a=arguments,b=0,c=typeof g=='number'?2:a.length;while(++b":">",'"':""","'":"'"},ce=L(fe),le=T(Ot,{h:Ot.h.replace(";",";if(c>3&&typeof a[c-2]=='function'){var d=p.createCallback(a[--c-1],a[c--],2);}else if(c>2&&typeof a[c-1]=='function'){d=a[--c];}"),f:"u[i]=d?d(u[i],m[i]):m[i]"}),pe=T(Ot),se=T(St,At,{i:!1}),ve=T(St,At),ge=function(n){if(!n||Lt.call(n)!=O)return!1; +var t=n.valueOf,e=typeof t=="function"&&(e=Ut(t))&&Ut(e);return e?n==e||Ut(n)==e:M(n)};return It&&u&&typeof Ht=="function"&&(kt=_t(Ht,r)),Ht=8==ee("08")?ee:function(n,t){return ee(Z(n)?n.replace(y,""):n,t||0)},a.after=function(n,t){return 1>n?t():function(){return 1>--n?t.apply(this,arguments):void 0}},a.assign=le,a.at=function(n){for(var t=-1,e=Kt.apply(Ft,U(arguments,1)),r=e.length,u=xt(r);++tht(c,l)){i&&c.push(l);for(var s=e;--s;)if(!(r[s]||(r[s]=q(t[s],0,100)))(l))continue n;o.push(l)}}return o +},a.invert=L,a.invoke=function(n,t){var e=U(arguments,2),r=-1,u=typeof t=="function",a=n?n.length:0,i=xt(typeof a=="number"?a:0);return it(n,function(n){i[++r]=(u?t:n[t]).apply(n,e)}),i},a.keys=oe,a.map=ot,a.max=ft,a.memoize=function(n,t){var e={};return function(){var r=f+(t?t.apply(this,arguments):arguments[0]);return Vt.call(e,r)?e[r]:e[r]=n.apply(this,arguments)}},a.merge=nt,a.min=function(n,t,e){var r=1/0,u=r;if(!t&&ie(n)){e=-1;for(var i=n.length;++eht(i,e,1))&&(u[e]=n)}),u},a.once=function(n){var t,e;return function(){return t?e:(t=!0,e=n.apply(this,arguments),n=null,e)}},a.pairs=function(n){for(var t=-1,e=oe(n),r=e.length,u=xt(r);++te?ne(0,r+e):te(e,r-1))+1);r--;)if(n[r]===t)return r;return-1},a.mixin=jt,a.noConflict=function(){return r._=Tt,this},a.parseInt=Ht,a.random=function(n,t){return null==n&&null==t&&(t=1),n=+n||0,null==t&&(t=n,n=0),n+Mt(re()*((+t||0)-n+1))},a.reduce=lt,a.reduceRight=pt,a.result=function(n,t){var r=n?n[t]:e;return W(r)?n[t]():r},a.runInContext=t,a.size=function(n){var t=n?n.length:0;return typeof t=="number"?t:oe(n).length +},a.some=st,a.sortedIndex=mt,a.template=function(n,t,r){var u=a.templateSettings;n||(n=""),r=pe({},r,u);var i,o=pe({},r.imports,u.imports),u=oe(o),o=tt(o),f=0,s=r.interpolate||m,g="__p+='",s=$t((r.escape||m).source+"|"+s.source+"|"+(s===h?v:m).source+"|"+(r.evaluate||m).source+"|$","g");n.replace(s,function(t,e,r,u,a,o){return r||(r=u),g+=n.slice(f,o).replace(d,D),e&&(g+="'+__e("+e+")+'"),a&&(i=!0,g+="';"+a+";__p+='"),r&&(g+="'+((__t=("+r+"))==null?'':__t)+'"),f=o+t.length,t}),g+="';\n",s=r=r.variable,s||(r="obj",g="with("+r+"){"+g+"}"),g=(i?g.replace(c,""):g).replace(l,"$1").replace(p,"$1;"),g="function("+r+"){"+(s?"":r+"||("+r+"={});")+"var __t,__p='',__e=_.escape"+(i?",__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}":";")+g+"return __p}"; +try{var y=Et(u,"return "+g).apply(e,o)}catch(b){throw b.source=g,b}return t?y(t):(y.source=g,y)},a.unescape=function(n){return null==n?"":qt(n).replace(s,V)},a.uniqueId=function(n){var t=++i;return qt(null==n?"":n)+t},a.all=rt,a.any=st,a.detect=at,a.foldl=lt,a.foldr=pt,a.include=et,a.inject=lt,ve(a,function(n,t){a.prototype[t]||(a.prototype[t]=function(){var t=[this.__wrapped__];return Gt.apply(t,arguments),n.apply(a,t)})}),a.first=vt,a.last=function(n,t,e){if(n){var r=0,u=n.length;if(typeof t!="number"&&null!=t){var i=u; +for(t=a.createCallback(t,e);i--&&t(n[i],i,n);)r++}else if(r=t,null==r||e)return n[u-1];return U(n,ne(0,u-r))}},a.take=vt,a.head=vt,ve(a,function(n,t){a.prototype[t]||(a.prototype[t]=function(t,e){var r=n(this.__wrapped__,t,e);return null==t||e&&typeof t!="function"?r:new P(r)})}),a.VERSION="1.1.1",a.prototype.toString=function(){return qt(this.__wrapped__)},a.prototype.value=Ct,a.prototype.valueOf=Ct,it(["join","pop","shift"],function(n){var t=Ft[n];a.prototype[n]=function(){return t.apply(this.__wrapped__,arguments) +}}),it(["push","reverse","sort","unshift"],function(n){var t=Ft[n];a.prototype[n]=function(){return t.apply(this.__wrapped__,arguments),this}}),it(["concat","slice","splice"],function(n){var t=Ft[n];a.prototype[n]=function(){return new P(t.apply(this.__wrapped__,arguments))}}),a}var e,r=typeof exports=="object"&&exports,u=typeof module=="object"&&module&&module.exports==r&&module,a=typeof global=="object"&&global;(a.global===a||a.window===a)&&(n=a);var i=0,o={},f=+new Date+"",c=/\b__p\+='';/g,l=/\b(__p\+=)''\+/g,p=/(__e\(.*?\)|\b__t\))\+'';/g,s=/&(?:amp|lt|gt|quot|#39);/g,v=/\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g,g=/\w*$/,h=/<%=([\s\S]+?)%>/g,y=/^0+(?=.$)/,m=/($^)/,b=/[&<>"']/g,d=/['\n\r\t\u2028\u2029\\]/g,_="Array Boolean Date Function Math Number Object RegExp String _ attachEvent clearTimeout isFinite isNaN parseInt setImmediate setTimeout".split(" "),k="[object Arguments]",w="[object Array]",j="[object Boolean]",C="[object Date]",x="[object Number]",O="[object Object]",N="[object RegExp]",E="[object String]",I={"[object Function]":!1}; +I[k]=I[w]=I[j]=I[C]=I[x]=I[O]=I[N]=I[E]=!0;var S={"boolean":!1,"function":!0,object:!0,number:!1,string:!1,undefined:!1},A={"\\":"\\","'":"'","\n":"n","\r":"r"," ":"t","\u2028":"u2028","\u2029":"u2029"},$=t();typeof define=="function"&&typeof define.amd=="object"&&define.amd?(n._=$,define(function(){return $})):r&&!r.nodeType?u?(u.exports=$)._=$:r._=$:n._=$})(this); \ No newline at end of file diff --git a/dist/lodash.underscore.js b/dist/lodash.underscore.js index b5f7cb9b6..380b57ddc 100644 --- a/dist/lodash.underscore.js +++ b/dist/lodash.underscore.js @@ -3461,9 +3461,9 @@ if (options === true) { var leading = true; trailing = false; - } else if (options) { + } else if (options && objectTypes[typeof options]) { leading = options.leading; - trailing = options.trailing; + trailing = 'trailing' in options ? options.trailing : trailing; } return function() { var isLeading = leading && !timeoutId; @@ -3649,9 +3649,9 @@ } if (options === false) { leading = false; - } else if (options) { - leading = options.leading; - trailing = options.trailing; + } else if (options && objectTypes[typeof options]) { + leading = 'leading' in options ? options.leading : leading; + trailing = 'trailing' in options ? options.trailing : trailing; } return function() { var now = new Date; diff --git a/dist/lodash.underscore.min.js b/dist/lodash.underscore.min.js index 6057ab5b8..8f6f8c70b 100644 --- a/dist/lodash.underscore.min.js +++ b/dist/lodash.underscore.min.js @@ -19,16 +19,17 @@ var e=typeof n;if("function"!=e){if("object"!=e)return function(t){return t[n]}; }})}var H=!0,J=null,K=!1,L=typeof exports=="object"&&exports,Q=typeof module=="object"&&module&&module.exports==L&&module,X=typeof global=="object"&&global;(X.global===X||X.window===X)&&(n=X);var Y=0,Z={},nt=+new Date+"",tt=/&(?:amp|lt|gt|quot|#39);/g,rt=/($^)/,et=/[&<>"']/g,ut=/['\n\r\t\u2028\u2029\\]/g,ot="[object Arguments]",it="[object Array]",at="[object Boolean]",ft="[object Date]",ct="[object Number]",lt="[object Object]",pt="[object RegExp]",st="[object String]",vt={"boolean":K,"function":H,object:H,number:K,string:K,undefined:K},gt={"\\":"\\","'":"'","\n":"n","\r":"r"," ":"t","\u2028":"u2028","\u2029":"u2029"},ht=[],X={},yt=n._,mt=RegExp("^"+(X.valueOf+"").replace(/[.*+?^${}()|[\]\\]/g,"\\$&").replace(/valueOf|for [^\]]+/g,".+?")+"$"),_t=Math.ceil,dt=n.clearTimeout,bt=ht.concat,jt=Math.floor,wt=X.hasOwnProperty,At=ht.push,xt=n.setTimeout,Ot=ht.slice,Et=X.toString,St=mt.test(St=Et.bind)&&St,Nt=mt.test(Nt=Array.isArray)&&Nt,kt=n.isFinite,Bt=n.isNaN,Ft=mt.test(Ft=Object.keys)&&Ft,Rt=Math.max,qt=Math.min,Dt=Math.random,X=mt.test(n.attachEvent),Mt=St&&!/\n|true/.test(St+X),Tt={}; (function(){var n={0:1,length:1};Tt.argsObject=arguments.constructor==Object,Tt.fastBind=St&&!Mt,Tt.spliceObjects=(ht.splice.call(n,0,1),!n[0])})(1),u.templateSettings={escape:/<%-([\s\S]+?)%>/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,variable:""},c.prototype=u.prototype,s(arguments)||(s=function(n){return n?wt.call(n,"callee"):K});var $t=Nt||function(n){return Tt.argsObject&&n instanceof Array||Et.call(n)==it},It=Ft?function(n){return b(n)?Ft(n):[]}:e,zt={"&":"&","<":"<",">":">",'"':""","'":"'"},Ct=y(zt); d(/x/)&&(d=function(n){return n instanceof Function||"[object Function]"==Et.call(n)}),u.after=function(n,t){return 1>n?t():function(){return 1>--n?t.apply(this,arguments):void 0}},u.bind=U,u.bindAll=function(n){for(var t=bt.apply(ht,arguments),r=1I(e,o,r)&&u.push(o)}return u},u.filter=E,u.flatten=$,u.forEach=N,u.functions=h,u.groupBy=function(n,t,r){var e={};return t=V(t,r),N(n,function(n,r,u){r=t(n,r,u)+"",(wt.call(e,r)?e[r]:e[r]=[]).push(n)}),e},u.initial=function(n,t,r){if(!n)return[];var e=0,u=n.length;if(typeof t!="number"&&t!=J){var o=u;for(t=V(t,r);o--&&t(n[o],o,n);)e++}else e=t==J||r?1:t||e; return Ot.call(n,0,qt(Rt(0,u-e),u))},u.intersection=function(n){var t=arguments,r=t.length,e=-1,u=n?n.length:0,o=[];n:for(;++eI(o,i)){for(var a=r;--a;)if(0>I(t[a],i))continue n;o.push(i)}}return o},u.invert=y,u.invoke=function(n,t){var r=Ot.call(arguments,2),e=-1,u=typeof t=="function",o=n?n.length:0,i=Array(typeof o=="number"?o:0);return N(n,function(n){i[++e]=(u?t:n[t]).apply(n,r)}),i},u.keys=It,u.map=k,u.max=B,u.memoize=function(n,t){var r={};return function(){var e=nt+(t?t.apply(this,arguments):arguments[0]); return wt.call(r,e)?r[e]:r[e]=n.apply(this,arguments)}},u.min=function(n,t,r){var e=1/0,u=e,o=-1,i=n?n.length:0;if(t||typeof i!="number")t=V(t,r),N(n,function(n,r,o){r=t(n,r,o),rI(t,r,1)&&(e[r]=n)}),e},u.once=function(n){var t,r;return function(){return t?r:(t=H,r=n.apply(this,arguments),n=J,r)}},u.pairs=function(n){for(var t=-1,r=It(n),e=r.length,u=Array(e);++tI(arguments,u,1)&&e.push(u)}return e},u.wrap=function(n,t){return function(){var r=[n];return At.apply(r,arguments),t.apply(this,r)}},u.zip=function(n){for(var t=-1,r=n?B(F(arguments,"length")):0,e=Array(r);++tr?Rt(0,e+r):qt(r,e-1))+1);e--;)if(n[e]===t)return e; -return-1},u.mixin=G,u.noConflict=function(){return n._=yt,this},u.random=function(n,t){return n==J&&t==J&&(t=1),n=+n||0,t==J&&(t=n,n=0),n+jt(Dt()*((+t||0)-n+1))},u.reduce=R,u.reduceRight=q,u.result=function(n,t){var r=n?n[t]:J;return d(r)?n[t]():r},u.size=function(n){var t=n?n.length:0;return typeof t=="number"?t:It(n).length},u.some=D,u.sortedIndex=C,u.template=function(n,t,r){n||(n=""),r=g({},r,u.templateSettings);var e=0,o="__p+='",i=r.variable;n.replace(RegExp((r.escape||rt).source+"|"+(r.interpolate||rt).source+"|"+(r.evaluate||rt).source+"|$","g"),function(t,r,u,i,f){return o+=n.slice(e,f).replace(ut,a),r&&(o+="'+_['escape']("+r+")+'"),i&&(o+="';"+i+";__p+='"),u&&(o+="'+((__t=("+u+"))==null?'':__t)+'"),e=f+t.length,t -}),o+="';\n",i||(i="obj",o="with("+i+"||{}){"+o+"}"),o="function("+i+"){var __t,__p='',__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}"+o+"return __p}";try{var f=Function("_","return "+o)(u)}catch(c){throw c.source=o,c}return t?f(t):(f.source=o,f)},u.unescape=function(n){return n==J?"":(n+"").replace(tt,p)},u.uniqueId=function(n){var t=++Y+"";return n?n+t:t},u.all=O,u.any=D,u.detect=S,u.foldl=R,u.foldr=q,u.include=x,u.inject=R,u.first=T,u.last=function(n,t,r){if(n){var e=0,u=n.length; -if(typeof t!="number"&&t!=J){var o=u;for(t=V(t,r);o--&&t(n[o],o,n);)e++}else if(e=t,e==J||r)return n[u-1];return Ot.call(n,Rt(0,u-e))}},u.take=T,u.head=T,u.chain=function(n){return n=new c(n),n.__chain__=H,n},u.VERSION="1.1.1",G(u),u.prototype.chain=function(){return this.__chain__=H,this},u.prototype.value=function(){return this.__wrapped__},N("pop push reverse shift sort splice unshift".split(" "),function(n){var t=ht[n];u.prototype[n]=function(){var n=this.__wrapped__;return t.apply(n,arguments),!Tt.spliceObjects&&0===n.length&&delete n[0],this -}}),N(["concat","join","slice"],function(n){var t=ht[n];u.prototype[n]=function(){var n=t.apply(this.__wrapped__,arguments);return this.__chain__&&(n=new c(n),n.__chain__=H),n}}),typeof define=="function"&&typeof define.amd=="object"&&define.amd?(n._=u,define(function(){return u})):L&&!L.nodeType?Q?(Q.exports=u)._=u:L._=u:n._=u})(this); \ No newline at end of file +return N(n,function(n){var r=jt(Dt()*(++t+1));e[t]=e[r],e[r]=n}),e},u.sortBy=function(n,t,r){var e=-1,u=n?n.length:0,i=Array(typeof u=="number"?u:0);for(t=V(t,r),N(n,function(n,r,u){i[++e]={a:t(n,r,u),b:e,c:n}}),u=i.length,i.sort(o);u--;)i[u]=i[u].c;return i},u.tap=function(n,t){return t(n),n},u.throttle=function(n,t,r){function e(){f=new Date,a=J,l&&(o=n.apply(i,u))}var u,o,i,a,f=0,c=H,l=H;return r===K?c=K:r&&vt[typeof r]&&(c="leading"in r?r.leading:c,l="trailing"in r?r.trailing:l),function(){var r=new Date; +!a&&!c&&(f=r);var l=t-(r-f);return u=arguments,i=this,0I(arguments,u,1)&&e.push(u)}return e},u.wrap=function(n,t){return function(){var r=[n]; +return At.apply(r,arguments),t.apply(this,r)}},u.zip=function(n){for(var t=-1,r=n?B(F(arguments,"length")):0,e=Array(r);++tr?Rt(0,e+r):qt(r,e-1))+1);e--;)if(n[e]===t)return e;return-1},u.mixin=G,u.noConflict=function(){return n._=yt,this},u.random=function(n,t){return n==J&&t==J&&(t=1),n=+n||0,t==J&&(t=n,n=0),n+jt(Dt()*((+t||0)-n+1))},u.reduce=R,u.reduceRight=q,u.result=function(n,t){var r=n?n[t]:J;return d(r)?n[t]():r},u.size=function(n){var t=n?n.length:0;return typeof t=="number"?t:It(n).length +},u.some=D,u.sortedIndex=C,u.template=function(n,t,r){n||(n=""),r=g({},r,u.templateSettings);var e=0,o="__p+='",i=r.variable;n.replace(RegExp((r.escape||rt).source+"|"+(r.interpolate||rt).source+"|"+(r.evaluate||rt).source+"|$","g"),function(t,r,u,i,f){return o+=n.slice(e,f).replace(ut,a),r&&(o+="'+_['escape']("+r+")+'"),i&&(o+="';"+i+";__p+='"),u&&(o+="'+((__t=("+u+"))==null?'':__t)+'"),e=f+t.length,t}),o+="';\n",i||(i="obj",o="with("+i+"||{}){"+o+"}"),o="function("+i+"){var __t,__p='',__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}"+o+"return __p}"; +try{var f=Function("_","return "+o)(u)}catch(c){throw c.source=o,c}return t?f(t):(f.source=o,f)},u.unescape=function(n){return n==J?"":(n+"").replace(tt,p)},u.uniqueId=function(n){var t=++Y+"";return n?n+t:t},u.all=O,u.any=D,u.detect=S,u.foldl=R,u.foldr=q,u.include=x,u.inject=R,u.first=T,u.last=function(n,t,r){if(n){var e=0,u=n.length;if(typeof t!="number"&&t!=J){var o=u;for(t=V(t,r);o--&&t(n[o],o,n);)e++}else if(e=t,e==J||r)return n[u-1];return Ot.call(n,Rt(0,u-e))}},u.take=T,u.head=T,u.chain=function(n){return n=new c(n),n.__chain__=H,n +},u.VERSION="1.1.1",G(u),u.prototype.chain=function(){return this.__chain__=H,this},u.prototype.value=function(){return this.__wrapped__},N("pop push reverse shift sort splice unshift".split(" "),function(n){var t=ht[n];u.prototype[n]=function(){var n=this.__wrapped__;return t.apply(n,arguments),!Tt.spliceObjects&&0===n.length&&delete n[0],this}}),N(["concat","join","slice"],function(n){var t=ht[n];u.prototype[n]=function(){var n=t.apply(this.__wrapped__,arguments);return this.__chain__&&(n=new c(n),n.__chain__=H),n +}}),typeof define=="function"&&typeof define.amd=="object"&&define.amd?(n._=u,define(function(){return u})):L&&!L.nodeType?Q?(Q.exports=u)._=u:L._=u:n._=u})(this); \ No newline at end of file diff --git a/lodash.js b/lodash.js index 1a8d099a2..5573e476e 100644 --- a/lodash.js +++ b/lodash.js @@ -4476,9 +4476,9 @@ if (options === true) { var leading = true; trailing = false; - } else if (options) { + } else if (options && objectTypes[typeof options]) { leading = options.leading; - trailing = options.trailing; + trailing = 'trailing' in options ? options.trailing : trailing; } return function() { var isLeading = leading && !timeoutId; @@ -4699,9 +4699,9 @@ } if (options === false) { leading = false; - } else if (options) { - leading = options.leading; - trailing = options.trailing; + } else if (options && objectTypes[typeof options]) { + leading = 'leading' in options ? options.leading : leading; + trailing = 'trailing' in options ? options.trailing : trailing; } return function() { var now = new Date; diff --git a/test/test.js b/test/test.js index 44c707979..c6d768dc9 100644 --- a/test/test.js +++ b/test/test.js @@ -507,14 +507,14 @@ (function() { test('subsequent "immediate" debounced calls return the last `func` result', function() { - var debounced = _.debounce(function(value) { return value; }, 32, true), + var debounced = _.debounce(_.identity, 32, true), result = [debounced('x'), debounced('y')]; deepEqual(result, ['x', 'x']); }); asyncTest('subsequent debounced calls return the last `func` result', function() { - var debounced = _.debounce(function(value) { return value; }, 32); + var debounced = _.debounce(_.identity, 32); debounced('x'); setTimeout(function() { @@ -522,6 +522,58 @@ QUnit.start(); }, 64); }); + + asyncTest('should apply default options correctly', function() { + var count = 0; + + var debounced = _.debounce(function(value) { + count++; + return value; + }, 32, {}); + + strictEqual(debounced('x'), undefined); + + setTimeout(function() { + strictEqual(count, 1); + QUnit.start(); + }, 64); + }); + + test('should work with `leading` option', function() { + _.each([true, { 'leading': true }], function(options) { + var withLeading = _.debounce(_.identity, 32, options); + equal(withLeading('x'), 'x'); + }); + + _.each([false, { 'leading': false }], function(options) { + var withoutLeading = _.debounce(_.identity, 32, options); + strictEqual(withoutLeading('x'), undefined); + }); + }); + + asyncTest('should work with `trailing` option', function() { + var withCount = 0, + withoutCount = 0; + + var withTrailing = _.debounce(function(value) { + withCount++; + return value; + }, 32, { 'trailing': true }); + + var withoutTrailing = _.debounce(function(value) { + withoutCount++; + return value; + }, 32, { 'trailing': false }); + + strictEqual(withTrailing('x'), undefined); + strictEqual(withoutTrailing('x'), undefined); + + setTimeout(function() { + strictEqual(withCount, 1); + strictEqual(withoutCount, 0); + QUnit.start(); + }, 64); + }); }()); /*--------------------------------------------------------------------------*/ @@ -2603,53 +2655,109 @@ }); asyncTest('supports recursive calls', function() { - var counter = 0; + var count = 0; var throttled = _.throttle(function() { - counter++; - if (counter < 10) { + count++; + if (count < 10) { throttled(); } }, 32); throttled(); - equal(counter, 1); + equal(count, 1); setTimeout(function() { - ok(counter < 3) + ok(count < 3) QUnit.start(); }, 32); }); asyncTest('should not trigger a trailing call when invoked once', function() { - var counter = 0, - throttled = _.throttle(function() { counter++; }, 32); + var count = 0, + throttled = _.throttle(function() { count++; }, 32); throttled(); - equal(counter, 1); + equal(count, 1); setTimeout(function() { - equal(counter, 1); + equal(count, 1); QUnit.start(); }, 96); }); asyncTest('should trigger trailing call when invoked repeatedly', function() { - var counter = 0, + var count = 0, limit = 48, - throttled = _.throttle(function() { counter++; }, 32), + throttled = _.throttle(function() { count++; }, 32), start = new Date; while ((new Date - start) < limit) { throttled(); } - var lastCount = counter; + var lastCount = count; ok(lastCount > 1); setTimeout(function() { - ok(counter > lastCount); + ok(count > lastCount); QUnit.start(); }, 96); }); + + asyncTest('should apply default options correctly', function() { + var count = 0; + + var throttled = _.throttle(function(value) { + count++; + return value; + }, 32, {}); + + _.times(2, function() { + equal(throttled('x'), 'x'); + }); + + setTimeout(function() { + strictEqual(count, 2); + QUnit.start(); + }, 64); + }); + + test('should work with `leading` option', function() { + _.each([true, { 'leading': true }], function(options) { + var withLeading = _.throttle(_.identity, 32, options); + equal(withLeading('x'), 'x'); + }); + + _.each([false, { 'leading': false }], function(options) { + var withoutLeading = _.throttle(_.identity, 32, options); + strictEqual(withoutLeading('x'), undefined); + }); + }); + + asyncTest('should work with `trailing` option', function() { + var withCount = 0, + withoutCount = 0; + + var withTrailing = _.throttle(function(value) { + withCount++; + return value; + }, 32, { 'trailing': true }); + + var withoutTrailing = _.throttle(function(value) { + withoutCount++; + return value; + }, 32, { 'trailing': false }); + + _.times(2, function() { + equal(withTrailing('x'), 'x'); + equal(withoutTrailing('x'), 'x'); + }); + + setTimeout(function() { + strictEqual(withCount, 2); + strictEqual(withoutCount, 1); + QUnit.start(); + }, 64); + }); }()); /*--------------------------------------------------------------------------*/