Merge pull request #8755 from chrootsu/lodash-sample

lodash: changed _.sample
This commit is contained in:
Masahiro Wakame
2016-03-31 00:25:23 +09:00
4 changed files with 421 additions and 104 deletions
+72 -7
View File
@@ -5003,13 +5003,6 @@ namespace TestReject {
}
}
result = <number>_.sample([1, 2, 3, 4]);
result = <number[]>_.sample([1, 2, 3, 4], 2);
result = <_.LoDashImplicitWrapper<number>>_([1, 2, 3, 4]).sample();
result = <_.LoDashImplicitArrayWrapper<number>>_([1, 2, 3, 4]).sample(2);
result = <number>_([1, 2, 3, 4]).sample().value();
result = <number[]>_([1, 2, 3, 4]).sample(2).value();
// _.select
namespace TestSelect {
let array: TResult[];
@@ -5108,6 +5101,78 @@ namespace TestSelect {
}
}
// _.sample
namespace TestSample {
let array: string[];
let list: _.List<string>;
let dictionary: _.Dictionary<string>;
let numericDictionary: _.NumericDictionary<string>;
{
let result: string;
result = _.sample('abc');
result = _.sample(array);
result = _.sample(list);
result = _.sample(dictionary);
result = _.sample(numericDictionary);
result = _.sample<{a: string}, string>({a: 'foo'});
result = _.sample<string>({a: 'foo'});
result = _('abc').sample();
result = _(array).sample();
result = _(list).sample<string>();
result = _(dictionary).sample<string>();
result = _(numericDictionary).sample<string>();
result = _({a: 'foo'}).sample<string>();
}
{
let result: string[];
result = _.sample('abc', 42);
result = _.sample(array, 42);
result = _.sample(list, 42);
result = _.sample(dictionary, 42);
result = _.sample(numericDictionary, 42);
result = _.sample<{a: string}, string>({a: 'foo'}, 42);
result = _.sample<string>({a: 'foo'}, 42);
}
{
let result: _.LoDashImplicitArrayWrapper<string>;
result = _('abc').sample(42);
result = _(array).sample(42);
result = _(list).sample<string>(42);
result = _(dictionary).sample<string>(42);
result = _(numericDictionary).sample<string>(42);
result = _({a: 'foo'}).sample<string>(42);
}
{
let result: _.LoDashExplicitWrapper<string>;
result = _('abc').chain().sample();
result = _(array).chain().sample<_.LoDashExplicitWrapper<string>>();
result = _(list).chain().sample<_.LoDashExplicitWrapper<string>>();
result = _(dictionary).chain().sample<_.LoDashExplicitWrapper<string>>();
result = _(numericDictionary).chain().sample<_.LoDashExplicitWrapper<string>>();
result = _({a: 'foo'}).chain().sample<_.LoDashExplicitWrapper<string>>();
}
{
let result: _.LoDashExplicitArrayWrapper<string>;
result = _('abc').chain().sample(42);
result = _(array).chain().sample(42);
result = _(list).chain().sample<string>(42);
result = _(dictionary).chain().sample<string>(42);
result = _(numericDictionary).chain().sample<string>(42);
result = _({a: 'foo'}).chain().sample<string>(42);
}
}
// _.shuffle
namespace TestShuffle {
let array: TResult[];
+135 -50
View File
@@ -8008,56 +8008,6 @@ declare module _ {
reject<W, T>(predicate: W): LoDashExplicitArrayWrapper<T>;
}
//_.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.
**/
sample<T>(collection: Array<T>): T;
/**
* @see _.sample
**/
sample<T>(collection: List<T>): T;
/**
* @see _.sample
**/
sample<T>(collection: Dictionary<T>): T;
/**
* @see _.sample
* @param n The number of elements to sample.
**/
sample<T>(collection: Array<T>, n: number): T[];
/**
* @see _.sample
* @param n The number of elements to sample.
**/
sample<T>(collection: List<T>, n: number): T[];
/**
* @see _.sample
* @param n The number of elements to sample.
**/
sample<T>(collection: Dictionary<T>, n: number): T[];
}
interface LoDashImplicitArrayWrapper<T> {
/**
* @see _.sample
**/
sample(n: number): LoDashImplicitArrayWrapper<T>;
/**
* @see _.sample
**/
sample(): LoDashImplicitWrapper<T>;
}
//_.select
interface LoDashStatic {
/**
@@ -8217,6 +8167,141 @@ declare module _ {
select<W, T>(predicate: W): LoDashExplicitArrayWrapper<T>;
}
//_.sample
interface LoDashStatic {
/**
* Gets a random element or n random elements from a collection.
*
* @param collection The collection to sample.
* @return Returns the random sample(s) of collection.
*/
sample<T>(
collection: List<T>|Dictionary<T>|NumericDictionary<T>,
n: number
): T[];
/**
* @see _.sample
*/
sample<O extends Object, T>(
collection: O,
n: number
): T[];
/**
* @see _.sample
*/
sample<T>(
collection: Object,
n: number
): T[];
/**
* @see _.sample
*/
sample<T>(
collection: List<T>|Dictionary<T>|NumericDictionary<T>
): T;
/**
* @see _.sample
*/
sample<O extends Object, T>(
collection: O
): T;
/**
* @see _.sample
*/
sample<T>(
collection: Object
): T;
}
interface LoDashImplicitWrapper<T> {
/**
* @see _.sample
*/
sample(
n: number
): LoDashImplicitArrayWrapper<string>;
/**
* @see _.sample
*/
sample(): string;
}
interface LoDashImplicitArrayWrapper<T> {
/**
* @see _.sample
*/
sample(
n: number
): LoDashImplicitArrayWrapper<T>;
/**
* @see _.sample
*/
sample(): T;
}
interface LoDashImplicitObjectWrapper<T> {
/**
* @see _.sample
*/
sample<T>(
n: number
): LoDashImplicitArrayWrapper<T>;
/**
* @see _.sample
*/
sample<T>(): T;
}
interface LoDashExplicitWrapper<T> {
/**
* @see _.sample
*/
sample(
n: number
): LoDashExplicitArrayWrapper<string>;
/**
* @see _.sample
*/
sample(): LoDashExplicitWrapper<string>;
}
interface LoDashExplicitArrayWrapper<T> {
/**
* @see _.sample
*/
sample(
n: number
): LoDashExplicitArrayWrapper<T>;
/**
* @see _.sample
*/
sample<TWrapper>(): TWrapper;
}
interface LoDashExplicitObjectWrapper<T> {
/**
* @see _.sample
*/
sample<T>(
n: number
): LoDashExplicitArrayWrapper<T>;
/**
* @see _.sample
*/
sample<TWrapper>(): TWrapper;
}
//_.shuffle
interface LoDashStatic {
/**
+95 -6
View File
@@ -5042,14 +5042,103 @@ namespace TestReject {
}
// _.sample
result = <number>_.sample([1, 2, 3, 4]);
result = <_.LoDashImplicitWrapper<number>>_([1, 2, 3, 4]).sample();
result = <number>_([1, 2, 3, 4]).sample().value();
namespace TestSample {
let array: string[];
let list: _.List<string>;
let dictionary: _.Dictionary<string>;
let numericDictionary: _.NumericDictionary<string>;
{
let result: string;
result = _.sample('abc');
result = _.sample(array);
result = _.sample(list);
result = _.sample(dictionary);
result = _.sample(numericDictionary);
result = _.sample<{a: string}, string>({a: 'foo'});
result = _.sample<string>({a: 'foo'});
result = _('abc').sample();
result = _(array).sample();
result = _(list).sample<string>();
result = _(dictionary).sample<string>();
result = _(numericDictionary).sample<string>();
result = _({a: 'foo'}).sample<string>();
}
{
let result: _.LoDashExplicitWrapper<string>;
result = _('abc').chain().sample();
result = _(array).chain().sample<_.LoDashExplicitWrapper<string>>();
result = _(list).chain().sample<_.LoDashExplicitWrapper<string>>();
result = _(dictionary).chain().sample<_.LoDashExplicitWrapper<string>>();
result = _(numericDictionary).chain().sample<_.LoDashExplicitWrapper<string>>();
result = _({a: 'foo'}).chain().sample<_.LoDashExplicitWrapper<string>>();
}
}
// _.sampleSize
result = <number[]>_.sampleSize([1, 2, 3, 4], 2);
result = <_.LoDashImplicitArrayWrapper<number>>_([1, 2, 3, 4]).sampleSize(2);
result = <number[]>_([1, 2, 3, 4]).sampleSize(2).value();
namespace TestSampleSize {
let array: string[];
let list: _.List<string>;
let dictionary: _.Dictionary<string>;
let numericDictionary: _.NumericDictionary<string>;
{
let result: string[];
result = _.sampleSize('abc');
result = _.sampleSize('abc', 42);
result = _.sampleSize(array);
result = _.sampleSize(array, 42);
result = _.sampleSize(list);
result = _.sampleSize(list, 42);
result = _.sampleSize(dictionary);
result = _.sampleSize(dictionary, 42);
result = _.sampleSize(numericDictionary);
result = _.sampleSize(numericDictionary, 42);
result = _.sampleSize<{a: string}, string>({a: 'foo'});
result = _.sampleSize<{a: string}, string>({a: 'foo'}, 42);
result = _.sampleSize<string>({a: 'foo'});
result = _.sampleSize<string>({a: 'foo'}, 42);
}
{
let result: _.LoDashImplicitArrayWrapper<string>;
result = _('abc').sampleSize();
result = _('abc').sampleSize(42);
result = _(array).sampleSize();
result = _(array).sampleSize(42);
result = _(list).sampleSize<string>();
result = _(list).sampleSize<string>(42);
result = _(dictionary).sampleSize<string>();
result = _(dictionary).sampleSize<string>(42);
result = _(numericDictionary).sampleSize<string>();
result = _(numericDictionary).sampleSize<string>(42);
result = _({a: 'foo'}).sampleSize<string>();
result = _({a: 'foo'}).sampleSize<string>(42);
}
{
let result: _.LoDashExplicitArrayWrapper<string>;
result = _('abc').chain().sampleSize();
result = _('abc').chain().sampleSize(42);
result = _(array).chain().sampleSize();
result = _(array).chain().sampleSize(42);
result = _(list).chain().sampleSize<string>();
result = _(list).chain().sampleSize<string>(42);
result = _(dictionary).chain().sampleSize<string>();
result = _(dictionary).chain().sampleSize<string>(42);
result = _(numericDictionary).chain().sampleSize<string>();
result = _(numericDictionary).chain().sampleSize<string>(42);
result = _({a: 'foo'}).chain().sampleSize<string>();
result = _({a: 'foo'}).chain().sampleSize<string>(42);
}
}
// _.shuffle
namespace TestShuffle {
+119 -41
View File
@@ -8981,77 +8981,155 @@ declare module _ {
//_.sample
interface LoDashStatic {
/**
* Gets a random element from `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
* @param collection The collection to sample.
* @return Returns the random element.
*/
sample<T>(collection: Array<T>): T;
sample<T>(
collection: List<T>|Dictionary<T>|NumericDictionary<T>
): T;
/**
* @see _.sample
**/
sample<T>(collection: List<T>): T;
* @see _.sample
*/
sample<O extends Object, T>(
collection: O
): T;
/**
* @see _.sample
**/
sample<T>(collection: Dictionary<T>): T;
* @see _.sample
*/
sample<T>(
collection: Object
): T;
}
interface LoDashImplicitWrapper<T> {
/**
* @see _.sample
*/
sample(): string;
}
interface LoDashImplicitArrayWrapper<T> {
/**
* @see _.sample
**/
sample(): LoDashImplicitWrapper<T>;
*/
sample(): T;
}
interface LoDashImplicitObjectWrapper<T> {
/**
* @see _.sample
*/
sample<T>(): T;
}
interface LoDashExplicitWrapper<T> {
/**
* @see _.sample
*/
sample(): LoDashExplicitWrapper<string>;
}
interface LoDashExplicitArrayWrapper<T> {
/**
* @see _.sample
*/
sample<TWrapper>(): TWrapper;
}
interface LoDashExplicitObjectWrapper<T> {
/**
* @see _.sample
*/
sample<TWrapper>(): TWrapper;
}
//_.sampleSize
interface LoDashStatic {
/**
* Gets `n` random elements from `collection`.
* Gets n random elements at unique keys from collection up to the size of 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]
* @param collection The collection to sample.
* @param n The number of elements to sample.
* @return Returns the random elements.
*/
sampleSize<T>(collection: Array<T>, n: number): T[];
sampleSize<T>(
collection: List<T>|Dictionary<T>|NumericDictionary<T>,
n?: number
): T[];
/**
* @see _.sampleSize
**/
sampleSize<T>(collection: List<T>, n: number): T[];
* @see _.sampleSize
*/
sampleSize<O extends Object, T>(
collection: O,
n?: number
): T[];
/**
* @see _.sampleSize
**/
sampleSize<T>(collection: Dictionary<T>, n: number): T[];
* @see _.sampleSize
*/
sampleSize<T>(
collection: Object,
n?: number
): T[];
}
interface LoDashImplicitWrapper<T> {
/**
* @see _.sampleSize
*/
sampleSize(
n?: number
): LoDashImplicitArrayWrapper<string>;
}
interface LoDashImplicitArrayWrapper<T> {
/**
* @see _.sampleSize
**/
sampleSize(n: number): LoDashImplicitArrayWrapper<T>;
*/
sampleSize(
n?: number
): LoDashImplicitArrayWrapper<T>;
}
interface LoDashImplicitObjectWrapper<T> {
/**
* @see _.sampleSize
**/
sampleSize(): LoDashImplicitWrapper<T>;
*/
sampleSize<T>(
n?: number
): LoDashImplicitArrayWrapper<T>;
}
interface LoDashExplicitWrapper<T> {
/**
* @see _.sampleSize
*/
sampleSize(
n?: number
): LoDashExplicitArrayWrapper<string>;
}
interface LoDashExplicitArrayWrapper<T> {
/**
* @see _.sampleSize
*/
sampleSize(
n?: number
): LoDashExplicitArrayWrapper<T>;
}
interface LoDashExplicitObjectWrapper<T> {
/**
* @see _.sampleSize
*/
sampleSize<T>(
n?: number
): LoDashExplicitArrayWrapper<T>;
}
//_.shuffle