From ab83f2d5e2326da587eda2c5e37c02ad19503359 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Wed, 16 Jan 2013 01:39:58 -0800 Subject: [PATCH] Ensure `_.first` and `_.last` have the correct chaining behavior when passing a `callback` and `thisArg`. Former-commit-id: 4d54fd677fa48bf8de033696c58ee66babd77a81 --- doc/README.md | 2 +- lodash.js | 8 +++++--- lodash.min.js | 2 +- test/test.js | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 5 deletions(-) diff --git a/doc/README.md b/doc/README.md index b44e247cf..4f513e0f6 100644 --- a/doc/README.md +++ b/doc/README.md @@ -3139,7 +3139,7 @@ A reference to the `lodash` function. ### `_.VERSION` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4561 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4563 "View in source") [Ⓣ][1] *(String)*: The semantic version number. diff --git a/lodash.js b/lodash.js index 718edbdfe..e10ebd502 100644 --- a/lodash.js +++ b/lodash.js @@ -4542,9 +4542,11 @@ forOwn(lodash, function(func, methodName) { if (!lodash.prototype[methodName]) { - lodash.prototype[methodName]= function(n, guard) { - var result = func(this.__wrapped__, n, guard); - return (n == null || guard) ? result : new lodash(result); + lodash.prototype[methodName]= function(callback, thisArg) { + var result = func(this.__wrapped__, callback, thisArg); + return callback == null || (thisArg && typeof callback != 'function') + ? result + : new lodash(result); }; } }); diff --git a/lodash.min.js b/lodash.min.js index f3df3bc19..ad9ab88dd 100644 --- a/lodash.min.js +++ b/lodash.min.js @@ -35,6 +35,6 @@ return lr(n,function(n,r,i){(e?!t(n,r,i):0>C(o,r,1))&&(u[r]=n)}),u},r.once=funct },r.random=function(n,t){return n==W&&t==W&&(t=1),n=+n||0,t==W&&(t=n,n=0),n+Ot(Bt()*((+t||0)-n+1))},r.reduce=T,r.reduceRight=B,r.result=function(n,t){var r=n?n[t]:W;return j(r)?n[t]():r},r.size=function(n){var t=n?n.length:0;return typeof t=="number"?t:sr(n).length},r.some=M,r.sortedIndex=L,r.template=function(n,e,u){var o=r.templateSettings;n||(n="");var u=hr({},u,o),i=hr({},u.imports,o.imports),o=sr(i),i=E(i),a=0,f=u.interpolate||_t,l=!(1==o.length&&"_"==o[0]&&i[0]===r),p="__p+='";if(n.replace(RegExp((u.escape||_t).source+"|"+f.source+"|"+(f===mt?yt:_t).source+"|"+(u.evaluate||_t).source+"|$","g"),function(t,r,e,u,o,i){return e||(e=u),p+=n.slice(a,i).replace(bt,c),r&&(p+="'+__e("+r+")+'"),o&&(p+="';"+o+";__p+='"),e&&(p+="'+((__t=("+e+"))==null?'':__t)+'"),l||(l=o||ft.test(r||e)),a=i+t.length,t }),p+="';\n",f=u=u.variable,!f)if(u="obj",l)p="with("+u+"){"+p+"}";else var s=RegExp("(\\(\\s*)"+u+"\\."+u+"\\b","g"),p=p.replace(gt,"$&"+u+".").replace(s,"$1__d");p=(l?p.replace(lt,""):p).replace(pt,"$1").replace(st,"$1;"),p="function("+u+"){"+(f?"":u+"||("+u+"={});")+"var __t,__p='',__e=_.escape"+(l?",__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}":(f?"":",__d="+u+"."+u+"||"+u)+";")+p+"return __p}";try{var v=Function(o,"return "+p).apply(t,i)}catch(g){throw g.source=p,g}return e?v(e):(v.source=p,v) },r.unescape=function(n){return n==W?"":(n+"").replace(ct,g)},r.uniqueId=function(n){var t=++ut;return(n==W?"":n+"")+t},r.all=$,r.any=M,r.detect=N,r.foldl=T,r.foldr=B,r.include=k,r.inject=T,pr(r,function(n,t){r.prototype[t]||(r.prototype[t]=function(){var t=[this.__wrapped__];return Et.apply(t,arguments),n.apply(r,t)})}),r.first=P,r.last=function(n,t,r){if(n){var e=0,u=n.length;if(typeof t=="function")for(var o=u,t=a(t,r);o--&&t(n[o],o,n);)e++;else if(e=t,e==W||r)return n[u-1];return v(n,It(0,u-e)) -}},r.take=P,r.head=P,pr(r,function(n,t){r.prototype[t]||(r.prototype[t]=function(t,e){var u=n(this.__wrapped__,t,e);return t==W||e?u:new r(u)})}),r.VERSION="1.0.0-rc.3",r.prototype.toString=function(){return this.__wrapped__+""},r.prototype.value=J,r.prototype.valueOf=J,fr(["join","pop","shift"],function(n){var t=rt[n];r.prototype[n]=function(){return t.apply(this.__wrapped__,arguments)}}),fr(["push","reverse","sort","unshift"],function(n){var t=rt[n];r.prototype[n]=function(){return t.apply(this.__wrapped__,arguments),this +}},r.take=P,r.head=P,pr(r,function(n,t){r.prototype[t]||(r.prototype[t]=function(t,e){var u=n(this.__wrapped__,t,e);return t==W||e&&typeof t!="function"?u:new r(u)})}),r.VERSION="1.0.0-rc.3",r.prototype.toString=function(){return this.__wrapped__+""},r.prototype.value=J,r.prototype.valueOf=J,fr(["join","pop","shift"],function(n){var t=rt[n];r.prototype[n]=function(){return t.apply(this.__wrapped__,arguments)}}),fr(["push","reverse","sort","unshift"],function(n){var t=rt[n];r.prototype[n]=function(){return t.apply(this.__wrapped__,arguments),this }}),fr(["concat","slice","splice"],function(n){var t=rt[n];r.prototype[n]=function(){return new r(t.apply(this.__wrapped__,arguments))}}),Jt&&fr(["pop","shift","splice"],function(n){var t=rt[n],e="splice"==n;r.prototype[n]=function(){var n=this.__wrapped__,u=t.apply(n,arguments);return 0===n.length&&delete n[0],e?new r(u):u}}),typeof define=="function"&&typeof define.amd=="object"&&define.amd?(n._=r,define(function(){return r})):Y?typeof module=="object"&&module&&module.exports==Y?(module.exports=r)._=r:Y._=r:n._=r })(this); \ No newline at end of file diff --git a/test/test.js b/test/test.js index 2978a38a5..532003bfc 100644 --- a/test/test.js +++ b/test/test.js @@ -640,6 +640,29 @@ deepEqual(actual, [1, 2]); }); + + test('should chain when passing `n`, `callback`, or `thisArg`', function() { + var actual = _(array).first(2); + + ok(actual instanceof _); + + actual = _(array).first(function(num) { + return num < 3; + }); + + ok(actual instanceof _); + + actual = _(array).first(function(value, index) { + return this[index] < 3; + }, array); + + ok(actual instanceof _); + }); + + test('should not chain when no arguments are passed', function() { + var actual = _(array).first(); + strictEqual(actual, 1); + }); }()); /*--------------------------------------------------------------------------*/ @@ -1192,6 +1215,29 @@ deepEqual(actual, [2, 3]); }); + + test('should chain when passing `n`, `callback`, or `thisArg`', function() { + var actual = _(array).last(2); + + ok(actual instanceof _); + + actual = _(array).last(function(num) { + return num > 1; + }); + + ok(actual instanceof _); + + actual = _(array).last(function(value, index) { + return this[index] > 1; + }, array); + + ok(actual instanceof _); + }); + + test('should not chain when no arguments are passed', function() { + var actual = _(array).last(); + equal(actual, 3); + }); }()); /*--------------------------------------------------------------------------*/