From 6fab2980b1795b5cea5b07952f95987bae7c0b26 Mon Sep 17 00:00:00 2001 From: DomiR Date: Wed, 13 Jan 2016 22:44:37 +0100 Subject: [PATCH] (feature) Split _.sample into _.sampleSize --- lodash/lodash-tests.ts | 9 +++-- lodash/lodash.d.ts | 78 ++++++++++++++++++++++++++++-------------- 2 files changed, 58 insertions(+), 29 deletions(-) diff --git a/lodash/lodash-tests.ts b/lodash/lodash-tests.ts index 4b06f98b0..f0abe1d44 100644 --- a/lodash/lodash-tests.ts +++ b/lodash/lodash-tests.ts @@ -4894,12 +4894,15 @@ module TestReject { } } +// _.sample result = _.sample([1, 2, 3, 4]); -result = _.sample([1, 2, 3, 4], 2); result = <_.LoDashImplicitWrapper>_([1, 2, 3, 4]).sample(); -result = <_.LoDashImplicitArrayWrapper>_([1, 2, 3, 4]).sample(2); result = _([1, 2, 3, 4]).sample().value(); -result = _([1, 2, 3, 4]).sample(2).value(); + +// _.sampleSize +result = _.sampleSize([1, 2, 3, 4], 2); +result = <_.LoDashImplicitArrayWrapper>_([1, 2, 3, 4]).sampleSize(2); +result = _([1, 2, 3, 4]).sampleSize(2).value(); // _.select module TestSelect { diff --git a/lodash/lodash.d.ts b/lodash/lodash.d.ts index 818c28076..ff005a991 100644 --- a/lodash/lodash.d.ts +++ b/lodash/lodash.d.ts @@ -31,7 +31,7 @@ TODO: - [x] Split _.indexOf & _.lastIndexOf into _.sortedIndexOf & _.sortedLastIndexOf - [x] Split _.max & _.min into _.maxBy & _.minBy - [x] Split _.omit & _.pick into _.omitBy & _.pickBy -- [ ] Split _.sample into _.sampleSize +- [x] Split _.sample into _.sampleSize - [ ] Split _.sortedIndex into _.sortedIndexBy - [ ] Split _.sortedLastIndex into _.sortedLastIndexBy - [ ] Split _.uniq into _.sortedUniq, _.sortedUniqBy, & _.uniqBy @@ -7909,10 +7909,18 @@ declare module _ { //_.sample interface LoDashStatic { /** - * Retrieves a random element or n random elements from a collection. - * @param collection The collection to sample. - * @return Returns the random sample(s) of collection. - **/ + * Gets a random element from `collection`. + * + * @static + * @memberOf _ + * @category Collection + * @param {Array|Object} collection The collection to sample. + * @returns {*} Returns the random element. + * @example + * + * _.sample([1, 2, 3, 4]); + * // => 2 + */ sample(collection: Array): T; /** @@ -7924,36 +7932,54 @@ declare module _ { * @see _.sample **/ sample(collection: Dictionary): T; - - /** - * @see _.sample - * @param n The number of elements to sample. - **/ - sample(collection: Array, n: number): T[]; - - /** - * @see _.sample - * @param n The number of elements to sample. - **/ - sample(collection: List, n: number): T[]; - - /** - * @see _.sample - * @param n The number of elements to sample. - **/ - sample(collection: Dictionary, n: number): T[]; } interface LoDashImplicitArrayWrapper { /** * @see _.sample **/ - sample(n: number): LoDashImplicitArrayWrapper; + sample(): LoDashImplicitWrapper; + } + + //_.sampleSize + interface LoDashStatic { + /** + * Gets `n` random elements from `collection`. + * + * @static + * @memberOf _ + * @category Collection + * @param {Array|Object} collection The collection to sample. + * @param {number} [n=0] The number of elements to sample. + * @returns {Array} Returns the random elements. + * @example + * + * _.sampleSize([1, 2, 3, 4], 2); + * // => [3, 1] + */ + sampleSize(collection: Array, n: number): T[]; /** - * @see _.sample + * @see _.sampleSize + **/ + sampleSize(collection: List, n: number): T[]; + + /** + * @see _.sampleSize + **/ + sampleSize(collection: Dictionary, n: number): T[]; + } + + interface LoDashImplicitArrayWrapper { + /** + * @see _.sampleSize **/ - sample(): LoDashImplicitWrapper; + sampleSize(n: number): LoDashImplicitArrayWrapper; + + /** + * @see _.sampleSize + **/ + sampleSize(): LoDashImplicitWrapper; } //_.select