0
0
mirror of https://github.com/renovatebot/renovate.git synced 2024-12-22 05:28:35 +00:00
renovatebot_renovate/lib/util/sample.ts
2023-06-27 08:27:19 +00:00

18 lines
522 B
TypeScript

export function sampleSize(array: string[], n: number): string[] {
const length = array ? array.length : 0;
if (!length || n < 1) {
return [];
}
const sampleNumber = n > length ? length : n;
let index = 0;
const lastIndex = length - 1;
const result = [...array];
while (index < sampleNumber) {
const rand = index + Math.floor(Math.random() * (lastIndex - index + 1));
[result[rand], result[index]] = [result[index], result[rand]];
index += 1;
}
return result.slice(0, sampleNumber);
}