UNPKG

typedash

Version:

modern, type-safe collection of utility functions

23 lines (22 loc) 800 B
//#region src/functions/sample/sample.ts /** * Implementation for all overloads. * @param source The iterable to get a random item from. * @param count The number of items to return. * @returns A random item from the input iterable. */ function sample(source, count = 1) { if (source == null) return; const sourceArray = [...source]; if (sourceArray.length === 0) return; const sampleCount = Math.min(count, sourceArray.length); const indices = /* @__PURE__ */ new Set(); while (indices.size < sampleCount) indices.add(Math.floor(Math.random() * sourceArray.length)); const result = []; for (const index of indices) result.push(sourceArray[index]); if (result.length === 1) return result[0]; return result; } //#endregion export { sample as t }; //# sourceMappingURL=sample-DteeLSDE.js.map