TreasureTrails/node_modules/remeda/dist/purryOrderRules-Si4oUxGJ.cjs.map

1 line
7.6 KiB
Text
Raw Permalink Normal View History

2026-03-18 09:02:21 -05:00
{"version":3,"file":"purryOrderRules-Si4oUxGJ.cjs","names":["arg: unknown","argRemoved: readonly unknown[]"],"sources":["../src/internal/purryOrderRules.ts"],"sourcesContent":["import type { CompareFunction } from \"./types/CompareFunction\";\nimport type { NonEmptyArray } from \"./types/NonEmptyArray\";\n\n// We define the comparators in a global const so that they are only\n// instantiated once, and so we can couple a label (string) for them that could\n// be used in runtime to refer to them (e.g. \"asc\", \"desc\").\nconst COMPARATORS = {\n asc: <T>(x: T, y: T) => x > y,\n desc: <T>(x: T, y: T) => x < y,\n} as const;\n\n/**\n * An order rule defines a projection/extractor that returns a comparable from\n * the data being compared. It would be run on each item being compared, and a\n * comparator would then be used on the results to determine the order.\n *\n * There are 2 forms of the order rule, a simple one which only provides the\n * projection function and assumes ordering is ascending, and a 2-tuple where\n * the first element is the projection function and the second is the direction;\n * this allows changing the direction without defining a more complex projection\n * to simply negate the value (e.g. `(x) => -x`).\n *\n * We rely on the javascript implementation of `<` and `>` for comparison, which\n * will attempt to transform both operands into a primitive comparable value via\n * the built in `valueOf` function (and then `toString`). It's up to the caller\n * to make sure that the projection is returning a value that makes sense for\n * this logic.\n *\n * It's important to note that there is no built-in caching/memoization of\n * projection function and therefore no guarantee that it would only be called\n * once.\n */\nexport type OrderRule<T> =\n | Projection<T>\n | readonly [projection: Projection<T>, direction: keyof typeof COMPARATORS];\n\ntype Projection<T> = (x: T) => Comparable;\n\n// We define the Comparable based on how JS coerces values into primitives when\n// used with the `<` and `>` operators.\n// @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#type_coercion\ntype Comparable =\n | ComparablePrimitive\n | { [Symbol.toPrimitive]: (hint: string) => ComparablePrimitive }\n | { toString: () => string }\n | { valueOf: () => ComparablePrimitive };\n\n// Notice that `boolean` is special in that it is coerced as a number (0 for\n// `false`, 1 for `true`) implicitly.\ntype ComparablePrimitive = bigint | boolean | number | string;\n\n/**\n * Allows functions that want to handle a variadic number of order rules a\n * a simplified API that hides most of the implementation details. The only\n * thing users of this function need to do is provide a function that would take\n * the data, and a compare function that can be used to determine the order\n * between the items of the array.\n * This functions takes care of the rest; it will parse rules, built the\n * comparer, and manage the purrying of the input arguments.\n */\nexport function purryOrderRules<T>(\n func: (data: readonly T[], compareFn: CompareFunction<T>) => unknown,\n inputArgs: readonly unknown[],\n): unknown {\n // We rely on casting blindly here, but we rely on casting blindly everywhere\n // else when we call purry so it's fine...\n const [dataOrRule, ...rules] = inputArgs as\n | Readonly<NonEmptyArray<OrderRule<T>>>\n | [\n data: OrderRule<T> | readonly T[],\n ...rules: Readonly<NonEmptyArray<OrderRule<T>>>,\n ];\n\n if (!isOrderRule<T>(dataOrRule)) {\n // dataFirst!\n\n // @ts-expect-error [ts2556]: Typescript is failing to infer the type of rules\n // correctly here after the type refinement above, rules should be non-empty\n // when we get here.\n const compareFn = orderRuleComparer(...rules);\n return func(dataOrRule, compareFn);\n }\n\n // dataLast!\n\n // Important: initialize the comparer outside of the returned function so it\n // it's constructed and shared everywhere (it's stateless so should be safe\n // if used multiple times).\n const