typedash
Version:
modern, type-safe collection of utility functions
1 lines • 3.39 kB
Source Map (JSON)
{"version":3,"sources":["../../src/functions/sample/sample.ts"],"names":[],"mappings":";AA8DO,SAAS,OACd,QACA,QAAQ,GACa;AACrB,MAAI,UAAU,MAAM;AAClB,WAAO;AAAA,EACT;AAEA,QAAM,cAAc,CAAC,GAAG,MAAM;AAC9B,MAAI,YAAY,WAAW,GAAG;AAC5B,WAAO;AAAA,EACT;AAEA,QAAM,cAAc,KAAK,IAAI,OAAO,YAAY,MAAM;AAEtD,QAAM,UAAU,oBAAI,IAAY;AAChC,SAAO,QAAQ,OAAO,aAAa;AACjC,YAAQ,IAAI,KAAK,MAAM,KAAK,OAAO,IAAI,YAAY,MAAM,CAAC;AAAA,EAC5D;AAEA,QAAM,SAAc,CAAC;AAErB,aAAW,SAAS,SAAS;AAE3B,WAAO,KAAK,YAAY,KAAK,CAAE;AAAA,EACjC;AAEA,MAAI,OAAO,WAAW,GAAG;AACvB,WAAO,OAAO,CAAC;AAAA,EACjB;AAEA,SAAO;AACT","sourcesContent":["import { 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 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- 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"]}