TreasureTrails/node_modules/remeda/dist/sample.cjs.map

1 line
9 KiB
Text
Raw Permalink Normal View History

2026-03-18 09:02:21 -05:00
{"version":3,"file":"sample.cjs","names":["purry"],"sources":["../src/sample.ts"],"sourcesContent":["import type {\n FixedLengthArray,\n IsEqual,\n IsNever,\n NonNegativeInteger,\n Or,\n Writable,\n} from \"type-fest\";\nimport type { CoercedArray } from \"./internal/types/CoercedArray\";\nimport type { IterableContainer } from \"./internal/types/IterableContainer\";\nimport type { NTuple } from \"./internal/types/NTuple\";\nimport type { PartialArray } from \"./internal/types/PartialArray\";\nimport type { TupleParts } from \"./internal/types/TupleParts\";\nimport { purry } from \"./purry\";\n\ntype Sampled<T extends IterableContainer, N extends number> =\n Or<IsEqual<N, 0>, IsEqual<T[\"length\"], 0>> extends true\n ? // Short-circuit on trivial inputs.\n []\n : IsNever<NonNegativeInteger<N>> extends true\n ? SampledPrimitive<T>\n : IsLongerThan<T, N> extends true\n ? SampledLiteral<T, N>\n : // If our tuple can never fulfil the sample size the only valid sample\n // is the whole input tuple. Because it's a shallow clone we also\n // strip any readonly-ness.\n Writable<T>;\n\n/**\n * When N is not a non-negative integer **literal** we can't use it in our\n * reconstructing logic so we fallback to a simpler definition of the output of\n * sample, which is any sub-tuple shape of T, of **any length**.\n */\ntype SampledPrimitive<T extends IterableContainer> = [\n ...FixedSubTuples<TupleParts<T>[\"required\"]>,\n // TODO: This might be accurate, but We currently have no tests that check optional elements!\n ...PartialArray<FixedSubTuples<TupleParts<T>[\"optional\"]>>,\n ...CoercedArray<TupleParts<T>[\"item\"]>,\n ...FixedSubTuples<TupleParts<T>[\"suffix\"]>,\n];\n\n/**\n * Knowing N is a non-negative literal integer we can construct all sub-tuples\n * of T that are exactly N elements long.\n */\ntype SampledLiteral<T extends IterableContainer, N extends number> =\n | Extract<\n FixedSubTuples<\n [\n ...TupleParts<T>[\"required\"],\n // TODO: This deliberately ignores optional elements which we don't have tests for either. In order to handle optional elements we can treat the \"optional\" tuple-part as more required elements.\n // We add N elements of the `item` type to the tuple so that we\n // consider any combination possible of elements of the prefix items,\n // any amount of rest items, and suffix items.\n ...(IsNever<TupleParts<T>[\"item\"]> extends true\n ? []\n : NTuple<TupleParts<T>[\"item\"], N>),\n ...TupleParts<T>[\"suffix\"],\n ]\n >,\n // This is just [unknown, unknown, ..., unknown] with N elements.\n FixedLengthArray<unknown, N>\n >\n // In addition to all sub-tuples of length N, we also need to consider all\n // tuples where the input is shorter than N. This will contribute exactly\n // one sub-tuple at each length from the minimum length of T and up to N-1.\n | SubSampled<\n TupleParts<T>[\"required\"],\n // TODO: This deliberately ignores optional elements which we don't have tests for either. In order to handle optional elements we can treat the \"optional\" tuple-part as more required elements.\n TupleParts<T>[\"item\"],\n TupleParts<T>[\"suffix\"],\n N\n >;\n\n// We want to create a union of all sub-tuples where we incrementally add an\n// additional element of the type of the rest element in the middle between the\n// prefix and suffix until we \"fill\" the tuple to size N.\ntype SubSampled<\n Prefix extends readonly unknown[],\n Item,\n Suffix extends readonly unknown[],\n N extends number,\n> =\n IsLongerThan<[...Prefix, ...Suffix], N> extends true\n ? // We need to prevent overflows in case Prefix and Suffix are already long\n // enough\n never\n : [...Prefix, ...Suffix][\"length\"] extends N\n ? never\n : [...Prefix, ...Suffix] | SubSampled<[...Prefix, Item], Item, Suffix, N>;\n\ntype IsLongerThan<T extends readonly unknown[], N extends numbe