diff --git a/dist/lodash.compat.js b/dist/lodash.compat.js index 176bc690b..d7f7788b3 100644 --- a/dist/lodash.compat.js +++ b/dist/lodash.compat.js @@ -2382,12 +2382,12 @@ * // => 2 */ function indexOf(array, value, fromIndex) { + var length = array ? array.length : 0; if (typeof fromIndex == 'number') { - var length = array ? array.length : 0; fromIndex = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex || 0); } else if (fromIndex) { var index = sortedIndex(array, value); - return array[index] === value ? index : -1; + return (length && array[index] === value) ? index : -1; } return baseIndexOf(array, value, fromIndex); } diff --git a/dist/lodash.compat.min.js b/dist/lodash.compat.min.js index 2a0d0dd69..d8056aef4 100644 --- a/dist/lodash.compat.min.js +++ b/dist/lodash.compat.min.js @@ -19,7 +19,7 @@ if(t&&((i=ae(t))||ie(t))){for(l=e.length;l--;)if(a=e[l]==t){f=u[l];break}if(!a){ }return c?(v(s.g),y(s)):u&&v(s),l}function vt(n){return function(t,r,u){var o={};if(r=e.createCallback(r,u,3),ae(t)){u=-1;for(var a=t.length;++uu;u++)e+="m='"+n.d[u]+"';if((!(l&&o[m])&&h.call(q,m))",n.f||(e+="||(!o[m]&&q[m]!==r[m])"),e+="){"+n.c+"}"; e+="}"}return t("a,f,g,h,j,k,r,p,v,w,y",r+(e+"return s;")+"}")(k,Y,dr,Ar,wt,Yt,br,Qr,et,_r,xr)}function dt(){var n=(n=e.indexOf)===xt?t:n;return n}function bt(n){return typeof n=="function"&&Cr.test(qr.call(n))}function _t(n){var t,r;return!n||xr.call(n)!=tt||!Ar.call(n,"constructor")&&(t=n.constructor,Qt(t)&&!(t instanceof t))||!Yr.argsClass&&wt(n)||!Yr.nodeClass&&h(n)?false:Yr.ownLast?(oe(n,function(n,t,e){return r=Ar.call(e,t),false}),false!==r):(oe(n,function(n,t){r=t}),typeof r=="undefined"||Ar.call(n,r)) -}function wt(n){return n&&typeof n=="object"&&typeof n.length=="number"&&xr.call(n)==G||false}function jt(n,t,r){var u=0,o=n?n.length:0;if(typeof t!="number"&&null!=t){var a=-1;for(t=e.createCallback(t,r,3);++ae?Kr(0,u+e):e||0}else if(e)return e=Ot(n,r),n[e]===r?e:-1;return t(n,r,e)}function Ct(n,t,r){if(typeof t!="number"&&null!=t){var u=0,o=-1,a=n?n.length:0; +}function wt(n){return n&&typeof n=="object"&&typeof n.length=="number"&&xr.call(n)==G||false}function jt(n,t,r){var u=0,o=n?n.length:0;if(typeof t!="number"&&null!=t){var a=-1;for(t=e.createCallback(t,r,3);++ae?Kr(0,u+e):e||0;else if(e)return e=Ot(n,r),u&&n[e]===r?e:-1;return t(n,r,e)}function Ct(n,t,r){if(typeof t!="number"&&null!=t){var u=0,o=-1,a=n?n.length:0; for(t=e.createCallback(t,r,3);++ot?t=Kr(u+t,0):t>u&&(t=u),typeof r=="undefined"?r=u:0>r?r=Kr(u+r,0):r>u&&(r=u),u=r-t||0,r=ir(u);++e>>1,r(n[u])r?0:r);++t=e)return false;if(typeof n=="string"||!ae(n)&&nr(n))return Fr?Fr.call(n,t,r):-1r?Kr(0,e+r):r)||0,-1 2 */ function indexOf(array, value, fromIndex) { + var length = array ? array.length : 0; if (typeof fromIndex == 'number') { - var length = array ? array.length : 0; fromIndex = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex || 0); } else if (fromIndex) { var index = sortedIndex(array, value); - return array[index] === value ? index : -1; + return (length && array[index] === value) ? index : -1; } return baseIndexOf(array, value, fromIndex); } diff --git a/dist/lodash.min.js b/dist/lodash.min.js index 7737b11b0..33ec7ba08 100644 --- a/dist/lodash.min.js +++ b/dist/lodash.min.js @@ -17,12 +17,12 @@ if(c||p)return at(c?n.__wrapped__:n,p?t.__wrapped__:t,r,u,o,i);if(l!=Y)return fa }else e(t,function(t,e,f){return Or.call(f,e)?(g++,a=Or.call(n,e)&&at(n[e],t,r,u,o,i)):void 0}),a&&!u&&e(n,function(n,t,r){return Or.call(r,t)?a=-1<--g:void 0});return o.pop(),i.pop(),l&&(h(o),h(i)),a}function lt(n,t,r,e,u){(Zr(t)?St:Mt)(t,function(t,o){var i,a,f=t,l=n[o];if(t&&((a=Zr(t))||ne(t))){for(f=e.length;f--;)if(i=e[f]==t){l=u[f];break}if(!i){var c;r&&(f=r(l,t),c=typeof f!="undefined")&&(l=f),c||(l=a?Zr(l)?l:[]:ne(l)?l:{}),e.push(t),u.push(l),c||lt(l,t,r,e,u)}}else r&&(f=r(l,t),typeof f=="undefined"&&(f=t)),typeof f!="undefined"&&(l=f); n[o]=l})}function ct(n,t){return n+kr(Lr()*(t-n+1))}function st(n,e,u){var o=-1,i=vt(),a=n?n.length:0,f=[],c=!e&&a>=A&&i===t,p=u||c?s():f;for(c&&(p=l(p),i=r);++oi(p,y))&&((u||c)&&p.push(y),f.push(g))}return c?(h(p.g),v(p)):u&&h(p),f}function gt(n){return function(t,r,e){var u={};r=i.createCallback(r,e,3),e=-1;var o=t?t.length:0;if(typeof o=="number")for(;++ee?qr(0,u+e):e||0}else if(e)return e=kt(n,r),n[e]===r?e:-1;return t(n,r,e)}function wt(n,t,r){if(typeof t!="number"&&null!=t){var e=0,u=-1,o=n?n.length:0;for(t=i.createCallback(t,r,3);++ut?t=qr(u+t,0):t>u&&(t=u),typeof r=="undefined"?r=u:0>r?r=qr(u+r,0):r>u&&(r=u),u=r-t||0,r=ur(u);++e>>1,r(n[e])r?0:r);++t=e)return false;if(typeof n=="string"||!Zr(n)&&Qt(n))return Sr?Sr.call(n,t,r):-1r?qr(0,e+r):r)||0,-1e?qr(0,u+e):e||0;else if(e)return e=kt(n,r),u&&n[e]===r?e:-1;return t(n,r,e)}function wt(n,t,r){if(typeof t!="number"&&null!=t){var e=0,u=-1,o=n?n.length:0;for(t=i.createCallback(t,r,3);++ut?t=qr(u+t,0):t>u&&(t=u),typeof r=="undefined"?r=u:0>r?r=qr(u+r,0):r>u&&(r=u),u=r-t||0,r=ur(u);++e>>1,r(n[e])r?0:r);++t=e)return false;if(typeof n=="string"||!Zr(n)&&Qt(n))return Sr?Sr.call(n,t,r):-1r?qr(0,e+r):r)||0,-1o&&(o=f)}else t=null==t&&Qt(n)?u:i.createCallback(t,r,3),St(n,function(n,r,u){r=t(n,r,u),r>e&&(e=r,o=n)});return o}function $t(n,t,r,e){var u=3>arguments.length;t=i.createCallback(t,e,4);var o=-1,a=n?n.length:0;if(typeof a=="number")for(u&&a&&(r=n[++o]);++oarguments.length;return t=i.createCallback(t,e,4),Tt(n,function(n,e,o){r=u?(u=false,n):t(r,n,e,o)}),r}function qt(n){var t=-1,r=n?n.length:0,e=ur(typeof r=="number"?r:0); return St(n,function(n){var r=ct(0,++t);e[t]=e[r],e[r]=n}),e}function Wt(n,t,r){var e;t=i.createCallback(t,r,3),r=-1;var u=n?n.length:0;if(typeof u=="number")for(;++r=y;m?(i&&(i=jr(i)),s=f,a=n.apply(l,o)):i||(i=Er(e,y))}return m&&c?c=jr(c):c||t===g||(c=Er(u,t)),r&&(m=true,a=n.apply(l,o)),!m||c||i||(o=l=null),a}}function Pt(n,t,r){var e=arguments,u=0,o=e.length,i=typeof r; diff --git a/dist/lodash.underscore.js b/dist/lodash.underscore.js index 922d6bae8..6a8592b91 100644 --- a/dist/lodash.underscore.js +++ b/dist/lodash.underscore.js @@ -1130,12 +1130,12 @@ * // => 2 */ function indexOf(array, value, fromIndex) { + var length = array ? array.length : 0; if (typeof fromIndex == 'number') { - var length = array ? array.length : 0; fromIndex = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex || 0); } else if (fromIndex) { var index = sortedIndex(array, value); - return array[index] === value ? index : -1; + return (length && array[index] === value) ? index : -1; } return baseIndexOf(array, value, fromIndex); } diff --git a/dist/lodash.underscore.min.js b/dist/lodash.underscore.min.js index 416b66583..7cbcf0bd9 100644 --- a/dist/lodash.underscore.min.js +++ b/dist/lodash.underscore.min.js @@ -10,7 +10,7 @@ for(var u=n?n.length:0,o=[];++eu(f,c))&&(t&&f.push(c),i.push(a))}return i}function h(n){return function(r,t,e){var u={};t=X(t,e,3),e=-1;var o=r?r.length:0;if(typeof o=="number")for(;++ee?Ur(0,u+e):e||0}else if(e)return e=T(r,t),r[e]===t?e:-1;return n(r,t,e)}function j(n,r,t){if(typeof r!="number"&&null!=r){var e=0,u=-1,o=n?n.length:0;for(r=X(r,t,3);++ur?r=Ur(u+r,0):r>u&&(r=u),typeof t=="undefined"?t=u:0>t?t=Ur(u+t,0):t>u&&(t=u),u=t-r||0,t=Array(u);++ee?Ur(0,u+e):e||0;else if(e)return e=T(r,t),u&&r[e]===t?e:-1;return n(r,t,e)}function j(n,r,t){if(typeof r!="number"&&null!=r){var e=0,u=-1,o=n?n.length:0;for(r=X(r,t,3);++ur?r=Ur(u+r,0):r>u&&(r=u),typeof t=="undefined"?t=u:0>t?t=Ur(u+t,0):t>u&&(t=u),u=t-r||0,t=Array(u);++e>>1,t(n[e])u&&(u=t); diff --git a/lodash.js b/lodash.js index f99f841f4..72bacfd99 100644 --- a/lodash.js +++ b/lodash.js @@ -2399,12 +2399,12 @@ * // => 2 */ function indexOf(array, value, fromIndex) { + var length = array ? array.length : 0; if (typeof fromIndex == 'number') { - var length = array ? array.length : 0; fromIndex = (fromIndex < 0 ? nativeMax(0, length + fromIndex) : fromIndex || 0); } else if (fromIndex) { var index = sortedIndex(array, value); - return (array[index] === value && array.length ? index : -1); + return (length && array[index] === value) ? index : -1; } return baseIndexOf(array, value, fromIndex); } diff --git a/test/test.js b/test/test.js index 939aeecd2..bee68d775 100644 --- a/test/test.js +++ b/test/test.js @@ -3165,9 +3165,13 @@ equal(_.indexOf(array, 3), 2); }); - test('should return `-1` for an unmatched value', 2, function() { + test('should return `-1` for an unmatched value', 4, function() { equal(_.indexOf(array, 4), -1); - equal(_.indexOf([], undefined, true), -1); + equal(_.indexOf(array, 4, true), -1); + + var empty = []; + equal(_.indexOf(empty, undefined), -1); + equal(_.indexOf(empty, undefined, true), -1); }); test('should work with a positive `fromIndex`', 1, function() { @@ -3198,7 +3202,6 @@ test('should work with `isSorted`', 1, function() { strictEqual(_.indexOf([1, 2, 3], 1, true), 0); }); - }()); /*--------------------------------------------------------------------------*/