typedash
Version:
modern, type-safe collection of utility functions
1 lines • 3.31 kB
Source Map (JSON)
{"version":3,"file":"sample-BNv7gEmd.cjs","names":[],"sources":["../src/functions/sample/sample.ts"],"sourcesContent":["import type { Maybe } from '../../types';\n\n/**\n * Returns a random item from the input iterable.\n * @param source The iterable to get a random item from.\n * @returns A random item from the input iterable.\n * @example\n * ```ts\n * sample([1, 2, 3]) // 2\n * ```\n */\nexport function sample<const T>(source: Iterable<T>): T;\n/**\n * Returns N random items from the input iterable.\n * If the input iterable has less than N items, all items will be returned.\n * @param source The iterable to get a random item from.\n * @param count The number of items to return.\n * @returns A random item from the input iterable.\n * @example\n * ```ts\n * sample([1, 2, 3], 2) // [2, 3]\n * sample([1, 2, 3], 4) // [1, 2, 3]\n * sample([1, 2, 3], 0) // []\n * sample([1, 2, 3], -1) // []\n * ```\n */\nexport function sample<const T>(source: Iterable<T>, count: number): T[];\n/**\n * Returns a random item from the input iterable.\n * @param source The iterable to get a random item from.\n * @returns A random item from the input iterable.\n * @example\n * ```ts\n * sample(null) // undefined\n * sample(undefined) // undefined\n * ```\n */\nexport function sample<const T>(source: Maybe<Iterable<T>>): T | undefined;\n/**\n * Returns N random items from the input iterable.\n * If the input iterable has less than N items, all items will be returned.\n * @param source The iterable to get a random item from.\n * @param count The number of items to return.\n * @returns A random item from the input iterable.\n * @example\n * ```ts\n * sample([1, 2, 3], 2) // [2, 3]\n * sample([1, 2, 3], 4) // [1, 2, 3]\n * sample([1, 2, 3], 0) // []\n * sample([1, 2, 3], -1) // []\n * ```\n */\nexport function sample<const T>(\n source: Maybe<Iterable<T>>,\n count: number\n): T[] | undefined;\n/**\n * Implementation for all overloads.\n * @param source The iterable to get a random item from.\n * @param count The number of items to return.\n * @returns A random item from the input iterable.\n */\nexport function sample<const T>(\n source: Maybe<Iterable<T>>,\n count = 1\n): T | T[] | undefined {\n if (source == null) {\n return undefined;\n }\n\n const sourceArray = [...source];\n if (sourceArray.length === 0) {\n return undefined;\n }\n\n const sampleCount = Math.min(count, sourceArray.length);\n\n const indices = new Set<number>();\n while (indices.size < sampleCount) {\n indices.add(Math.floor(Math.random() * sourceArray.length));\n }\n\n const result: T[] = [];\n\n for (const index of indices) {\n // biome-ignore lint/style/noNonNullAssertion: we already know the index is valid because we generated to be within the bounds of the array\n result.push(sourceArray[index]!);\n }\n\n if (result.length === 1) {\n return result[0];\n }\n\n return result;\n}\n"],"mappings":";;;;;;;;AA8DA,SAAgB,OACd,QACA,QAAQ,GACa;AACrB,KAAI,UAAU,KACZ;CAGF,MAAM,cAAc,CAAC,GAAG,OAAO;AAC/B,KAAI,YAAY,WAAW,EACzB;CAGF,MAAM,cAAc,KAAK,IAAI,OAAO,YAAY,OAAO;CAEvD,MAAM,0BAAU,IAAI,KAAa;AACjC,QAAO,QAAQ,OAAO,YACpB,SAAQ,IAAI,KAAK,MAAM,KAAK,QAAQ,GAAG,YAAY,OAAO,CAAC;CAG7D,MAAM,SAAc,EAAE;AAEtB,MAAK,MAAM,SAAS,QAElB,QAAO,KAAK,YAAY,OAAQ;AAGlC,KAAI,OAAO,WAAW,EACpB,QAAO,OAAO;AAGhB,QAAO"}