{"version":3,"file":"sampleSize.cjs","names":[],"sources":["../../src/collection/sampleSize.ts"],"sourcesContent":["/**\n * Gets n random elements at unique keys from collection up to the size of collection\n * @param collection - The collection to sample\n * @param n - The number of elements to sample\n * @returns Returns the array of sampled elements\n * @example\n * const arr = [1, 2, 3, 4, 5];\n * sampleSize(arr, 2); // => random 2 elements from arr\n */\nexport function sampleSize<T>(collection: T[], n: number): T[] {\n  if (n <= 0 || collection.length === 0) {\n    return [];\n  }\n\n  const size = Math.min(n, collection.length);\n  const result: T[] = [];\n  const indices = new Set<number>();\n\n  while (indices.size < size) {\n    const randomIndex = Math.floor(Math.random() * collection.length);\n    if (!indices.has(randomIndex)) {\n      indices.add(randomIndex);\n      result.push(collection[randomIndex]);\n    }\n  }\n\n  return result;\n}\n"],"mappings":";;;;;;;;;;;AASA,SAAgB,WAAc,YAAiB,GAAgB;CAC7D,IAAI,KAAK,KAAK,WAAW,WAAW,GAClC,OAAO,CAAC;CAGV,MAAM,OAAO,KAAK,IAAI,GAAG,WAAW,MAAM;CAC1C,MAAM,SAAc,CAAC;CACrB,MAAM,0BAAU,IAAI,IAAY;CAEhC,OAAO,QAAQ,OAAO,MAAM;EAC1B,MAAM,cAAc,KAAK,MAAM,KAAK,OAAO,IAAI,WAAW,MAAM;EAChE,IAAI,CAAC,QAAQ,IAAI,WAAW,GAAG;GAC7B,QAAQ,IAAI,WAAW;GACvB,OAAO,KAAK,WAAW,YAAY;EACrC;CACF;CAEA,OAAO;AACT"}