diff --git a/build.js b/build.js index 924c5f1be..c56297460 100644 --- a/build.js +++ b/build.js @@ -201,7 +201,7 @@ 'rest': ['createCallback', 'slice'], 'result': ['isFunction'], 'runInContext': ['defaults', 'pick'], - 'sample': ['random', 'shuffle', 'toArray'], + 'sample': ['isArray', 'random', 'shuffle', 'toArray'], 'shuffle': ['forEach', 'random'], 'size': ['keys'], 'some': ['baseEach', 'createCallback', 'isArray'], diff --git a/dist/lodash.compat.js b/dist/lodash.compat.js index cd33dab86..68a5eb99d 100644 --- a/dist/lodash.compat.js +++ b/dist/lodash.compat.js @@ -1940,8 +1940,8 @@ * @category Objects * @param {Object} object The destination object. * @param {Object} [source1, source2, ...] The source objects. - * @param- {Object} [guard] Allows working with `_.reduce` without using - * their `key` and `object` arguments as sources. + * @param- {Object} [guard] Allows working with `_.reduce` without using its + * `key` and `object` arguments as sources. * @returns {Object} Returns the destination object. * @example * @@ -3766,6 +3766,9 @@ * @memberOf _ * @category Collections * @param {Array|Object|String} collection The collection to sample. + * @param {Number} [n] The number of elements to sample. + * @param- {Object} [guard] Allows working with functions, like `_.map`, + * without using their `key` and `object` arguments as sources. * @returns {Array} Returns the random sample(s) of `collection`. * @example * @@ -3779,11 +3782,8 @@ if (!isArray(collection)) { collection = toArray(collection); } - var index = -1, - maxIndex = collection.length - 1; - if (n == null || guard) { - return collection[random(maxIndex)]; + return collection[random(collection.length - 1)]; } var result = shuffle(collection); result.length = nativeMin(nativeMax(0, n), result.length); diff --git a/dist/lodash.compat.min.js b/dist/lodash.compat.min.js index e82699132..0b424baf0 100644 --- a/dist/lodash.compat.min.js +++ b/dist/lodash.compat.min.js @@ -48,7 +48,7 @@ return a},_.tap=function(n,t){return t(n),n},_.throttle=function(n,t,e){var r=m, },_.isString=_t,_.isUndefined=function(n){return typeof n=="undefined"},_.lastIndexOf=function(n,t,e){var r=n?n.length:0;for(typeof e=="number"&&(r=(0>e?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 +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){return qe(n)||(n=Rt(n)),t==d||e?n[Ut(n.length-1)]:(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 diff --git a/dist/lodash.js b/dist/lodash.js index f3b8372fa..0f30d3ccc 100644 --- a/dist/lodash.js +++ b/dist/lodash.js @@ -1596,8 +1596,8 @@ * @category Objects * @param {Object} object The destination object. * @param {Object} [source1, source2, ...] The source objects. - * @param- {Object} [guard] Allows working with `_.reduce` without using - * their `key` and `object` arguments as sources. + * @param- {Object} [guard] Allows working with `_.reduce` without using its + * `key` and `object` arguments as sources. * @returns {Object} Returns the destination object. * @example * @@ -3465,6 +3465,9 @@ * @memberOf _ * @category Collections * @param {Array|Object|String} collection The collection to sample. + * @param {Number} [n] The number of elements to sample. + * @param- {Object} [guard] Allows working with functions, like `_.map`, + * without using their `key` and `object` arguments as sources. * @returns {Array} Returns the random sample(s) of `collection`. * @example * @@ -3478,11 +3481,8 @@ if (!isArray(collection)) { collection = toArray(collection); } - var index = -1, - maxIndex = collection.length - 1; - if (n == null || guard) { - return collection[random(maxIndex)]; + return collection[random(collection.length - 1)]; } var result = shuffle(collection); result.length = nativeMin(nativeMax(0, n), result.length); diff --git a/dist/lodash.min.js b/dist/lodash.min.js index afb6f3674..75a880443 100644 --- a/dist/lodash.min.js +++ b/dist/lodash.min.js @@ -45,7 +45,7 @@ return a},Z.tap=function(n,t){return t(n),n},Z.throttle=function(n,t,r){var e=y, },Z.isString=dt,Z.isUndefined=function(n){return typeof n=="undefined"},Z.lastIndexOf=function(n,t,r){var e=n?n.length:0;for(typeof r=="number"&&(e=(0>r?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 +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){return Pr(n)||(n=Dt(n)),t==m||r?n[Jt(n.length-1)]:(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 diff --git a/dist/lodash.underscore.js b/dist/lodash.underscore.js index 70cdc32dd..3b71729f0 100644 --- a/dist/lodash.underscore.js +++ b/dist/lodash.underscore.js @@ -947,8 +947,8 @@ * @category Objects * @param {Object} object The destination object. * @param {Object} [source1, source2, ...] The source objects. - * @param- {Object} [guard] Allows working with `_.reduce` without using - * their `key` and `object` arguments as sources. + * @param- {Object} [guard] Allows working with `_.reduce` without using its + * `key` and `object` arguments as sources. * @returns {Object} Returns the destination object. * @example * diff --git a/doc/README.md b/doc/README.md index 35e4617c6..c3c686206 100644 --- a/doc/README.md +++ b/doc/README.md @@ -85,7 +85,7 @@ * [`_.reduce`](#_reducecollection--callbackidentity-accumulator-thisarg) * [`_.reduceRight`](#_reducerightcollection--callbackidentity-accumulator-thisarg) * [`_.reject`](#_rejectcollection--callbackidentity-thisarg) -* [`_.sample`](#_samplecollection) +* [`_.sample`](#_samplecollection--n) * [`_.select`](#_filtercollection--callbackidentity-thisarg) * [`_.shuffle`](#_shufflecollection) * [`_.size`](#_sizecollection) @@ -1910,13 +1910,14 @@ _.reject(food, { 'type': 'fruit' }); -### `_.sample(collection)` -# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L3834 "View in source") [Ⓣ][1] +### `_.sample(collection [, n])` +# [Ⓢ](https://github.com/bestiejs/lodash/blob/master/lodash.js#L3837 "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. +2. `[n]` *(Number)*: The number of elements to sample. #### Returns *(Array)*: Returns the random sample(s) of `collection`. diff --git a/lodash.js b/lodash.js index a0765543c..f62f0a7dc 100644 --- a/lodash.js +++ b/lodash.js @@ -1996,8 +1996,8 @@ * @category Objects * @param {Object} object The destination object. * @param {Object} [source1, source2, ...] The source objects. - * @param- {Object} [guard] Allows working with `_.reduce` without using - * their `key` and `object` arguments as sources. + * @param- {Object} [guard] Allows working with `_.reduce` without using its + * `key` and `object` arguments as sources. * @returns {Object} Returns the destination object. * @example * @@ -3822,6 +3822,9 @@ * @memberOf _ * @category Collections * @param {Array|Object|String} collection The collection to sample. + * @param {Number} [n] The number of elements to sample. + * @param- {Object} [guard] Allows working with functions, like `_.map`, + * without using their `key` and `object` arguments as sources. * @returns {Array} Returns the random sample(s) of `collection`. * @example * @@ -3835,11 +3838,8 @@ if (!isArray(collection)) { collection = toArray(collection); } - var index = -1, - maxIndex = collection.length - 1; - if (n == null || guard) { - return collection[random(maxIndex)]; + return collection[random(collection.length - 1)]; } var result = shuffle(collection); result.length = nativeMin(nativeMax(0, n), result.length);