From c3ac17296078b94aa20bd00faf30d4e018c46d87 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 16 Apr 2013 01:52:47 -0700 Subject: [PATCH] Simplify `_.debounce` and `_.throttle` for the `underscore` build. Former-commit-id: 56dc526cf71130084651d06b8caadce573471131 --- build.js | 65 +++++++++++++++++++++++++++++++++++ dist/lodash.underscore.js | 43 ++++++----------------- dist/lodash.underscore.min.js | 56 +++++++++++++++--------------- 3 files changed, 104 insertions(+), 60 deletions(-) diff --git a/build.js b/build.js index 47ed7c2ae..f25063733 100755 --- a/build.js +++ b/build.js @@ -2059,6 +2059,36 @@ '}' ].join('\n')); + // replace `_.debounce` + source = replaceFunction(source, 'debounce', [ + 'function debounce(func, wait, immediate) {', + ' var args,', + ' result,', + ' thisArg,', + ' timeoutId;', + '', + ' function delayed() {', + ' timeoutId = null;', + ' if (!immediate) {', + ' result = func.apply(thisArg, args);', + ' }', + ' }', + ' return function() {', + ' var isImmediate = immediate && !timeoutId;', + ' args = arguments;', + ' thisArg = this;', + '', + ' clearTimeout(timeoutId);', + ' timeoutId = setTimeout(delayed, wait);', + '', + ' if (isImmediate) {', + ' result = func.apply(thisArg, args);', + ' }', + ' return result;', + ' };', + '}' + ].join('\n')); + // replace `_.defaults` source = replaceFunction(source, 'defaults', [ 'function defaults(object) {', @@ -2374,6 +2404,41 @@ '}' ].join('\n')); + // replace `_.throttle` + source = replaceFunction(source, 'throttle', [ + 'function throttle(func, wait) {', + ' var args,', + ' result,', + ' thisArg,', + ' timeoutId,', + ' lastCalled = 0;', + '', + ' function trailingCall() {', + ' lastCalled = new Date;', + ' timeoutId = null;', + ' result = func.apply(thisArg, args);', + ' }', + ' return function() {', + ' var now = new Date,', + ' remaining = wait - (now - lastCalled);', + '', + ' args = arguments;', + ' thisArg = this;', + '', + ' if (remaining <= 0) {', + ' clearTimeout(timeoutId);', + ' timeoutId = null;', + ' lastCalled = now;', + ' result = func.apply(thisArg, args);', + ' }', + ' else if (!timeoutId) {', + ' timeoutId = setTimeout(trailingCall, remaining);', + ' }', + ' return result;', + ' };', + '}' + ].join('\n')); + // replace `_.times` source = replaceFunction(source, 'times', [ 'function times(n, callback, thisArg) {', diff --git a/dist/lodash.underscore.js b/dist/lodash.underscore.js index 0dbf74319..5712d6669 100644 --- a/dist/lodash.underscore.js +++ b/dist/lodash.underscore.js @@ -3482,35 +3482,27 @@ * var lazyLayout = _.debounce(calculateLayout, 300); * jQuery(window).on('resize', lazyLayout); */ - function debounce(func, wait, options) { + function debounce(func, wait, immediate) { var args, result, thisArg, - timeoutId, - trailing = true; + timeoutId; function delayed() { timeoutId = null; - if (trailing) { + if (!immediate) { result = func.apply(thisArg, args); } } - if (options === true) { - var leading = true; - trailing = false; - } else if (options && objectTypes[typeof options]) { - leading = options.leading; - trailing = 'trailing' in options ? options.trailing : trailing; - } return function() { - var isLeading = leading && !timeoutId; + var isImmediate = immediate && !timeoutId; args = arguments; thisArg = this; clearTimeout(timeoutId); timeoutId = setTimeout(delayed, wait); - if (isLeading) { + if (isImmediate) { result = func.apply(thisArg, args); } return result; @@ -3667,35 +3659,22 @@ * var throttled = _.throttle(updatePosition, 100); * jQuery(window).on('scroll', throttled); */ - function throttle(func, wait, options) { + function throttle(func, wait) { var args, result, thisArg, timeoutId, - lastCalled = 0, - leading = true, - trailing = true; + lastCalled = 0; function trailingCall() { lastCalled = new Date; timeoutId = null; - - if (trailing) { - result = func.apply(thisArg, args); - } - } - if (options === false) { - leading = false; - } else if (options && objectTypes[typeof options]) { - leading = 'leading' in options ? options.leading : leading; - trailing = 'trailing' in options ? options.trailing : trailing; + result = func.apply(thisArg, args); } return function() { - var now = new Date; - if (!timeoutId && !leading) { - lastCalled = now; - } - var remaining = wait - (now - lastCalled); + var now = new Date, + remaining = wait - (now - lastCalled); + args = arguments; thisArg = this; diff --git a/dist/lodash.underscore.min.js b/dist/lodash.underscore.min.js index e30ba8222..dd6ef3ea8 100644 --- a/dist/lodash.underscore.min.js +++ b/dist/lodash.underscore.min.js @@ -4,31 +4,31 @@ * Build: `lodash underscore exports="amd,commonjs,global,node" -o ./dist/lodash.underscore.js` * Underscore.js 1.4.4 underscorejs.org/LICENSE */ -;(function(n){function r(n,r){var t;if(n&&vr[typeof n])for(t in n)if(Ar.call(n,t)&&r(n[t],t,n)===nr)break}function t(n,r){var t;if(n&&vr[typeof n])for(t in n)if(r(n[t],t,n)===nr)break}function e(n){var r,t=[];if(!n||!vr[typeof n])return t;for(r in n)Ar.call(n,r)&&t.push(r);return t}function u(n){return n instanceof u?n:new c(n)}function o(n,r){var t=n.b,e=r.b;if(n=n.a,r=r.a,n!==r){if(n>r||typeof n=="undefined")return 1;if(ne&&(e=t,u=n)});else for(;++ou&&(u=t);return u}function q(n,r){var t=-1,e=n?n.length:0;if(typeof e=="number")for(var u=Array(e);++targuments.length;t=W(t,u,4);var i=-1,a=n.length;if(typeof a=="number")for(o&&(e=n[++i]);++iarguments.length;if(typeof u!="number")var i=Ir(n),u=i.length;return r=W(r,e,4),N(n,function(e,a,f){a=i?i[--u]:--u,t=o?(o=L,n[a]):r(t,n[a],a,f)}),t}function D(n,t,e){var u;t=W(t,e),e=-1;var o=n?n.length:0;if(typeof o=="number")for(;++ez(e,o)&&u.push(o)}return u}function $(n,r,t){if(n){var e=0,u=n.length;if(typeof r!="number"&&r!=K){var o=-1;for(r=W(r,t);++ot?Fr(0,u+t):t||0)-1;else if(t)return e=P(n,r),n[e]===r?e:-1;for(;++e>>1,t(n[e])z(a,f))&&(t&&a.push(f),i.push(e))}return i}function V(n,r){return $r.fastBind||Sr&&2"']/g,or=/['\n\r\t\u2028\u2029\\]/g,ir="[object Arguments]",ar="[object Array]",fr="[object Boolean]",cr="[object Date]",lr="[object Number]",pr="[object Object]",sr="[object RegExp]",gr="[object String]",vr={"boolean":L,"function":J,object:J,number:L,string:L,undefined:L},hr={"\\":"\\","'":"'","\n":"n","\r":"r"," ":"t","\u2028":"u2028","\u2029":"u2029"},yr=[],Y={},mr=n._,_r=RegExp("^"+(Y.valueOf+"").replace(/[.*+?^${}()|[\]\\]/g,"\\$&").replace(/valueOf|for [^\]]+/g,".+?")+"$"),dr=Math.ceil,br=n.clearTimeout,jr=yr.concat,wr=Math.floor,Ar=Y.hasOwnProperty,xr=yr.push,Or=n.setTimeout,Er=Y.toString,Sr=_r.test(Sr=Er.bind)&&Sr,Nr=_r.test(Nr=Array.isArray)&&Nr,kr=n.isFinite,Br=n.isNaN,qr=_r.test(qr=Object.keys)&&qr,Fr=Math.max,Rr=Math.min,Dr=Math.random,Mr=yr.slice,Y=_r.test(n.attachEvent),Tr=Sr&&!/\n|true/.test(Sr+Y),$r={}; -(function(){var n={0:1,length:1};$r.argsObject=arguments.constructor==Object&&!(arguments instanceof Array),$r.fastBind=Sr&&!Tr,$r.spliceObjects=(yr.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?Ar.call(n,"callee"):L});var Ir=qr?function(n){return b(n)?qr(n):[]}:e,zr={"&":"&","<":"<",">":">",'"':""","'":"'"},Cr=y(zr),Pr=Nr||function(n){return Er.call(n)==ar -};d(/x/)&&(d=function(n){return"[object Function]"==Er.call(n)}),u.after=function(n,r){return 1>n?r():function(){return 1>--n?r.apply(this,arguments):void 0}},u.bind=V,u.bindAll=function(n){for(var r=1z(o,i)){for(var a=t;--a;)if(0>z(r[a],i))continue n;o.push(i)}}return o},u.invert=y,u.invoke=function(n,r){var t=Mr.call(arguments,2),e=-1,u=typeof r=="function",o=n?n.length:0,i=Array(typeof o=="number"?o:0);return N(n,function(n){i[++e]=(u?r:n[r]).apply(n,t)}),i},u.keys=Ir,u.map=k,u.max=B,u.memoize=function(n,r){var t={};return function(){var e=rr+(r?r.apply(this,arguments):arguments[0]);return Ar.call(t,e)?t[e]:t[e]=n.apply(this,arguments)}},u.min=function(n,r,t){var e=1/0,u=e,o=-1,i=n?n.length:0; -if(r||typeof i!="number")r=W(r,t),N(n,function(n,t,o){t=r(n,t,o),tz(r,t)&&(e[t]=n)}),e},u.once=function(n){var r,t;return function(){return r?t:(r=J,t=n.apply(this,arguments),n=K,t)}},u.pairs=function(n){for(var r=-1,t=Ir(n),e=t.length,u=Array(e);++rt?Fr(0,e+t):Rr(t,e-1))+1);e--;)if(n[e]===r)return e;return-1},u.mixin=H,u.noConflict=function(){return n._=mr,this -},u.random=function(n,r){return n==K&&r==K&&(r=1),n=+n||0,r==K&&(r=n,n=0),n+wr(Dr()*((+r||0)-n+1))},u.reduce=F,u.reduceRight=R,u.result=function(n,r){var t=n?n[r]:K;return d(t)?n[r]():t},u.size=function(n){var r=n?n.length:0;return typeof r=="number"?r:Ir(n).length},u.some=D,u.sortedIndex=P,u.template=function(n,r,t){n||(n=""),t=v({},t,u.templateSettings);var e=0,o="__p+='",i=t.variable;n.replace(RegExp((t.escape||er).source+"|"+(t.interpolate||er).source+"|"+(t.evaluate||er).source+"|$","g"),function(r,t,u,i,f){return o+=n.slice(e,f).replace(or,a),t&&(o+="'+_['escape']("+t+")+'"),i&&(o+="';"+i+";__p+='"),u&&(o+="'+((__t=("+u+"))==null?'':__t)+'"),e=f+r.length,r -}),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 r?f(r):(f.source=o,f)},u.unescape=function(n){return n==K?"":(n+"").replace(tr,p)},u.uniqueId=function(n){var r=++Z+"";return n?n+r:r},u.all=O,u.any=D,u.detect=S,u.foldl=F,u.foldr=R,u.include=x,u.inject=F,u.first=$,u.last=function(n,r,t){if(n){var e=0,u=n.length; -if(typeof r!="number"&&r!=K){var o=u;for(r=W(r,t);o--&&r(n[o],o,n);)e++}else if(e=r,e==K||t)return n[u-1];return Mr.call(n,Fr(0,u-e))}},u.take=$,u.head=$,u.chain=function(n){return n=new c(n),n.__chain__=J,n},u.VERSION="1.1.1",H(u),u.prototype.chain=function(){return this.__chain__=J,this},u.prototype.value=function(){return this.__wrapped__},N("pop push reverse shift sort splice unshift".split(" "),function(n){var r=yr[n];u.prototype[n]=function(){var n=this.__wrapped__;return r.apply(n,arguments),!$r.spliceObjects&&0===n.length&&delete n[0],this -}}),N(["concat","join","slice"],function(n){var r=yr[n];u.prototype[n]=function(){var n=r.apply(this.__wrapped__,arguments);return this.__chain__&&(n=new c(n),n.__chain__=J),n}}),typeof define=="function"&&typeof define.amd=="object"&&define.amd?(n._=u,define(function(){return u})):Q&&!Q.nodeType?X?(X.exports=u)._=u:Q._=u:n._=u})(this); \ No newline at end of file +;(function(n){function r(n){return n instanceof r?n:new i(n)}function t(n,r){var t=n.b,e=r.b;if(n=n.a,r=r.a,n!==r){if(n>r||typeof n=="undefined")return 1;if(ne&&(e=t,u=n)});else for(;++ou&&(u=t);return u}function N(n,r){var t=-1,e=n?n.length:0; +if(typeof e=="number")for(var u=Array(e);++targuments.length;r=P(r,e,4);var o=-1,i=n.length;if(typeof i=="number")for(u&&(t=n[++o]);++oarguments.length;if(typeof u!="number")var i=Fr(n),u=i.length;return r=P(r,e,4),O(n,function(e,a,f){a=i?i[--u]:--u,t=o?(o=!1,n[a]):r(t,n[a],a,f)}),t}function q(n,r,t){var e; +r=P(r,t),t=-1;var u=n?n.length:0;if(typeof u=="number")for(;++tT(e,o)&&u.push(o)}return u}function D(n,r,t){if(n){var e=0,u=n.length;if(typeof r!="number"&&null!=r){var o=-1;for(r=P(r,t);++ot?Er(0,u+t):t||0)-1;else if(t)return e=I(n,r),n[e]===r?e:-1;for(;++e>>1,t(n[e])T(a,f))&&(t&&a.push(f),i.push(e))}return i}function C(n,r){return qr.fastBind||jr&&2"']/g,Z=/['\n\r\t\u2028\u2029\\]/g,nr="[object Arguments]",rr="[object Array]",tr="[object Boolean]",er="[object Date]",ur="[object Number]",or="[object Object]",ir="[object RegExp]",ar="[object String]",fr={"boolean":!1,"function":!0,object:!0,number:!1,string:!1,undefined:!1},lr={"\\":"\\","'":"'","\n":"n","\r":"r"," ":"t","\u2028":"u2028","\u2029":"u2029"},cr=[],H={},pr=n._,sr=RegExp("^"+(H.valueOf+"").replace(/[.*+?^${}()|[\]\\]/g,"\\$&").replace(/valueOf|for [^\]]+/g,".+?")+"$"),vr=Math.ceil,gr=n.clearTimeout,hr=cr.concat,yr=Math.floor,mr=H.hasOwnProperty,_r=cr.push,br=n.setTimeout,dr=H.toString,jr=sr.test(jr=dr.bind)&&jr,wr=sr.test(wr=Array.isArray)&&wr,Ar=n.isFinite,xr=n.isNaN,Or=sr.test(Or=Object.keys)&&Or,Er=Math.max,Sr=Math.min,Nr=Math.random,Br=cr.slice,H=sr.test(n.attachEvent),kr=jr&&!/\n|true/.test(jr+H),qr={}; +(function(){var n={0:1,length:1};qr.argsObject=arguments.constructor==Object&&!(arguments instanceof Array),qr.fastBind=jr&&!kr,qr.spliceObjects=(cr.splice.call(n,0,1),!n[0])})(1),r.templateSettings={escape:/<%-([\s\S]+?)%>/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,variable:""},i.prototype=r.prototype,l(arguments)||(l=function(n){return n?mr.call(n,"callee"):!1});var H=function(n){var r,t=[];if(!n||!fr[typeof n])return t;for(r in n)mr.call(n,r)&&t.push(r);return t},Fr=Or?function(n){return m(n)?Or(n):[] +}:H,Rr={"&":"&","<":"<",">":">",'"':""","'":"'"},Dr=v(Rr),Mr=function(n,r){var t;if(!n||!fr[typeof n])return n;for(t in n)if(r(n[t],t,n)===K)break;return n},Tr=function(n,r){var t;if(!n||!fr[typeof n])return n;for(t in n)if(mr.call(n,t)&&r(n[t],t,n)===K)break;return n},$r=wr||function(n){return dr.call(n)==rr};y(/x/)&&(y=function(n){return"[object Function]"==dr.call(n)}),r.after=function(n,r){return 1>n?r():function(){return 1>--n?r.apply(this,arguments):void 0}},r.bind=C,r.bindAll=function(n){for(var r=1T(o,i)){for(var a=t;--a;)if(0>T(r[a],i))continue n;o.push(i)}}return o},r.invert=v,r.invoke=function(n,r){var t=Br.call(arguments,2),e=-1,u=typeof r=="function",o=n?n.length:0,i=Array(typeof o=="number"?o:0);return O(n,function(n){i[++e]=(u?r:n[r]).apply(n,t)}),i},r.keys=Fr,r.map=E,r.max=S,r.memoize=function(n,r){var t={};return function(){var e=L+(r?r.apply(this,arguments):arguments[0]); +return mr.call(t,e)?t[e]:t[e]=n.apply(this,arguments)}},r.min=function(n,r,t){var e=1/0,u=e,o=-1,i=n?n.length:0;if(r||typeof i!="number")r=P(r,t),O(n,function(n,t,o){t=r(n,t,o),tT(r,e)&&(t[e]=n)}),t},r.once=function(n){var r,t;return function(){return r?t:(r=!0,t=n.apply(this,arguments),n=null,t)}},r.pairs=function(n){for(var r=-1,t=Fr(n),e=t.length,u=Array(e);++rt?Er(0,e+t):Sr(t,e-1))+1);e--;)if(n[e]===r)return e; +return-1},r.mixin=V,r.noConflict=function(){return n._=pr,this},r.random=function(n,r){return null==n&&null==r&&(r=1),n=+n||0,null==r&&(r=n,n=0),n+yr(Nr()*((+r||0)-n+1))},r.reduce=B,r.reduceRight=k,r.result=function(n,r){var t=n?n[r]:null;return y(t)?n[r]():t},r.size=function(n){var r=n?n.length:0;return typeof r=="number"?r:Fr(n).length},r.some=q,r.sortedIndex=I,r.template=function(n,t,e){n||(n=""),e=p({},e,r.templateSettings);var o=0,i="__p+='",a=e.variable;n.replace(RegExp((e.escape||X).source+"|"+(e.interpolate||X).source+"|"+(e.evaluate||X).source+"|$","g"),function(r,t,e,a,f){return i+=n.slice(o,f).replace(Z,u),t&&(i+="'+_['escape']("+t+")+'"),a&&(i+="';"+a+";__p+='"),e&&(i+="'+((__t=("+e+"))==null?'':__t)+'"),o=f+r.length,r +}),i+="';\n",a||(a="obj",i="with("+a+"||{}){"+i+"}"),i="function("+a+"){var __t,__p='',__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}"+i+"return __p}";try{var f=Function("_","return "+i)(r)}catch(l){throw l.source=i,l}return t?f(t):(f.source=i,f)},r.unescape=function(n){return null==n?"":(n+"").replace(Q,f)},r.uniqueId=function(n){var r=++J+"";return n?n+r:r},r.all=w,r.any=q,r.detect=x,r.foldl=B,r.foldr=k,r.include=j,r.inject=B,r.first=D,r.last=function(n,r,t){if(n){var e=0,u=n.length; +if(typeof r!="number"&&null!=r){var o=u;for(r=P(r,t);o--&&r(n[o],o,n);)e++}else if(e=r,null==e||t)return n[u-1];return Br.call(n,Er(0,u-e))}},r.take=D,r.head=D,r.chain=function(n){return n=new i(n),n.__chain__=!0,n},r.VERSION="1.1.1",V(r),r.prototype.chain=function(){return this.__chain__=!0,this},r.prototype.value=function(){return this.__wrapped__},O("pop push reverse shift sort splice unshift".split(" "),function(n){var t=cr[n];r.prototype[n]=function(){var n=this.__wrapped__;return t.apply(n,arguments),!qr.spliceObjects&&0===n.length&&delete n[0],this +}}),O(["concat","join","slice"],function(n){var t=cr[n];r.prototype[n]=function(){var n=t.apply(this.__wrapped__,arguments);return this.__chain__&&(n=new i(n),n.__chain__=!0),n}}),typeof define=="function"&&typeof define.amd=="object"&&define.amd?(n._=r,define(function(){return r})):W&&!W.nodeType?G?(G.exports=r)._=r:W._=r:n._=r})(this); \ No newline at end of file