mirror of
https://github.com/logos-messaging/logos-delivery-js.git
synced 2026-03-14 13:43:16 +00:00
* chore: update noise * update: package.lock * update: @chainsafe/libp2p-gossipsub * rm unwanted libp2p interface deps & bump up libp2p * refactor code for new deps * update: new package.lock * setup prettier, refactor eslint and rm trailing commas * update package.lock * fix build * import type for interface * fix imports for merge * update typedoc exports * add: CustomEvent import * use new libp2p interface * add aegir as dev dep for tests
31 lines
613 B
TypeScript
31 lines
613 B
TypeScript
/**
|
|
* Return pseudo random subset of the input.
|
|
*/
|
|
export function getPseudoRandomSubset<T>(
|
|
values: T[],
|
|
wantedNumber: number
|
|
): T[] {
|
|
if (values.length <= wantedNumber || values.length <= 1) {
|
|
return values;
|
|
}
|
|
|
|
return shuffle(values).slice(0, wantedNumber);
|
|
}
|
|
|
|
function shuffle<T>(arr: T[]): T[] {
|
|
if (arr.length <= 1) {
|
|
return arr;
|
|
}
|
|
const randInt = (): number => {
|
|
return Math.floor(Math.random() * Math.floor(arr.length));
|
|
};
|
|
|
|
for (let i = 0; i < arr.length; i++) {
|
|
const j = randInt();
|
|
const tmp = arr[i];
|
|
arr[i] = arr[j];
|
|
arr[j] = tmp;
|
|
}
|
|
return arr;
|
|
}
|