@yanick/remeda-extra
Version:
A handful of added functions for Remeda
46 lines (45 loc) • 1.84 kB
TypeScript
export interface SampleType {
size: number;
repeating: boolean;
}
/**
* Returns random elements of the array.
* @param items the array
* @param options The number of samples to take. If the same item can be
* picked multiple times, you can pass the object `{ size: number, repeating: boolean }`.
* If the number of samples to return exceeds the array size, and `repeating`
* is false, returns a number of sample equals to the size of the array.
* @signature
* R.sample(array,size)
* R.sample(array, {size, repeating})
* @example
* R.sample([1,2,3,4],2); // [3,1]
* R.sample([1,2,3,4],{size:5, repeating: true}); // [3,1,2,2,4]
* R.sample([1,2,3,4],{size:5, repeating: false}); // [3,1,2,4]
* R.pipe(
* [{a: 5}, {a: 1}, {a: 3}],
* R.sumBy(x => x.a)
* ) // 9
* @data_first
* @category Array
*/
export declare function sample<T>(items: ReadonlyArray<T>, size: number): Array<T>;
export declare function sample<T>(items: ReadonlyArray<T>, options: SampleType): Array<T>;
/**
* Returns random elements of the array.
* @param options The number of samples to take. If the same item can be
* picked multiple times, you can pass the object `{ size: number, repeating: boolean }`.
* If the number of samples to return exceeds the array size, and `repeating`
* is false, returns a number of sample equals to the size of the array.
* @signature
* R.sample(size)(array)
* R.sample({size, repeating})(array)
* @example
* R.sample(2)([1,2,3,4]); // [3,1]
* R.sample({size:5, repeating: true},[1,2,3,4]); // [3,1,2,2,4]
* R.sample({size:5, repeating: false},[1,2,3,4]); // [3,1,2,4]
* @data_last
* @category Array
*/
export declare function sample(size: number): <T>(items: Array<T>) => Array<T>;
export declare function sample(options: SampleType): <T>(items: Array<T>) => Array<T>;