renovate/lib/util/index.ts
Sergei Zharinov fbbbce608e
chore: Configure fs local dirs via admin config (#9990)
Co-authored-by: Rhys Arkins <rhys@arkins.net>
2021-05-17 13:21:28 +00:00

17 lines
538 B
TypeScript

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