UNPKG

typedash

Version:

modern, type-safe collection of utility functions

29 lines (27 loc) 698 B
'use strict'; // src/functions/sample/sample.ts function sample(source, count = 1) { if (source == null) { return void 0; } const sourceArray = [...source]; if (sourceArray.length === 0) { return void 0; } 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; } exports.sample = sample; //# sourceMappingURL=out.js.map //# sourceMappingURL=index.cjs.map