From 425499b3aa5edfaa1a6b9b5e89b587ad530589a4 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Fri, 16 Aug 2013 00:26:41 -0700 Subject: [PATCH] Add `_.sample`. Former-commit-id: 00e27cca2a65e1310b26904173ffec18aa484e48 --- build.js | 5 +- build/pre-compile.js | 1 + dist/lodash.compat.js | 80 +++++++++++------ dist/lodash.compat.min.js | 93 ++++++++++---------- dist/lodash.js | 80 +++++++++++------ dist/lodash.min.js | 85 +++++++++--------- dist/lodash.underscore.js | 41 ++++----- dist/lodash.underscore.min.js | 64 +++++++------- doc/README.md | 158 ++++++++++++++++++++-------------- lodash.js | 80 +++++++++++------ test/test-build.js | 2 + test/test.js | 137 +++++++++++++++++++++++++++-- 12 files changed, 530 insertions(+), 296 deletions(-) diff --git a/build.js b/build.js index f5b901e38..924c5f1be 100644 --- a/build.js +++ b/build.js @@ -201,7 +201,8 @@ 'rest': ['createCallback', 'slice'], 'result': ['isFunction'], 'runInContext': ['defaults', 'pick'], - 'shuffle': ['forEach'], + 'sample': ['random', 'shuffle', 'toArray'], + 'shuffle': ['forEach', 'random'], 'size': ['keys'], 'some': ['baseEach', 'createCallback', 'isArray'], 'sortBy': ['compareAscending', 'createCallback', 'forEach', 'getObject', 'releaseObject'], @@ -380,6 +381,7 @@ 'reduce', 'reduceRight', 'reject', + 'sample', 'shuffle', 'size', 'some', @@ -584,6 +586,7 @@ 'pull', 'remove', 'runInContext', + 'sample', 'transform', 'wrapperToString' ]; diff --git a/build/pre-compile.js b/build/pre-compile.js index 8dfaa2ca3..f01e3b3f0 100644 --- a/build/pre-compile.js +++ b/build/pre-compile.js @@ -225,6 +225,7 @@ 'rest', 'result', 'runInContext', + 'sample', 'select', 'setImmediate', 'setTimeout', diff --git a/dist/lodash.compat.js b/dist/lodash.compat.js index a026c3b41..cd33dab86 100644 --- a/dist/lodash.compat.js +++ b/dist/lodash.compat.js @@ -3759,6 +3759,37 @@ }); } + /** + * Retrieves a random element or `n` random elements from a collection. + * + * @static + * @memberOf _ + * @category Collections + * @param {Array|Object|String} collection The collection to sample. + * @returns {Array} Returns the random sample(s) of `collection`. + * @example + * + * _.sample([1, 2, 3, 4]); + * // => 2 + * + * _.sample([1, 2, 3, 4], 2); + * // => [3, 1] + */ + function sample(collection, n, guard) { + if (!isArray(collection)) { + collection = toArray(collection); + } + var index = -1, + maxIndex = collection.length - 1; + + if (n == null || guard) { + return collection[random(maxIndex)]; + } + var result = shuffle(collection); + result.length = nativeMin(nativeMax(0, n), result.length); + return result; + } + /** * Creates an array of shuffled values, using a version of the Fisher-Yates * shuffle. See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle. @@ -3779,7 +3810,7 @@ result = Array(typeof length == 'number' ? length : 0); forEach(collection, function(value) { - var rand = floor(nativeRandom() * (++index + 1)); + var rand = random(++index); result[index] = result[rand]; result[rand] = value; }); @@ -4121,11 +4152,10 @@ } /** - * Gets the first element of an array. If a number `n` is provided the first - * `n` elements of the array are returned. If a callback is provided elements - * at the beginning of the array are returned as long as the callback returns - * truthy. The callback is bound to `thisArg` and invoked with three arguments; - * (value, index, array). + * Gets the first element or first `n` elements of an array. If a callback + * is provided elements at the beginning of the array are returned as long + * as the callback returns truthy. The callback is bound to `thisArg` and + * invoked with three arguments; (value, index, array). * * If a property name is provided for `callback` the created "_.pluck" style * callback will return the property value of the given element. @@ -4288,11 +4318,10 @@ } /** - * Gets all but the last element of an array. If a number `n` is provided - * the last `n` elements are excluded from the result. If a callback is - * provided elements at the end of the array are excluded from the result - * as long as the callback returns truthy. The callback is bound to `thisArg` - * and invoked with three arguments; (value, index, array). + * Gets all but the last element or last `n` elements of an array. If a + * callback is provided elements at the end of the array are excluded from + * the result as long as the callback returns truthy. The callback is bound + * to `thisArg` and invoked with three arguments; (value, index, array). * * If a property name is provided for `callback` the created "_.pluck" style * callback will return the property value of the given element. @@ -4422,12 +4451,10 @@ } /** - * Gets the last element of an array. If a number `n` is provided the last - * `n` elements of the array are returned. If a callback is provided elements - * at the end of the array are returned as long as the callback returns truthy. - * The callback is bound to `thisArg` and invoked with three arguments; - * (value, index, array). - * + * Gets the last element or last `n` elements of an array. If a callback is + * provided elements at the end of the array are returned as long as the + * callback returns truthy. The callback is bound to `thisArg` and invoked + * with three arguments; (value, index, array). * * If a property name is provided for `callback` the created "_.pluck" style * callback will return the property value of the given element. @@ -4671,12 +4698,11 @@ } /** - * The opposite of `_.initial` this method gets all but the first value of - * an array. If a number `n` is provided the first `n` values are excluded - * from the result. If a callback function is provided elements at the beginning - * of the array are excluded from the result as long as the callback returns - * truthy. The callback is bound to `thisArg` and invoked with three - * arguments; (value, index, array). + * The opposite of `_.initial` this method gets all but the first element or + * first `n` elements of an array. If a callback function is provided elements + * at the beginning of the array are excluded from the result as long as the + * callback returns truthy. The callback is bound to `thisArg` and invoked + * with three arguments; (value, index, array). * * If a property name is provided for `callback` the created "_.pluck" style * callback will return the property value of the given element. @@ -6373,18 +6399,20 @@ // add functions capable of returning wrapped and unwrapped values when chaining lodash.first = first; lodash.last = last; + lodash.sample = sample; // add aliases lodash.take = first; lodash.head = first; forOwn(lodash, function(func, methodName) { + var callbackable = methodName !== 'sample'; if (!lodash.prototype[methodName]) { - lodash.prototype[methodName]= function(callback, thisArg) { + lodash.prototype[methodName]= function(n, guard) { var chainAll = this.__chain__, - result = func(this.__wrapped__, callback, thisArg); + result = func(this.__wrapped__, n, guard); - return !chainAll && (callback == null || (thisArg && typeof callback != 'function')) + return !chainAll && (n == null || (guard && !(callbackable && typeof n == 'function'))) ? result : new lodashWrapper(result, chainAll); }; diff --git a/dist/lodash.compat.min.js b/dist/lodash.compat.min.js index b53495f71..e82699132 100644 --- a/dist/lodash.compat.min.js +++ b/dist/lodash.compat.min.js @@ -4,51 +4,52 @@ * Build: `lodash -o ./dist/lodash.compat.js` */ ;!function(n){function t(n,t,e){e=(e||0)-1;for(var r=n?n.length:0;++er||typeof e=="undefined")return 1;if(ee?0:e);++r=E&&i===t,h=u||p?f():l; -if(p){var v=a(h);v?(i=e,h=v):(p=b,h=u?h:(s(h),l))}for(;++oi(h,y))&&((u||p)&&h.push(y),l.push(v))}return p?(s(h.b),g(h)):u&&s(h),l}function ot(n){return function(t,e,r){var u={};return e=_.createCallback(e,r,3),Et(t,function(t,r,o){r=te(e(t,r,o)),n(u,t,r,o)}),u}}function at(n,t,e,r,u,o){var a=1&t,i=2&t,f=4&t,c=8&t,l=32&t;if(!i&&!mt(n))throw new ee;if(!a||i||f||l||!($e.fastBind||je&&e.length))p=function(){var l=arguments,g=a?u:this;return e&&we.apply(l,e),r&&ve.apply(l,r),f&&l.lengthk;k++)r+="n='"+n.h[k]+"';if((!(r&&x[n])&&m.call(t,n))",n.j||(r+="||(!x[n]&&t[n]!==A[n])"),r+="){"+n.g+"}"; -r+="}"}return(n.b||$e.nonEnumArgs)&&(r+="}"),r+=n.c+";return E",t=t("d,j,k,m,o,p,q,s,v,A,B,y,I,J,L",e+r+"}"),g(n),t(Y,G,ue,he,x,gt,ze,_t,n.f,oe,X,De,V,ae,_e)}function ft(n){return dt(n)?ke(n):{}}function ct(n){return Ge[n]}function lt(){var n=(n=_.indexOf)===Ft?t:n;return n}function pt(n){var t,e;return!n||_e.call(n)!=H||(t=n.constructor,mt(t)&&!(t instanceof t))||!$e.argsClass&>(n)||!$e.nodeClass&&l(n)?b:$e.ownLast?(Xe(n,function(n,t,r){return e=he.call(r,t),b}),e!==false):(Xe(n,function(n,t){e=t -}),e===y||he.call(n,e))}function st(n){return Je[n]}function gt(n){return n&&typeof n=="object"?_e.call(n)==T:b}function ht(n,t,e){var r=Te(n),u=r.length;for(t=Y(t,e,3);u--&&(e=r[u],!(t(n[e],e,n)===false)););return n}function vt(n){var t=[];return Xe(n,function(n,e){mt(n)&&t.push(e)}),t.sort()}function yt(n){for(var t=-1,e=Te(n),r=e.length,u={};++te?Se(0,o+e):e)||0,o&&typeof o=="number"?a=-1<(_t(n)?n.indexOf(t,e):u(n,t,e)):Ue(n,function(n){return++ro&&(o=i) -}}else t=!t&&_t(n)?u:_.createCallback(t,e,3),Ue(n,function(n,e,u){e=t(n,e,u),e>r&&(r=e,o=n)});return o}function It(n,t,e,r){var u=3>arguments.length;if(t=Y(t,r,4),ze(n)){var o=-1,a=n.length;for(u&&(e=n[++o]);++oarguments.length;return t=Y(t,r,4),Ot(n,function(n,r,o){e=u?(u=b,n):t(e,n,r,o)}),e}function Pt(n,t,e){var r;if(t=_.createCallback(t,e,3),ze(n)){e=-1;for(var u=n.length;++e=E&&u===t;if(c){var l=a(i);l?(u=e,i=l):c=b}for(;++ru(i,l)&&f.push(l);return c&&g(i),f}function Rt(n,t,e){if(n){var r=0,u=n.length;if(typeof t!="number"&&t!=d){var o=-1;for(t=_.createCallback(t,e,3);++or?Se(0,u+r):r||0}else if(r)return r=$t(n,e),n[r]===e?r:-1; -return n?t(n,e,r):-1}function Dt(n,t,e){if(typeof t!="number"&&t!=d){var r=0,u=-1,o=n?n.length:0;for(t=_.createCallback(t,e,3);++u>>1,e(n[r])e?0:e);++tc&&(i=n.apply(f,a));else{var e=+new Vt;!s&&!v&&(l=e);var o=p-(e-l);0/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:R,variable:"",imports:{_:_}},ke||(ft=function(n){if(dt(n)){p.prototype=n;var t=new p;p.prototype=d}return t||{}}),$e.argsClass||(gt=function(n){return n&&typeof n=="object"?he.call(n,"callee"):b});var ze=xe||function(n){return n&&typeof n=="object"?_e.call(n)==q:b},Le=it({a:"z",e:"[]",i:"if(!(B[typeof z]))return E",g:"E.push(n)"}),Te=Oe?function(n){return dt(n)?$e.enumPrototypes&&typeof n=="function"||$e.nonEnumArgs&&n.length&>(n)?Le(n):Oe(n):[] -}:Le,qe={a:"g,e,K",i:"e=e&&typeof K=='undefined'?e:d(e,K,3)",b:"typeof u=='number'",v:Te,g:"if(e(t[n],n,g)===false)return E"},Ke={a:"z,H,l",i:"var a=arguments,b=0,c=typeof l=='number'?2:a.length;while(++b":">",'"':""","'":"'"},Je=yt(Ge),Me=ne("("+Te(Je).join("|")+")","g"),He=ne("["+Te(Ge).join("")+"]","g"),Ue=it(qe),Ve=it(Ke,{i:Ke.i.replace(";",";if(c>3&&typeof a[c-2]=='function'){var e=d(a[--c-1],a[c--],2)}else if(c>2&&typeof a[c-1]=='function'){e=a[--c]}"),g:"E[n]=e?e(E[n],t[n]):t[n]"}),Qe=it(Ke),Xe=it(qe,We,{j:b}),Ye=it(qe,We); -mt(/x/)&&(mt=function(n){return typeof n=="function"&&_e.call(n)==J});var Ze=ge?function(n){if(!n||_e.call(n)!=H||!$e.argsClass&>(n))return b;var t=n.valueOf,e=typeof t=="function"&&(e=ge(t))&&ge(e);return e?n==e||ge(n)==e:pt(n)}:pt,nr=ot(function(n,t,e){he.call(n,e)?n[e]++:n[e]=1}),tr=ot(function(n,t,e){(he.call(n,e)?n[e]:n[e]=[]).push(t)}),er=ot(function(n,t,e){n[e]=t}),rr=St;Re&&nt&&typeof me=="function"&&(Wt=function(n){if(!mt(n))throw new ee;return me.apply(r,arguments)});var ur=8==Ie(S+"08")?Ie:function(n,t){return Ie(_t(n)?n.replace(F,""):n,t||0) -};return _.after=function(n,t){if(!mt(t))throw new ee;return function(){return 1>--n?t.apply(this,arguments):void 0}},_.assign=Ve,_.at=function(n){var t=arguments,e=-1,r=Z(t,m,b,1),t=t[2]&&t[2][t[1]]===n?1:r.length,u=Ht(t);for($e.unindexedChars&&_t(n)&&(n=n.split(""));++e=E&&a(o?r[o]:v)}n:for(;++c(m?e(m,y):l(v,y))){for(o=u,(m||v).push(y);--o;)if(m=i[o],0>(m?e(m,y):l(r[o],y)))continue n;h.push(y)}}for(;u--;)(m=i[u])&&g(m);return s(i),s(v),h},_.invert=yt,_.invoke=function(n,t){var e=Pe.call(arguments,2),r=-1,u=typeof t=="function",o=n?n.length:0,a=Ht(typeof o=="number"?o:0);return Et(n,function(n){a[++r]=(u?t:n[t]).apply(n,e)}),a},_.keys=Te,_.map=St,_.max=At,_.memoize=function(n,t){function e(){var r=e.cache,u=C+(t?t.apply(this,arguments):arguments[0]); -return he.call(r,u)?r[u]:r[u]=n.apply(this,arguments)}if(!mt(n))throw new ee;return e.cache={},e},_.merge=function(n){var t=arguments,e=2;if(!dt(n))return n;if("number"!=typeof t[2]&&(e=t.length),3r(a,e))&&(o[e]=n)}),o},_.once=function(n){var t,e;if(!mt(n))throw new ee;return function(){return t?e:(t=m,e=n.apply(this,arguments),n=d,e)}},_.pairs=function(n){for(var t=-1,e=Te(n),r=e.length,u=Ht(r);++te?Se(0,r+e):Ae(e,r-1))+1);r--;)if(n[r]===t)return r;return-1},_.mixin=Jt,_.noConflict=function(){return r._=ie,this},_.parseInt=ur,_.random=function(n,t){n==d&&t==d&&(t=1),n=+n||0,t==d?(t=n,n=0):t=+t||0;var e=Be();return n%1||t%1?n+Ae(e*(t-n+parseFloat("1e-"+((e+"").length-1))),t):n+se(e*(t-n+1))},_.reduce=It,_.reduceRight=Bt,_.result=function(n,t){var e=n?n[t]:y; -return mt(e)?n[t]():e},_.runInContext=v,_.size=function(n){var t=n?n.length:0;return typeof t=="number"?t:Te(n).length},_.some=Pt,_.sortedIndex=$t,_.template=function(n,t,e){var r=_.templateSettings;n||(n=""),e=Qe({},e,r);var u,o=Qe({},e.imports,r.imports),r=Te(o),o=wt(o),a=0,f=e.interpolate||D,c="__p+='",f=ne((e.escape||D).source+"|"+f.source+"|"+(f===R?P:D).source+"|"+(e.evaluate||D).source+"|$","g");n.replace(f,function(t,e,r,o,f,l){return r||(r=o),c+=n.slice(a,l).replace($,i),e&&(c+="'+__e("+e+")+'"),f&&(u=m,c+="';"+f+";__p+='"),r&&(c+="'+((__t=("+r+"))==null?'':__t)+'"),a=l+t.length,t -}),c+="';\n",f=e=e.variable,f||(e="obj",c="with("+e+"){"+c+"}"),c=(u?c.replace(A,""):c).replace(I,"$1").replace(B,"$1;"),c="function("+e+"){"+(f?"":e+"||("+e+"={});")+"var __t,__p='',__e=_.escape"+(u?",__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}":";")+c+"return __p}";try{var l=Qt(r,"return "+c).apply(y,o)}catch(p){throw p.source=c,p}return t?l(t):(l.source=c,l)},_.unescape=function(n){return n==d?"":te(n).replace(Me,st)},_.uniqueId=function(n){var t=++j;return te(n==d?"":n)+t -},_.all=kt,_.any=Pt,_.detect=Ct,_.findWhere=Ct,_.foldl=It,_.foldr=Bt,_.include=jt,_.inject=It,Ye(_,function(n,t){_.prototype[t]||(_.prototype[t]=function(){var t=[this.__wrapped__],e=this.__chain__;return ve.apply(t,arguments),t=n.apply(_,t),e?new w(t,e):t})}),_.first=Rt,_.last=function(n,t,e){if(n){var r=0,u=n.length;if(typeof t!="number"&&t!=d){var o=u;for(t=_.createCallback(t,e,3);o--&&t(n[o],o,n);)r++}else if(r=t,r==d||e)return n[u-1];return h(n,Se(0,u-r))}},_.take=Rt,_.head=Rt,Ye(_,function(n,t){_.prototype[t]||(_.prototype[t]=function(t,e){var r=this.__chain__,u=n(this.__wrapped__,t,e); -return!r&&(t==d||e&&typeof t!="function")?u:new w(u,r)})}),_.VERSION="1.3.1",_.prototype.chain=function(){return this.__chain__=m,this},_.prototype.toString=function(){return te(this.__wrapped__)},_.prototype.value=Mt,_.prototype.valueOf=Mt,Ue(["join","pop","shift"],function(n){var t=re[n];_.prototype[n]=function(){var n=this.__chain__,e=t.apply(this.__wrapped__,arguments);return n?new w(e,n):e}}),Ue(["push","reverse","sort","unshift"],function(n){var t=re[n];_.prototype[n]=function(){return t.apply(this.__wrapped__,arguments),this -}}),Ue(["concat","slice","splice"],function(n){var t=re[n];_.prototype[n]=function(){return new w(t.apply(this.__wrapped__,arguments),this.__chain__)}}),$e.spliceObjects||Ue(["pop","shift","splice"],function(n){var t=re[n],e="splice"==n;_.prototype[n]=function(){var n=this.__chain__,r=this.__wrapped__,u=t.apply(r,arguments);return 0===r.length&&delete r[0],n||e?new w(u,n):u}}),_}var y,m=!0,d=null,b=!1,_=[],w=[],j=0,x={},C=+new Date+"",E=75,O=40,S=" \t\x0B\f\xa0\ufeff\n\r\u2028\u2029\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000",A=/\b__p\+='';/g,I=/\b(__p\+=)''\+/g,B=/(__e\(.*?\)|\b__t\))\+'';/g,P=/\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g,N=/\w*$/,R=/<%=([\s\S]+?)%>/g,F=RegExp("^["+S+"]*0+(?=.$)"),D=/($^)/,$=/['\n\r\t\u2028\u2029\\]/g,z="Array Boolean Date Error Function Math Number Object RegExp String _ attachEvent clearTimeout isFinite isNaN parseInt setImmediate setTimeout".split(" "),L="constructor hasOwnProperty isPrototypeOf propertyIsEnumerable toLocaleString toString valueOf".split(" "),T="[object Arguments]",q="[object Array]",K="[object Boolean]",W="[object Date]",G="[object Error]",J="[object Function]",M="[object Number]",H="[object Object]",U="[object RegExp]",V="[object String]",Q={}; +}}function u(n){return n.charCodeAt(0)}function o(n,t){var e=n.l,r=t.l;if(e!==r){if(e>r||typeof e=="undefined")return 1;if(ee?0:e);++r=E&&i===t,h=u||p?f():c; +if(p){var v=a(h);v?(i=e,h=v):(p=b,h=u?h:(s(h),c))}for(;++oi(h,y))&&((u||p)&&h.push(y),c.push(v))}return p?(s(h.b),g(h)):u&&s(h),c}function ot(n){return function(t,e,r){var u={};return e=_.createCallback(e,r,3),Et(t,function(t,r,o){r=ue(e(t,r,o)),n(u,t,r,o)}),u}}function at(n,t,e,r,u,o){var a=1&t,i=2&t,f=4&t,l=8&t,c=32&t;if(!i&&!mt(n))throw new oe;if(!a||i||f||c||!(Te.fastBind||Ce&&e.length))p=function(){var c=arguments,g=a?u:this;return e&&xe.apply(c,e),r&&de.apply(c,r),f&&c.lengthk;k++)r+="n='"+n.h[k]+"';if((!(r&&x[n])&&m.call(t,n))",n.j||(r+="||(!x[n]&&t[n]!==A[n])"),r+="){"+n.g+"}"; +r+="}"}return(n.b||Te.nonEnumArgs)&&(r+="}"),r+=n.c+";return E",t=t("d,j,k,m,o,p,q,s,v,A,B,y,I,J,L",e+r+"}"),g(n),t(Y,G,ie,me,x,gt,qe,_t,n.f,fe,X,Le,V,le,ke)}function ft(n){return dt(n)?Ee(n):{}}function lt(n){return He[n]}function ct(){var n=(n=_.indexOf)===$t?t:n;return n}function pt(n){var t,e;return!n||ke.call(n)!=H||(t=n.constructor,mt(t)&&!(t instanceof t))||!Te.argsClass&>(n)||!Te.nodeClass&&c(n)?b:Te.ownLast?(nr(n,function(n,t,r){return e=me.call(r,t),b}),e!==false):(nr(n,function(n,t){e=t +}),e===y||me.call(n,e))}function st(n){return Ue[n]}function gt(n){return n&&typeof n=="object"?ke.call(n)==T:b}function ht(n,t,e){var r=We(n),u=r.length;for(t=Y(t,e,3);u--&&(e=r[u],!(t(n[e],e,n)===false)););return n}function vt(n){var t=[];return nr(n,function(n,e){mt(n)&&t.push(e)}),t.sort()}function yt(n){for(var t=-1,e=We(n),r=e.length,u={};++te?Be(0,o+e):e)||0,o&&typeof o=="number"?a=-1<(_t(n)?n.indexOf(t,e):u(n,t,e)):Xe(n,function(n){return++ro&&(o=i) +}}else t=!t&&_t(n)?u:_.createCallback(t,e,3),Xe(n,function(n,e,u){e=t(n,e,u),e>r&&(r=e,o=n)});return o}function It(n,t,e,r){var u=3>arguments.length;if(t=Y(t,r,4),qe(n)){var o=-1,a=n.length;for(u&&(e=n[++o]);++oarguments.length;return t=Y(t,r,4),Ot(n,function(n,r,o){e=u?(u=b,n):t(e,n,r,o)}),e}function Pt(n){var t=-1,e=n?n.length:0,r=Qt(typeof e=="number"?e:0);return Et(n,function(n){var e=Ut(++t); +r[t]=r[e],r[e]=n}),r}function Nt(n,t,e){var r;if(t=_.createCallback(t,e,3),qe(n)){e=-1;for(var u=n.length;++e=E&&u===t;if(l){var c=a(i);c?(u=e,i=c):l=b}for(;++ru(i,c)&&f.push(c);return l&&g(i),f}function Dt(n,t,e){if(n){var r=0,u=n.length; +if(typeof t!="number"&&t!=d){var o=-1;for(t=_.createCallback(t,e,3);++or?Be(0,u+r):r||0}else if(r)return r=Lt(n,e),n[r]===e?r:-1;return n?t(n,e,r):-1}function zt(n,t,e){if(typeof t!="number"&&t!=d){var r=0,u=-1,o=n?n.length:0;for(t=_.createCallback(t,e,3);++u>>1,e(n[r])e?0:e);++tl&&(i=n.apply(f,a));else{var e=+new Yt;!s&&!v&&(c=e);var o=p-(e-c);0/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:R,variable:"",imports:{_:_}},Ee||(ft=function(n){if(dt(n)){p.prototype=n;var t=new p;p.prototype=d}return t||{}}),Te.argsClass||(gt=function(n){return n&&typeof n=="object"?me.call(n,"callee"):b});var qe=Oe||function(n){return n&&typeof n=="object"?ke.call(n)==q:b},Ke=it({a:"z",e:"[]",i:"if(!(B[typeof z]))return E",g:"E.push(n)"}),We=Ie?function(n){return dt(n)?Te.enumPrototypes&&typeof n=="function"||Te.nonEnumArgs&&n.length&>(n)?Ke(n):Ie(n):[] +}:Ke,Ge={a:"g,e,K",i:"e=e&&typeof K=='undefined'?e:d(e,K,3)",b:"typeof u=='number'",v:We,g:"if(e(t[n],n,g)===false)return E"},Je={a:"z,H,l",i:"var a=arguments,b=0,c=typeof l=='number'?2:a.length;while(++b":">",'"':""","'":"'"},Ue=yt(He),Ve=re("("+We(Ue).join("|")+")","g"),Qe=re("["+We(He).join("")+"]","g"),Xe=it(Ge),Ye=it(Je,{i:Je.i.replace(";",";if(c>3&&typeof a[c-2]=='function'){var e=d(a[--c-1],a[c--],2)}else if(c>2&&typeof a[c-1]=='function'){e=a[--c]}"),g:"E[n]=e?e(E[n],t[n]):t[n]"}),Ze=it(Je),nr=it(Ge,Me,{j:b}),tr=it(Ge,Me); +mt(/x/)&&(mt=function(n){return typeof n=="function"&&ke.call(n)==J});var er=ye?function(n){if(!n||ke.call(n)!=H||!Te.argsClass&>(n))return b;var t=n.valueOf,e=typeof t=="function"&&(e=ye(t))&&ye(e);return e?n==e||ye(n)==e:pt(n)}:pt,rr=ot(function(n,t,e){me.call(n,e)?n[e]++:n[e]=1}),ur=ot(function(n,t,e){(me.call(n,e)?n[e]:n[e]=[]).push(t)}),or=ot(function(n,t,e){n[e]=t}),ar=St;$e&&nt&&typeof _e=="function"&&(Jt=function(n){if(!mt(n))throw new oe;return _e.apply(r,arguments)});var ir=8==Ne(S+"08")?Ne:function(n,t){return Ne(_t(n)?n.replace(F,""):n,t||0) +};return _.after=function(n,t){if(!mt(t))throw new oe;return function(){return 1>--n?t.apply(this,arguments):void 0}},_.assign=Ye,_.at=function(n){var t=arguments,e=-1,r=Z(t,m,b,1),t=t[2]&&t[2][t[1]]===n?1:r.length,u=Qt(t);for(Te.unindexedChars&&_t(n)&&(n=n.split(""));++e=E&&a(o?r[o]:v)}n:for(;++l(m?e(m,y):c(v,y))){for(o=u,(m||v).push(y);--o;)if(m=i[o],0>(m?e(m,y):c(r[o],y)))continue n;h.push(y)}}for(;u--;)(m=i[u])&&g(m);return s(i),s(v),h},_.invert=yt,_.invoke=function(n,t){var e=Fe.call(arguments,2),r=-1,u=typeof t=="function",o=n?n.length:0,a=Qt(typeof o=="number"?o:0);return Et(n,function(n){a[++r]=(u?t:n[t]).apply(n,e)}),a},_.keys=We,_.map=St,_.max=At,_.memoize=function(n,t){function e(){var r=e.cache,u=C+(t?t.apply(this,arguments):arguments[0]); +return me.call(r,u)?r[u]:r[u]=n.apply(this,arguments)}if(!mt(n))throw new oe;return e.cache={},e},_.merge=function(n){var t=arguments,e=2;if(!dt(n))return n;if("number"!=typeof t[2]&&(e=t.length),3r(a,e))&&(o[e]=n)}),o},_.once=function(n){var t,e;if(!mt(n))throw new oe;return function(){return t?e:(t=m,e=n.apply(this,arguments),n=d,e)}},_.pairs=function(n){for(var t=-1,e=We(n),r=e.length,u=Qt(r);++te?Be(0,r+e):Pe(e,r-1))+1);r--;)if(n[r]===t)return r;return-1},_.mixin=Ht,_.noConflict=function(){return r._=ce,this},_.parseInt=ir,_.random=Ut,_.reduce=It,_.reduceRight=Bt,_.result=function(n,t){var e=n?n[t]:y;return mt(e)?n[t]():e},_.runInContext=v,_.size=function(n){var t=n?n.length:0;return typeof t=="number"?t:We(n).length},_.some=Nt,_.sortedIndex=Lt,_.template=function(n,t,e){var r=_.templateSettings; +n||(n=""),e=Ze({},e,r);var u,o=Ze({},e.imports,r.imports),r=We(o),o=wt(o),a=0,f=e.interpolate||D,l="__p+='",f=re((e.escape||D).source+"|"+f.source+"|"+(f===R?P:D).source+"|"+(e.evaluate||D).source+"|$","g");n.replace(f,function(t,e,r,o,f,c){return r||(r=o),l+=n.slice(a,c).replace($,i),e&&(l+="'+__e("+e+")+'"),f&&(u=m,l+="';"+f+";__p+='"),r&&(l+="'+((__t=("+r+"))==null?'':__t)+'"),a=c+t.length,t}),l+="';\n",f=e=e.variable,f||(e="obj",l="with("+e+"){"+l+"}"),l=(u?l.replace(A,""):l).replace(I,"$1").replace(B,"$1;"),l="function("+e+"){"+(f?"":e+"||("+e+"={});")+"var __t,__p='',__e=_.escape"+(u?",__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}":";")+l+"return __p}"; +try{var c=Zt(r,"return "+l).apply(y,o)}catch(p){throw p.source=l,p}return t?c(t):(c.source=l,c)},_.unescape=function(n){return n==d?"":ue(n).replace(Ve,st)},_.uniqueId=function(n){var t=++j;return ue(n==d?"":n)+t},_.all=kt,_.any=Nt,_.detect=Ct,_.findWhere=Ct,_.foldl=It,_.foldr=Bt,_.include=jt,_.inject=It,tr(_,function(n,t){_.prototype[t]||(_.prototype[t]=function(){var t=[this.__wrapped__],e=this.__chain__;return de.apply(t,arguments),t=n.apply(_,t),e?new w(t,e):t})}),_.first=Dt,_.last=function(n,t,e){if(n){var r=0,u=n.length; +if(typeof t!="number"&&t!=d){var o=u;for(t=_.createCallback(t,e,3);o--&&t(n[o],o,n);)r++}else if(r=t,r==d||e)return n[u-1];return h(n,Be(0,u-r))}},_.sample=function(n,t,e){qe(n)||(n=Rt(n));var r=n.length-1;return t==d||e?n[Ut(r)]:(n=Pt(n),n.length=Pe(Be(0,t),n.length),n)},_.take=Dt,_.head=Dt,tr(_,function(n,t){var e="sample"!==t;_.prototype[t]||(_.prototype[t]=function(t,r){var u=this.__chain__,o=n(this.__wrapped__,t,r);return u||t!=d&&(!r||e&&typeof t=="function")?new w(o,u):o})}),_.VERSION="1.3.1",_.prototype.chain=function(){return this.__chain__=m,this +},_.prototype.toString=function(){return ue(this.__wrapped__)},_.prototype.value=Vt,_.prototype.valueOf=Vt,Xe(["join","pop","shift"],function(n){var t=ae[n];_.prototype[n]=function(){var n=this.__chain__,e=t.apply(this.__wrapped__,arguments);return n?new w(e,n):e}}),Xe(["push","reverse","sort","unshift"],function(n){var t=ae[n];_.prototype[n]=function(){return t.apply(this.__wrapped__,arguments),this}}),Xe(["concat","slice","splice"],function(n){var t=ae[n];_.prototype[n]=function(){return new w(t.apply(this.__wrapped__,arguments),this.__chain__) +}}),Te.spliceObjects||Xe(["pop","shift","splice"],function(n){var t=ae[n],e="splice"==n;_.prototype[n]=function(){var n=this.__chain__,r=this.__wrapped__,u=t.apply(r,arguments);return 0===r.length&&delete r[0],n||e?new w(u,n):u}}),_}var y,m=!0,d=null,b=!1,_=[],w=[],j=0,x={},C=+new Date+"",E=75,O=40,S=" \t\x0B\f\xa0\ufeff\n\r\u2028\u2029\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000",A=/\b__p\+='';/g,I=/\b(__p\+=)''\+/g,B=/(__e\(.*?\)|\b__t\))\+'';/g,P=/\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g,N=/\w*$/,R=/<%=([\s\S]+?)%>/g,F=RegExp("^["+S+"]*0+(?=.$)"),D=/($^)/,$=/['\n\r\t\u2028\u2029\\]/g,z="Array Boolean Date Error Function Math Number Object RegExp String _ attachEvent clearTimeout isFinite isNaN parseInt setImmediate setTimeout".split(" "),L="constructor hasOwnProperty isPrototypeOf propertyIsEnumerable toLocaleString toString valueOf".split(" "),T="[object Arguments]",q="[object Array]",K="[object Boolean]",W="[object Date]",G="[object Error]",J="[object Function]",M="[object Number]",H="[object Object]",U="[object RegExp]",V="[object String]",Q={}; Q[J]=b,Q[T]=Q[q]=Q[K]=Q[W]=Q[M]=Q[H]=Q[U]=Q[V]=m;var X={"boolean":b,"function":m,object:m,number:b,string:b,undefined:b},Y={"\\":"\\","'":"'","\n":"n","\r":"r","\t":"t","\u2028":"u2028","\u2029":"u2029"},Z=X[typeof exports]&&exports,nt=X[typeof module]&&module&&module.exports==Z&&module,tt=X[typeof global]&&global;!tt||tt.global!==tt&&tt.window!==tt||(n=tt);var et=v();typeof define=="function"&&typeof define.amd=="object"&&define.amd?(n._=et, define(function(){return et})):Z&&!Z.nodeType?nt?(nt.exports=et)._=et:Z._=et:n._=et }(this); \ No newline at end of file diff --git a/dist/lodash.js b/dist/lodash.js index 354a89b4e..f3b8372fa 100644 --- a/dist/lodash.js +++ b/dist/lodash.js @@ -3458,6 +3458,37 @@ }); } + /** + * Retrieves a random element or `n` random elements from a collection. + * + * @static + * @memberOf _ + * @category Collections + * @param {Array|Object|String} collection The collection to sample. + * @returns {Array} Returns the random sample(s) of `collection`. + * @example + * + * _.sample([1, 2, 3, 4]); + * // => 2 + * + * _.sample([1, 2, 3, 4], 2); + * // => [3, 1] + */ + function sample(collection, n, guard) { + if (!isArray(collection)) { + collection = toArray(collection); + } + var index = -1, + maxIndex = collection.length - 1; + + if (n == null || guard) { + return collection[random(maxIndex)]; + } + var result = shuffle(collection); + result.length = nativeMin(nativeMax(0, n), result.length); + return result; + } + /** * Creates an array of shuffled values, using a version of the Fisher-Yates * shuffle. See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle. @@ -3478,7 +3509,7 @@ result = Array(typeof length == 'number' ? length : 0); forEach(collection, function(value) { - var rand = floor(nativeRandom() * (++index + 1)); + var rand = random(++index); result[index] = result[rand]; result[rand] = value; }); @@ -3818,11 +3849,10 @@ } /** - * Gets the first element of an array. If a number `n` is provided the first - * `n` elements of the array are returned. If a callback is provided elements - * at the beginning of the array are returned as long as the callback returns - * truthy. The callback is bound to `thisArg` and invoked with three arguments; - * (value, index, array). + * Gets the first element or first `n` elements of an array. If a callback + * is provided elements at the beginning of the array are returned as long + * as the callback returns truthy. The callback is bound to `thisArg` and + * invoked with three arguments; (value, index, array). * * If a property name is provided for `callback` the created "_.pluck" style * callback will return the property value of the given element. @@ -3985,11 +4015,10 @@ } /** - * Gets all but the last element of an array. If a number `n` is provided - * the last `n` elements are excluded from the result. If a callback is - * provided elements at the end of the array are excluded from the result - * as long as the callback returns truthy. The callback is bound to `thisArg` - * and invoked with three arguments; (value, index, array). + * Gets all but the last element or last `n` elements of an array. If a + * callback is provided elements at the end of the array are excluded from + * the result as long as the callback returns truthy. The callback is bound + * to `thisArg` and invoked with three arguments; (value, index, array). * * If a property name is provided for `callback` the created "_.pluck" style * callback will return the property value of the given element. @@ -4119,12 +4148,10 @@ } /** - * Gets the last element of an array. If a number `n` is provided the last - * `n` elements of the array are returned. If a callback is provided elements - * at the end of the array are returned as long as the callback returns truthy. - * The callback is bound to `thisArg` and invoked with three arguments; - * (value, index, array). - * + * Gets the last element or last `n` elements of an array. If a callback is + * provided elements at the end of the array are returned as long as the + * callback returns truthy. The callback is bound to `thisArg` and invoked + * with three arguments; (value, index, array). * * If a property name is provided for `callback` the created "_.pluck" style * callback will return the property value of the given element. @@ -4368,12 +4395,11 @@ } /** - * The opposite of `_.initial` this method gets all but the first value of - * an array. If a number `n` is provided the first `n` values are excluded - * from the result. If a callback function is provided elements at the beginning - * of the array are excluded from the result as long as the callback returns - * truthy. The callback is bound to `thisArg` and invoked with three - * arguments; (value, index, array). + * The opposite of `_.initial` this method gets all but the first element or + * first `n` elements of an array. If a callback function is provided elements + * at the beginning of the array are excluded from the result as long as the + * callback returns truthy. The callback is bound to `thisArg` and invoked + * with three arguments; (value, index, array). * * If a property name is provided for `callback` the created "_.pluck" style * callback will return the property value of the given element. @@ -6070,18 +6096,20 @@ // add functions capable of returning wrapped and unwrapped values when chaining lodash.first = first; lodash.last = last; + lodash.sample = sample; // add aliases lodash.take = first; lodash.head = first; forOwn(lodash, function(func, methodName) { + var callbackable = methodName !== 'sample'; if (!lodash.prototype[methodName]) { - lodash.prototype[methodName]= function(callback, thisArg) { + lodash.prototype[methodName]= function(n, guard) { var chainAll = this.__chain__, - result = func(this.__wrapped__, callback, thisArg); + result = func(this.__wrapped__, n, guard); - return !chainAll && (callback == null || (thisArg && typeof callback != 'function')) + return !chainAll && (n == null || (guard && !(callbackable && typeof n == 'function'))) ? result : new lodashWrapper(result, chainAll); }; diff --git a/dist/lodash.min.js b/dist/lodash.min.js index fd6e79fff..afb6f3674 100644 --- a/dist/lodash.min.js +++ b/dist/lodash.min.js @@ -4,48 +4,49 @@ * Build: `lodash modern -o ./dist/lodash.js` */ ;!function(n){function t(n,t,r){r=(r||0)-1;for(var e=n?n.length:0;++re||typeof r=="undefined")return 1;if(rr?0:r);++e=k&&a===t,h=u||v?f():l;if(v){var g=i(h);g?(a=r,h=g):(v=_,h=u?h:(p(h),l)) -}for(;++oa(h,y))&&((u||v)&&h.push(y),l.push(g))}return v?(p(h.b),s(h)):u&&p(h),l}function at(n){return function(t,r,e){var u={};return r=Z.createCallback(r,e,3),Ot(t,function(t,e,o){e=rr(r(t,e,o)),n(u,t,e,o)}),u}}function ft(n,t,r,e,u,o){var i=1&t,a=2&t,f=4&t,c=8&t,l=16&t,p=32&t;if(!a&&!mt(n))throw new er;var s=n&&n.__bindData__;if(s)return i&&!(1&s[1])&&(s[4]=u),!i&&1&s[1]&&(t|=8),f&&!(4&s[1])&&(s[5]=o),l&&yr.apply(s[2]||(s[2]=[]),r),p&&yr.apply(s[3]||(s[3]=[]),e),s[1]|=t,ft.apply(m,s); -if(!i||a||f||p||!(Fr.fastBind||jr&&r.length))v=function(){var l=arguments,p=i?u:this;return r&&wr.apply(l,r),e&&yr.apply(l,e),f&&l.lengthr?Ir(0,o+r):r)||0,o&&typeof o=="number"?i=-1<(dt(n)?n.indexOf(t,r):u(n,t,r)):d(n,function(n){return++ee||typeof r=="undefined")return 1;if(rr?0:r);++e=k&&i===t,h=u||v?f():l;if(v){var g=a(h);g?(i=r,h=g):(v=_,h=u?h:(p(h),l)) +}for(;++oi(h,y))&&((u||v)&&h.push(y),l.push(g))}return v?(p(h.b),s(h)):u&&p(h),l}function it(n){return function(t,r,e){var u={};return r=Z.createCallback(r,e,3),Ot(t,function(t,e,o){e=or(r(t,e,o)),n(u,t,e,o)}),u}}function ft(n,t,r,e,u,o){var a=1&t,i=2&t,f=4&t,c=8&t,l=16&t,p=32&t;if(!i&&!mt(n))throw new ar;var s=n&&n.__bindData__;if(s)return a&&!(1&s[1])&&(s[4]=u),!a&&1&s[1]&&(t|=8),f&&!(4&s[1])&&(s[5]=o),l&&br.apply(s[2]||(s[2]=[]),r),p&&br.apply(s[3]||(s[3]=[]),e),s[1]|=t,ft.apply(m,s); +if(!a||i||f||p||!(qr.fastBind||Cr&&r.length))v=function(){var l=arguments,p=a?u:this;return r&&xr.apply(l,r),e&&br.apply(l,e),f&&l.lengthr?Nr(0,o+r):r)||0,o&&typeof o=="number"?a=-1<(dt(n)?n.indexOf(t,r):u(n,t,r)):d(n,function(n){return++eo&&(o=a)}}else t=!t&&dt(n)?u:Z.createCallback(t,r,3),Ot(n,function(n,r,u){r=t(n,r,u),r>e&&(e=r,o=n) -});return o}function At(n,t){var r=-1,e=n?n.length:0;if(typeof e=="number")for(var u=Ht(e);++rarguments.length;t=rt(t,e,4);var o=-1,i=n.length;if(typeof i=="number")for(u&&(r=n[++o]);++oarguments.length;return t=rt(t,e,4),Et(n,function(n,e,o){r=u?(u=_,n):t(r,n,e,o)}),r}function Bt(n,t,r){var e;t=Z.createCallback(t,r,3),r=-1; -var u=n?n.length:0;if(typeof u=="number")for(;++r=k&&u===t;if(c){var l=i(a);l?(u=r,a=l):c=_}for(;++eu(a,l)&&f.push(l);return c&&s(a),f}function Dt(n,t,r){if(n){var e=0,u=n.length;if(typeof t!="number"&&t!=m){var o=-1;for(t=Z.createCallback(t,r,3);++oe?Ir(0,u+e):e||0}else if(e)return e=zt(n,r),n[e]===r?e:-1;return n?t(n,r,e):-1}function Tt(n,t,r){if(typeof t!="number"&&t!=m){var e=0,u=-1,o=n?n.length:0;for(t=Z.createCallback(t,r,3);++u>>1,r(n[e])r?0:r);++tc&&(a=n.apply(f,i));else{var r=gr();!s&&!g&&(l=r);var o=p-(r-l);0/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:N,variable:"",imports:{_:Z}}; -var Tr=lr?function(n,t){var r=c();r.value=t,lr(n,"__bindData__",r),s(r)}:l,zr=xr||function(n){return n&&typeof n=="object"?dr.call(n)==z:_},qr=Er?function(n){return _t(n)?Er(n):[]}:X,Wr={"&":"&","<":"<",">":">",'"':""","'":"'"},Pr=yt(Wr),Kr=tr("("+qr(Pr).join("|")+")","g"),Lr=tr("["+qr(Wr).join("")+"]","g"),Mr=at(function(n,t,r){hr.call(n,r)?n[r]++:n[r]=1}),Ur=at(function(n,t,r){(hr.call(n,r)?n[r]:n[r]=[]).push(t)}),Vr=at(function(n,t,r){n[r]=t});$r&&Q&&typeof mr=="function"&&(Mt=function(n){if(!mt(n))throw new er; -return mr.apply(e,arguments)});var Gr=8==Ar(C+"08")?Ar:function(n,t){return Ar(dt(n)?n.replace(R,""):n,t||0)};return Z.after=function(n,t){if(!mt(t))throw new er;return function(){return 1>--n?t.apply(this,arguments):void 0}},Z.assign=J,Z.at=function(n){for(var t=arguments,r=-1,e=et(t,y,_,1),t=t[2]&&t[2][t[1]]===n?1:e.length,u=Ht(t);++r=k&&i(o?e[o]:g)}n:for(;++c(m?r(m,y):l(g,y))){for(o=u,(m||g).push(y);--o;)if(m=a[o],0>(m?r(m,y):l(e[o],y)))continue n;h.push(y)}}for(;u--;)(m=a[u])&&s(m);return p(a),p(g),h},Z.invert=yt,Z.invoke=function(n,t){var r=Rr.call(arguments,2),e=-1,u=typeof t=="function",o=n?n.length:0,i=Ht(typeof o=="number"?o:0);return Ot(n,function(n){i[++e]=(u?t:n[t]).apply(n,r)}),i},Z.keys=qr,Z.map=It,Z.max=St,Z.memoize=function(n,t){function r(){var e=r.cache,u=j+(t?t.apply(this,arguments):arguments[0]); -return hr.call(e,u)?e[u]:e[u]=n.apply(this,arguments)}if(!mt(n))throw new er;return r.cache={},r},Z.merge=function(n){var t=arguments,r=2;if(!_t(n))return n;if("number"!=typeof t[2]&&(r=t.length),3e(i,r))&&(o[r]=n)}),o},Z.once=function(n){var t,r;if(!mt(n))throw new er;return function(){return t?r:(t=y,r=n.apply(this,arguments),n=m,r)}},Z.pairs=function(n){for(var t=-1,r=qr(n),e=r.length,u=Ht(e);++tr?Ir(0,e+r):Sr(r,e-1))+1);e--;)if(n[e]===t)return e;return-1},Z.mixin=Vt,Z.noConflict=function(){return e._=ir,this},Z.parseInt=Gr,Z.random=function(n,t){n==m&&t==m&&(t=1),n=+n||0,t==m?(t=n,n=0):t=+t||0;var r=Nr();return n%1||t%1?n+Sr(r*(t-n+parseFloat("1e-"+((r+"").length-1))),t):n+pr(r*(t-n+1))},Z.reduce=Nt,Z.reduceRight=Rt,Z.result=function(n,t){var r=n?n[t]:g; -return mt(r)?n[t]():r},Z.runInContext=h,Z.size=function(n){var t=n?n.length:0;return typeof t=="number"?t:qr(n).length},Z.some=Bt,Z.sortedIndex=zt,Z.template=function(n,t,r){var e=Z.templateSettings;n||(n=""),r=H({},r,e);var u,o=H({},r.imports,e.imports),e=qr(o),o=wt(o),i=0,f=r.interpolate||B,c="__p+='",f=tr((r.escape||B).source+"|"+f.source+"|"+(f===N?S:B).source+"|"+(r.evaluate||B).source+"|$","g");n.replace(f,function(t,r,e,o,f,l){return e||(e=o),c+=n.slice(i,l).replace(D,a),r&&(c+="'+__e("+r+")+'"),f&&(u=y,c+="';"+f+";__p+='"),e&&(c+="'+((__t=("+e+"))==null?'':__t)+'"),i=l+t.length,t -}),c+="';\n",f=r=r.variable,f||(r="obj",c="with("+r+"){"+c+"}"),c=(u?c.replace(O,""):c).replace(E,"$1").replace(I,"$1;"),c="function("+r+"){"+(f?"":r+"||("+r+"={});")+"var __t,__p='',__e=_.escape"+(u?",__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}":";")+c+"return __p}";try{var l=Xt(e,"return "+c).apply(g,o)}catch(p){throw p.source=c,p}return t?l(t):(l.source=c,l)},Z.unescape=function(n){return n==m?"":rr(n).replace(Kr,st)},Z.uniqueId=function(n){var t=++w;return rr(n==m?"":n)+t -},Z.all=kt,Z.any=Bt,Z.detect=Ct,Z.findWhere=Ct,Z.foldl=Nt,Z.foldr=Rt,Z.include=jt,Z.inject=Nt,d(Z,function(n,t){Z.prototype[t]||(Z.prototype[t]=function(){var t=[this.__wrapped__],r=this.__chain__;return yr.apply(t,arguments),t=n.apply(Z,t),r?new nt(t,r):t})}),Z.first=Dt,Z.last=function(n,t,r){if(n){var e=0,u=n.length;if(typeof t!="number"&&t!=m){var o=u;for(t=Z.createCallback(t,r,3);o--&&t(n[o],o,n);)e++}else if(e=t,e==m||r)return n[u-1];return v(n,Ir(0,u-e))}},Z.take=Dt,Z.head=Dt,d(Z,function(n,t){Z.prototype[t]||(Z.prototype[t]=function(t,r){var e=this.__chain__,u=n(this.__wrapped__,t,r); -return!e&&(t==m||r&&typeof t!="function")?u:new nt(u,e)})}),Z.VERSION="1.3.1",Z.prototype.chain=function(){return this.__chain__=y,this},Z.prototype.toString=function(){return rr(this.__wrapped__)},Z.prototype.value=Gt,Z.prototype.valueOf=Gt,Ot(["join","pop","shift"],function(n){var t=ur[n];Z.prototype[n]=function(){var n=this.__chain__,r=t.apply(this.__wrapped__,arguments);return n?new nt(r,n):r}}),Ot(["push","reverse","sort","unshift"],function(n){var t=ur[n];Z.prototype[n]=function(){return t.apply(this.__wrapped__,arguments),this -}}),Ot(["concat","slice","splice"],function(n){var t=ur[n];Z.prototype[n]=function(){return new nt(t.apply(this.__wrapped__,arguments),this.__chain__)}}),Z}var g,y=!0,m=null,_=!1,b=[],d=[],w=0,j=+new Date+"",k=75,x=40,C=" \t\x0B\f\xa0\ufeff\n\r\u2028\u2029\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000",O=/\b__p\+='';/g,E=/\b(__p\+=)''\+/g,I=/(__e\(.*?\)|\b__t\))\+'';/g,S=/\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g,A=/\w*$/,N=/<%=([\s\S]+?)%>/g,R=RegExp("^["+C+"]*0+(?=.$)"),B=/($^)/,$=($=/\bthis\b/)&&$.test(h)&&$,D=/['\n\r\t\u2028\u2029\\]/g,F="Array Boolean Date Function Math Number Object RegExp String _ attachEvent clearTimeout isFinite isNaN parseInt setImmediate setTimeout".split(" "),T="[object Arguments]",z="[object Array]",q="[object Boolean]",W="[object Date]",P="[object Function]",K="[object Number]",L="[object Object]",M="[object RegExp]",U="[object String]",V={}; +return n}function Et(n,t,r){var e=n?n.length:0;if(typeof e!="number")var u=Kr(n),e=u.length;return t=rt(t,r,3),Ot(n,function(r,o,a){return o=u?u[--e]:--e,t(n[o],o,a)}),n}function It(n,t,r){var e=-1,u=n?n.length:0;if(t=Z.createCallback(t,r,3),typeof u=="number")for(var o=Xt(u);++eo&&(o=i)}}else t=!t&&dt(n)?u:Z.createCallback(t,r,3),Ot(n,function(n,r,u){r=t(n,r,u),r>e&&(e=r,o=n) +});return o}function At(n,t){var r=-1,e=n?n.length:0;if(typeof e=="number")for(var u=Xt(e);++rarguments.length;t=rt(t,e,4);var o=-1,a=n.length;if(typeof a=="number")for(u&&(r=n[++o]);++oarguments.length;return t=rt(t,e,4),Et(n,function(n,e,o){r=u?(u=_,n):t(r,n,e,o)}),r}function Bt(n){var t=-1,r=n?n.length:0,e=Xt(typeof r=="number"?r:0); +return Ot(n,function(n){var r=Jt(++t);e[t]=e[r],e[r]=n}),e}function $t(n,t,r){var e;t=Z.createCallback(t,r,3),r=-1;var u=n?n.length:0;if(typeof u=="number")for(;++r=k&&u===t;if(c){var l=a(i);l?(u=r,i=l):c=_}for(;++eu(i,l)&&f.push(l);return c&&s(i),f}function Tt(n,t,r){if(n){var e=0,u=n.length; +if(typeof t!="number"&&t!=m){var o=-1;for(t=Z.createCallback(t,r,3);++oe?Nr(0,u+e):e||0}else if(e)return e=Wt(n,r),n[e]===r?e:-1;return n?t(n,r,e):-1}function qt(n,t,r){if(typeof t!="number"&&t!=m){var e=0,u=-1,o=n?n.length:0;for(t=Z.createCallback(t,r,3);++u>>1,r(n[e])r?0:r);++tc&&(i=n.apply(f,a));else{var r=_r();!s&&!g&&(l=r);var o=p-(r-l);0/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:N,variable:"",imports:{_:Z}}; +var Wr=vr?function(n,t){var r=c();r.value=t,vr(n,"__bindData__",r),s(r)}:l,Pr=Er||function(n){return n&&typeof n=="object"?kr.call(n)==z:_},Kr=Ar?function(n){return _t(n)?Ar(n):[]}:X,Lr={"&":"&","<":"<",">":">",'"':""","'":"'"},Mr=yt(Lr),Ur=ur("("+Kr(Mr).join("|")+")","g"),Vr=ur("["+Kr(Lr).join("")+"]","g"),Gr=it(function(n,t,r){mr.call(n,r)?n[r]++:n[r]=1}),Hr=it(function(n,t,r){(mr.call(n,r)?n[r]:n[r]=[]).push(t)}),Jr=it(function(n,t,r){n[r]=t});Tr&&Q&&typeof dr=="function"&&(Vt=function(n){if(!mt(n))throw new ar; +return dr.apply(e,arguments)});var Qr=8==Br(C+"08")?Br:function(n,t){return Br(dt(n)?n.replace(R,""):n,t||0)};return Z.after=function(n,t){if(!mt(t))throw new ar;return function(){return 1>--n?t.apply(this,arguments):void 0}},Z.assign=J,Z.at=function(n){for(var t=arguments,r=-1,e=et(t,y,_,1),t=t[2]&&t[2][t[1]]===n?1:e.length,u=Xt(t);++r=k&&a(o?e[o]:g)}n:for(;++c(m?r(m,y):l(g,y))){for(o=u,(m||g).push(y);--o;)if(m=i[o],0>(m?r(m,y):l(e[o],y)))continue n;h.push(y)}}for(;u--;)(m=i[u])&&s(m);return p(i),p(g),h},Z.invert=yt,Z.invoke=function(n,t){var r=Dr.call(arguments,2),e=-1,u=typeof t=="function",o=n?n.length:0,a=Xt(typeof o=="number"?o:0);return Ot(n,function(n){a[++e]=(u?t:n[t]).apply(n,r)}),a},Z.keys=Kr,Z.map=It,Z.max=St,Z.memoize=function(n,t){function r(){var e=r.cache,u=j+(t?t.apply(this,arguments):arguments[0]); +return mr.call(e,u)?e[u]:e[u]=n.apply(this,arguments)}if(!mt(n))throw new ar;return r.cache={},r},Z.merge=function(n){var t=arguments,r=2;if(!_t(n))return n;if("number"!=typeof t[2]&&(r=t.length),3e(a,r))&&(o[r]=n)}),o},Z.once=function(n){var t,r;if(!mt(n))throw new ar;return function(){return t?r:(t=y,r=n.apply(this,arguments),n=m,r)}},Z.pairs=function(n){for(var t=-1,r=Kr(n),e=r.length,u=Xt(e);++tr?Nr(0,e+r):Rr(r,e-1))+1);e--;)if(n[e]===t)return e;return-1},Z.mixin=Ht,Z.noConflict=function(){return e._=cr,this},Z.parseInt=Qr,Z.random=Jt,Z.reduce=Nt,Z.reduceRight=Rt,Z.result=function(n,t){var r=n?n[t]:g;return mt(r)?n[t]():r},Z.runInContext=h,Z.size=function(n){var t=n?n.length:0;return typeof t=="number"?t:Kr(n).length},Z.some=$t,Z.sortedIndex=Wt,Z.template=function(n,t,r){var e=Z.templateSettings; +n||(n=""),r=H({},r,e);var u,o=H({},r.imports,e.imports),e=Kr(o),o=wt(o),a=0,f=r.interpolate||B,c="__p+='",f=ur((r.escape||B).source+"|"+f.source+"|"+(f===N?S:B).source+"|"+(r.evaluate||B).source+"|$","g");n.replace(f,function(t,r,e,o,f,l){return e||(e=o),c+=n.slice(a,l).replace(D,i),r&&(c+="'+__e("+r+")+'"),f&&(u=y,c+="';"+f+";__p+='"),e&&(c+="'+((__t=("+e+"))==null?'':__t)+'"),a=l+t.length,t}),c+="';\n",f=r=r.variable,f||(r="obj",c="with("+r+"){"+c+"}"),c=(u?c.replace(O,""):c).replace(E,"$1").replace(I,"$1;"),c="function("+r+"){"+(f?"":r+"||("+r+"={});")+"var __t,__p='',__e=_.escape"+(u?",__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}":";")+c+"return __p}"; +try{var l=nr(e,"return "+c).apply(g,o)}catch(p){throw p.source=c,p}return t?l(t):(l.source=c,l)},Z.unescape=function(n){return n==m?"":or(n).replace(Ur,st)},Z.uniqueId=function(n){var t=++w;return or(n==m?"":n)+t},Z.all=kt,Z.any=$t,Z.detect=Ct,Z.findWhere=Ct,Z.foldl=Nt,Z.foldr=Rt,Z.include=jt,Z.inject=Nt,d(Z,function(n,t){Z.prototype[t]||(Z.prototype[t]=function(){var t=[this.__wrapped__],r=this.__chain__;return br.apply(t,arguments),t=n.apply(Z,t),r?new nt(t,r):t})}),Z.first=Tt,Z.last=function(n,t,r){if(n){var e=0,u=n.length; +if(typeof t!="number"&&t!=m){var o=u;for(t=Z.createCallback(t,r,3);o--&&t(n[o],o,n);)e++}else if(e=t,e==m||r)return n[u-1];return v(n,Nr(0,u-e))}},Z.sample=function(n,t,r){Pr(n)||(n=Dt(n));var e=n.length-1;return t==m||r?n[Jt(e)]:(n=Bt(n),n.length=Rr(Nr(0,t),n.length),n)},Z.take=Tt,Z.head=Tt,d(Z,function(n,t){var r="sample"!==t;Z.prototype[t]||(Z.prototype[t]=function(t,e){var u=this.__chain__,o=n(this.__wrapped__,t,e);return u||t!=m&&(!e||r&&typeof t=="function")?new nt(o,u):o})}),Z.VERSION="1.3.1",Z.prototype.chain=function(){return this.__chain__=y,this +},Z.prototype.toString=function(){return or(this.__wrapped__)},Z.prototype.value=Qt,Z.prototype.valueOf=Qt,Ot(["join","pop","shift"],function(n){var t=ir[n];Z.prototype[n]=function(){var n=this.__chain__,r=t.apply(this.__wrapped__,arguments);return n?new nt(r,n):r}}),Ot(["push","reverse","sort","unshift"],function(n){var t=ir[n];Z.prototype[n]=function(){return t.apply(this.__wrapped__,arguments),this}}),Ot(["concat","slice","splice"],function(n){var t=ir[n];Z.prototype[n]=function(){return new nt(t.apply(this.__wrapped__,arguments),this.__chain__) +}}),Z}var g,y=!0,m=null,_=!1,b=[],d=[],w=0,j=+new Date+"",k=75,x=40,C=" \t\x0B\f\xa0\ufeff\n\r\u2028\u2029\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000",O=/\b__p\+='';/g,E=/\b(__p\+=)''\+/g,I=/(__e\(.*?\)|\b__t\))\+'';/g,S=/\$\{([^\\}]*(?:\\.[^\\}]*)*)\}/g,A=/\w*$/,N=/<%=([\s\S]+?)%>/g,R=RegExp("^["+C+"]*0+(?=.$)"),B=/($^)/,$=($=/\bthis\b/)&&$.test(h)&&$,D=/['\n\r\t\u2028\u2029\\]/g,F="Array Boolean Date Function Math Number Object RegExp String _ attachEvent clearTimeout isFinite isNaN parseInt setImmediate setTimeout".split(" "),T="[object Arguments]",z="[object Array]",q="[object Boolean]",W="[object Date]",P="[object Function]",K="[object Number]",L="[object Object]",M="[object RegExp]",U="[object String]",V={}; V[P]=_,V[T]=V[z]=V[q]=V[W]=V[K]=V[L]=V[M]=V[U]=y;var G={"boolean":_,"function":y,object:y,number:_,string:_,undefined:_},H={"\\":"\\","'":"'","\n":"n","\r":"r","\t":"t","\u2028":"u2028","\u2029":"u2029"},J=G[typeof exports]&&exports,Q=G[typeof module]&&module&&module.exports==J&&module,X=G[typeof global]&&global;!X||X.global!==X&&X.window!==X||(n=X);var Y=h();typeof define=="function"&&typeof define.amd=="object"&&define.amd?(n._=Y, define(function(){return Y})):J&&!J.nodeType?Q?(Q.exports=Y)._=Y:J._=Y:n._=Y }(this); \ No newline at end of file diff --git a/dist/lodash.underscore.js b/dist/lodash.underscore.js index 9b12e3eaa..70cdc32dd 100644 --- a/dist/lodash.underscore.js +++ b/dist/lodash.underscore.js @@ -2401,7 +2401,7 @@ result = Array(typeof length == 'number' ? length : 0); forEach(collection, function(value) { - var rand = floor(nativeRandom() * (++index + 1)); + var rand = random(++index); result[index] = result[rand]; result[rand] = value; }); @@ -2669,11 +2669,10 @@ } /** - * Gets the first element of an array. If a number `n` is provided the first - * `n` elements of the array are returned. If a callback is provided elements - * at the beginning of the array are returned as long as the callback returns - * truthy. The callback is bound to `thisArg` and invoked with three arguments; - * (value, index, array). + * Gets the first element or first `n` elements of an array. If a callback + * is provided elements at the beginning of the array are returned as long + * as the callback returns truthy. The callback is bound to `thisArg` and + * invoked with three arguments; (value, index, array). * * If a property name is provided for `callback` the created "_.pluck" style * callback will return the property value of the given element. @@ -2827,11 +2826,10 @@ } /** - * Gets all but the last element of an array. If a number `n` is provided - * the last `n` elements are excluded from the result. If a callback is - * provided elements at the end of the array are excluded from the result - * as long as the callback returns truthy. The callback is bound to `thisArg` - * and invoked with three arguments; (value, index, array). + * Gets all but the last element or last `n` elements of an array. If a + * callback is provided elements at the end of the array are excluded from + * the result as long as the callback returns truthy. The callback is bound + * to `thisArg` and invoked with three arguments; (value, index, array). * * If a property name is provided for `callback` the created "_.pluck" style * callback will return the property value of the given element. @@ -2940,12 +2938,10 @@ } /** - * Gets the last element of an array. If a number `n` is provided the last - * `n` elements of the array are returned. If a callback is provided elements - * at the end of the array are returned as long as the callback returns truthy. - * The callback is bound to `thisArg` and invoked with three arguments; - * (value, index, array). - * + * Gets the last element or last `n` elements of an array. If a callback is + * provided elements at the end of the array are returned as long as the + * callback returns truthy. The callback is bound to `thisArg` and invoked + * with three arguments; (value, index, array). * * If a property name is provided for `callback` the created "_.pluck" style * callback will return the property value of the given element. @@ -3104,12 +3100,11 @@ } /** - * The opposite of `_.initial` this method gets all but the first value of - * an array. If a number `n` is provided the first `n` values are excluded - * from the result. If a callback function is provided elements at the beginning - * of the array are excluded from the result as long as the callback returns - * truthy. The callback is bound to `thisArg` and invoked with three - * arguments; (value, index, array). + * The opposite of `_.initial` this method gets all but the first element or + * first `n` elements of an array. If a callback function is provided elements + * at the beginning of the array are excluded from the result as long as the + * callback returns truthy. The callback is bound to `thisArg` and invoked + * with three arguments; (value, index, array). * * If a property name is provided for `callback` the created "_.pluck" style * callback will return the property value of the given element. diff --git a/dist/lodash.underscore.min.js b/dist/lodash.underscore.min.js index d04bece9b..b428d273c 100644 --- a/dist/lodash.underscore.min.js +++ b/dist/lodash.underscore.min.js @@ -3,35 +3,35 @@ * Lo-Dash 1.3.1 (Custom Build) lodash.com/license | Underscore.js 1.5.1 underscorejs.org/LICENSE * Build: `lodash underscore exports="amd,commonjs,global,node" -o ./dist/lodash.underscore.js` */ -;!function(n){function r(n,r,t){t=(t||0)-1;for(var e=n?n.length:0;++te||typeof t=="undefined")return 1;if(tu(a,l))&&(t&&a.push(l),o.push(f))}return o}function p(n){return function(r,t,e){var u={};return t=K(t,e,3),B(r,function(r,e,i){e=t(r,e,i)+"",n(u,r,e,i)}),u}}function s(n,r,t,e,u,i){var o=1&r,a=2&r,f=4&r,l=8&r,c=32&r; -if(!a&&!x(n))throw new TypeError;if(!o||a||f||c||!(Wr.fastBind||Or&&t.length))p=function(){var c=arguments,g=o?u:this;return t&&Tr.apply(c,t),e&&Er.apply(c,e),f&&c.lengthe&&(e=t,u=n)});else for(;++iu&&(u=t);return u}function M(n,r){var t=-1,e=n?n.length:0;if(typeof e=="number")for(var u=Array(e);++targuments.length;r=a(r,e,4);var i=-1,o=n.length;if(typeof o=="number")for(u&&(t=n[++i]);++iarguments.length;return r=a(r,e,4),k(n,function(n,e,i){t=u?(u=!1,n):r(t,n,e,i)}),t}function W(n,r,t){var e;r=K(r,t,3),t=-1;var u=n?n.length:0;if(typeof u=="number")for(;++tt(u,o)&&i.push(o)}return i}function P(n,r,t){if(n){var e=0,u=n.length; -if(typeof r!="number"&&null!=r){var i=-1;for(r=K(r,t,3);++ie?kr(0,u+e):e||0}else if(e)return e=G(n,t),n[e]===t?e:-1;return n?r(n,t,e):-1}function V(n,r,t){if(typeof r!="number"&&null!=r){var e=0,u=-1,i=n?n.length:0;for(r=K(r,t,3);++u>>1,t(n[e])o&&(u=n.apply(i,e));else{var t=+new Date;!l&&!s&&(a=t);var p=f-(t-a);0/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,variable:""},Sr||(v=function(n){if(E(n)){u.prototype=n;var r=new u;u.prototype=null}return r||{}}),m(arguments)||(m=function(n){return n&&typeof n=="object"?xr.call(n,"callee"):!1});var zr=Fr||function(n){return n&&typeof n=="object"?Ar.call(n)==ir:!1},Cr=function(n){var r,t=[]; -if(!n||!sr[typeof n])return t;for(r in n)xr.call(n,r)&&t.push(r);return t},Pr=Br?function(n){return E(n)?Br(n):[]}:Cr,Ur={"&":"&","<":"<",">":">",'"':""","'":"'","/":"/"},Vr=w(Ur),Gr=RegExp("("+Pr(Vr).join("|")+")","g"),Hr=RegExp("["+Pr(Ur).join("")+"]","g"),Jr=function(n,r){var t;if(!n||!sr[typeof n])return n;for(t in n)if(r(n[t],t,n)===nr)break;return n},Kr=function(n,r){var t;if(!n||!sr[typeof n])return n;for(t in n)if(xr.call(n,t)&&r(n[t],t,n)===nr)break;return n};x(/x/)&&(x=function(n){return typeof n=="function"&&"[object Function]"==Ar.call(n) -});var Lr=p(function(n,r,t){xr.call(n,t)?n[t]++:n[t]=1}),Qr=p(function(n,r,t){(xr.call(n,t)?n[t]:n[t]=[]).push(r)});i.after=function(n,r){if(!x(r))throw new TypeError;return function(){return 1>--n?r.apply(this,arguments):void 0}},i.bind=J,i.bindAll=function(n){for(var r=1u(o,a)){for(var f=t;--f;)if(0>u(r[f],a))continue n;o.push(a)}}return o},i.invert=w,i.invoke=function(n,r){var t=Mr.call(arguments,2),e=-1,u=typeof r=="function",i=n?n.length:0,o=Array(typeof i=="number"?i:0); -return B(n,function(n){o[++e]=(u?r:n[r]).apply(n,t)}),o},i.keys=Pr,i.map=D,i.max=q,i.memoize=function(n,r){var t={};return function(){var e=rr+(r?r.apply(this,arguments):arguments[0]);return xr.call(t,e)?t[e]:t[e]=n.apply(this,arguments)}},i.min=function(n,r,t){var e=1/0,u=e,i=-1,o=n?n.length:0;if(r||typeof o!="number")r=K(r,t,3),B(n,function(n,t,i){t=r(n,t,i),tr(t,u)&&(e[u]=n) -}),e},i.once=function(n){var r,t;if(!x(n))throw new TypeError;return function(){return r?t:(r=!0,t=n.apply(this,arguments),n=null,t)}},i.pairs=function(n){for(var r=-1,t=Pr(n),e=t.length,u=Array(e);++rr?0:r);++nt?kr(0,e+t):Dr(t,e-1))+1);e--;)if(n[e]===r)return e;return-1},i.mixin=X,i.noConflict=function(){return n._=dr,this},i.random=function(n,r){null==n&&null==r&&(r=1),n=+n||0,null==r?(r=n,n=0):r=+r||0;var t=qr();return n%1||r%1?n+Dr(t*(r-n+parseFloat("1e-"+((t+"").length-1))),r):n+jr(t*(r-n+1)) -},i.reduce=$,i.reduceRight=I,i.result=function(n,r){var t=n?n[r]:Y;return x(t)?n[r]():t},i.size=function(n){var r=n?n.length:0;return typeof r=="number"?r:Pr(n).length},i.some=W,i.sortedIndex=G,i.template=function(n,r,t){var u=i,o=u.templateSettings;n||(n=""),t=d({},t,o);var a=0,f="__p+='",o=t.variable;n.replace(RegExp((t.escape||tr).source+"|"+(t.interpolate||tr).source+"|"+(t.evaluate||tr).source+"|$","g"),function(r,t,u,i,o){return f+=n.slice(a,o).replace(er,e),t&&(f+="'+_.escape("+t+")+'"),i&&(f+="';"+i+";__p+='"),u&&(f+="'+((__t=("+u+"))==null?'':__t)+'"),a=o+r.length,r -}),f+="';\n",o||(o="obj",f="with("+o+"||{}){"+f+"}"),f="function("+o+"){var __t,__p='',__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}"+f+"return __p}";try{var l=Function("_","return "+f)(u)}catch(c){throw c.source=f,c}return r?l(r):(l.source=f,l)},i.unescape=function(n){return null==n?"":(n+"").replace(Gr,y)},i.uniqueId=function(n){var r=++Z+"";return n?n+r:r},i.all=F,i.any=W,i.detect=R,i.findWhere=function(n,r){return z(n,r,!0)},i.foldl=$,i.foldr=I,i.include=S,i.inject=$,i.first=P,i.last=function(n,r,t){if(n){var e=0,u=n.length; -if(typeof r!="number"&&null!=r){var i=u;for(r=K(r,t,3);i--&&r(n[i],i,n);)e++}else if(e=r,null==e||t)return n[u-1];return Mr.call(n,kr(0,u-e))}},i.take=P,i.head=P,X(i),i.VERSION="1.3.1",i.prototype.chain=function(){return this.__chain__=!0,this},i.prototype.value=function(){return this.__wrapped__},B("pop push reverse shift sort splice unshift".split(" "),function(n){var r=mr[n];i.prototype[n]=function(){var n=this.__wrapped__;return r.apply(n,arguments),!Wr.spliceObjects&&0===n.length&&delete n[0],this -}}),B(["concat","join","slice"],function(n){var r=mr[n];i.prototype[n]=function(){var n=r.apply(this.__wrapped__,arguments);return this.__chain__&&(n=new o(n),n.__chain__=!0),n}}),typeof define=="function"&&typeof define.amd=="object"&&define.amd?(n._=i, define(function(){return i})):hr&&!hr.nodeType?gr?(gr.exports=i)._=i:hr._=i:n._=i}(this); \ No newline at end of file +;!function(n){function r(n,r,t){t=(t||0)-1;for(var e=n?n.length:0;++te||typeof t=="undefined")return 1;if(tu(a,l))&&(t&&a.push(l),o.push(f))}return o}function p(n){return function(r,t,e){var u={};return t=K(t,e,3),B(r,function(r,e,i){e=t(r,e,i)+"",n(u,r,e,i)}),u}}function s(n,r,t,e,u,i){var o=1&r,a=2&r,f=4&r,l=8&r,c=32&r; +if(!a&&!x(n))throw new TypeError;if(!o||a||f||c||!(zr.fastBind||Sr&&t.length))p=function(){var c=arguments,g=o?u:this;return t&&Or.apply(c,t),e&&Ar.apply(c,e),f&&c.lengthe&&(e=t,u=n)});else for(;++iu&&(u=t);return u}function M(n,r){var t=-1,e=n?n.length:0;if(typeof e=="number")for(var u=Array(e);++targuments.length;r=a(r,e,4);var i=-1,o=n.length;if(typeof o=="number")for(u&&(t=n[++i]);++iarguments.length;return r=a(r,e,4),k(n,function(n,e,i){t=u?(u=!1,n):r(t,n,e,i)}),t}function W(n,r,t){var e;r=K(r,t,3),t=-1;var u=n?n.length:0;if(typeof u=="number")for(;++tt(u,o)&&i.push(o)}return i}function P(n,r,t){if(n){var e=0,u=n.length; +if(typeof r!="number"&&null!=r){var i=-1;for(r=K(r,t,3);++ie?Dr(0,u+e):e||0}else if(e)return e=G(n,t),n[e]===t?e:-1;return n?r(n,t,e):-1}function V(n,r,t){if(typeof r!="number"&&null!=r){var e=0,u=-1,i=n?n.length:0;for(r=K(r,t,3);++u>>1,t(n[e])o&&(u=n.apply(i,e));else{var t=+new Date;!l&&!s&&(a=t);var p=f-(t-a);0/g,evaluate:/<%([\s\S]+?)%>/g,interpolate:/<%=([\s\S]+?)%>/g,variable:""},Fr||(v=function(n){if(E(n)){u.prototype=n;var r=new u;u.prototype=null}return r||{}}),m(arguments)||(m=function(n){return n&&typeof n=="object"?Er.call(n,"callee"):!1});var Cr=Nr||function(n){return n&&typeof n=="object"?Tr.call(n)==or:!1},Pr=function(n){var r,t=[]; +if(!n||!vr[typeof n])return t;for(r in n)Er.call(n,r)&&t.push(r);return t},Ur=kr?function(n){return E(n)?kr(n):[]}:Pr,Vr={"&":"&","<":"<",">":">",'"':""","'":"'","/":"/"},Gr=w(Vr),Hr=RegExp("("+Ur(Gr).join("|")+")","g"),Jr=RegExp("["+Ur(Vr).join("")+"]","g"),Kr=function(n,r){var t;if(!n||!vr[typeof n])return n;for(t in n)if(r(n[t],t,n)===rr)break;return n},Lr=function(n,r){var t;if(!n||!vr[typeof n])return n;for(t in n)if(Er.call(n,t)&&r(n[t],t,n)===rr)break;return n};x(/x/)&&(x=function(n){return typeof n=="function"&&"[object Function]"==Tr.call(n) +});var Qr=p(function(n,r,t){Er.call(n,t)?n[t]++:n[t]=1}),Xr=p(function(n,r,t){(Er.call(n,t)?n[t]:n[t]=[]).push(r)});i.after=function(n,r){if(!x(r))throw new TypeError;return function(){return 1>--n?r.apply(this,arguments):void 0}},i.bind=J,i.bindAll=function(n){for(var r=1u(o,a)){for(var f=t;--f;)if(0>u(r[f],a))continue n;o.push(a)}}return o},i.invert=w,i.invoke=function(n,r){var t=$r.call(arguments,2),e=-1,u=typeof r=="function",i=n?n.length:0,o=Array(typeof i=="number"?i:0); +return B(n,function(n){o[++e]=(u?r:n[r]).apply(n,t)}),o},i.keys=Ur,i.map=D,i.max=q,i.memoize=function(n,r){var t={};return function(){var e=tr+(r?r.apply(this,arguments):arguments[0]);return Er.call(t,e)?t[e]:t[e]=n.apply(this,arguments)}},i.min=function(n,r,t){var e=1/0,u=e,i=-1,o=n?n.length:0;if(r||typeof o!="number")r=K(r,t,3),B(n,function(n,t,i){t=r(n,t,i),tr(t,u)&&(e[u]=n) +}),e},i.once=function(n){var r,t;if(!x(n))throw new TypeError;return function(){return r?t:(r=!0,t=n.apply(this,arguments),n=null,t)}},i.pairs=function(n){for(var r=-1,t=Ur(n),e=t.length,u=Array(e);++rr?0:r);++nt?Dr(0,e+t):qr(t,e-1))+1);e--;)if(n[e]===r)return e;return-1},i.mixin=X,i.noConflict=function(){return n._=br,this},i.random=Y,i.reduce=$,i.reduceRight=I,i.result=function(n,r){var t=n?n[r]:Z;return x(t)?n[r]():t},i.size=function(n){var r=n?n.length:0;return typeof r=="number"?r:Ur(n).length +},i.some=W,i.sortedIndex=G,i.template=function(n,r,t){var u=i,o=u.templateSettings;n||(n=""),t=d({},t,o);var a=0,f="__p+='",o=t.variable;n.replace(RegExp((t.escape||er).source+"|"+(t.interpolate||er).source+"|"+(t.evaluate||er).source+"|$","g"),function(r,t,u,i,o){return f+=n.slice(a,o).replace(ur,e),t&&(f+="'+_.escape("+t+")+'"),i&&(f+="';"+i+";__p+='"),u&&(f+="'+((__t=("+u+"))==null?'':__t)+'"),a=o+r.length,r}),f+="';\n",o||(o="obj",f="with("+o+"||{}){"+f+"}"),f="function("+o+"){var __t,__p='',__j=Array.prototype.join;function print(){__p+=__j.call(arguments,'')}"+f+"return __p}"; +try{var l=Function("_","return "+f)(u)}catch(c){throw c.source=f,c}return r?l(r):(l.source=f,l)},i.unescape=function(n){return null==n?"":(n+"").replace(Hr,y)},i.uniqueId=function(n){var r=++nr+"";return n?n+r:r},i.all=F,i.any=W,i.detect=R,i.findWhere=function(n,r){return z(n,r,!0)},i.foldl=$,i.foldr=I,i.include=S,i.inject=$,i.first=P,i.last=function(n,r,t){if(n){var e=0,u=n.length;if(typeof r!="number"&&null!=r){var i=u;for(r=K(r,t,3);i--&&r(n[i],i,n);)e++}else if(e=r,null==e||t)return n[u-1];return $r.call(n,Dr(0,u-e)) +}},i.take=P,i.head=P,X(i),i.VERSION="1.3.1",i.prototype.chain=function(){return this.__chain__=!0,this},i.prototype.value=function(){return this.__wrapped__},B("pop push reverse shift sort splice unshift".split(" "),function(n){var r=_r[n];i.prototype[n]=function(){var n=this.__wrapped__;return r.apply(n,arguments),!zr.spliceObjects&&0===n.length&&delete n[0],this}}),B(["concat","join","slice"],function(n){var r=_r[n];i.prototype[n]=function(){var n=r.apply(this.__wrapped__,arguments);return this.__chain__&&(n=new o(n),n.__chain__=!0),n +}}),typeof define=="function"&&typeof define.amd=="object"&&define.amd?(n._=i, define(function(){return i})):gr&&!gr.nodeType?yr?(yr.exports=i)._=i:gr._=i:n._=i}(this); \ No newline at end of file diff --git a/doc/README.md b/doc/README.md index 312ff49da..35e4617c6 100644 --- a/doc/README.md +++ b/doc/README.md @@ -85,6 +85,7 @@ * [`_.reduce`](#_reducecollection--callbackidentity-accumulator-thisarg) * [`_.reduceRight`](#_reducerightcollection--callbackidentity-accumulator-thisarg) * [`_.reject`](#_rejectcollection--callbackidentity-thisarg) +* [`_.sample`](#_samplecollection) * [`_.select`](#_filtercollection--callbackidentity-thisarg) * [`_.shuffle`](#_shufflecollection) * [`_.size`](#_sizecollection) @@ -230,7 +231,7 @@ ### `_.compact(array)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4056 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4087 "View in source") [Ⓣ][1] Creates an array with all falsey values removed. The values `false`, `null`, `0`, `""`, `undefined`, and `NaN` are all falsey. @@ -254,7 +255,7 @@ _.compact([0, 1, false, 2, '', 3]); ### `_.difference(array [, array1, array2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4085 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4116 "View in source") [Ⓣ][1] Creates an array excluding all values of the provided arrays using strict equality for comparisons, i.e. `===`. @@ -279,7 +280,7 @@ _.difference([1, 2, 3, 4, 5], [5, 2, 10]); ### `_.findIndex(array [, callback=identity, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4135 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4166 "View in source") [Ⓣ][1] This method is like `_.find` except that it returns the index of the first element that passes the callback check, instead of the element itself. @@ -307,7 +308,7 @@ _.findIndex(['apple', 'banana', 'beet'], function(food) { ### `_.findLastIndex(array [, callback=identity, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4168 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4199 "View in source") [Ⓣ][1] This method is like `_.findIndex` except that it iterates over elements of a `collection` from right to left. @@ -335,9 +336,9 @@ _.findLastIndex(['apple', 'banana', 'beet'], function(food) { ### `_.first(array [, callback|n, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4236 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4266 "View in source") [Ⓣ][1] -Gets the first element of an array. If a number `n` is provided the first `n` elements of the array are returned. If a callback is provided elements at the beginning of the array are returned as long as the callback returns truthy. The callback is bound to `thisArg` and invoked with three arguments; *(value, index, array)*. +Gets the first element or first `n` elements of an array. If a callback is provided elements at the beginning of the array are returned as long as the callback returns truthy. The callback is bound to `thisArg` and invoked with three arguments; *(value, index, array)*. If a property name is provided for `callback` the created "_.pluck" style callback will return the property value of the given element. @@ -395,7 +396,7 @@ _.first(food, { 'type': 'fruit' }); ### `_.flatten(array [, isShallow=false, callback=identity, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4298 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4328 "View in source") [Ⓣ][1] Flattens a nested array *(the nesting can be to any depth)*. If `isShallow` is truthy, the array will only be flattened a single level. If a callback is provided each element of the array is passed through the callback before flattening. The callback is bound to `thisArg` and invoked with three arguments; *(value, index, array)*. @@ -438,7 +439,7 @@ _.flatten(stooges, 'quotes'); ### `_.indexOf(array, value [, fromIndex=0])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4335 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4365 "View in source") [Ⓣ][1] Gets the index at which the first occurrence of `value` is found using strict equality for comparisons, i.e. `===`. If the array is already sorted providing `true` for `fromIndex` will run a faster binary search. @@ -470,9 +471,9 @@ _.indexOf([1, 1, 2, 2, 3, 3], 2, true); ### `_.initial(array [, callback|n=1, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4402 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4431 "View in source") [Ⓣ][1] -Gets all but the last element of an array. If a number `n` is provided the last `n` elements are excluded from the result. If a callback is provided elements at the end of the array are excluded from the result as long as the callback returns truthy. The callback is bound to `thisArg` and invoked with three arguments; *(value, index, array)*. +Gets all but the last element or last `n` elements of an array. If a callback is provided elements at the end of the array are excluded from the result as long as the callback returns truthy. The callback is bound to `thisArg` and invoked with three arguments; *(value, index, array)*. If a property name is provided for `callback` the created "_.pluck" style callback will return the property value of the given element. @@ -527,7 +528,7 @@ _.initial(food, { 'type': 'vegetable' }); ### `_.intersection([array1, array2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4435 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4464 "View in source") [Ⓣ][1] Creates an array of unique values present in all provided arrays using strict equality for comparisons, i.e. `===`. @@ -551,11 +552,11 @@ _.intersection([1, 2, 3], [101, 2, 1, 10], [2, 1]); ### `_.last(array [, callback|n, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4537 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4564 "View in source") [Ⓣ][1] -Gets the last element of an array. If a number `n` is provided the last `n` elements of the array are returned. If a callback is provided elements at the end of the array are returned as long as the callback returns truthy. The callback is bound to `thisArg` and invoked with three arguments; *(value, index, array)*. +Gets the last element or last `n` elements of an array. If a callback is provided elements at the end of the array are returned as long as the callback returns truthy. The callback is bound to `thisArg` and invoked with three arguments; *(value, index, array)*. - If a property name is provided for `callback` the created "_.pluck" style callback will return the property value of the given element. +If a property name is provided for `callback` the created "_.pluck" style callback will return the property value of the given element. If an object is provided for `callback` the created "_.where" style callback will return `true` for elements that have the properties of the given object, else `false`. @@ -608,7 +609,7 @@ _.last(food, { 'type': 'vegetable' }); ### `_.lastIndexOf(array, value [, fromIndex=array.length-1])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4578 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4605 "View in source") [Ⓣ][1] Gets the index at which the last occurrence of `value` is found using strict equality for comparisons, i.e. `===`. If `fromIndex` is negative, it is used as the offset from the end of the collection. @@ -637,7 +638,7 @@ _.lastIndexOf([1, 2, 3, 1, 2, 3], 2, 3); ### `_.pull(array [, value1, value2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4608 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4635 "View in source") [Ⓣ][1] Removes all provided values from the given array using strict equality for comparisons, i.e. `===`. @@ -664,7 +665,7 @@ console.log(array); ### `_.range([start=0], end [, step=1])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4659 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4686 "View in source") [Ⓣ][1] Creates an array of numbers *(positive and/or negative)* progressing from `start` up to but not including `end`. If `start` is less than `stop` a zero-length range is created unless a negative `step` is specified. @@ -705,7 +706,7 @@ _.range(0); ### `_.remove(array [, callback=identity, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4712 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4739 "View in source") [Ⓣ][1] Removes all elements from an array that the callback returns truthy for and returns an array of removed elements. The callback is bound to `thisArg` and invoked with three arguments; *(value, index, array)*. @@ -741,9 +742,9 @@ console.log(evens); ### `_.rest(array [, callback|n=1, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4787 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4813 "View in source") [Ⓣ][1] -The opposite of `_.initial` this method gets all but the first value of an array. If a number `n` is provided the first `n` values are excluded from the result. If a callback function is provided elements at the beginning of the array are excluded from the result as long as the callback returns truthy. The callback is bound to `thisArg` and invoked with three arguments; *(value, index, array)*. +The opposite of `_.initial` this method gets all but the first element or first `n` elements of an array. If a callback function is provided elements at the beginning of the array are excluded from the result as long as the callback returns truthy. The callback is bound to `thisArg` and invoked with three arguments; *(value, index, array)*. If a property name is provided for `callback` the created "_.pluck" style callback will return the property value of the given element. @@ -801,7 +802,7 @@ _.rest(food, { 'type': 'fruit' }); ### `_.sortedIndex(array, value [, callback=identity, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4851 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4877 "View in source") [Ⓣ][1] Uses a binary search to determine the smallest index at which a value should be inserted into a given sorted array in order to maintain the sort order of the array. If a callback is provided it will be executed for `value` and each element of `array` to compute their sort ranking. The callback is bound to `thisArg` and invoked with one argument; *(value)*. @@ -850,7 +851,7 @@ _.sortedIndex(['twenty', 'thirty', 'fifty'], 'fourty', function(word) { ### `_.union([array1, array2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4882 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4908 "View in source") [Ⓣ][1] Creates an array of unique values, in order, of the provided arrays using strict equality for comparisons, i.e. `===`. @@ -874,7 +875,7 @@ _.union([1, 2, 3], [101, 2, 1, 10], [2, 1]); ### `_.uniq(array [, isSorted=false, callback=identity, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4930 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4956 "View in source") [Ⓣ][1] Creates a duplicate-value-free version of an array using strict equality for comparisons, i.e. `===`. If the array is sorted, providing `true` for `isSorted` will use a faster algorithm. If a callback is provided each element of `array` is passed through the callback before uniqueness is computed. The callback is bound to `thisArg` and invoked with three arguments; *(value, index, array)*. @@ -921,7 +922,7 @@ _.uniq([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x'); ### `_.without(array [, value1, value2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4958 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4984 "View in source") [Ⓣ][1] Creates an array excluding all provided values using strict equality for comparisons, i.e. `===`. @@ -946,7 +947,7 @@ _.without([1, 2, 1, 0, 3, 1, 4], 0, 1); ### `_.zip([array1, array2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4978 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5004 "View in source") [Ⓣ][1] Creates an array of grouped elements, the first of which contains the first elements of the given arrays, the second of which contains the second elements of the given arrays, and so on. @@ -973,7 +974,7 @@ _.zip(['moe', 'larry'], [30, 40], [true, false]); ### `_.zipObject(keys [, values=[]])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5008 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5034 "View in source") [Ⓣ][1] Creates an object composed from arrays of `keys` and `values`. Provide either a single two dimensional array, i.e. `[[key1, value1], [key2, value2]]` or two arrays, one of `keys` and one of corresponding `values`. @@ -1061,7 +1062,7 @@ _.isArray(squares.value()); ### `_.chain(value)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6181 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6207 "View in source") [Ⓣ][1] Creates a `lodash` object that wraps the given `value`. @@ -1094,7 +1095,7 @@ var youngest = _.chain(stooges) ### `_.tap(value, interceptor)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6209 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6235 "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. @@ -1124,7 +1125,7 @@ _([1, 2, 3, 4]) ### `_.prototype.chain()` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6229 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6255 "View in source") [Ⓣ][1] Enables method chaining on the wrapper object. @@ -1148,7 +1149,7 @@ var sum = _([1, 2, 3]) ### `_.prototype.toString()` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6246 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6272 "View in source") [Ⓣ][1] Produces the `toString` result of the wrapped value. @@ -1169,7 +1170,7 @@ _([1, 2, 3]).toString(); ### `_.prototype.valueOf()` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6263 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6289 "View in source") [Ⓣ][1] Extracts the wrapped value. @@ -1907,10 +1908,37 @@ _.reject(food, { 'type': 'fruit' }); + + +### `_.sample(collection)` +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L3834 "View in source") [Ⓣ][1] + +Retrieves a random element or `n` random elements from a collection. + +#### Arguments +1. `collection` *(Array|Object|String)*: The collection to sample. + +#### Returns +*(Array)*: Returns the random sample(s) of `collection`. + +#### Example +```js +_.sample([1, 2, 3, 4]); +// => 2 + +_.sample([1, 2, 3, 4], 2); +// => [3, 1] +``` + +* * * + + + + ### `_.shuffle(collection)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L3832 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L3863 "View in source") [Ⓣ][1] Creates an array of shuffled values, using a version of the Fisher-Yates shuffle. See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle. @@ -1934,7 +1962,7 @@ _.shuffle([1, 2, 3, 4, 5, 6]); ### `_.size(collection)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L3865 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L3896 "View in source") [Ⓣ][1] Gets the size of the `collection` by returning `collection.length` for arrays and array-like objects or the number of own enumerable properties for objects. @@ -1964,7 +1992,7 @@ _.size('curly'); ### `_.some(collection [, callback=identity, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L3912 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L3943 "View in source") [Ⓣ][1] Checks if the callback returns a truthy value for **any** element of a collection. The function returns as soon as it finds a passing value and does not iterate over the entire collection. The callback is bound to `thisArg` and invoked with three arguments; *(value, index|key, collection)*. @@ -2010,7 +2038,7 @@ _.some(food, { 'type': 'meat' }); ### `_.sortBy(collection [, callback=identity, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L3968 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L3999 "View in source") [Ⓣ][1] Creates an array of elements, sorted in ascending order by the results of running each element in a collection through the callback. This method performs a stable sort, that is, it will preserve the original sort order of equal elements. The callback is bound to `thisArg` and invoked with three arguments; *(value, index|key, collection)*. @@ -2047,7 +2075,7 @@ _.sortBy(['banana', 'strawberry', 'apple'], 'length'); ### `_.toArray(collection)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4004 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4035 "View in source") [Ⓣ][1] Converts the `collection` to an array. @@ -2071,7 +2099,7 @@ Converts the `collection` to an array. ### `_.where(collection, properties)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4038 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L4069 "View in source") [Ⓣ][1] Performs a deep comparison of each element in a `collection` to the given `properties` object, returning an array of all elements that have equivalent property values. @@ -2111,7 +2139,7 @@ _.where(stooges, { 'quotes': ['Poifect!'] }); ### `_.after(n, func)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5045 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5071 "View in source") [Ⓣ][1] Creates a function this is restricted to executing `func` with the `this` binding and arguments of the created function, only after it is called `n` times. @@ -2139,7 +2167,7 @@ _.forEach(notes, function(note) { ### `_.bind(func [, thisArg, arg1, arg2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5078 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5104 "View in source") [Ⓣ][1] Creates a function that, when called, invokes `func` with the `this` binding of `thisArg` and prepends any additional `bind` arguments to those provided to the bound function. @@ -2170,7 +2198,7 @@ func(); ### `_.bindAll(object [, methodName1, methodName2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5106 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5132 "View in source") [Ⓣ][1] Binds methods of an object to the object itself, overwriting the existing method. Method names may be specified as individual arguments or as arrays of method names. If no method names are provided all the function properties of `object` will be bound. @@ -2201,7 +2229,7 @@ jQuery('#docs').on('click', view.onClick); ### `_.bindKey(object, key [, arg1, arg2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5152 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5178 "View in source") [Ⓣ][1] Creates a function that, when called, invokes the method at `object[key]` and prepends any additional `bindKey` arguments to those provided to the bound function. This method differs from `_.bind` by allowing bound functions to reference methods that will be redefined or don't yet exist. See http://michaux.ca/articles/lazy-function-definition-pattern. @@ -2242,7 +2270,7 @@ func(); ### `_.compose([func1, func2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5186 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5212 "View in source") [Ⓣ][1] Creates a function that is the composition of the provided functions, where each function consumes the return value of the function that follows. For example, composing the functions `f()`, `g()`, and `h()` produces `f(g(h()))`. Each function is executed with the `this` binding of the composed function. @@ -2280,7 +2308,7 @@ welcome('curly'); ### `_.createCallback([func=identity, thisArg, argCount])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5237 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5263 "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`. @@ -2319,7 +2347,7 @@ _.filter(stooges, 'age__gt45'); ### `_.curry(func [, arity=func.length])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5302 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5328 "View in source") [Ⓣ][1] Creates a function which accepts one or more arguments of `func` that when invoked either executes `func` returning its result, if all `func` arguments have been provided, or returns a function that accepts one or more of the remaining `func` arguments, and so on. The arity of `func` can be specified if `func.length` is not sufficient. @@ -2354,7 +2382,7 @@ curried(1, 2, 3); ### `_.debounce(func, wait, options)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5346 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5372 "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. Provide an options object to indicate that `func` should be invoked on the leading and/or trailing edge of the `wait` timeout. Subsequent calls to the debounced function will return the result of the last `func` call. @@ -2395,7 +2423,7 @@ source.addEventListener('message', _.debounce(batchLog, 250, { ### `_.defer(func [, arg1, arg2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5444 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5470 "View in source") [Ⓣ][1] Defers executing the `func` function until the current call stack has cleared. Additional arguments will be provided to `func` when it is invoked. @@ -2420,7 +2448,7 @@ _.defer(function() { console.log('deferred'); }); ### `_.delay(func, wait [, arg1, arg2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5478 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5504 "View in source") [Ⓣ][1] Executes the `func` function after `wait` milliseconds. Additional arguments will be provided to `func` when it is invoked. @@ -2447,7 +2475,7 @@ _.delay(log, 1000, 'logged later'); ### `_.memoize(func [, resolver])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5506 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5532 "View in source") [Ⓣ][1] Creates a function that memoizes the result of `func`. If `resolver` is provided it will be used to determine the cache key for storing the result based on the arguments provided to the memoized function. By default, the first argument provided to the memoized function is used as the cache key. The `func` is executed with the `this` binding of the memoized function. The result cache is exposed as the `cache` property on the memoized function. @@ -2473,7 +2501,7 @@ var fibonacci = _.memoize(function(n) { ### `_.once(func)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5539 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5565 "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. @@ -2499,7 +2527,7 @@ initialize(); ### `_.partial(func [, arg1, arg2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5577 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5603 "View in source") [Ⓣ][1] Creates a function that, when called, invokes `func` with any additional `partial` arguments prepended to those provided to the new function. This method is similar to `_.bind` except it does **not** alter the `this` binding. @@ -2526,7 +2554,7 @@ hi('moe'); ### `_.partialRight(func [, arg1, arg2, ...])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5608 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5634 "View in source") [Ⓣ][1] This method is like `_.partial` except that `partial` arguments are appended to those provided to the new function. @@ -2563,7 +2591,7 @@ options.imports ### `_.throttle(func, wait, options)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5643 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5669 "View in source") [Ⓣ][1] Creates a function that, when executed, will only call the `func` function at most once per every `wait` milliseconds. Provide an options object to indicate that `func` should be invoked on the leading and/or trailing edge of the `wait` timeout. Subsequent calls to the throttled function will return the result of the last `func` call. @@ -2597,7 +2625,7 @@ jQuery('.interactive').on('click', _.throttle(renewToken, 300000, { ### `_.wrap(value, wrapper)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5687 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5713 "View in source") [Ⓣ][1] Creates a function that provides `value` to the wrapper function as its first argument. Additional arguments provided to the function are appended to those provided to the wrapper function. The wrapper is executed with the `this` binding of the created function. @@ -3772,7 +3800,7 @@ _.values({ 'one': 1, 'two': 2, 'three': 3 }); ### `_.escape(string)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5714 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5740 "View in source") [Ⓣ][1] Converts the characters `&`, `<`, `>`, `"`, and `'` in `string` to their corresponding HTML entities. @@ -3796,7 +3824,7 @@ _.escape('Moe, Larry & Curly'); ### `_.identity(value)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5732 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5758 "View in source") [Ⓣ][1] This method returns the first argument provided to it. @@ -3821,7 +3849,7 @@ moe === _.identity(moe); ### `_.mixin(object, object)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5759 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5785 "View in source") [Ⓣ][1] Adds function properties of a source object to the `lodash` function and chainable wrapper. @@ -3852,7 +3880,7 @@ _('moe').capitalize(); ### `_.noConflict()` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5797 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5823 "View in source") [Ⓣ][1] Reverts the '_' variable to its previous value and returns a reference to the `lodash` function. @@ -3872,7 +3900,7 @@ var lodash = _.noConflict(); ### `_.parseInt(value [, radix])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5821 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5847 "View in source") [Ⓣ][1] Converts the given `value` into an integer of the specified `radix`. If `radix` is `undefined` or `0` a `radix` of `10` is used unless the `value` is a hexadecimal, in which case a `radix` of `16` is used. @@ -3899,7 +3927,7 @@ _.parseInt('08'); ### `_.random([min=0, max=1])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5845 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5871 "View in source") [Ⓣ][1] Produces a random number between `min` and `max` *(inclusive)*. If only one argument is provided a number between `0` and the given number will be returned. @@ -3927,7 +3955,7 @@ _.random(5); ### `_.result(object, property)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5889 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5915 "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. @@ -3980,7 +4008,7 @@ Create a new `lodash` function using the given `context` object. ### `_.template(text, data, options)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L5980 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6006 "View in source") [Ⓣ][1] A micro-templating method that handles arbitrary delimiters, preserves whitespace, and correctly escapes quotes within interpolated code. @@ -4068,7 +4096,7 @@ fs.writeFileSync(path.join(cwd, 'jst.js'), '\ ### `_.times(n, callback [, thisArg])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6105 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6131 "View in source") [Ⓣ][1] Executes the callback `n` times, returning an array of the results of each callback execution. The callback is bound to `thisArg` and invoked with one argument; *(index)*. @@ -4100,7 +4128,7 @@ _.times(3, function(n) { this.cast(n); }, mage); ### `_.unescape(string)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6132 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6158 "View in source") [Ⓣ][1] The inverse of `_.escape` this method converts the HTML entities `&`, `<`, `>`, `"`, and `'` in `string` to their corresponding characters. @@ -4124,7 +4152,7 @@ _.unescape('Moe, Larry & Curly'); ### `_.uniqueId([prefix])` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6152 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6178 "View in source") [Ⓣ][1] Generates a unique ID. If `prefix` is provided the ID will be appended to it. @@ -4177,7 +4205,7 @@ A reference to the `lodash` function. ### `_.VERSION` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6459 "View in source") [Ⓣ][1] +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L6487 "View in source") [Ⓣ][1] *(String)*: The semantic version number. diff --git a/lodash.js b/lodash.js index 85ca23f0f..a0765543c 100644 --- a/lodash.js +++ b/lodash.js @@ -3815,6 +3815,37 @@ }); } + /** + * Retrieves a random element or `n` random elements from a collection. + * + * @static + * @memberOf _ + * @category Collections + * @param {Array|Object|String} collection The collection to sample. + * @returns {Array} Returns the random sample(s) of `collection`. + * @example + * + * _.sample([1, 2, 3, 4]); + * // => 2 + * + * _.sample([1, 2, 3, 4], 2); + * // => [3, 1] + */ + function sample(collection, n, guard) { + if (!isArray(collection)) { + collection = toArray(collection); + } + var index = -1, + maxIndex = collection.length - 1; + + if (n == null || guard) { + return collection[random(maxIndex)]; + } + var result = shuffle(collection); + result.length = nativeMin(nativeMax(0, n), result.length); + return result; + } + /** * Creates an array of shuffled values, using a version of the Fisher-Yates * shuffle. See http://en.wikipedia.org/wiki/Fisher-Yates_shuffle. @@ -3835,7 +3866,7 @@ result = Array(typeof length == 'number' ? length : 0); forEach(collection, function(value) { - var rand = floor(nativeRandom() * (++index + 1)); + var rand = random(++index); result[index] = result[rand]; result[rand] = value; }); @@ -4177,11 +4208,10 @@ } /** - * Gets the first element of an array. If a number `n` is provided the first - * `n` elements of the array are returned. If a callback is provided elements - * at the beginning of the array are returned as long as the callback returns - * truthy. The callback is bound to `thisArg` and invoked with three arguments; - * (value, index, array). + * Gets the first element or first `n` elements of an array. If a callback + * is provided elements at the beginning of the array are returned as long + * as the callback returns truthy. The callback is bound to `thisArg` and + * invoked with three arguments; (value, index, array). * * If a property name is provided for `callback` the created "_.pluck" style * callback will return the property value of the given element. @@ -4344,11 +4374,10 @@ } /** - * Gets all but the last element of an array. If a number `n` is provided - * the last `n` elements are excluded from the result. If a callback is - * provided elements at the end of the array are excluded from the result - * as long as the callback returns truthy. The callback is bound to `thisArg` - * and invoked with three arguments; (value, index, array). + * Gets all but the last element or last `n` elements of an array. If a + * callback is provided elements at the end of the array are excluded from + * the result as long as the callback returns truthy. The callback is bound + * to `thisArg` and invoked with three arguments; (value, index, array). * * If a property name is provided for `callback` the created "_.pluck" style * callback will return the property value of the given element. @@ -4478,12 +4507,10 @@ } /** - * Gets the last element of an array. If a number `n` is provided the last - * `n` elements of the array are returned. If a callback is provided elements - * at the end of the array are returned as long as the callback returns truthy. - * The callback is bound to `thisArg` and invoked with three arguments; - * (value, index, array). - * + * Gets the last element or last `n` elements of an array. If a callback is + * provided elements at the end of the array are returned as long as the + * callback returns truthy. The callback is bound to `thisArg` and invoked + * with three arguments; (value, index, array). * * If a property name is provided for `callback` the created "_.pluck" style * callback will return the property value of the given element. @@ -4727,12 +4754,11 @@ } /** - * The opposite of `_.initial` this method gets all but the first value of - * an array. If a number `n` is provided the first `n` values are excluded - * from the result. If a callback function is provided elements at the beginning - * of the array are excluded from the result as long as the callback returns - * truthy. The callback is bound to `thisArg` and invoked with three - * arguments; (value, index, array). + * The opposite of `_.initial` this method gets all but the first element or + * first `n` elements of an array. If a callback function is provided elements + * at the beginning of the array are excluded from the result as long as the + * callback returns truthy. The callback is bound to `thisArg` and invoked + * with three arguments; (value, index, array). * * If a property name is provided for `callback` the created "_.pluck" style * callback will return the property value of the given element. @@ -6429,18 +6455,20 @@ // add functions capable of returning wrapped and unwrapped values when chaining lodash.first = first; lodash.last = last; + lodash.sample = sample; // add aliases lodash.take = first; lodash.head = first; forOwn(lodash, function(func, methodName) { + var callbackable = methodName !== 'sample'; if (!lodash.prototype[methodName]) { - lodash.prototype[methodName]= function(callback, thisArg) { + lodash.prototype[methodName]= function(n, guard) { var chainAll = this.__chain__, - result = func(this.__wrapped__, callback, thisArg); + result = func(this.__wrapped__, n, guard); - return !chainAll && (callback == null || (thisArg && typeof callback != 'function')) + return !chainAll && (n == null || (guard && !(callbackable && typeof n == 'function'))) ? result : new lodashWrapper(result, chainAll); }; diff --git a/test/test-build.js b/test/test-build.js index b5a3e9aa6..0a99c11f2 100644 --- a/test/test-build.js +++ b/test/test-build.js @@ -145,6 +145,7 @@ 'reduce', 'reduceRight', 'reject', + 'sample', 'shuffle', 'size', 'some', @@ -309,6 +310,7 @@ 'pull', 'remove', 'runInContext', + 'sample', 'transform' ]; diff --git a/test/test.js b/test/test.js index f1d32d3f3..f8a29a5f1 100644 --- a/test/test.js +++ b/test/test.js @@ -1080,6 +1080,22 @@ deepEqual(_.first(array, 2), [1, 2]); }); + test('should return an empty array when `n` < `1`', function() { + _.each([0, -1, -2], function(n) { + deepEqual(_.first(array, n), []); + }); + }); + + test('should return all elements when `n` >= `array.length`', function() { + _.each([3, 4], function(n) { + deepEqual(_.first(array, n), array.slice()); + }); + }); + + test('should return `undefined` when querying empty arrays', function() { + strictEqual(_.first([]), undefined); + }); + test('should work when used as `callback` for `_.map`', function() { var array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], actual = _.map(array, _.first); @@ -1771,10 +1787,6 @@ { 'a': 2, 'b': 2 } ]; - test('returns all elements for `n` of `0`', function() { - deepEqual(_.initial(array, 0), [1, 2, 3]); - }); - test('should accept a falsey `array` argument', function() { _.forEach(falsey, function(index, value) { try { @@ -1788,10 +1800,26 @@ deepEqual(_.initial(array), [1, 2]); }); - test('should exlcude the last two elements', function() { + test('should exclude the last two elements', function() { deepEqual(_.initial(array, 2), [1]); }); + test('should return an empty when querying empty arrays', function() { + deepEqual(_.initial([]), []); + }); + + test('should return all elements when `n` < `1`', function() { + _.each([0, -1, -2], function(n) { + deepEqual(_.initial(array, n), array.slice()); + }); + }); + + test('should return an empty array when `n` >= `array.length`', function() { + _.each([3, 4], function(n) { + deepEqual(_.initial(array, n), []); + }); + }); + test('should work when used as `callback` for `_.map`', function() { var array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], actual = _.map(array, _.initial); @@ -2197,6 +2225,22 @@ deepEqual(_.last(array, 2), [2, 3]); }); + test('should return an empty array when `n` < `1`', function() { + _.each([0, -1, -2], function(n) { + deepEqual(_.last(array, n), []); + }); + }); + + test('should return all elements when `n` >= `array.length`', function() { + _.each([3, 4], function(n) { + deepEqual(_.last(array, n), array.slice()); + }); + }); + + test('should return `undefined` when querying empty arrays', function() { + strictEqual(_.last([]), undefined); + }); + test('should work when used as `callback` for `_.map`', function() { var array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], actual = _.map(array, _.last); @@ -3096,10 +3140,6 @@ { 'a': 0, 'b': 0 } ]; - test('returns all elements for `n` of `0`', function() { - deepEqual(_.rest(array, 0), [1, 2, 3]); - }); - test('should accept a falsey `array` argument', function() { _.forEach(falsey, function(index, value) { try { @@ -3117,6 +3157,22 @@ deepEqual(_.rest(array, 2), [3]); }); + test('should return all elements when `n` < `1`', function() { + _.each([0, -1, -2], function(n) { + deepEqual(_.rest(array, n), [1, 2, 3]); + }); + }); + + test('should return an empty array when `n` >= `array.length`', function() { + _.each([3, 4], function(n) { + deepEqual(_.rest(array, n), []); + }); + }); + + test('should return an empty when querying empty arrays', function() { + deepEqual(_.rest([]), []); + }); + test('should work when used as `callback` for `_.map`', function() { var array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], actual = _.map(array, _.rest); @@ -3183,6 +3239,69 @@ /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.sample'); + + (function() { + var array = [1, 2, 3]; + + test('should return a random element', function() { + var actual = _.sample(array); + ok(_.contains(array, actual)); + }); + + test('should return two random elements', function() { + var actual = _.sample(array, 2); + ok(actual[0] !== actual[1] && _.contains(array, actual[0]) && _.contains(array, actual[1])); + }); + + test('should return an empty array when `n` < `1`', function() { + _.each([0, -1, -2], function(n) { + deepEqual(_.sample(array, n), []); + }); + }); + + test('should return all elements when `n` >= `array.length`', function() { + _.each([3, 4], function(n) { + deepEqual(_.sample(array, n).sort(), array.slice()); + }); + }); + + test('should return `undefined` when sampling an empty array', function() { + strictEqual(_.sample([]), undefined); + }); + + test('should sample an object', function() { + var object = { 'a': 1, 'b': 2, 'c': 3 }, + actual = _.sample(object); + + ok(_.contains(array, actual)); + + actual = _.sample(object, 2); + ok(actual[0] !== actual[1] && _.contains(array, actual[0]) && _.contains(array, actual[1])); + }); + + test('should work when used as `callback` for `_.map`', function() { + var a = [1, 2, 3], + b = [4, 5, 6], + c = [7, 8, 9], + actual = _.map([a, b, c], _.sample); + + ok(_.contains(a, actual[0]) && _.contains(b, actual[1]) && _.contains(c, actual[2])); + }); + + test('should chain when passing `n`', function() { + var actual = _(array).sample(2); + ok(actual instanceof _); + }); + + test('should not chain when arguments are not provided', function() { + var actual = _(array).sample(); + ok(_.contains(array, actual)); + }); + }()); + + /*--------------------------------------------------------------------------*/ + QUnit.module('lodash.shuffle'); (function() {