From bc8ec9cd3e5e1dc085ddf79e6cf7463fe9aded68 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sat, 9 Mar 2013 01:29:23 -0800 Subject: [PATCH] Make `_.times` use `_.createCallback` and rebuild docs and dist/builds. Former-commit-id: 62393b4833b64cea226abdbb6f1488369de46677 --- dist/lodash.compat.js | 64 +++++++++++++++++++++------------ dist/lodash.compat.min.js | 76 +++++++++++++++++++-------------------- dist/lodash.js | 64 +++++++++++++++++++++------------ dist/lodash.min.js | 69 ++++++++++++++++++----------------- dist/lodash.underscore.js | 57 ++++++++++++++++++----------- doc/README.md | 69 ++++++++++++++++++++++------------- lodash.js | 64 +++++++++++++++++++++------------ 7 files changed, 276 insertions(+), 187 deletions(-) diff --git a/dist/lodash.compat.js b/dist/lodash.compat.js index df96a2a0f..823c1a50c 100644 --- a/dist/lodash.compat.js +++ b/dist/lodash.compat.js @@ -4240,6 +4240,23 @@ * @param {Mixed} [thisArg] The `this` binding of the created callback. * @param {Number} [argCount=3] The number of arguments the callback accepts. * @returns {Function} Returns a callback function. + * @example + * + * var stooges = [ + * { 'name': 'moe', 'age': 40 }, + * { 'name': 'larry', 'age': 50 } + * ]; + * + * // wrap to create custom callback shorthands + * _.createCallback = _.wrap(_.createCallback, function(func, callback, thisArg) { + * var match = /^(.+?)__([gl]t)(.+)$/.exec(callback); + * return !match ? func(callback, thisArg) : function(object) { + * return match[2] == 'gt' ? object[match[1]] > match[3] : object[match[1]] < match[3]; + * }; + * }); + * + * _.filter(stooges, 'age__gt45'); + * // => [{ 'name': 'larry', 'age': 50 }] */ function createCallback(func, thisArg, argCount) { if (func == null) { @@ -4334,28 +4351,6 @@ }; } - /** - * Executes the `func` function after `wait` milliseconds. Additional arguments - * will be passed to `func` when it is invoked. - * - * @static - * @memberOf _ - * @category Functions - * @param {Function} func The function to delay. - * @param {Number} wait The number of milliseconds to delay execution. - * @param {Mixed} [arg1, arg2, ...] Arguments to invoke the function with. - * @returns {Number} Returns the timer id. - * @example - * - * var log = _.bind(console.log, console); - * _.delay(log, 1000, 'logged later'); - * // => 'logged later' (Appears after one second.) - */ - function delay(func, wait) { - var args = slice(arguments, 2); - return setTimeout(function() { func.apply(undefined, args); }, wait); - } - /** * Defers executing the `func` function until the current call stack has cleared. * Additional arguments will be passed to `func` when it is invoked. @@ -4380,6 +4375,28 @@ defer = bind(setImmediate, context); } + /** + * Executes the `func` function after `wait` milliseconds. Additional arguments + * will be passed to `func` when it is invoked. + * + * @static + * @memberOf _ + * @category Functions + * @param {Function} func The function to delay. + * @param {Number} wait The number of milliseconds to delay execution. + * @param {Mixed} [arg1, arg2, ...] Arguments to invoke the function with. + * @returns {Number} Returns the timer id. + * @example + * + * var log = _.bind(console.log, console); + * _.delay(log, 1000, 'logged later'); + * // => 'logged later' (Appears after one second.) + */ + function delay(func, wait) { + var args = slice(arguments, 2); + return setTimeout(function() { func.apply(undefined, args); }, wait); + } + /** * Creates a function that memoizes the result of `func`. If `resolver` is * passed, it will be used to determine the cache key for storing the result @@ -4964,8 +4981,9 @@ var index = -1, result = Array(n); + callback = createCallback(callback, thisArg, 1); while (++index < n) { - result[index] = callback.call(thisArg, index); + result[index] = callback(index); } return result; } diff --git a/dist/lodash.compat.min.js b/dist/lodash.compat.min.js index 45d8591e4..3dcd6776d 100644 --- a/dist/lodash.compat.min.js +++ b/dist/lodash.compat.min.js @@ -4,42 +4,42 @@ * Build: `lodash -o ./dist/lodash.compat.js` * Underscore.js 1.4.4 underscorejs.org/LICENSE */ -;(function(n){function t(r){function a(n){return n&&typeof n=="object"&&Vt.call(n,"__wrapped__")?n:new L(n)}function B(n,t,e){t||(t=0);var r=n.length,u=r-t>=(e||f);if(u){var a={};for(e=t-1;++et||typeof n=="undefined")return 1;if(nu;u++)r+="i='"+t.f[u]+"';if(","constructor"==t.f[u]&&(r+="!(f&&f.prototype===m)&&"),r+="h.call(m,i)){"+t.e+"}"}return(t.b||ce.nonEnumArgs)&&(r+="}"),r+=t.c+";return u",e("h,j,k,l,o,r,p","return function("+n+"){"+r+"}")(Vt,G,ve,et,a,I,ne)}function D(n){return"\\"+N[n]}function z(n){return ye[n]}function K(n){return typeof n.toString!="function"&&typeof(n+"")=="string"}function L(n){this.__wrapped__=n}function M(){}function U(n,t,e){t||(t=0),typeof e=="undefined"&&(e=n?n.length:0);var r=-1; -e=e-t||0;for(var u=Et(0>e?0:e);++re?te(0,u+e):e)||0,typeof u=="number"?a=-1<(et(n)?n.indexOf(t,e):mt(n,t,e)):le(n,function(n){return++ru&&(u=i)}}else t=!t&&et(n)?F:a.createCallback(t,e),le(n,function(n,e,a){e=t(n,e,a),e>r&&(r=e,u=n)});return u}function st(n,t,e,r){var u=3>arguments.length;if(t=a.createCallback(t,r,4),ve(n)){var o=-1,i=n.length;for(u&&(e=n[++o]);++oarguments.length;if(typeof o!="number")var f=ge(n),o=f.length; -else ce.unindexedChars&&et(n)&&(u=n.split(""));return t=a.createCallback(t,r,4),ct(n,function(n,r,a){r=f?f[--o]:--o,e=i?(i=!1,u[r]):t(e,u[r],r,a)}),e}function gt(n,t,e){var r;if(t=a.createCallback(t,e),ve(n)){e=-1;for(var u=n.length;++ee?te(0,u+e):e||0)-1;else if(e)return r=bt(n,t),n[r]===t?r:-1;for(;++r>>1,e(n[r])mt(f,p))&&((e||c)&&f.push(p),i.push(r))}return i -}function wt(n,t){for(var e=-1,r=n?n.length:0,u={};++e/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:y,variable:"",imports:{_:a}};var St={a:"q,w,g",g:"var a=arguments,b=0,c=typeof g=='number'?2:a.length;while(++b":">",'"':""","'":"'"},he=X(ye),me=T(St,{g:St.g.replace(";",";if(c>3&&typeof a[c-2]=='function'){var d=o.createCallback(a[--c-1],a[c--],2);}else if(c>2&&typeof a[c-1]=='function'){d=a[--c];}"),e:"u[i]=d?d(u[i],m[i]):m[i]"}),de=T(St); -Z(/x/)&&(Z=function(n){return n instanceof Pt||Qt.call(n)==x});var be=Ut?function(n){if(!n||typeof n!="object")return!1;var t=n.valueOf,e=typeof t=="function"&&(e=Ut(t))&&Ut(e);return e?n==e||Ut(n)==e&&!G(n):H(n)}:H;return ie&&u&&typeof Ht=="function"&&(Ct=jt(Ht,r)),Ht=8==re("08")?re:function(n,t){return re(et(n)?n.replace(/^0+(?=.$)/,""):n,t||0)},a.after=function(n,t){return 1>n?t():function(){return 1>--n?t.apply(this,arguments):void 0}},a.assign=me,a.at=function(n){var t=-1,e=Lt.apply(qt,U(arguments,1)),r=e.length,u=Et(r); -for(ce.unindexedChars&&et(n)&&(n=n.split(""));++tmt(f,c)){o&&f.push(c); -for(var p=e;--p;)if(!(r[p]||(r[p]=B(t[p],0,100)))(c))continue n;i.push(c)}}return i},a.invert=X,a.invoke=function(n,t){var e=U(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=ge,a.map=lt,a.max=pt,a.memoize=function(n,t){var e={};return function(){var r=(t?t.apply(this,arguments):arguments[0])+"";return Vt.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&&ve(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=ge(n),r=e.length,u=Et(r);++te?te(0,r+e):ee(e,r-1))+1);r--;)if(n[r]===t)return r;return-1},a.mixin=xt,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(ue()*((+t||0)-n+1)) -},a.reduce=st,a.reduceRight=vt,a.result=function(n,t){var r=n?n[t]:e;return Z(r)?n[t]():r},a.runInContext=t,a.size=function(n){var t=n?n.length:0;return typeof t=="number"?t:ge(n).length},a.some=gt,a.sortedIndex=bt,a.template=function(n,t,r){var u=a.templateSettings;n||(n=""),r=de({},r,u);var o,i=de({},r.imports,u.imports),u=ge(i),i=ut(i),f=0,c=r.interpolate||h,v="__p+='",c=Bt((r.escape||h).source+"|"+c.source+"|"+(c===y?g:h).source+"|"+(r.evaluate||h).source+"|$","g");n.replace(c,function(t,e,r,u,a,i){return r||(r=u),v+=n.slice(f,i).replace(d,D),e&&(v+="'+__e("+e+")+'"),a&&(o=!0,v+="';"+a+";__p+='"),r&&(v+="'+((__t=("+r+"))==null?'':__t)+'"),f=i+t.length,t -}),v+="';\n",c=r=r.variable,c||(r="obj",v="with("+r+"){"+v+"}"),v=(o?v.replace(l,""):v).replace(p,"$1").replace(s,"$1;"),v="function("+r+"){"+(c?"":r+"||("+r+"={});")+"var __t,__p='',__e=_.escape"+(o?",__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}":";")+v+"return __p}";try{var m=Pt(u,"return "+v).apply(e,i)}catch(b){throw b.source=v,b}return t?m(t):(m.source=v,m)},a.unescape=function(n){return null==n?"":(n+"").replace(c,V)},a.uniqueId=function(n){var t=++o;return(null==n?"":n+"")+t -},a.all=ot,a.any=gt,a.detect=ft,a.foldl=st,a.foldr=vt,a.include=at,a.inject=st,se(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=yt,a.last=function(n,t,e){if(n){var r=0,u=n.length;if(typeof t!="number"&&null!=t){var o=u;for(t=a.createCallback(t,e);o--&&t(n[o],o,n);)r++}else if(r=t,null==r||e)return n[u-1];return U(n,te(0,u-r))}},a.take=yt,a.head=yt,se(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 L(r)})}),a.VERSION="1.0.1",a.prototype.toString=function(){return this.__wrapped__+""},a.prototype.value=Ot,a.prototype.valueOf=Ot,le(["join","pop","shift"],function(n){var t=qt[n];a.prototype[n]=function(){return t.apply(this.__wrapped__,arguments)}}),le(["push","reverse","sort","unshift"],function(n){var t=qt[n];a.prototype[n]=function(){return t.apply(this.__wrapped__,arguments),this}}),le(["concat","slice","splice"],function(n){var t=qt[n];a.prototype[n]=function(){return new L(t.apply(this.__wrapped__,arguments)) -}}),ce.spliceObjects||le(["pop","shift","splice"],function(n){var t=qt[n],e="splice"==n;a.prototype[n]=function(){var n=this.__wrapped__,r=t.apply(n,arguments);return 0===n.length&&delete n[0],e?new L(r):r}}),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&&(n=a);var o=0,i={},f=30,c=/&(?:amp|lt|gt|quot|#39);/g,l=/\b__p\+='';/g,p=/\b(__p\+=)''\+/g,s=/(__e\(.*?\)|\b__t\))\+'';/g,v=/\w*$/,g=/\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g,y=/<%=([\s\S]+?)%>/g,h=/($^)/,m=/[&<>"']/g,d=/['\n\r\t\u2028\u2029\\]/g,b="Array Boolean Date Function Math Number Object RegExp String _ attachEvent clearTimeout isFinite isNaN parseInt setImmediate setTimeout".split(" "),_="constructor hasOwnProperty isPrototypeOf propertyIsEnumerable toLocaleString toString valueOf".split(" "),w="[object Arguments]",j="[object Array]",C="[object Boolean]",k="[object Date]",x="[object Function]",O="[object Number]",E="[object Object]",S="[object RegExp]",A="[object String]",P={}; +;(function(n){function t(r){function a(n){return n&&typeof n=="object"&&Gt.call(n,"__wrapped__")?n:new L(n)}function B(n,t,e){t||(t=0);var r=n.length,u=r-t>=(e||f);if(u){var a={};for(e=t-1;++et||typeof n=="undefined")return 1;if(nu;u++)r+="i='"+t.f[u]+"';if(","constructor"==t.f[u]&&(r+="!(f&&f.prototype===m)&&"),r+="h.call(m,i)){"+t.e+"}"}return(t.b||le.nonEnumArgs)&&(r+="}"),r+=t.c+";return u",e("h,j,k,l,o,r,p","return function("+n+"){"+r+"}")(Gt,G,me,et,a,I,te)}function D(n){return"\\"+N[n]}function z(n){return be[n]}function K(n){return typeof n.toString!="function"&&typeof(n+"")=="string"}function L(n){this.__wrapped__=n}function M(){}function U(n,t,e){t||(t=0),typeof e=="undefined"&&(e=n?n.length:0);var r=-1; +e=e-t||0;for(var u=St(0>e?0:e);++re?ee(0,u+e):e)||0,typeof u=="number"?a=-1<(et(n)?n.indexOf(t,e):mt(n,t,e)):ge(n,function(n){return++ru&&(u=i)}}else t=!t&&et(n)?F:a.createCallback(t,e),ge(n,function(n,e,a){e=t(n,e,a),e>r&&(r=e,u=n)});return u}function st(n,t,e,r){var u=3>arguments.length;if(t=a.createCallback(t,r,4),me(n)){var o=-1,i=n.length;for(u&&(e=n[++o]);++oarguments.length;if(typeof o!="number")var f=de(n),o=f.length; +else le.unindexedChars&&et(n)&&(u=n.split(""));return t=a.createCallback(t,r,4),ct(n,function(n,r,a){r=f?f[--o]:--o,e=i?(i=!1,u[r]):t(e,u[r],r,a)}),e}function gt(n,t,e){var r;if(t=a.createCallback(t,e),me(n)){e=-1;for(var u=n.length;++ee?ee(0,u+e):e||0)-1;else if(e)return r=bt(n,t),n[r]===t?r:-1;for(;++r>>1,e(n[r])mt(f,p))&&((e||c)&&f.push(p),i.push(r))}return i +}function wt(n,t){for(var e=-1,r=n?n.length:0,u={};++e/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:y,variable:"",imports:{_:a}};var pe={a:"q,w,g",g:"var a=arguments,b=0,c=typeof g=='number'?2:a.length;while(++b":">",'"':""","'":"'"},_e=X(be),we=T(pe,{g:pe.g.replace(";",";if(c>3&&typeof a[c-2]=='function'){var d=o.createCallback(a[--c-1],a[c--],2);}else if(c>2&&typeof a[c-1]=='function'){d=a[--c];}"),e:"u[i]=d?d(u[i],m[i]):m[i]"}),je=T(pe); +Z(/x/)&&(Z=function(n){return n instanceof It||Wt.call(n)==x});var Ce=Vt?function(n){if(!n||typeof n!="object")return!1;var t=n.valueOf,e=typeof t=="function"&&(e=Vt(t))&&Vt(e);return e?n==e||Vt(n)==e&&!G(n):H(n)}:H;fe&&u&&typeof Jt=="function"&&(kt=jt(Jt,r));var ke=8==ue("08")?ue:function(n,t){return ue(et(n)?n.replace(/^0+(?=.$)/,""):n,t||0)};return a.after=function(n,t){return 1>n?t():function(){return 1>--n?t.apply(this,arguments):void 0}},a.assign=we,a.at=function(n){var t=-1,e=Mt.apply(Rt,U(arguments,1)),r=e.length,u=St(r); +for(le.unindexedChars&&et(n)&&(n=n.split(""));++tmt(f,c)){o&&f.push(c);for(var p=e;--p;)if(!(r[p]||(r[p]=B(t[p],0,100)))(c))continue n;i.push(c)}}return i},a.invert=X,a.invoke=function(n,t){var e=U(arguments,2),r=-1,u=typeof t=="function",a=n?n.length:0,o=St(typeof a=="number"?a:0);return ct(n,function(n){o[++r]=(u?t:n[t]).apply(n,e)}),o},a.keys=de,a.map=lt,a.max=pt,a.memoize=function(n,t){var e={};return function(){var r=(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&&me(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=de(n),r=e.length,u=St(r);++te?ee(0,r+e):re(e,r-1))+1);r--;)if(n[r]===t)return r;return-1},a.mixin=Ot,a.noConflict=function(){return r._=Dt,this +},a.parseInt=ke,a.random=function(n,t){return null==n&&null==t&&(t=1),n=+n||0,null==t&&(t=n,n=0),n+Ut(ae()*((+t||0)-n+1))},a.reduce=st,a.reduceRight=vt,a.result=function(n,t){var r=n?n[t]:e;return Z(r)?n[t]():r},a.runInContext=t,a.size=function(n){var t=n?n.length:0;return typeof t=="number"?t:de(n).length},a.some=gt,a.sortedIndex=bt,a.template=function(n,t,r){var u=a.templateSettings;n||(n=""),r=je({},r,u);var o,i=je({},r.imports,u.imports),u=de(i),i=ut(i),f=0,c=r.interpolate||h,v="__p+='",c=Ft((r.escape||h).source+"|"+c.source+"|"+(c===y?g:h).source+"|"+(r.evaluate||h).source+"|$","g"); +n.replace(c,function(t,e,r,u,a,i){return r||(r=u),v+=n.slice(f,i).replace(d,D),e&&(v+="'+__e("+e+")+'"),a&&(o=!0,v+="';"+a+";__p+='"),r&&(v+="'+((__t=("+r+"))==null?'':__t)+'"),f=i+t.length,t}),v+="';\n",c=r=r.variable,c||(r="obj",v="with("+r+"){"+v+"}"),v=(o?v.replace(l,""):v).replace(p,"$1").replace(s,"$1;"),v="function("+r+"){"+(c?"":r+"||("+r+"={});")+"var __t,__p='',__e=_.escape"+(o?",__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}":";")+v+"return __p}";try{var m=It(u,"return "+v).apply(e,i) +}catch(b){throw b.source=v,b}return t?m(t):(m.source=v,m)},a.unescape=function(n){return null==n?"":(n+"").replace(c,V)},a.uniqueId=function(n){var t=++o;return(null==n?"":n+"")+t},a.all=ot,a.any=gt,a.detect=ft,a.foldl=st,a.foldr=vt,a.include=at,a.inject=st,he(a,function(n,t){a.prototype[t]||(a.prototype[t]=function(){var t=[this.__wrapped__];return Ht.apply(t,arguments),n.apply(a,t)})}),a.first=yt,a.last=function(n,t,e){if(n){var r=0,u=n.length;if(typeof t!="number"&&null!=t){var o=u;for(t=a.createCallback(t,e);o--&&t(n[o],o,n);)r++ +}else if(r=t,null==r||e)return n[u-1];return U(n,ee(0,u-r))}},a.take=yt,a.head=yt,he(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 L(r)})}),a.VERSION="1.0.1",a.prototype.toString=function(){return this.__wrapped__+""},a.prototype.value=Et,a.prototype.valueOf=Et,ge(["join","pop","shift"],function(n){var t=Rt[n];a.prototype[n]=function(){return t.apply(this.__wrapped__,arguments)}}),ge(["push","reverse","sort","unshift"],function(n){var t=Rt[n]; +a.prototype[n]=function(){return t.apply(this.__wrapped__,arguments),this}}),ge(["concat","slice","splice"],function(n){var t=Rt[n];a.prototype[n]=function(){return new L(t.apply(this.__wrapped__,arguments))}}),le.spliceObjects||ge(["pop","shift","splice"],function(n){var t=Rt[n],e="splice"==n;a.prototype[n]=function(){var n=this.__wrapped__,r=t.apply(n,arguments);return 0===n.length&&delete n[0],e?new L(r):r}}),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&&(n=a);var o=0,i={},f=30,c=/&(?:amp|lt|gt|quot|#39);/g,l=/\b__p\+='';/g,p=/\b(__p\+=)''\+/g,s=/(__e\(.*?\)|\b__t\))\+'';/g,v=/\w*$/,g=/\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g,y=/<%=([\s\S]+?)%>/g,h=/($^)/,m=/[&<>"']/g,d=/['\n\r\t\u2028\u2029\\]/g,b="Array Boolean Date Function Math Number Object RegExp String _ attachEvent clearTimeout isFinite isNaN parseInt setImmediate setTimeout".split(" "),_="constructor hasOwnProperty isPrototypeOf propertyIsEnumerable toLocaleString toString valueOf".split(" "),w="[object Arguments]",j="[object Array]",C="[object Boolean]",k="[object Date]",x="[object Function]",O="[object Number]",E="[object Object]",S="[object RegExp]",A="[object String]",P={}; P[x]=!1,P[w]=P[j]=P[C]=P[k]=P[O]=P[E]=P[S]=P[A]=!0;var I={"boolean":!1,"function":!0,object:!0,number:!1,string:!1,undefined:!1},N={"\\":"\\","'":"'","\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.js b/dist/lodash.js index 0c97b5ee8..a1c9628ee 100644 --- a/dist/lodash.js +++ b/dist/lodash.js @@ -4052,6 +4052,23 @@ * @param {Mixed} [thisArg] The `this` binding of the created callback. * @param {Number} [argCount=3] The number of arguments the callback accepts. * @returns {Function} Returns a callback function. + * @example + * + * var stooges = [ + * { 'name': 'moe', 'age': 40 }, + * { 'name': 'larry', 'age': 50 } + * ]; + * + * // wrap to create custom callback shorthands + * _.createCallback = _.wrap(_.createCallback, function(func, callback, thisArg) { + * var match = /^(.+?)__([gl]t)(.+)$/.exec(callback); + * return !match ? func(callback, thisArg) : function(object) { + * return match[2] == 'gt' ? object[match[1]] > match[3] : object[match[1]] < match[3]; + * }; + * }); + * + * _.filter(stooges, 'age__gt45'); + * // => [{ 'name': 'larry', 'age': 50 }] */ function createCallback(func, thisArg, argCount) { if (func == null) { @@ -4146,28 +4163,6 @@ }; } - /** - * Executes the `func` function after `wait` milliseconds. Additional arguments - * will be passed to `func` when it is invoked. - * - * @static - * @memberOf _ - * @category Functions - * @param {Function} func The function to delay. - * @param {Number} wait The number of milliseconds to delay execution. - * @param {Mixed} [arg1, arg2, ...] Arguments to invoke the function with. - * @returns {Number} Returns the timer id. - * @example - * - * var log = _.bind(console.log, console); - * _.delay(log, 1000, 'logged later'); - * // => 'logged later' (Appears after one second.) - */ - function delay(func, wait) { - var args = slice(arguments, 2); - return setTimeout(function() { func.apply(undefined, args); }, wait); - } - /** * Defers executing the `func` function until the current call stack has cleared. * Additional arguments will be passed to `func` when it is invoked. @@ -4192,6 +4187,28 @@ defer = bind(setImmediate, context); } + /** + * Executes the `func` function after `wait` milliseconds. Additional arguments + * will be passed to `func` when it is invoked. + * + * @static + * @memberOf _ + * @category Functions + * @param {Function} func The function to delay. + * @param {Number} wait The number of milliseconds to delay execution. + * @param {Mixed} [arg1, arg2, ...] Arguments to invoke the function with. + * @returns {Number} Returns the timer id. + * @example + * + * var log = _.bind(console.log, console); + * _.delay(log, 1000, 'logged later'); + * // => 'logged later' (Appears after one second.) + */ + function delay(func, wait) { + var args = slice(arguments, 2); + return setTimeout(function() { func.apply(undefined, args); }, wait); + } + /** * Creates a function that memoizes the result of `func`. If `resolver` is * passed, it will be used to determine the cache key for storing the result @@ -4776,8 +4793,9 @@ var index = -1, result = Array(n); + callback = createCallback(callback, thisArg, 1); while (++index < n) { - result[index] = callback.call(thisArg, index); + result[index] = callback(index); } return result; } diff --git a/dist/lodash.min.js b/dist/lodash.min.js index a55545519..b752f9fb3 100644 --- a/dist/lodash.min.js +++ b/dist/lodash.min.js @@ -4,39 +4,38 @@ * Build: `lodash modern -o ./dist/lodash.js` * Underscore.js 1.4.4 underscorejs.org/LICENSE */ -;(function(n){function t(r){function a(n){return n&&typeof n=="object"&&Mt.call(n,"__wrapped__")?n:new z(n)}function $(n,t,e){t||(t=0);var r=n.length,u=r-t>=(e||f);if(u){var a={};for(e=t-1;++et||typeof n=="undefined")return 1;if(ne?0:e);++re?Yt(0,u+e):e)||0,typeof u=="number"?a=-1<(Z(n)?n.indexOf(t,e):gt(n,t,e)):ue(n,function(n){return++ru&&(u=i)}}else t=!t&&Z(n)?B:a.createCallback(t,e),ue(n,function(n,e,a){e=t(n,e,a),e>r&&(r=e,u=n)});return u}function ct(n,t,e,r){var u=3>arguments.length;if(t=a.createCallback(t,r,4),ie(n)){var o=-1,i=n.length;for(u&&(e=n[++o]);++oarguments.length;if(typeof u!="number")var i=fe(n),u=i.length; -return t=a.createCallback(t,r,4),ot(n,function(r,a,f){a=i?i[--u]:--u,e=o?(o=!1,n[a]):t(e,n[a],a,f)}),e}function pt(n,t,e){var r;if(t=a.createCallback(t,e),ie(n)){e=-1;for(var u=n.length;++ee?Yt(0,u+e):e||0)-1;else if(e)return r=yt(n,t),n[r]===t?r:-1;for(;++r>>1,e(n[r])gt(f,p))&&((e||c)&&f.push(p),i.push(r))}return i}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}};var xt={a:"q,w,g",g:"var a=arguments,b=0,c=typeof g=='number'?2:a.length;while(++b":">",'"':""","'":"'"},le=L(ce),pe=R(xt,{g:xt.g.replace(";",";if(c>3&&typeof a[c-2]=='function'){var d=o.createCallback(a[--c-1],a[c--],2);}else if(c>2&&typeof a[c-1]=='function'){d=a[--c];}"),e:"u[i]=d?d(u[i],m[i]):m[i]"}),se=R(xt),ve=function(n){if(!n||typeof n!="object")return!1;var t=n.valueOf,e=typeof t=="function"&&(e=Pt(t))&&Pt(e); -return e?n==e||Pt(n)==e&&!U(n):V(n)};return St&&u&&typeof Vt=="function"&&(_t=bt(Vt,r)),Vt=8==ne("08")?ne:function(n,t){return ne(Z(n)?n.replace(/^0+(?=.$)/,""):n,t||0)},a.after=function(n,t){return 1>n?t():function(){return 1>--n?t.apply(this,arguments):void 0}},a.assign=pe,a.at=function(n){for(var t=-1,e=zt.apply(Bt,P(arguments,1)),r=e.length,u=Ct(r);++tgt(f,c)){o&&f.push(c);for(var p=e;--p;)if(!(r[p]||(r[p]=$(t[p],0,100)))(c))continue n;i.push(c)}}return i},a.invert=L,a.invoke=function(n,t){var e=P(arguments,2),r=-1,u=typeof t=="function",a=n?n.length:0,o=Ct(typeof a=="number"?a:0); -return ot(n,function(n){o[++r]=(u?t:n[t]).apply(n,e)}),o},a.keys=fe,a.map=it,a.max=ft,a.memoize=function(n,t){var e={};return function(){var r=(t?t.apply(this,arguments):arguments[0])+"";return Mt.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 o=n.length;++egt(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=fe(n),r=e.length,u=Ct(r);++te?Yt(0,r+e):Zt(e,r-1))+1);r--;)if(n[r]===t)return r;return-1},a.mixin=jt,a.noConflict=function(){return r._=qt,this -},a.parseInt=Vt,a.random=function(n,t){return null==n&&null==t&&(t=1),n=+n||0,null==t&&(t=n,n=0),n+Kt(te()*((+t||0)-n+1))},a.reduce=ct,a.reduceRight=lt,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:fe(n).length},a.some=pt,a.sortedIndex=yt,a.template=function(n,t,r){var u=a.templateSettings;n||(n=""),r=se({},r,u);var o,i=se({},r.imports,u.imports),u=fe(i),i=tt(i),f=0,c=r.interpolate||y,v="__p+='",c=It((r.escape||y).source+"|"+c.source+"|"+(c===h?g:y).source+"|"+(r.evaluate||y).source+"|$","g"); -n.replace(c,function(t,e,r,u,a,i){return r||(r=u),v+=n.slice(f,i).replace(d,T),e&&(v+="'+__e("+e+")+'"),a&&(o=!0,v+="';"+a+";__p+='"),r&&(v+="'+((__t=("+r+"))==null?'':__t)+'"),f=i+t.length,t}),v+="';\n",c=r=r.variable,c||(r="obj",v="with("+r+"){"+v+"}"),v=(o?v.replace(l,""):v).replace(p,"$1").replace(s,"$1;"),v="function("+r+"){"+(c?"":r+"||("+r+"={});")+"var __t,__p='',__e=_.escape"+(o?",__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}":";")+v+"return __p}";try{var m=Nt(u,"return "+v).apply(e,i) -}catch(b){throw b.source=v,b}return t?m(t):(m.source=v,m)},a.unescape=function(n){return null==n?"":(n+"").replace(c,M)},a.uniqueId=function(n){var t=++o;return(null==n?"":n+"")+t},a.all=rt,a.any=pt,a.detect=at,a.foldl=ct,a.foldr=lt,a.include=et,a.inject=ct,oe(a,function(n,t){a.prototype[t]||(a.prototype[t]=function(){var t=[this.__wrapped__];return Ut.apply(t,arguments),n.apply(a,t)})}),a.first=st,a.last=function(n,t,e){if(n){var r=0,u=n.length;if(typeof t!="number"&&null!=t){var o=u;for(t=a.createCallback(t,e);o--&&t(n[o],o,n);)r++ -}else if(r=t,null==r||e)return n[u-1];return P(n,Yt(0,u-r))}},a.take=st,a.head=st,oe(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 z(r)})}),a.VERSION="1.0.1",a.prototype.toString=function(){return this.__wrapped__+""},a.prototype.value=wt,a.prototype.valueOf=wt,ue(["join","pop","shift"],function(n){var t=Bt[n];a.prototype[n]=function(){return t.apply(this.__wrapped__,arguments)}}),ue(["push","reverse","sort","unshift"],function(n){var t=Bt[n]; -a.prototype[n]=function(){return t.apply(this.__wrapped__,arguments),this}}),ue(["concat","slice","splice"],function(n){var t=Bt[n];a.prototype[n]=function(){return new z(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&&(n=a);var o=0,i={},f=30,c=/&(?:amp|lt|gt|quot|#39);/g,l=/\b__p\+='';/g,p=/\b(__p\+=)''\+/g,s=/(__e\(.*?\)|\b__t\))\+'';/g,v=/\w*$/,g=/\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g,h=/<%=([\s\S]+?)%>/g,y=/($^)/,m=/[&<>"']/g,d=/['\n\r\t\u2028\u2029\\]/g,b="Array Boolean Date Function Math Number Object RegExp String _ attachEvent clearTimeout isFinite isNaN parseInt setImmediate setTimeout".split(" "),_="[object Arguments]",k="[object Array]",j="[object Boolean]",w="[object Date]",C="[object Number]",x="[object Object]",O="[object RegExp]",N="[object String]",S={"[object Function]":!1}; +;(function(n){function t(r){function a(n){return n&&typeof n=="object"&&Ut.call(n,"__wrapped__")?n:new z(n)}function $(n,t,e){t||(t=0);var r=n.length,u=r-t>=(e||f);if(u){var a={};for(e=t-1;++et||typeof n=="undefined")return 1;if(ne?0:e);++re?Zt(0,u+e):e)||0,typeof u=="number"?a=-1<(Z(n)?n.indexOf(t,e):gt(n,t,e)):ae(n,function(n){return++ru&&(u=i)}}else t=!t&&Z(n)?B:a.createCallback(t,e),ae(n,function(n,e,a){e=t(n,e,a),e>r&&(r=e,u=n)});return u}function ct(n,t,e,r){var u=3>arguments.length;if(t=a.createCallback(t,r,4),fe(n)){var o=-1,i=n.length;for(u&&(e=n[++o]);++oarguments.length;if(typeof u!="number")var i=ce(n),u=i.length; +return t=a.createCallback(t,r,4),ot(n,function(r,a,f){a=i?i[--u]:--u,e=o?(o=!1,n[a]):t(e,n[a],a,f)}),e}function pt(n,t,e){var r;if(t=a.createCallback(t,e),fe(n)){e=-1;for(var u=n.length;++ee?Zt(0,u+e):e||0)-1;else if(e)return r=yt(n,t),n[r]===t?r:-1;for(;++r>>1,e(n[r])gt(f,p))&&((e||c)&&f.push(p),i.push(r))}return i}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}};var Ot={a:"q,w,g",g:"var a=arguments,b=0,c=typeof g=='number'?2:a.length;while(++b":">",'"':""","'":"'"},pe=L(le),se=R(Ot,{g:Ot.g.replace(";",";if(c>3&&typeof a[c-2]=='function'){var d=o.createCallback(a[--c-1],a[c--],2);}else if(c>2&&typeof a[c-1]=='function'){d=a[--c];}"),e:"u[i]=d?d(u[i],m[i]):m[i]"}),ve=R(Ot),ge=function(n){if(!n||typeof n!="object")return!1;var t=n.valueOf,e=typeof t=="function"&&(e=Mt(t))&&Mt(e); +return e?n==e||Mt(n)==e&&!U(n):V(n)};return At&&u&&typeof Gt=="function"&&(kt=bt(Gt,r)),Gt=8==te("08")?te:function(n,t){return te(Z(n)?n.replace(/^0+(?=.$)/,""):n,t||0)},a.after=function(n,t){return 1>n?t():function(){return 1>--n?t.apply(this,arguments):void 0}},a.assign=se,a.at=function(n){for(var t=-1,e=Kt.apply(Ft,P(arguments,1)),r=e.length,u=xt(r);++tgt(f,c)){o&&f.push(c);for(var p=e;--p;)if(!(r[p]||(r[p]=$(t[p],0,100)))(c))continue n;i.push(c) +}}return i},a.invert=L,a.invoke=function(n,t){var e=P(arguments,2),r=-1,u=typeof t=="function",a=n?n.length:0,o=xt(typeof a=="number"?a:0);return ot(n,function(n){o[++r]=(u?t:n[t]).apply(n,e)}),o},a.keys=ce,a.map=it,a.max=ft,a.memoize=function(n,t){var e={};return function(){var r=(t?t.apply(this,arguments):arguments[0])+"";return Ut.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&&fe(n)){e=-1;for(var o=n.length;++egt(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=ce(n),r=e.length,u=xt(r);++te?Zt(0,r+e):ne(e,r-1))+1);r--;)if(n[r]===t)return r;return-1},a.mixin=wt,a.noConflict=function(){return r._=Rt,this},a.parseInt=Gt,a.random=function(n,t){return null==n&&null==t&&(t=1),n=+n||0,null==t&&(t=n,n=0),n+Pt(ee()*((+t||0)-n+1))},a.reduce=ct,a.reduceRight=lt,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:ce(n).length +},a.some=pt,a.sortedIndex=yt,a.template=function(n,t,r){var u=a.templateSettings;n||(n=""),r=ve({},r,u);var o,i=ve({},r.imports,u.imports),u=ce(i),i=tt(i),f=0,c=r.interpolate||y,v="__p+='",c=$t((r.escape||y).source+"|"+c.source+"|"+(c===h?g:y).source+"|"+(r.evaluate||y).source+"|$","g");n.replace(c,function(t,e,r,u,a,i){return r||(r=u),v+=n.slice(f,i).replace(d,T),e&&(v+="'+__e("+e+")+'"),a&&(o=!0,v+="';"+a+";__p+='"),r&&(v+="'+((__t=("+r+"))==null?'':__t)+'"),f=i+t.length,t}),v+="';\n",c=r=r.variable,c||(r="obj",v="with("+r+"){"+v+"}"),v=(o?v.replace(l,""):v).replace(p,"$1").replace(s,"$1;"),v="function("+r+"){"+(c?"":r+"||("+r+"={});")+"var __t,__p='',__e=_.escape"+(o?",__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}":";")+v+"return __p}"; +try{var m=St(u,"return "+v).apply(e,i)}catch(b){throw b.source=v,b}return t?m(t):(m.source=v,m)},a.unescape=function(n){return null==n?"":(n+"").replace(c,M)},a.uniqueId=function(n){var t=++o;return(null==n?"":n+"")+t},a.all=rt,a.any=pt,a.detect=at,a.foldl=ct,a.foldr=lt,a.include=et,a.inject=ct,ie(a,function(n,t){a.prototype[t]||(a.prototype[t]=function(){var t=[this.__wrapped__];return Vt.apply(t,arguments),n.apply(a,t)})}),a.first=st,a.last=function(n,t,e){if(n){var r=0,u=n.length;if(typeof t!="number"&&null!=t){var o=u; +for(t=a.createCallback(t,e);o--&&t(n[o],o,n);)r++}else if(r=t,null==r||e)return n[u-1];return P(n,Zt(0,u-r))}},a.take=st,a.head=st,ie(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 z(r)})}),a.VERSION="1.0.1",a.prototype.toString=function(){return this.__wrapped__+""},a.prototype.value=Ct,a.prototype.valueOf=Ct,ae(["join","pop","shift"],function(n){var t=Ft[n];a.prototype[n]=function(){return t.apply(this.__wrapped__,arguments) +}}),ae(["push","reverse","sort","unshift"],function(n){var t=Ft[n];a.prototype[n]=function(){return t.apply(this.__wrapped__,arguments),this}}),ae(["concat","slice","splice"],function(n){var t=Ft[n];a.prototype[n]=function(){return new z(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&&(n=a);var o=0,i={},f=30,c=/&(?:amp|lt|gt|quot|#39);/g,l=/\b__p\+='';/g,p=/\b(__p\+=)''\+/g,s=/(__e\(.*?\)|\b__t\))\+'';/g,v=/\w*$/,g=/\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g,h=/<%=([\s\S]+?)%>/g,y=/($^)/,m=/[&<>"']/g,d=/['\n\r\t\u2028\u2029\\]/g,b="Array Boolean Date Function Math Number Object RegExp String _ attachEvent clearTimeout isFinite isNaN parseInt setImmediate setTimeout".split(" "),_="[object Arguments]",k="[object Array]",j="[object Boolean]",w="[object Date]",C="[object Number]",x="[object Object]",O="[object RegExp]",N="[object String]",S={"[object Function]":!1}; S[_]=S[k]=S[j]=S[w]=S[C]=S[x]=S[O]=S[N]=!0;var A={"boolean":!1,"function":!0,object:!0,number:!1,string:!1,undefined:!1},E={"\\":"\\","'":"'","\n":"n","\r":"r"," ":"t","\u2028":"u2028","\u2029":"u2029"},I=t();typeof define=="function"&&typeof define.amd=="object"&&define.amd?(n._=I,define(function(){return I})):r&&!r.nodeType?u?(u.exports=I)._=I:r._=I:n._=I})(this); \ No newline at end of file diff --git a/dist/lodash.underscore.js b/dist/lodash.underscore.js index cc670fc43..b2bcc7175 100644 --- a/dist/lodash.underscore.js +++ b/dist/lodash.underscore.js @@ -3404,6 +3404,23 @@ * @param {Mixed} [thisArg] The `this` binding of the created callback. * @param {Number} [argCount=3] The number of arguments the callback accepts. * @returns {Function} Returns a callback function. + * @example + * + * var stooges = [ + * { 'name': 'moe', 'age': 40 }, + * { 'name': 'larry', 'age': 50 } + * ]; + * + * // wrap to create custom callback shorthands + * _.createCallback = _.wrap(_.createCallback, function(func, callback, thisArg) { + * var match = /^(.+?)__([gl]t)(.+)$/.exec(callback); + * return !match ? func(callback, thisArg) : function(object) { + * return match[2] == 'gt' ? object[match[1]] > match[3] : object[match[1]] < match[3]; + * }; + * }); + * + * _.filter(stooges, 'age__gt45'); + * // => [{ 'name': 'larry', 'age': 50 }] */ function createCallback(func, thisArg, argCount) { if (func == null) { @@ -3498,6 +3515,26 @@ }; } + /** + * Defers executing the `func` function until the current call stack has cleared. + * Additional arguments will be passed to `func` when it is invoked. + * + * @static + * @memberOf _ + * @category Functions + * @param {Function} func The function to defer. + * @param {Mixed} [arg1, arg2, ...] Arguments to invoke the function with. + * @returns {Number} Returns the timer id. + * @example + * + * _.defer(function() { alert('deferred'); }); + * // returns from the function before `alert` is called + */ + function defer(func) { + var args = slice(arguments, 1); + return setTimeout(function() { func.apply(undefined, args); }, 1); + } + /** * Executes the `func` function after `wait` milliseconds. Additional arguments * will be passed to `func` when it is invoked. @@ -3520,26 +3557,6 @@ return setTimeout(function() { func.apply(undefined, args); }, wait); } - /** - * Defers executing the `func` function until the current call stack has cleared. - * Additional arguments will be passed to `func` when it is invoked. - * - * @static - * @memberOf _ - * @category Functions - * @param {Function} func The function to defer. - * @param {Mixed} [arg1, arg2, ...] Arguments to invoke the function with. - * @returns {Number} Returns the timer id. - * @example - * - * _.defer(function() { alert('deferred'); }); - * // returns from the function before `alert` is called - */ - function defer(func) { - var args = slice(arguments, 1); - return setTimeout(function() { func.apply(undefined, args); }, 1); - } - /** * Creates a function that memoizes the result of `func`. If `resolver` is * passed, it will be used to determine the cache key for storing the result diff --git a/doc/README.md b/doc/README.md index 692445437..3f0bc7f5d 100644 --- a/doc/README.md +++ b/doc/README.md @@ -896,7 +896,7 @@ The wrapper functions `first` and `last` return wrapped values when `n` is passe ### `_.tap(value, interceptor)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5047 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5065 "View in source") [Ⓣ][1] Invokes `interceptor` with the `value` as the first argument, and then returns `value`. The purpose of this method is to "tap into" a method chain, in order to perform operations on intermediate results within the chain. @@ -926,7 +926,7 @@ _([1, 2, 3, 4]) ### `_.prototype.toString()` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5064 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5082 "View in source") [Ⓣ][1] Produces the `toString` result of the wrapped value. @@ -947,7 +947,7 @@ _([1, 2, 3]).toString(); ### `_.prototype.valueOf()` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5081 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5099 "View in source") [Ⓣ][1] Extracts the wrapped value. @@ -1945,7 +1945,7 @@ welcome('moe'); ### `_.createCallback([func=identity, thisArg, argCount=3])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4255 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4272 "View in source") [Ⓣ][1] Produces a callback bound to an optional `thisArg`. If `func` is a property name, the created callback will return the property value for a given element. If `func` is an object, the created callback will return `true` for elements that contain the equivalent object properties, otherwise it will return `false`. @@ -1957,6 +1957,25 @@ Produces a callback bound to an optional `thisArg`. If `func` is a property name #### Returns *(Function)*: Returns a callback function. +#### Example +```js +var stooges = [ + { 'name': 'moe', 'age': 40 }, + { 'name': 'larry', 'age': 50 } +]; + +// wrap to create custom callback shorthands +_.createCallback = _.wrap(_.createCallback, function(func, callback, thisArg) { + var match = /^(.+?)__([gl]t)(.+)$/.exec(callback); + return !match ? func(callback, thisArg) : function(object) { + return match[2] == 'gt' ? object[match[1]] > match[3] : object[match[1]] < match[3]; + }; +}); + +_.filter(stooges, 'age__gt45'); +// => [{ 'name': 'larry', 'age': 50 }] +``` + * * * @@ -1965,7 +1984,7 @@ Produces a callback bound to an optional `thisArg`. If `func` is a property name ### `_.debounce(func, wait, immediate)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4321 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4338 "View in source") [Ⓣ][1] Creates a function that will delay the execution of `func` until after `wait` milliseconds have elapsed since the last time it was invoked. Pass `true` for `immediate` to cause debounce to invoke `func` on the leading, instead of the trailing, edge of the `wait` timeout. Subsequent calls to the debounced function will return the result of the last `func` call. @@ -1991,7 +2010,7 @@ jQuery(window).on('resize', lazyLayout); ### `_.defer(func [, arg1, arg2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4385 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4380 "View in source") [Ⓣ][1] Defers executing the `func` function until the current call stack has cleared. Additional arguments will be passed to `func` when it is invoked. @@ -2016,7 +2035,7 @@ _.defer(function() { alert('deferred'); }); ### `_.delay(func, wait [, arg1, arg2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4365 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4406 "View in source") [Ⓣ][1] Executes the `func` function after `wait` milliseconds. Additional arguments will be passed to `func` when it is invoked. @@ -2043,7 +2062,7 @@ _.delay(log, 1000, 'logged later'); ### `_.memoize(func [, resolver])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4413 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4430 "View in source") [Ⓣ][1] Creates a function that memoizes the result of `func`. If `resolver` is passed, it will be used to determine the cache key for storing the result based on the arguments passed to the memoized function. By default, the first argument passed to the memoized function is used as the cache key. The `func` is executed with the `this` binding of the memoized function. @@ -2069,7 +2088,7 @@ var fibonacci = _.memoize(function(n) { ### `_.once(func)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4440 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4457 "View in source") [Ⓣ][1] Creates a function that is restricted to execute `func` once. Repeat calls to the function will return the value of the first call. The `func` is executed with the `this` binding of the created function. @@ -2095,7 +2114,7 @@ initialize(); ### `_.partial(func [, arg1, arg2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4475 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4492 "View in source") [Ⓣ][1] Creates a function that, when called, invokes `func` with any additional `partial` arguments prepended to those passed to the new function. This method is similar to `_.bind`, except it does **not** alter the `this` binding. @@ -2122,7 +2141,7 @@ hi('moe'); ### `_.partialRight(func [, arg1, arg2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4506 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4523 "View in source") [Ⓣ][1] This method is similar to `_.partial`, except that `partial` arguments are appended to those passed to the new function. @@ -2159,7 +2178,7 @@ options.imports ### `_.throttle(func, wait)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4528 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4545 "View in source") [Ⓣ][1] Creates a function that, when executed, will only call the `func` function at most once per every `wait` milliseconds. If the throttled function is invoked more than once during the `wait` timeout, `func` will also be called on the trailing edge of the timeout. Subsequent calls to the throttled function will return the result of the last `func` call. @@ -2184,7 +2203,7 @@ jQuery(window).on('scroll', throttled); ### `_.wrap(value, wrapper)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4581 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4598 "View in source") [Ⓣ][1] Creates a function that passes `value` to the `wrapper` function as its first argument. Additional arguments passed to the function are appended to those passed to the `wrapper` function. The `wrapper` is executed with the `this` binding of the created function. @@ -3200,7 +3219,7 @@ _.values({ 'one': 1, 'two': 2, 'three': 3 }); ### `_.escape(string)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4605 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4622 "View in source") [Ⓣ][1] Converts the characters `&`, `<`, `>`, `"`, and `'` in `string` to their corresponding HTML entities. @@ -3224,7 +3243,7 @@ _.escape('Moe, Larry & Curly'); ### `_.identity(value)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4623 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4640 "View in source") [Ⓣ][1] This function returns the first argument passed to it. @@ -3249,7 +3268,7 @@ moe === _.identity(moe); ### `_.mixin(object)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4649 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4666 "View in source") [Ⓣ][1] Adds functions properties of `object` to the `lodash` function and chainable wrapper. @@ -3279,7 +3298,7 @@ _('moe').capitalize(); ### `_.noConflict()` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4678 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4695 "View in source") [Ⓣ][1] Reverts the '_' variable to its previous value and returns a reference to the `lodash` function. @@ -3299,7 +3318,7 @@ var lodash = _.noConflict(); ### `_.parseInt(value)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4699 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4716 "View in source") [Ⓣ][1] Converts the given `value` into an integer of the specified `radix`. @@ -3325,7 +3344,7 @@ _.parseInt('08'); ### `_.random([min=0, max=1])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4722 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4739 "View in source") [Ⓣ][1] Produces a random number between `min` and `max` *(inclusive)*. If only one argument is passed, a number between `0` and the given number will be returned. @@ -3353,7 +3372,7 @@ _.random(5); ### `_.result(object, property)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4761 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4778 "View in source") [Ⓣ][1] Resolves the value of `property` on `object`. If `property` is a function, it will be invoked with the `this` binding of `object` and its result returned, else the property value is returned. If `object` is falsey, then `undefined` is returned. @@ -3406,7 +3425,7 @@ Create a new `lodash` function using the given `context` object. ### `_.template(text, data, options)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4848 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4865 "View in source") [Ⓣ][1] A micro-templating method that handles arbitrary delimiters, preserves whitespace, and correctly escapes quotes within interpolated code. @@ -3490,7 +3509,7 @@ fs.writeFileSync(path.join(cwd, 'jst.js'), '\ ### `_.times(n, callback [, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4973 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4990 "View in source") [Ⓣ][1] Executes the `callback` function `n` times, returning an array of the results of each `callback` execution. The `callback` is bound to `thisArg` and invoked with one argument; *(index)*. @@ -3522,7 +3541,7 @@ _.times(3, function(n) { this.cast(n); }, mage); ### `_.unescape(string)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4999 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5017 "View in source") [Ⓣ][1] The opposite of `_.escape`, this method converts the HTML entities `&`, `<`, `>`, `"`, and `'` in `string` to their corresponding characters. @@ -3546,7 +3565,7 @@ _.unescape('Moe, Larry & Curly'); ### `_.uniqueId([prefix])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5019 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5037 "View in source") [Ⓣ][1] Generates a unique ID. If `prefix` is passed, the ID will be appended to it. @@ -3599,7 +3618,7 @@ A reference to the `lodash` function. ### `_.VERSION` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5253 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5271 "View in source") [Ⓣ][1] *(String)*: The semantic version number. diff --git a/lodash.js b/lodash.js index 07b170286..47df5fb49 100644 --- a/lodash.js +++ b/lodash.js @@ -4251,6 +4251,23 @@ * @param {Mixed} [thisArg] The `this` binding of the created callback. * @param {Number} [argCount=3] The number of arguments the callback accepts. * @returns {Function} Returns a callback function. + * @example + * + * var stooges = [ + * { 'name': 'moe', 'age': 40 }, + * { 'name': 'larry', 'age': 50 } + * ]; + * + * // wrap to create custom callback shorthands + * _.createCallback = _.wrap(_.createCallback, function(func, callback, thisArg) { + * var match = /^(.+?)__([gl]t)(.+)$/.exec(callback); + * return !match ? func(callback, thisArg) : function(object) { + * return match[2] == 'gt' ? object[match[1]] > match[3] : object[match[1]] < match[3]; + * }; + * }); + * + * _.filter(stooges, 'age__gt45'); + * // => [{ 'name': 'larry', 'age': 50 }] */ function createCallback(func, thisArg, argCount) { if (func == null) { @@ -4345,28 +4362,6 @@ }; } - /** - * Executes the `func` function after `wait` milliseconds. Additional arguments - * will be passed to `func` when it is invoked. - * - * @static - * @memberOf _ - * @category Functions - * @param {Function} func The function to delay. - * @param {Number} wait The number of milliseconds to delay execution. - * @param {Mixed} [arg1, arg2, ...] Arguments to invoke the function with. - * @returns {Number} Returns the timer id. - * @example - * - * var log = _.bind(console.log, console); - * _.delay(log, 1000, 'logged later'); - * // => 'logged later' (Appears after one second.) - */ - function delay(func, wait) { - var args = slice(arguments, 2); - return setTimeout(function() { func.apply(undefined, args); }, wait); - } - /** * Defers executing the `func` function until the current call stack has cleared. * Additional arguments will be passed to `func` when it is invoked. @@ -4391,6 +4386,28 @@ defer = bind(setImmediate, context); } + /** + * Executes the `func` function after `wait` milliseconds. Additional arguments + * will be passed to `func` when it is invoked. + * + * @static + * @memberOf _ + * @category Functions + * @param {Function} func The function to delay. + * @param {Number} wait The number of milliseconds to delay execution. + * @param {Mixed} [arg1, arg2, ...] Arguments to invoke the function with. + * @returns {Number} Returns the timer id. + * @example + * + * var log = _.bind(console.log, console); + * _.delay(log, 1000, 'logged later'); + * // => 'logged later' (Appears after one second.) + */ + function delay(func, wait) { + var args = slice(arguments, 2); + return setTimeout(function() { func.apply(undefined, args); }, wait); + } + /** * Creates a function that memoizes the result of `func`. If `resolver` is * passed, it will be used to determine the cache key for storing the result @@ -4975,8 +4992,9 @@ var index = -1, result = Array(n); + callback = createCallback(callback, thisArg, 1); while (++index < n) { - result[index] = callback.call(thisArg, index); + result[index] = callback(index); } return result; }