ts-data-forge 14.0.1 → 14.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -5
- package/dist/array/impl/array-utils-transformation.d.mts +4 -1
- package/dist/array/impl/array-utils-transformation.d.mts.map +1 -1
- package/dist/array/impl/array-utils-transformation.mjs +4 -5
- package/dist/array/impl/array-utils-transformation.mjs.map +1 -1
- package/dist/array/impl/array-utils-validation.d.mts +8 -3
- package/dist/array/impl/array-utils-validation.d.mts.map +1 -1
- package/dist/array/impl/array-utils-validation.mjs.map +1 -1
- package/dist/collections/imap-mapped.mjs +1 -1
- package/dist/collections/imap-mapped.mjs.map +1 -1
- package/dist/collections/imap.mjs +1 -1
- package/dist/collections/imap.mjs.map +1 -1
- package/dist/functional/match.d.mts.map +1 -1
- package/dist/functional/match.mjs.map +1 -1
- package/dist/functional/optional/impl/optional-map.d.mts +1 -1
- package/dist/functional/optional/impl/optional-map.d.mts.map +1 -1
- package/dist/functional/optional/impl/optional-map.mjs.map +1 -1
- package/dist/functional/result/impl/result-map-err.d.mts +1 -1
- package/dist/functional/result/impl/result-map-err.d.mts.map +1 -1
- package/dist/functional/result/impl/result-map-err.mjs.map +1 -1
- package/dist/functional/result/impl/result-map.d.mts +1 -1
- package/dist/functional/result/impl/result-map.d.mts.map +1 -1
- package/dist/functional/result/impl/result-map.mjs.map +1 -1
- package/dist/number/branded-types/int.d.mts +1 -1
- package/dist/number/branded-types/int16.d.mts +1 -1
- package/dist/number/branded-types/int32.d.mts +1 -1
- package/dist/number/branded-types/non-negative-int16.d.mts +1 -1
- package/dist/number/branded-types/non-negative-int32.d.mts +1 -1
- package/dist/number/branded-types/non-positive-finite-number.mjs +1 -1
- package/dist/number/branded-types/non-positive-finite-number.mjs.map +1 -1
- package/dist/number/branded-types/positive-int.d.mts +1 -1
- package/dist/number/branded-types/positive-int16.d.mts +1 -1
- package/dist/number/branded-types/positive-int32.d.mts +1 -1
- package/dist/number/branded-types/positive-safe-int.d.mts +1 -1
- package/dist/number/branded-types/positive-uint16.d.mts +1 -1
- package/dist/number/branded-types/positive-uint32.d.mts +1 -1
- package/dist/number/branded-types/safe-int.d.mts +1 -1
- package/dist/number/branded-types/safe-uint.d.mts +1 -1
- package/dist/number/branded-types/uint.d.mts +1 -1
- package/dist/number/branded-types/uint16.d.mts +1 -1
- package/dist/number/branded-types/uint32.d.mts +1 -1
- package/dist/number/enum/int8.d.mts +2 -2
- package/dist/number/enum/int8.d.mts.map +1 -1
- package/dist/number/enum/int8.mjs.map +1 -1
- package/dist/number/enum/uint8.d.mts +2 -2
- package/dist/number/enum/uint8.d.mts.map +1 -1
- package/dist/number/enum/uint8.mjs.map +1 -1
- package/dist/number/num-arithmetic-types.d.mts +8 -9
- package/dist/number/num-arithmetic-types.d.mts.map +1 -1
- package/dist/number/refined-number-utils.d.mts +2 -2
- package/dist/number/refined-number-utils.d.mts.map +1 -1
- package/dist/number/refined-number-utils.mjs.map +1 -1
- package/dist/object/object.d.mts +12 -12
- package/dist/object/object.d.mts.map +1 -1
- package/dist/object/object.mjs +5 -1
- package/dist/object/object.mjs.map +1 -1
- package/dist/others/fast-deep-equal.d.mts.map +1 -1
- package/dist/others/fast-deep-equal.mjs +2 -2
- package/dist/others/fast-deep-equal.mjs.map +1 -1
- package/package.json +2 -2
- package/src/array/impl/array-utils-modification.test.mts +26 -5
- package/src/array/impl/array-utils-shape-invariants.test.mts +259 -17
- package/src/array/impl/array-utils-slicing.test.mts +11 -3
- package/src/array/impl/array-utils-transformation.mts +41 -18
- package/src/array/impl/array-utils-transformation.test.mts +10 -3
- package/src/array/impl/array-utils-validation.mts +54 -10
- package/src/array/impl/array-utils-validation.test.mts +153 -3
- package/src/collections/imap-mapped.mts +1 -1
- package/src/collections/imap-mapped.test.mts +2 -2
- package/src/collections/imap.mts +1 -1
- package/src/collections/iset-mapped.test.mts +1 -1
- package/src/functional/match.mts +5 -4
- package/src/functional/optional/impl/optional-map.mts +16 -1
- package/src/functional/optional.test.mts +48 -1
- package/src/functional/result/impl/result-map-err.mts +16 -1
- package/src/functional/result/impl/result-map.mts +16 -1
- package/src/functional/result.test.mts +83 -3
- package/src/guard/is-primitive.test.mts +1 -1
- package/src/guard/is-record.test.mts +4 -4
- package/src/guard/is-type.test.mts +2 -2
- package/src/number/branded-types/finite-number.test.mts +2 -2
- package/src/number/branded-types/non-negative-finite-number.test.mts +2 -2
- package/src/number/branded-types/non-positive-finite-number.mts +1 -1
- package/src/number/branded-types/non-positive-finite-number.test.mts +5 -9
- package/src/number/branded-types/non-zero-finite-number.test.mts +2 -2
- package/src/number/branded-types/positive-finite-number.test.mts +2 -2
- package/src/number/enum/int8.mts +2 -1
- package/src/number/enum/uint8.mts +6 -2
- package/src/number/num-arithmetic-types.mts +125 -24
- package/src/number/num.test.mts +9 -9
- package/src/number/refined-number-utils.mts +2 -1
- package/src/object/object.mts +29 -17
- package/src/object/object.test.mts +2 -1
- package/src/others/fast-deep-equal.mts +6 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"refined-number-utils.mjs","sources":["../../src/number/refined-number-utils.mts"],"sourcesContent":["import {\n type Brand,\n type ChangeBaseBrand,\n type GetBrandValuePart,\n type Int,\n type IntersectBrand,\n type NonNegativeNumber,\n type NonZeroNumber,\n type PositiveNumber,\n type RelaxedExclude,\n type TypeEq,\n type UnknownBrand,\n type UnwrapBrandFalseKeys,\n type UnwrapBrandTrueKeys,\n type WithSmallInt,\n} from 'ts-type-forge';\nimport { expectType } from '../expect-type.mjs';\nimport { pipe } from '../functional/index.mjs';\nimport { Num } from './num.mjs';\n\n/** @internal */\nexport namespace TsDataForgeInternals {\n /**\n * Internal utilities for creating and managing refined (branded) number\n * types.\n *\n * This namespace provides factory functions and type utilities for building\n * type-safe numeric operations with compile-time constraints. It serves as\n * the foundation for all branded number types in the library, including:\n *\n * - Integer types (Int, SafeInt, Int8, Int16, Int32)\n * - Unsigned types (UInt, UInt8, UInt16, UInt32)\n * - Constrained types (NonZero, NonNegative, Positive)\n * - Range-bounded types\n *\n * The utilities handle:\n *\n * - Type validation and narrowing\n * - Arithmetic operations that preserve type constraints\n * - Automatic clamping for bounded types\n * - Random number generation within type bounds\n *\n * @internal This namespace is not part of the public API\n */\n export namespace RefinedNumberUtils {\n const castTypeImpl =\n <BrandedType extends number>(\n is: (n: number) => n is BrandedType,\n typeNameInErrorMessage: string,\n ) =>\n <N extends number>(a: N): BrandedType & N => {\n if (!is(a)) {\n throw new TypeError(`Expected ${typeNameInErrorMessage}, got: ${a}`);\n }\n\n return a;\n };\n\n type UnknownNumberBrand = ChangeBaseBrand<UnknownBrand, number>;\n\n /**\n * Converts a branded number type to include the Int brand.\n *\n * @template N - A branded number type\n * @internal\n */\n export type ToInt<N extends UnknownNumberBrand> = IntersectBrand<N, Int>;\n\n type ToNonZero<N extends UnknownNumberBrand> = IntersectBrand<\n N,\n NonZeroNumber\n >;\n\n const isNonZero = <N extends UnknownNumberBrand>(\n n: N,\n ): n is N & ToNonZero<N> => n !== 0;\n\n type ToNonZeroIntWithSmallInt<N extends Int> = WithSmallInt<\n CastToInt<ToNonZero<N>>\n >;\n\n /**\n * Converts a branded number type to include the NonNegativeNumber brand.\n *\n * @template N - A branded number type\n * @internal\n */\n export type ToNonNegative<N extends UnknownNumberBrand> =\n '!=0' extends UnwrapBrandTrueKeys<N>\n ? IntersectBrand<N, PositiveNumber>\n : IntersectBrand<N, NonNegativeNumber>;\n\n /**\n * Removes the non-zero brand constraint from a branded number type. Used\n * when operations may produce zero values.\n *\n * @template N - A branded number type\n * @internal\n */\n export type RemoveNonZeroBrandKey<N extends UnknownNumberBrand> = Brand<\n GetBrandValuePart<N>,\n RelaxedExclude<UnwrapBrandTrueKeys<N>, '!=0'> & string,\n RelaxedExclude<UnwrapBrandFalseKeys<N>, '<=0' | '>=0'> & string\n >;\n\n type CastToInt<N> = N extends Int ? N : never;\n\n /**\n * Generates a type-safe API for a branded number type based on its\n * characteristics.\n *\n * This type dynamically constructs an object type with appropriate methods\n * based on the number class. For example:\n *\n * - Integer types don't get floor/ceil/round methods\n * - Non-negative types don't get abs method\n * - Range-bounded types get MIN_VALUE/MAX_VALUE constants\n *\n * @template N - The branded number type\n * @template classes - Union of characteristics: 'int' | 'non-negative' |\n * 'positive' | 'range'\n * @internal\n */\n /** The closed binary arithmetic operations a branded namespace may expose. */\n export type NumberClassBinOp = 'add' | 'div' | 'mul' | 'sub';\n\n export type NumberClass<\n N extends UnknownNumberBrand,\n classes extends 'int' | 'non-negative' | 'positive' | 'range',\n binOps extends NumberClassBinOp = NumberClassBinOp,\n > = ('int' extends classes\n ? unknown\n : 'positive' extends classes\n ? Readonly<{\n floor: (x: N, y: N) => RemoveNonZeroBrandKey<ToInt<N>>;\n ceil: (x: N, y: N) => ToInt<N>;\n round: (x: N, y: N) => RemoveNonZeroBrandKey<ToInt<N>>;\n }>\n : Readonly<{\n floor: (x: N, y: N) => ToInt<N>;\n ceil: (x: N, y: N) => ToInt<N>;\n round: (x: N, y: N) => ToInt<N>;\n }>) &\n ('non-negative' extends classes\n ? Readonly<{\n MIN_VALUE: number;\n fromNumber: (a: number) => N;\n }>\n : unknown) &\n ('non-negative' extends classes\n ? unknown\n : 'positive' extends classes\n ? unknown\n : Readonly<{\n abs: (x: N) => ToNonNegative<N>;\n }>) &\n ('positive' extends classes\n ? Readonly<{\n MIN_VALUE: number;\n fromNumber: (a: number) => N;\n }>\n : unknown) &\n ('range' extends classes\n ? Readonly<{\n MIN_VALUE: number;\n MAX_VALUE: number;\n fromNumber: (a: number) => N;\n }>\n : unknown) &\n Pick<\n Readonly<{\n add: (x: N, y: N) => N;\n sub: (x: N, y: N) => N;\n mul: (x: N, y: N) => N;\n div: (x: N, y: ToNonZero<N>) => N;\n }>,\n binOps\n > &\n Readonly<{\n is: (a: number) => a is N;\n min: (...values: readonly N[]) => N;\n max: (...values: readonly N[]) => N;\n random: (min: N, max: N) => N;\n pow: (x: N, y: N) => N;\n }>;\n\n type BaseKeys =\n 'add' | 'div' | 'is' | 'max' | 'min' | 'mul' | 'pow' | 'random' | 'sub';\n\n type FloatMethods = 'ceil' | 'floor' | 'round';\n\n expectType<keyof NumberClass<UnknownNumberBrand, 'int'>, BaseKeys | 'abs'>(\n '=',\n );\n\n expectType<\n keyof NumberClass<UnknownNumberBrand, never>,\n BaseKeys | FloatMethods | 'abs'\n >('=');\n\n expectType<\n keyof NumberClass<UnknownNumberBrand, 'non-negative'>,\n BaseKeys | FloatMethods | 'fromNumber' | 'MIN_VALUE'\n >('=');\n\n expectType<\n keyof NumberClass<UnknownNumberBrand, 'positive'>,\n BaseKeys | FloatMethods | 'fromNumber' | 'MIN_VALUE'\n >('=');\n\n expectType<\n keyof NumberClass<UnknownNumberBrand, 'int' | 'range'>,\n BaseKeys | 'abs' | 'fromNumber' | 'MAX_VALUE' | 'MIN_VALUE'\n >('=');\n\n const isFnOrUndefined = (\n min: number | undefined,\n max: number | undefined,\n ): ((n: number) => boolean) | undefined =>\n min === undefined\n ? max === undefined\n ? undefined\n : (n) => n <= max\n : max === undefined\n ? (n) => min <= n\n : Num.isInRangeInclusive(min, max);\n\n const clampFnOrUndefined = (\n min: number | undefined,\n max: number | undefined,\n ): ((n: number) => number) | undefined =>\n min === undefined\n ? max === undefined\n ? undefined\n : (n) => Math.min(max, n)\n : max === undefined\n ? (n) => Math.max(min, n)\n : Num.clamp(min, max);\n\n type OperatorsForInteger<\n ElementType extends Int,\n MIN_VALUE extends number,\n MAX_VALUE extends number,\n ElementTypeWithSmallInt extends WithSmallInt<ElementType> =\n WithSmallInt<ElementType>,\n > = Readonly<{\n MIN_VALUE: MIN_VALUE;\n MAX_VALUE: MAX_VALUE;\n\n is: (a: number) => a is ElementType;\n\n abs: (x: ElementTypeWithSmallInt) => ToNonNegative<ElementType>;\n\n min: (...values: readonly ElementTypeWithSmallInt[]) => ElementType;\n\n max: (...values: readonly ElementTypeWithSmallInt[]) => ElementType;\n\n pow: (\n x: ElementTypeWithSmallInt,\n y: ElementTypeWithSmallInt,\n ) => ElementType;\n\n add: (\n x: ElementTypeWithSmallInt,\n y: ElementTypeWithSmallInt,\n ) => ElementType;\n\n sub: (\n x: ElementTypeWithSmallInt,\n y: ElementTypeWithSmallInt,\n ) => ElementType;\n\n mul: (\n x: ElementTypeWithSmallInt,\n y: ElementTypeWithSmallInt,\n ) => ElementType;\n\n div: (\n x: ElementTypeWithSmallInt,\n y: ToNonZeroIntWithSmallInt<ElementType>,\n ) => ElementType;\n\n random: (\n min?: ElementTypeWithSmallInt,\n max?: ElementTypeWithSmallInt,\n ) => ElementType;\n\n randomNonZero: (\n min?: ElementTypeWithSmallInt,\n max?: ElementTypeWithSmallInt,\n ) => ElementType;\n\n castType: <N extends number>(x: N) => ElementType & N;\n\n fromNumber: TypeEq<MAX_VALUE | MIN_VALUE, undefined> extends true\n ? undefined\n : (x: number) => ElementType;\n }>;\n\n /**\n * Factory function that creates a complete set of type-safe operations for\n * integer types.\n *\n * This function generates:\n *\n * - Type guards and validators\n * - Arithmetic operations that preserve type constraints\n * - Utility functions (min, max, abs, random)\n * - Automatic clamping for bounded types\n *\n * All operations ensure results remain within the type's constraints, using\n * clamping when bounds are specified.\n *\n * @example\n *\n * ```ts\n * const intOps = TsDataForgeInternals.RefinedNumberUtils.operatorsForInteger<\n * SafeInt,\n * number,\n * number\n * >({\n * integerOrSafeInteger: 'SafeInteger',\n * MIN_VALUE: Number.MIN_SAFE_INTEGER,\n * MAX_VALUE: Number.MAX_SAFE_INTEGER,\n * typeNameInMessage: 'SafeInt',\n * } as const);\n *\n * const six = intOps.castType(6);\n *\n * const four = intOps.castType(4);\n *\n * const sum = intOps.add(six, four);\n *\n * const difference = intOps.sub(six, four);\n *\n * const product = intOps.mul(six, four);\n *\n * const quotient = intOps.div(six, intOps.castType(2));\n *\n * const coerced = intOps.fromNumber(1.5);\n *\n * const randomValue = intOps.random();\n *\n * assert.isTrue(sum === 10);\n *\n * assert.isTrue(difference === 2);\n *\n * assert.isTrue(product === 24);\n *\n * assert.isTrue(quotient === 3);\n *\n * assert.isTrue(coerced === 2);\n *\n * assert.isTrue(Number.isSafeInteger(randomValue));\n * ```\n *\n * @template ElementType - The integer branded type\n * @template MIN_VALUE - Optional minimum value for bounded types\n * @template MAX_VALUE - Optional maximum value for bounded types\n * @param config - Configuration object\n * @param config.integerOrSafeInteger - Whether to use Number.isInteger or\n * Number.isSafeInteger\n * @param config.nonZero - If true, excludes zero from valid values\n * @param config.MIN_VALUE - Minimum valid value (inclusive)\n * @param config.MAX_VALUE - Maximum valid value (inclusive)\n * @param config.typeNameInMessage - Human-readable type name for error\n * messages\n * @returns Object containing all type-safe operations for the integer type\n * @internal\n */\n export const operatorsForInteger = <\n ElementType extends Int,\n MIN_VALUE extends number,\n MAX_VALUE extends number,\n >({\n integerOrSafeInteger,\n nonZero,\n MIN_VALUE,\n MAX_VALUE,\n typeNameInMessage,\n }: Readonly<{\n integerOrSafeInteger: 'Integer' | 'SafeInteger';\n nonZero?: boolean;\n MIN_VALUE: MIN_VALUE;\n MAX_VALUE: MAX_VALUE;\n typeNameInMessage: string;\n }>): OperatorsForInteger<ElementType, MIN_VALUE, MAX_VALUE> => {\n type ElementTypeWithSmallInt = WithSmallInt<ElementType>;\n\n const is = (a: number): a is ElementType =>\n (integerOrSafeInteger === 'Integer'\n ? Number.isInteger(a)\n : Number.isSafeInteger(a)) &&\n (nonZero === true ? a !== 0 : true) &&\n (isFnOrUndefined(MIN_VALUE, MAX_VALUE)?.(a) ?? true);\n\n const castType = castTypeImpl<ElementType>(is, typeNameInMessage);\n\n const fromNumber: ((a: number) => ElementType) | undefined = pipe(\n clampFnOrUndefined(MIN_VALUE, MAX_VALUE),\n ).mapNullable(\n (cl) =>\n (x: number): ElementType =>\n castType(Math.round(cl(x))),\n ).value;\n\n const clampOrCastFn: (a: number) => ElementType = fromNumber ?? castType;\n\n const abs = (x: ElementTypeWithSmallInt): ToNonNegative<ElementType> =>\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n Math.abs(clampOrCastFn(x)) as ToNonNegative<ElementType>;\n\n const min_ = (\n ...values: readonly ElementTypeWithSmallInt[]\n ): ElementType => clampOrCastFn(Math.min(...values));\n\n const max_ = (\n ...values: readonly ElementTypeWithSmallInt[]\n ): ElementType => clampOrCastFn(Math.max(...values));\n\n const pow = (\n x: ElementTypeWithSmallInt,\n y: ElementTypeWithSmallInt,\n ): ElementType => clampOrCastFn(x ** y);\n\n const add = (\n x: ElementTypeWithSmallInt,\n y: ElementTypeWithSmallInt,\n ): ElementType => clampOrCastFn(x + y);\n\n const sub = (\n x: ElementTypeWithSmallInt,\n y: ElementTypeWithSmallInt,\n ): ElementType => clampOrCastFn(x - y);\n\n const mul = (\n x: ElementTypeWithSmallInt,\n y: ElementTypeWithSmallInt,\n ): ElementType => clampOrCastFn(x * y);\n\n const div = (\n x: ElementTypeWithSmallInt,\n y: ToNonZeroIntWithSmallInt<ElementType>,\n ): ElementType =>\n clampOrCastFn(\n Math.floor(\n // eslint-disable-next-line total-functions/no-partial-division\n x / y,\n ),\n );\n\n const randomImpl = (\n min: number = MIN_VALUE,\n max: number = MAX_VALUE,\n ): number =>\n min + Math.floor((Math.max(max, min) - min + 1) * Math.random());\n\n // [-5, 5] -> floor(11 * Math.random()) + (-5)\n const random = (\n min?: ElementTypeWithSmallInt,\n max?: ElementTypeWithSmallInt,\n ): ElementType => clampOrCastFn(randomImpl(min, max));\n\n const randomNonZero = (\n min?: ElementTypeWithSmallInt,\n max?: ElementTypeWithSmallInt,\n ): ElementType => {\n while (true) {\n const r = randomImpl(min, max);\n\n if (Num.isNonZero(r)) return clampOrCastFn(r);\n }\n };\n\n return {\n MIN_VALUE,\n MAX_VALUE,\n is,\n abs,\n min: min_,\n max: max_,\n pow,\n add,\n sub,\n mul,\n div,\n random,\n randomNonZero,\n castType,\n\n fromNumber:\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n fromNumber as TypeEq<MAX_VALUE | MIN_VALUE, undefined> extends true\n ? undefined\n : (x: number) => ElementType,\n } as const;\n };\n\n type OperatorsForFloat<\n ElementType extends UnknownNumberBrand,\n MIN_VALUE extends number | undefined,\n MAX_VALUE extends number | undefined,\n > = Readonly<{\n MIN_VALUE: MIN_VALUE;\n MAX_VALUE: MAX_VALUE;\n\n is: (a: number) => a is ElementType;\n\n abs: (x: ElementType) => ToNonNegative<ElementType>;\n min: (...values: readonly ElementType[]) => ElementType;\n max: (...values: readonly ElementType[]) => ElementType;\n pow: (x: ElementType, y: ElementType) => ElementType;\n add: (x: ElementType, y: ElementType) => ElementType;\n sub: (x: ElementType, y: ElementType) => ElementType;\n mul: (x: ElementType, y: ElementType) => ElementType;\n div: (x: ElementType, y: ToNonZero<ElementType>) => ElementType;\n random: (min?: ElementType, max?: ElementType) => ElementType;\n randomNonZero: (min?: ElementType, max?: ElementType) => ElementType;\n\n castType: <N extends number>(x: N) => ElementType & N;\n\n fromNumber: TypeEq<MAX_VALUE | MIN_VALUE, undefined> extends true\n ? undefined\n : (x: number) => ElementType;\n }>;\n\n /**\n * Factory function that creates a complete set of type-safe operations for\n * floating-point types.\n *\n * This function generates:\n *\n * - Type guards and validators (checking for finite values)\n * - Arithmetic operations that preserve type constraints\n * - Utility functions (min, max, abs, random)\n * - Automatic clamping for bounded types\n *\n * All operations ensure results remain finite and within any specified\n * bounds. Division by zero is prevented through type constraints.\n *\n * @example\n *\n * ```ts\n * const floatOps = TsDataForgeInternals.RefinedNumberUtils.operatorsForFloat<\n * PositiveFiniteNumber,\n * number,\n * number\n * >({\n * nonZero: true,\n * MIN_VALUE: Number.MIN_VALUE,\n * MAX_VALUE: Number.MAX_VALUE,\n * typeNameInMessage: 'PositiveFiniteNumber',\n * } as const);\n *\n * const fortyTwo = floatOps.castType(42.5);\n *\n * const seven = floatOps.castType(7.5);\n *\n * const sum = floatOps.add(fortyTwo, seven);\n *\n * const ratio = floatOps.div(sum, floatOps.castType(10));\n *\n * const coerced = floatOps.fromNumber(0);\n *\n * const boundedRandom = floatOps.random(\n * floatOps.castType(10),\n * floatOps.castType(20),\n * );\n *\n * const nonZeroRandom = floatOps.randomNonZero();\n *\n * assert.isTrue(sum === 50);\n *\n * assert.isTrue(ratio === 5);\n *\n * assert.isTrue(coerced >= Number.MIN_VALUE);\n *\n * assert.isTrue(boundedRandom >= 10 && boundedRandom <= 20);\n *\n * assert.isTrue(nonZeroRandom > 0);\n * ```\n *\n * @template ElementType - The floating-point branded type\n * @template MIN_VALUE - Optional minimum value for bounded types\n * @template MAX_VALUE - Optional maximum value for bounded types\n * @param config - Configuration object\n * @param config.nonZero - If true, excludes zero from valid values\n * @param config.MIN_VALUE - Minimum valid value (inclusive)\n * @param config.MAX_VALUE - Maximum valid value (inclusive)\n * @param config.typeNameInMessage - Human-readable type name for error\n * messages\n * @returns Object containing all type-safe operations for the\n * floating-point type\n * @internal\n */\n export const operatorsForFloat = <\n ElementType extends UnknownNumberBrand,\n MIN_VALUE extends number,\n MAX_VALUE extends number,\n >({\n nonZero,\n MIN_VALUE,\n MAX_VALUE,\n typeNameInMessage,\n }: Readonly<{\n nonZero?: boolean;\n MIN_VALUE: MIN_VALUE;\n MAX_VALUE: MAX_VALUE;\n typeNameInMessage: string;\n }>): OperatorsForFloat<ElementType, MIN_VALUE, MAX_VALUE> => {\n const is = (a: number): a is ElementType =>\n Number.isFinite(a) &&\n (nonZero === true ? a !== 0 : true) &&\n (isFnOrUndefined(MIN_VALUE, MAX_VALUE)?.(a) ?? true);\n\n const castType = castTypeImpl<ElementType>(is, typeNameInMessage);\n\n const fromNumber: ((a: number) => ElementType) | undefined = pipe(\n clampFnOrUndefined(MIN_VALUE, MAX_VALUE),\n ).mapNullable(\n (cl) =>\n (x: number): ElementType =>\n castType(cl(x)),\n ).value;\n\n const clampOrCastFn: (a: number) => ElementType = fromNumber ?? castType;\n\n const abs = (x: ElementType): ToNonNegative<ElementType> =>\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n Math.abs(x) as ToNonNegative<ElementType>;\n\n const min_ = (...values: readonly ElementType[]): ElementType =>\n clampOrCastFn(Math.min(...values));\n\n const max_ = (...values: readonly ElementType[]): ElementType =>\n clampOrCastFn(Math.max(...values));\n\n const pow = (x: ElementType, y: ElementType): ElementType =>\n clampOrCastFn(x ** y);\n\n const add = (x: ElementType, y: ElementType): ElementType =>\n clampOrCastFn(x + y);\n\n const sub = (x: ElementType, y: ElementType): ElementType =>\n clampOrCastFn(x - y);\n\n const mul = (x: ElementType, y: ElementType): ElementType =>\n clampOrCastFn(x * y);\n\n const div = (x: ElementType, y: ToNonZero<ElementType>): ElementType =>\n // eslint-disable-next-line total-functions/no-partial-division\n clampOrCastFn(x / y);\n\n const randomImpl = (\n min: number = MIN_VALUE,\n max: number = MAX_VALUE,\n ): number => min + (Math.max(max, min) - min) * Math.random();\n\n const random = (min?: ElementType, max?: ElementType): ElementType =>\n clampOrCastFn(randomImpl(min, max));\n\n const randomNonZero = (\n min?: ElementType,\n max?: ElementType,\n ): ElementType => {\n while (true) {\n const r = random(min, max);\n\n if (isNonZero(r)) return r;\n }\n };\n\n return {\n MIN_VALUE,\n MAX_VALUE,\n is,\n abs,\n min: min_,\n max: max_,\n pow,\n add,\n sub,\n mul,\n div,\n random,\n randomNonZero,\n castType,\n\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n fromNumber: fromNumber as TypeEq<\n MAX_VALUE | MIN_VALUE,\n undefined\n > extends true\n ? undefined\n : (x: number) => ElementType,\n } as const;\n };\n }\n}\n"],"names":["TsDataForgeInternals","RefinedNumberUtils"],"mappings":";;;;IAqBiB;AAAA,CAAV,CAAUA,qBAAAA,KAAV;AAuBE,EAAA,CAAA,CAAUC,mBAAAA,KAAV;AACL,IAAA,MAAM,YAAA,GACJ,CACE,EAAA,EACA,sBAAA,KAEF,CAAmB,CAAA,KAA0B;AAC3C,MAAA,IAAI,CAAC,EAAA,CAAG,CAAC,CAAA,EAAG;AACV,QAAA,MAAM,IAAI,SAAA,CAAU,CAAA,SAAA,EAAY,sBA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EAAK,GAAG,CAAC,CAAA;AAEpC,MAAA,MAAM,aAAA,GAAgB,CACpB,GAAA,EACA,GAAA,KACgB;AAChB,QAAA,OAAO,IAAA,EAAM;AACX,UAAA,MAAM,CAAA,GAAI,MAAA,CAAO,GAAA,EAAK,GAAG,CAAA;AAEzB,UAAA,IAAI,SAAA,CAAU,CAAC,CAAA,EAAG,OAAO,CAAA;AAAA,QAAA;AAC3B,MAAA,CACF;AAEA,MAAA,OAAO;AAAA,QACL,SAAA;AAAA,QACA,SAAA;AAAA,QACA,EAAA;AAAA,QACA,GAAA;AAAA,QACA,GAAA,EAAK,IAAA;AAAA,QACL,GAAA,EAAK,IAAA;AAAA,QACL,GAAA;AAAA,QACA,GAAA;AAAA,QACA,GAAA;AAAA,QACA,GAAA;AAAA,QACA,GAAA;AAAA,QACA,MAAA;AAAA,QACA,aAAA;AAAA,QACA,QAAA;AAAA;AAAA,QAGA;AAAA,OAMF;AAAA,IAAA,CACF;AAAA,EAAA,CAAA,EA5oBeD,qBAAAA,CAAA,kBAAA,KAAAA,qBAAAA,CAAA,kBAAA,GAAA,EAAA,CAAA,CAAA;AAAA,CAAA,EAvBF,oBAAA,KAAA,oBAAA,GAAA,EAAA,CAAA,CAAA;;;;"}
|
|
1
|
+
{"version":3,"file":"refined-number-utils.mjs","sources":["../../src/number/refined-number-utils.mts"],"sourcesContent":["import {\n type Brand,\n type ChangeBaseBrand,\n type GetBrandValuePart,\n type Int,\n type IntersectBrand,\n type NonNegativeNumber,\n type NonZeroNumber,\n type PositiveNumber,\n type RelaxedExclude,\n type StrictPick,\n type TypeEq,\n type UnknownBrand,\n type UnwrapBrandFalseKeys,\n type UnwrapBrandTrueKeys,\n type WithSmallInt,\n} from 'ts-type-forge';\nimport { expectType } from '../expect-type.mjs';\nimport { pipe } from '../functional/index.mjs';\nimport { Num } from './num.mjs';\n\n/** @internal */\nexport namespace TsDataForgeInternals {\n /**\n * Internal utilities for creating and managing refined (branded) number\n * types.\n *\n * This namespace provides factory functions and type utilities for building\n * type-safe numeric operations with compile-time constraints. It serves as\n * the foundation for all branded number types in the library, including:\n *\n * - Integer types (Int, SafeInt, Int8, Int16, Int32)\n * - Unsigned types (UInt, UInt8, UInt16, UInt32)\n * - Constrained types (NonZero, NonNegative, Positive)\n * - Range-bounded types\n *\n * The utilities handle:\n *\n * - Type validation and narrowing\n * - Arithmetic operations that preserve type constraints\n * - Automatic clamping for bounded types\n * - Random number generation within type bounds\n *\n * @internal This namespace is not part of the public API\n */\n export namespace RefinedNumberUtils {\n const castTypeImpl =\n <BrandedType extends number>(\n is: (n: number) => n is BrandedType,\n typeNameInErrorMessage: string,\n ) =>\n <N extends number>(a: N): BrandedType & N => {\n if (!is(a)) {\n throw new TypeError(`Expected ${typeNameInErrorMessage}, got: ${a}`);\n }\n\n return a;\n };\n\n type UnknownNumberBrand = ChangeBaseBrand<UnknownBrand, number>;\n\n /**\n * Converts a branded number type to include the Int brand.\n *\n * @template N - A branded number type\n * @internal\n */\n export type ToInt<N extends UnknownNumberBrand> = IntersectBrand<N, Int>;\n\n type ToNonZero<N extends UnknownNumberBrand> = IntersectBrand<\n N,\n NonZeroNumber\n >;\n\n const isNonZero = <N extends UnknownNumberBrand>(\n n: N,\n ): n is N & ToNonZero<N> => n !== 0;\n\n type ToNonZeroIntWithSmallInt<N extends Int> = WithSmallInt<\n CastToInt<ToNonZero<N>>\n >;\n\n /**\n * Converts a branded number type to include the NonNegativeNumber brand.\n *\n * @template N - A branded number type\n * @internal\n */\n export type ToNonNegative<N extends UnknownNumberBrand> =\n '!=0' extends UnwrapBrandTrueKeys<N>\n ? IntersectBrand<N, PositiveNumber>\n : IntersectBrand<N, NonNegativeNumber>;\n\n /**\n * Removes the non-zero brand constraint from a branded number type. Used\n * when operations may produce zero values.\n *\n * @template N - A branded number type\n * @internal\n */\n export type RemoveNonZeroBrandKey<N extends UnknownNumberBrand> = Brand<\n GetBrandValuePart<N>,\n RelaxedExclude<UnwrapBrandTrueKeys<N>, '!=0'> & string,\n RelaxedExclude<UnwrapBrandFalseKeys<N>, '<=0' | '>=0'> & string\n >;\n\n type CastToInt<N> = N extends Int ? N : never;\n\n /**\n * Generates a type-safe API for a branded number type based on its\n * characteristics.\n *\n * This type dynamically constructs an object type with appropriate methods\n * based on the number class. For example:\n *\n * - Integer types don't get floor/ceil/round methods\n * - Non-negative types don't get abs method\n * - Range-bounded types get MIN_VALUE/MAX_VALUE constants\n *\n * @template N - The branded number type\n * @template classes - Union of characteristics: 'int' | 'non-negative' |\n * 'positive' | 'range'\n * @internal\n */\n /** The closed binary arithmetic operations a branded namespace may expose. */\n export type NumberClassBinOp = 'add' | 'div' | 'mul' | 'sub';\n\n export type NumberClass<\n N extends UnknownNumberBrand,\n classes extends 'int' | 'non-negative' | 'positive' | 'range',\n binOps extends NumberClassBinOp = NumberClassBinOp,\n > = ('int' extends classes\n ? unknown\n : 'positive' extends classes\n ? Readonly<{\n floor: (x: N, y: N) => RemoveNonZeroBrandKey<ToInt<N>>;\n ceil: (x: N, y: N) => ToInt<N>;\n round: (x: N, y: N) => RemoveNonZeroBrandKey<ToInt<N>>;\n }>\n : Readonly<{\n floor: (x: N, y: N) => ToInt<N>;\n ceil: (x: N, y: N) => ToInt<N>;\n round: (x: N, y: N) => ToInt<N>;\n }>) &\n ('non-negative' extends classes\n ? Readonly<{\n MIN_VALUE: number;\n fromNumber: (a: number) => N;\n }>\n : unknown) &\n ('non-negative' extends classes\n ? unknown\n : 'positive' extends classes\n ? unknown\n : Readonly<{\n abs: (x: N) => ToNonNegative<N>;\n }>) &\n ('positive' extends classes\n ? Readonly<{\n MIN_VALUE: number;\n fromNumber: (a: number) => N;\n }>\n : unknown) &\n ('range' extends classes\n ? Readonly<{\n MIN_VALUE: number;\n MAX_VALUE: number;\n fromNumber: (a: number) => N;\n }>\n : unknown) &\n StrictPick<\n Readonly<{\n add: (x: N, y: N) => N;\n sub: (x: N, y: N) => N;\n mul: (x: N, y: N) => N;\n div: (x: N, y: ToNonZero<N>) => N;\n }>,\n binOps\n > &\n Readonly<{\n is: (a: number) => a is N;\n min: (...values: readonly N[]) => N;\n max: (...values: readonly N[]) => N;\n random: (min: N, max: N) => N;\n pow: (x: N, y: N) => N;\n }>;\n\n type BaseKeys =\n 'add' | 'div' | 'is' | 'max' | 'min' | 'mul' | 'pow' | 'random' | 'sub';\n\n type FloatMethods = 'ceil' | 'floor' | 'round';\n\n expectType<keyof NumberClass<UnknownNumberBrand, 'int'>, BaseKeys | 'abs'>(\n '=',\n );\n\n expectType<\n keyof NumberClass<UnknownNumberBrand, never>,\n BaseKeys | FloatMethods | 'abs'\n >('=');\n\n expectType<\n keyof NumberClass<UnknownNumberBrand, 'non-negative'>,\n BaseKeys | FloatMethods | 'fromNumber' | 'MIN_VALUE'\n >('=');\n\n expectType<\n keyof NumberClass<UnknownNumberBrand, 'positive'>,\n BaseKeys | FloatMethods | 'fromNumber' | 'MIN_VALUE'\n >('=');\n\n expectType<\n keyof NumberClass<UnknownNumberBrand, 'int' | 'range'>,\n BaseKeys | 'abs' | 'fromNumber' | 'MAX_VALUE' | 'MIN_VALUE'\n >('=');\n\n const isFnOrUndefined = (\n min: number | undefined,\n max: number | undefined,\n ): ((n: number) => boolean) | undefined =>\n min === undefined\n ? max === undefined\n ? undefined\n : (n) => n <= max\n : max === undefined\n ? (n) => min <= n\n : Num.isInRangeInclusive(min, max);\n\n const clampFnOrUndefined = (\n min: number | undefined,\n max: number | undefined,\n ): ((n: number) => number) | undefined =>\n min === undefined\n ? max === undefined\n ? undefined\n : (n) => Math.min(max, n)\n : max === undefined\n ? (n) => Math.max(min, n)\n : Num.clamp(min, max);\n\n type OperatorsForInteger<\n ElementType extends Int,\n MIN_VALUE extends number,\n MAX_VALUE extends number,\n ElementTypeWithSmallInt extends WithSmallInt<ElementType> =\n WithSmallInt<ElementType>,\n > = Readonly<{\n MIN_VALUE: MIN_VALUE;\n MAX_VALUE: MAX_VALUE;\n\n is: (a: number) => a is ElementType;\n\n abs: (x: ElementTypeWithSmallInt) => ToNonNegative<ElementType>;\n\n min: (...values: readonly ElementTypeWithSmallInt[]) => ElementType;\n\n max: (...values: readonly ElementTypeWithSmallInt[]) => ElementType;\n\n pow: (\n x: ElementTypeWithSmallInt,\n y: ElementTypeWithSmallInt,\n ) => ElementType;\n\n add: (\n x: ElementTypeWithSmallInt,\n y: ElementTypeWithSmallInt,\n ) => ElementType;\n\n sub: (\n x: ElementTypeWithSmallInt,\n y: ElementTypeWithSmallInt,\n ) => ElementType;\n\n mul: (\n x: ElementTypeWithSmallInt,\n y: ElementTypeWithSmallInt,\n ) => ElementType;\n\n div: (\n x: ElementTypeWithSmallInt,\n y: ToNonZeroIntWithSmallInt<ElementType>,\n ) => ElementType;\n\n random: (\n min?: ElementTypeWithSmallInt,\n max?: ElementTypeWithSmallInt,\n ) => ElementType;\n\n randomNonZero: (\n min?: ElementTypeWithSmallInt,\n max?: ElementTypeWithSmallInt,\n ) => ElementType;\n\n castType: <N extends number>(x: N) => ElementType & N;\n\n fromNumber: TypeEq<MAX_VALUE | MIN_VALUE, undefined> extends true\n ? undefined\n : (x: number) => ElementType;\n }>;\n\n /**\n * Factory function that creates a complete set of type-safe operations for\n * integer types.\n *\n * This function generates:\n *\n * - Type guards and validators\n * - Arithmetic operations that preserve type constraints\n * - Utility functions (min, max, abs, random)\n * - Automatic clamping for bounded types\n *\n * All operations ensure results remain within the type's constraints, using\n * clamping when bounds are specified.\n *\n * @example\n *\n * ```ts\n * const intOps = TsDataForgeInternals.RefinedNumberUtils.operatorsForInteger<\n * SafeInt,\n * number,\n * number\n * >({\n * integerOrSafeInteger: 'SafeInteger',\n * MIN_VALUE: Number.MIN_SAFE_INTEGER,\n * MAX_VALUE: Number.MAX_SAFE_INTEGER,\n * typeNameInMessage: 'SafeInt',\n * } as const);\n *\n * const six = intOps.castType(6);\n *\n * const four = intOps.castType(4);\n *\n * const sum = intOps.add(six, four);\n *\n * const difference = intOps.sub(six, four);\n *\n * const product = intOps.mul(six, four);\n *\n * const quotient = intOps.div(six, intOps.castType(2));\n *\n * const coerced = intOps.fromNumber(1.5);\n *\n * const randomValue = intOps.random();\n *\n * assert.isTrue(sum === 10);\n *\n * assert.isTrue(difference === 2);\n *\n * assert.isTrue(product === 24);\n *\n * assert.isTrue(quotient === 3);\n *\n * assert.isTrue(coerced === 2);\n *\n * assert.isTrue(Number.isSafeInteger(randomValue));\n * ```\n *\n * @template ElementType - The integer branded type\n * @template MIN_VALUE - Optional minimum value for bounded types\n * @template MAX_VALUE - Optional maximum value for bounded types\n * @param config - Configuration object\n * @param config.integerOrSafeInteger - Whether to use Number.isInteger or\n * Number.isSafeInteger\n * @param config.nonZero - If true, excludes zero from valid values\n * @param config.MIN_VALUE - Minimum valid value (inclusive)\n * @param config.MAX_VALUE - Maximum valid value (inclusive)\n * @param config.typeNameInMessage - Human-readable type name for error\n * messages\n * @returns Object containing all type-safe operations for the integer type\n * @internal\n */\n export const operatorsForInteger = <\n ElementType extends Int,\n MIN_VALUE extends number,\n MAX_VALUE extends number,\n >({\n integerOrSafeInteger,\n nonZero,\n MIN_VALUE,\n MAX_VALUE,\n typeNameInMessage,\n }: Readonly<{\n integerOrSafeInteger: 'Integer' | 'SafeInteger';\n nonZero?: boolean;\n MIN_VALUE: MIN_VALUE;\n MAX_VALUE: MAX_VALUE;\n typeNameInMessage: string;\n }>): OperatorsForInteger<ElementType, MIN_VALUE, MAX_VALUE> => {\n type ElementTypeWithSmallInt = WithSmallInt<ElementType>;\n\n const is = (a: number): a is ElementType =>\n (integerOrSafeInteger === 'Integer'\n ? Number.isInteger(a)\n : Number.isSafeInteger(a)) &&\n (nonZero === true ? a !== 0 : true) &&\n (isFnOrUndefined(MIN_VALUE, MAX_VALUE)?.(a) ?? true);\n\n const castType = castTypeImpl<ElementType>(is, typeNameInMessage);\n\n const fromNumber: ((a: number) => ElementType) | undefined = pipe(\n clampFnOrUndefined(MIN_VALUE, MAX_VALUE),\n ).mapNullable(\n (cl) =>\n (x: number): ElementType =>\n castType(Math.round(cl(x))),\n ).value;\n\n const clampOrCastFn: (a: number) => ElementType = fromNumber ?? castType;\n\n const abs = (x: ElementTypeWithSmallInt): ToNonNegative<ElementType> =>\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n Math.abs(clampOrCastFn(x)) as ToNonNegative<ElementType>;\n\n const min_ = (\n ...values: readonly ElementTypeWithSmallInt[]\n ): ElementType => clampOrCastFn(Math.min(...values));\n\n const max_ = (\n ...values: readonly ElementTypeWithSmallInt[]\n ): ElementType => clampOrCastFn(Math.max(...values));\n\n const pow = (\n x: ElementTypeWithSmallInt,\n y: ElementTypeWithSmallInt,\n ): ElementType => clampOrCastFn(x ** y);\n\n const add = (\n x: ElementTypeWithSmallInt,\n y: ElementTypeWithSmallInt,\n ): ElementType => clampOrCastFn(x + y);\n\n const sub = (\n x: ElementTypeWithSmallInt,\n y: ElementTypeWithSmallInt,\n ): ElementType => clampOrCastFn(x - y);\n\n const mul = (\n x: ElementTypeWithSmallInt,\n y: ElementTypeWithSmallInt,\n ): ElementType => clampOrCastFn(x * y);\n\n const div = (\n x: ElementTypeWithSmallInt,\n y: ToNonZeroIntWithSmallInt<ElementType>,\n ): ElementType =>\n clampOrCastFn(\n Math.floor(\n // eslint-disable-next-line total-functions/no-partial-division\n x / y,\n ),\n );\n\n const randomImpl = (\n min: number = MIN_VALUE,\n max: number = MAX_VALUE,\n ): number =>\n min + Math.floor((Math.max(max, min) - min + 1) * Math.random());\n\n // [-5, 5] -> floor(11 * Math.random()) + (-5)\n const random = (\n min?: ElementTypeWithSmallInt,\n max?: ElementTypeWithSmallInt,\n ): ElementType => clampOrCastFn(randomImpl(min, max));\n\n const randomNonZero = (\n min?: ElementTypeWithSmallInt,\n max?: ElementTypeWithSmallInt,\n ): ElementType => {\n while (true) {\n const r = randomImpl(min, max);\n\n if (Num.isNonZero(r)) return clampOrCastFn(r);\n }\n };\n\n return {\n MIN_VALUE,\n MAX_VALUE,\n is,\n abs,\n min: min_,\n max: max_,\n pow,\n add,\n sub,\n mul,\n div,\n random,\n randomNonZero,\n castType,\n\n fromNumber:\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n fromNumber as TypeEq<MAX_VALUE | MIN_VALUE, undefined> extends true\n ? undefined\n : (x: number) => ElementType,\n } as const;\n };\n\n type OperatorsForFloat<\n ElementType extends UnknownNumberBrand,\n MIN_VALUE extends number | undefined,\n MAX_VALUE extends number | undefined,\n > = Readonly<{\n MIN_VALUE: MIN_VALUE;\n MAX_VALUE: MAX_VALUE;\n\n is: (a: number) => a is ElementType;\n\n abs: (x: ElementType) => ToNonNegative<ElementType>;\n min: (...values: readonly ElementType[]) => ElementType;\n max: (...values: readonly ElementType[]) => ElementType;\n pow: (x: ElementType, y: ElementType) => ElementType;\n add: (x: ElementType, y: ElementType) => ElementType;\n sub: (x: ElementType, y: ElementType) => ElementType;\n mul: (x: ElementType, y: ElementType) => ElementType;\n div: (x: ElementType, y: ToNonZero<ElementType>) => ElementType;\n random: (min?: ElementType, max?: ElementType) => ElementType;\n randomNonZero: (min?: ElementType, max?: ElementType) => ElementType;\n\n castType: <N extends number>(x: N) => ElementType & N;\n\n fromNumber: TypeEq<MAX_VALUE | MIN_VALUE, undefined> extends true\n ? undefined\n : (x: number) => ElementType;\n }>;\n\n /**\n * Factory function that creates a complete set of type-safe operations for\n * floating-point types.\n *\n * This function generates:\n *\n * - Type guards and validators (checking for finite values)\n * - Arithmetic operations that preserve type constraints\n * - Utility functions (min, max, abs, random)\n * - Automatic clamping for bounded types\n *\n * All operations ensure results remain finite and within any specified\n * bounds. Division by zero is prevented through type constraints.\n *\n * @example\n *\n * ```ts\n * const floatOps = TsDataForgeInternals.RefinedNumberUtils.operatorsForFloat<\n * PositiveFiniteNumber,\n * number,\n * number\n * >({\n * nonZero: true,\n * MIN_VALUE: Number.MIN_VALUE,\n * MAX_VALUE: Number.MAX_VALUE,\n * typeNameInMessage: 'PositiveFiniteNumber',\n * } as const);\n *\n * const fortyTwo = floatOps.castType(42.5);\n *\n * const seven = floatOps.castType(7.5);\n *\n * const sum = floatOps.add(fortyTwo, seven);\n *\n * const ratio = floatOps.div(sum, floatOps.castType(10));\n *\n * const coerced = floatOps.fromNumber(0);\n *\n * const boundedRandom = floatOps.random(\n * floatOps.castType(10),\n * floatOps.castType(20),\n * );\n *\n * const nonZeroRandom = floatOps.randomNonZero();\n *\n * assert.isTrue(sum === 50);\n *\n * assert.isTrue(ratio === 5);\n *\n * assert.isTrue(coerced >= Number.MIN_VALUE);\n *\n * assert.isTrue(boundedRandom >= 10 && boundedRandom <= 20);\n *\n * assert.isTrue(nonZeroRandom > 0);\n * ```\n *\n * @template ElementType - The floating-point branded type\n * @template MIN_VALUE - Optional minimum value for bounded types\n * @template MAX_VALUE - Optional maximum value for bounded types\n * @param config - Configuration object\n * @param config.nonZero - If true, excludes zero from valid values\n * @param config.MIN_VALUE - Minimum valid value (inclusive)\n * @param config.MAX_VALUE - Maximum valid value (inclusive)\n * @param config.typeNameInMessage - Human-readable type name for error\n * messages\n * @returns Object containing all type-safe operations for the\n * floating-point type\n * @internal\n */\n export const operatorsForFloat = <\n ElementType extends UnknownNumberBrand,\n MIN_VALUE extends number,\n MAX_VALUE extends number,\n >({\n nonZero,\n MIN_VALUE,\n MAX_VALUE,\n typeNameInMessage,\n }: Readonly<{\n nonZero?: boolean;\n MIN_VALUE: MIN_VALUE;\n MAX_VALUE: MAX_VALUE;\n typeNameInMessage: string;\n }>): OperatorsForFloat<ElementType, MIN_VALUE, MAX_VALUE> => {\n const is = (a: number): a is ElementType =>\n Number.isFinite(a) &&\n (nonZero === true ? a !== 0 : true) &&\n (isFnOrUndefined(MIN_VALUE, MAX_VALUE)?.(a) ?? true);\n\n const castType = castTypeImpl<ElementType>(is, typeNameInMessage);\n\n const fromNumber: ((a: number) => ElementType) | undefined = pipe(\n clampFnOrUndefined(MIN_VALUE, MAX_VALUE),\n ).mapNullable(\n (cl) =>\n (x: number): ElementType =>\n castType(cl(x)),\n ).value;\n\n const clampOrCastFn: (a: number) => ElementType = fromNumber ?? castType;\n\n const abs = (x: ElementType): ToNonNegative<ElementType> =>\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n Math.abs(x) as ToNonNegative<ElementType>;\n\n const min_ = (...values: readonly ElementType[]): ElementType =>\n clampOrCastFn(Math.min(...values));\n\n const max_ = (...values: readonly ElementType[]): ElementType =>\n clampOrCastFn(Math.max(...values));\n\n const pow = (x: ElementType, y: ElementType): ElementType =>\n clampOrCastFn(x ** y);\n\n const add = (x: ElementType, y: ElementType): ElementType =>\n clampOrCastFn(x + y);\n\n const sub = (x: ElementType, y: ElementType): ElementType =>\n clampOrCastFn(x - y);\n\n const mul = (x: ElementType, y: ElementType): ElementType =>\n clampOrCastFn(x * y);\n\n const div = (x: ElementType, y: ToNonZero<ElementType>): ElementType =>\n // eslint-disable-next-line total-functions/no-partial-division\n clampOrCastFn(x / y);\n\n const randomImpl = (\n min: number = MIN_VALUE,\n max: number = MAX_VALUE,\n ): number => min + (Math.max(max, min) - min) * Math.random();\n\n const random = (min?: ElementType, max?: ElementType): ElementType =>\n clampOrCastFn(randomImpl(min, max));\n\n const randomNonZero = (\n min?: ElementType,\n max?: ElementType,\n ): ElementType => {\n while (true) {\n const r = random(min, max);\n\n if (isNonZero(r)) return r;\n }\n };\n\n return {\n MIN_VALUE,\n MAX_VALUE,\n is,\n abs,\n min: min_,\n max: max_,\n pow,\n add,\n sub,\n mul,\n div,\n random,\n randomNonZero,\n castType,\n\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n fromNumber: fromNumber as TypeEq<\n MAX_VALUE | MIN_VALUE,\n undefined\n > extends true\n ? undefined\n : (x: number) => ElementType,\n } as const;\n };\n }\n}\n"],"names":["TsDataForgeInternals","RefinedNumberUtils"],"mappings":";;;;IAsBiB;AAAA,CAAV,CAAUA,qBAAAA,KAAV;AAuBE,EAAA,CAAA,CAAUC,mBAAAA,KAAV;AACL,IAAA,MAAM,YAAA,GACJ,CACE,EAAA,EACA,sBAAA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AAK,GAAG,CAAA,GAAI,GAAA,IAAO,KAAK,MAAA,EAAO;AAE5D,MAAA,MAAM,MAAA,GAAS,CAAC,GAAA,EAAmB,GAAA,KACjC,cAAc,UAAA,CAAW,GAAA,EAAK,GAAG,CAAC,CAAA;AAEpC,MAAA,MAAM,aAAA,GAAgB,CACpB,GAAA,EACA,GAAA,KACgB;AAChB,QAAA,OAAO,IAAA,EAAM;AACX,UAAA,MAAM,CAAA,GAAI,MAAA,CAAO,GAAA,EAAK,GAAG,CAAA;AAEzB,UAAA,IAAI,SAAA,CAAU,CAAC,CAAA,EAAG,OAAO,CAAA;AAAA,QAAA;AAC3B,MAAA,CACF;AAEA,MAAA,OAAO;AAAA,QACL,SAAA;AAAA,QACA,SAAA;AAAA,QACA,EAAA;AAAA,QACA,GAAA;AAAA,QACA,GAAA,EAAK,IAAA;AAAA,QACL,GAAA,EAAK,IAAA;AAAA,QACL,GAAA;AAAA,QACA,GAAA;AAAA,QACA,GAAA;AAAA,QACA,GAAA;AAAA,QACA,GAAA;AAAA,QACA,MAAA;AAAA,QACA,aAAA;AAAA,QACA,QAAA;AAAA;AAAA,QAGA;AAAA,OAMF;AAAA,IAAA,CACF;AAAA,EAAA,CAAA,EA5oBeD,qBAAAA,CAAA,kBAAA,KAAAA,qBAAAA,CAAA,kBAAA,GAAA,EAAA,CAAA,CAAA;AAAA,CAAA,EAvBF,oBAAA,KAAA,oBAAA,GAAA,EAAA,CAAA,CAAA;;;;"}
|
package/dist/object/object.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type ArrayElement, type Decrement, type DeepOmit, type DeepPick, type IsFixedLengthList, type IsUnion, type List, type ReadonlyRecord, type RelaxedPick, type TypeEq, type UnknownRecord } from 'ts-type-forge';
|
|
1
|
+
import { type ArrayElement, type Decrement, type DeepOmit, type DeepPick, type IsFixedLengthList, type IsUnion, type List, type ReadonlyRecord, type RelaxedExclude, type RelaxedOmit, type RelaxedPick, type StrictOmit, type StrictPick, type TypeEq, type UnknownRecord } from 'ts-type-forge';
|
|
2
2
|
/**
|
|
3
3
|
* A collection of type-safe object utility functions providing functional
|
|
4
4
|
* programming patterns for object manipulation, including pick, omit, shallow
|
|
@@ -94,7 +94,7 @@ export declare namespace Obj {
|
|
|
94
94
|
* @param keys - A readonly array of keys to include in the result
|
|
95
95
|
* @returns A new record containing only the specified keys and their values
|
|
96
96
|
*/
|
|
97
|
-
function pick<const R extends UnknownRecord, const Keys extends readonly (keyof R)[]>(record: R, keys: Keys):
|
|
97
|
+
function pick<const R extends UnknownRecord, const Keys extends readonly (keyof R)[]>(record: R, keys: Keys): StrictPick<R, ArrayElement<Keys>>;
|
|
98
98
|
function pick<const Keys extends readonly PropertyKey[]>(keys: Keys): <const R extends UnknownRecord>(record: R) => RelaxedPick<R, ArrayElement<Keys>>;
|
|
99
99
|
/**
|
|
100
100
|
* Creates a new record that excludes the specified keys from the source
|
|
@@ -141,8 +141,8 @@ export declare namespace Obj {
|
|
|
141
141
|
* @param keys - A readonly array of keys to exclude from the result
|
|
142
142
|
* @returns A new record containing all properties except the specified keys
|
|
143
143
|
*/
|
|
144
|
-
function omit<const R extends UnknownRecord, const Keys extends readonly (keyof R)[]>(record: R, keys: Keys):
|
|
145
|
-
function omit<const Keys extends readonly PropertyKey[]>(keys: Keys): <const R extends UnknownRecord>(record: R) =>
|
|
144
|
+
function omit<const R extends UnknownRecord, const Keys extends readonly (keyof R)[]>(record: R, keys: Keys): StrictOmit<R, ArrayElement<Keys>>;
|
|
145
|
+
function omit<const Keys extends readonly PropertyKey[]>(keys: Keys): <const R extends UnknownRecord>(record: R) => RelaxedOmit<R, ArrayElement<Keys>>;
|
|
146
146
|
/**
|
|
147
147
|
* Creates an object from an array of key-value pairs with precise TypeScript
|
|
148
148
|
* typing. This is a type-safe wrapper around `Object.fromEntries` that
|
|
@@ -283,7 +283,7 @@ declare namespace TsDataForgeInternals {
|
|
|
283
283
|
/** - `[['x', 1], ['y', 3]]` -> `{ x: 1, y: 3 }` */
|
|
284
284
|
type EntriesToObject<Entries extends readonly (readonly [PropertyKey, unknown])[]> = Readonly<EntriesToObjectImpl<{}, Entries>>;
|
|
285
285
|
/** @internal */
|
|
286
|
-
type EntriesToObjectImpl<R, Entries extends readonly (readonly [PropertyKey, unknown])[]> = TypeEq<Entries['length'], 0> extends true ? R : EntriesToObjectImpl<R &
|
|
286
|
+
type EntriesToObjectImpl<R, Entries extends readonly (readonly [PropertyKey, unknown])[]> = TypeEq<Entries['length'], 0> extends true ? R : EntriesToObjectImpl<R & ReadonlyRecord<Entries[0][0], Entries[0][1]>, List.Tail<Entries>>;
|
|
287
287
|
/**
|
|
288
288
|
* - `['x' | 'y' | 'z', number][]]` -> `'x' | 'y' | 'z'`
|
|
289
289
|
* - `[['a' | 'b' | 'c', number], ...['x' | 'y' | 'z', number][]]` -> `'a' |
|
|
@@ -336,19 +336,19 @@ declare namespace TsDataForgeInternals {
|
|
|
336
336
|
* ```
|
|
337
337
|
*/
|
|
338
338
|
type MergeTwo<A extends UnknownRecord, B extends UnknownRecord> = {
|
|
339
|
-
readonly [K in keyof B as {} extends
|
|
339
|
+
readonly [K in keyof B as {} extends StrictPick<B, K> ? never : K]: B[K];
|
|
340
340
|
} & {
|
|
341
|
-
readonly [K in keyof B as {} extends
|
|
341
|
+
readonly [K in keyof B as {} extends StrictPick<B, K> ? K extends keyof A ? never : K : never]?: B[K];
|
|
342
342
|
} & {
|
|
343
|
-
readonly [K in keyof B as {} extends
|
|
343
|
+
readonly [K in keyof B as {} extends StrictPick<B, K> ? K extends keyof A ? K : never : never]?: K extends keyof A ? A[K] | B[K] : never;
|
|
344
344
|
} & {
|
|
345
|
-
readonly [K in
|
|
345
|
+
readonly [K in RelaxedExclude<keyof A, keyof B> as {} extends StrictPick<A, K> ? never : K]: A[K];
|
|
346
346
|
} & {
|
|
347
|
-
readonly [K in
|
|
347
|
+
readonly [K in RelaxedExclude<keyof A, keyof B> as {} extends StrictPick<A, K> ? K : never]?: A[K];
|
|
348
348
|
} extends infer O ? Readonly<{
|
|
349
|
-
[K in keyof O as {} extends
|
|
349
|
+
[K in keyof O as {} extends StrictPick<O, K> ? never : K]: O[K];
|
|
350
350
|
} & {
|
|
351
|
-
[K in keyof O as {} extends
|
|
351
|
+
[K in keyof O as {} extends StrictPick<O, K> ? K : never]?: O[K];
|
|
352
352
|
}> : never;
|
|
353
353
|
/** Sequentially merges a tuple of object types from left to right. */
|
|
354
354
|
type MergeAll<Records extends readonly UnknownRecord[], Acc extends UnknownRecord = {}> = IsFixedLengthList<Records> extends false ? ArrayElement<Records> : Records extends readonly [
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"object.d.mts","sourceRoot":"","sources":["../../src/object/object.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,SAAS,EACd,KAAK,QAAQ,EACb,KAAK,QAAQ,EACb,KAAK,iBAAiB,EACtB,KAAK,OAAO,EACZ,KAAK,IAAI,EACT,KAAK,cAAc,EACnB,KAAK,WAAW,EAChB,KAAK,MAAM,EACX,KAAK,aAAa,EACnB,MAAM,eAAe,CAAC;AAIvB;;;;;;;GAOG;AACH,yBAAiB,GAAG,CAAC;IACnB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OAyCG;IACI,MAAM,SAAS,MACjB,aAAa,KACb,aAAa,OACZ,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,KAAK,OAAO,KACtC,OAQF,CAAC;IAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;OA0CG;IACH,SAAgB,IAAI,CAClB,KAAK,CAAC,CAAC,SAAS,aAAa,EAC7B,KAAK,CAAC,IAAI,SAAS,SAAS,CAAC,MAAM,CAAC,CAAC,EAAE,EACvC,MAAM,EAAE,CAAC,EAAE,IAAI,EAAE,IAAI,GAAG,
|
|
1
|
+
{"version":3,"file":"object.d.mts","sourceRoot":"","sources":["../../src/object/object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= omit(
|
|
88
|
+
const result = omit(
|
|
89
|
+
record,
|
|
90
|
+
// eslint-disable-next-line total-functions/no-unsafe-type-assertion
|
|
91
|
+
keys
|
|
92
|
+
);
|
|
89
93
|
return result;
|
|
90
94
|
};
|
|
91
95
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"object.mjs","sources":["../../src/object/object.mts"],"sourcesContent":["import {\n type ArrayElement,\n type Decrement,\n type DeepOmit,\n type DeepPick,\n type IsFixedLengthList,\n type IsUnion,\n type List,\n type ReadonlyRecord,\n type RelaxedPick,\n type TypeEq,\n type UnknownRecord,\n} from 'ts-type-forge';\nimport { Arr } from '../array/index.mjs';\nimport { hasKey, isRecord } from '../guard/index.mjs';\n\n/**\n * A collection of type-safe object utility functions providing functional\n * programming patterns for object manipulation, including pick, omit, shallow\n * equality checks, and more.\n *\n * All functions maintain TypeScript type safety and support both direct and\n * curried usage patterns for better composition with pipe operations.\n */\nexport namespace Obj {\n /**\n * Performs a shallow equality check on two records using a configurable\n * equality function. Verifies that both records have the same number of\n * entries and that for every key in the first record, the corresponding value\n * passes the equality test with the value in the second record.\n *\n * @example\n *\n * ```ts\n * const obj1 = { name: 'Alice', age: 30 } as const;\n *\n * const obj2 = { name: 'Alice', age: 30 } as const;\n *\n * const obj3 = { name: 'Alice', age: 31 } as const;\n *\n * assert.isTrue(Obj.shallowEq(obj1, obj2));\n *\n * assert.isFalse(Obj.shallowEq(obj1, obj3));\n *\n * // Custom equality function\n * const obj4 = { value: 1 } as const;\n *\n * const obj5 = { value: 1.00001 } as const;\n *\n * const closeEnough = (a: unknown, b: unknown): boolean => {\n * if (typeof a === 'number' && typeof b === 'number') {\n * return Math.abs(a - b) < 0.001;\n * }\n *\n * return Object.is(a, b);\n * };\n *\n * assert.isTrue(Obj.shallowEq(obj4, obj5, closeEnough));\n * ```\n *\n * @param a - The first record to compare\n * @param b - The second record to compare\n * @param eq - Optional equality function (defaults to Object.is for strict\n * equality)\n * @returns `true` if the records are shallowly equal according to the\n * equality function, `false` otherwise\n */\n export const shallowEq = (\n a: UnknownRecord,\n b: UnknownRecord,\n eq: (x: unknown, y: unknown) => boolean = Object.is,\n ): boolean => {\n const aEntries = Object.entries(a);\n\n const bEntries = Object.entries(b);\n\n if (aEntries.length !== bEntries.length) return false;\n\n return aEntries.every(([k, v]) => eq(b[k], v));\n };\n\n /**\n * Creates a new record that contains only the specified keys from the source\n * record. This function supports both direct usage and curried form for\n * functional composition.\n *\n * **Type Safety**: Only keys that exist in the source record type are\n * allowed, preventing runtime errors from accessing non-existent properties.\n *\n * @example\n *\n * ```ts\n * const user = {\n * id: 1,\n * name: 'Bob',\n * email: 'bob@example.com',\n * password: 'secret',\n * role: 'admin',\n * } as const;\n *\n * // Direct usage\n * const publicInfo = Obj.pick(user, ['id', 'name', 'role']);\n *\n * assert.deepStrictEqual(publicInfo, {\n * id: 1,\n * name: 'Bob',\n * role: 'admin',\n * });\n *\n * // Curried usage with pipe\n * const nameAndEmail = pipe(user).map(Obj.pick(['name', 'email'])).value;\n *\n * assert.deepStrictEqual(nameAndEmail, {\n * name: 'Bob',\n * email: 'bob@example.com',\n * });\n * ```\n *\n * @template R - The type of the input record\n * @template Keys - The readonly array type of keys to pick from the record\n * @param record - The source record to pick properties from\n * @param keys - A readonly array of keys to include in the result\n * @returns A new record containing only the specified keys and their values\n */\n export function pick<\n const R extends UnknownRecord,\n const Keys extends readonly (keyof R)[],\n >(record: R, keys: Keys): Pick<R, ArrayElement<Keys>>;\n\n // Curried version\n export function pick<const Keys extends readonly PropertyKey[]>(\n keys: Keys,\n ): <const R extends UnknownRecord>(\n record: R,\n ) => RelaxedPick<R, ArrayElement<Keys>>;\n\n export function pick<\n const R extends UnknownRecord,\n const Keys extends readonly (keyof R)[],\n >(\n ...args: readonly [record: R, keys: Keys] | readonly [keys: Keys]\n ):\n | Pick<R, ArrayElement<Keys>>\n | ((record: R) => RelaxedPick<R, ArrayElement<Keys>>) {\n switch (args.length) {\n case 2: {\n const [record, keys] = args;\n\n const keysSet = new Set<keyof R>(keys);\n\n return (\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n Object.fromEntries(\n Object.entries(record).filter(([k, _v]) => keysSet.has(k)),\n ) as never\n );\n }\n\n case 1: {\n const [keys] = args;\n\n return (record: R) => pick(record, keys);\n }\n }\n }\n\n /**\n * Creates a new record that excludes the specified keys from the source\n * record. This function supports both direct usage and curried form for\n * functional composition.\n *\n * **Type Safety**: Only keys that exist in the source record type are\n * allowed, and the return type precisely reflects which properties remain\n * after omission.\n *\n * @example\n *\n * ```ts\n * const user = {\n * id: 1,\n * name: 'Charlie',\n * email: 'charlie@example.com',\n * password: 'secret123',\n * internalNote: 'VIP customer',\n * } as const;\n *\n * // Direct usage - remove sensitive fields\n * const safeUser = Obj.omit(user, ['password', 'internalNote']);\n *\n * assert.deepStrictEqual(safeUser, {\n * id: 1,\n * name: 'Charlie',\n * email: 'charlie@example.com',\n * });\n *\n * // Curried usage with pipe\n * const withoutEmail = pipe(user).map(Obj.omit(['email', 'password'])).value;\n *\n * assert.deepStrictEqual(withoutEmail, {\n * id: 1,\n * name: 'Charlie',\n * internalNote: 'VIP customer',\n * });\n * ```\n *\n * @template R - The type of the input record\n * @template Keys - The readonly array type of keys to omit from the record\n * @param record - The source record to omit properties from\n * @param keys - A readonly array of keys to exclude from the result\n * @returns A new record containing all properties except the specified keys\n */\n export function omit<\n const R extends UnknownRecord,\n const Keys extends readonly (keyof R)[],\n >(record: R, keys: Keys): Omit<R, ArrayElement<Keys>>;\n\n // Curried version\n export function omit<const Keys extends readonly PropertyKey[]>(\n keys: Keys,\n ): <const R extends UnknownRecord>(record: R) => Omit<R, ArrayElement<Keys>>;\n\n export function omit<\n const R extends UnknownRecord,\n const Keys extends readonly (keyof R)[],\n >(\n ...args:\n readonly [record: R, keys: Keys] | readonly [keys: readonly PropertyKey[]]\n ):\n Omit<R, ArrayElement<Keys>> | ((record: R) => Omit<R, ArrayElement<Keys>>) {\n switch (args.length) {\n case 2: {\n const [record, keys] = args;\n\n const keysSet = new Set<keyof R>(keys);\n\n return (\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n Object.fromEntries(\n Object.entries(record).filter(([k, _v]) => !keysSet.has(k)),\n ) as never\n );\n }\n\n case 1: {\n const [keys] = args;\n\n return <R2 extends UnknownRecord>(record: R2) => {\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n const result = omit(record, keys as readonly (keyof R2)[]) as Omit<\n R2,\n ArrayElement<Keys>\n >;\n\n return result;\n };\n }\n }\n }\n\n /**\n * Creates an object from an array of key-value pairs with precise TypeScript\n * typing. This is a type-safe wrapper around `Object.fromEntries` that\n * provides better type inference and compile-time guarantees about the\n * resulting object structure.\n *\n * **Type Behavior**:\n *\n * - When entries is a fixed-length tuple, the exact object type is inferred\n * - When entries has dynamic length with union key types, `Partial` is applied\n * to prevent incorrect assumptions about which keys will be present\n *\n * @example\n *\n * ```ts\n * // Fixed-length tuple - exact type inferred\n * const entries1 = [\n * ['name', 'David'],\n * ['age', 25],\n * ['active', true],\n * ] as const;\n *\n * const obj1 = Obj.fromEntries(entries1);\n *\n * assert.deepStrictEqual(obj1, {\n * name: 'David',\n * age: 25,\n * active: true,\n * });\n *\n * // Dynamic length array - Partial type applied\n * const dynamicEntries: readonly (readonly ['x' | 'y', number])[] = [\n * ['x', 10],\n * ['y', 20],\n * ] as const;\n *\n * const obj2 = Obj.fromEntries(dynamicEntries);\n *\n * assert.deepStrictEqual(obj2, { x: 10, y: 20 });\n * ```\n *\n * @template Entries - The readonly array type of key-value entry tuples\n * @param entries - An array of readonly key-value entry tuples `[key, value]`\n * @returns An object created from the entries with precise typing\n */\n export const fromEntries = <\n const Entries extends readonly (readonly [PropertyKey, unknown])[],\n >(\n entries: Entries,\n ): IsFixedLengthList<Entries> extends true\n ? TsDataForgeInternals.EntriesToObject<Entries>\n : TsDataForgeInternals.PartialIfKeyIsUnion<\n TsDataForgeInternals.KeysOfEntries<Entries>,\n ReadonlyRecord<\n TsDataForgeInternals.KeysOfEntries<Entries>,\n TsDataForgeInternals.ValuesOfEntries<Entries>\n >\n > =>\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n Object.fromEntries(entries) as never;\n\n /**\n * Merges multiple records into a single record using `Object.assign`.\n * Later records override properties from earlier records with the same key.\n *\n * @example\n *\n * <!-- doc:embed:jsdoc:example:./samples/src/object/merge-example.mts -->\n *\n * ```ts\n * const a = { a: 0, b: 0 } as const;\n *\n * const b = { b: 1, c: 0 } as const;\n *\n * const result = Obj.merge(a, b);\n *\n * assert.deepStrictEqual(result, { a: 0, b: 1, c: 0 });\n * ```\n *\n * <!-- /doc:embed:jsdoc:example:./samples/src/object/merge-example.mts -->\n *\n * @param records - The records to merge\n * @returns A new record with all properties merged\n */\n export const merge = <const Records extends readonly UnknownRecord[]>(\n ...records: Records\n ): TsDataForgeInternals.MergeAll<Records> =>\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n Object.fromEntries(records.flatMap((r) => Object.entries(r))) as never;\n\n /**\n * Deeply picks a nested property from an object along the specified key path.\n * Supports both direct and curried usage.\n *\n * @example\n *\n * ```ts\n * const data = { a: { b: { c: 1, d: 2 }, e: 3 }, f: 4 } as const;\n *\n * // Direct usage\n * const result = Obj.deepPick(data, ['a', 'b', 'c']);\n *\n * assert.deepStrictEqual(result, { a: { b: { c: 1 } } });\n *\n * // Curried usage with pipe\n * const pickAB = Obj.deepPick(['a', 'b']);\n *\n * const result2 = pipe(data).map(pickAB).value;\n *\n * assert.deepStrictEqual(result2, { a: { b: { c: 1, d: 2 } } });\n * ```\n *\n * @template R - The type of the input record\n * @template Path - The key path tuple\n * @param record - The source record\n * @param path - A readonly tuple of keys representing the nested path\n * @returns A new record containing only the nested property at the path\n */\n export function deepPick<\n const R extends UnknownRecord,\n const Path extends readonly (string | number)[],\n >(record: R, path: Path): DeepPick<R, Path>;\n\n // Curried version\n export function deepPick<const Path extends readonly (string | number)[]>(\n path: Path,\n ): <const R extends UnknownRecord>(record: R) => DeepPick<R, Path>;\n\n export function deepPick<\n const R extends UnknownRecord,\n const Path extends readonly (string | number)[],\n >(\n ...args: readonly [record: R, path: Path] | readonly [path: Path]\n ): DeepPick<R, Path> | ((record: R) => DeepPick<R, Path>) {\n switch (args.length) {\n case 2: {\n const [record, path] = args;\n\n return (\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n deepPickImpl(record, path) as never\n );\n }\n\n case 1: {\n const [path] = args;\n\n return (record: R) => deepPick(record, path);\n }\n }\n }\n\n /**\n * Deeply omits a nested property from an object along the specified key path.\n * Supports both direct and curried usage.\n *\n * @example\n *\n * ```ts\n * const data = { a: { b: { c: 1, d: 2 }, e: 3 }, f: 4 } as const;\n *\n * // Direct usage\n * const result = Obj.deepOmit(data, ['a', 'b', 'c']);\n *\n * assert.deepStrictEqual(result, { a: { b: { d: 2 }, e: 3 }, f: 4 });\n *\n * // Curried usage with pipe\n * const omitAB = Obj.deepOmit(['a', 'b']);\n *\n * const result2 = pipe(data).map(omitAB).value;\n *\n * assert.deepStrictEqual(result2, { a: { e: 3 }, f: 4 });\n * ```\n *\n * @template R - The type of the input record\n * @template Path - The key path tuple\n * @param record - The source record\n * @param path - A readonly tuple of keys representing the nested path to omit\n * @returns A new record with the nested property at the path removed\n */\n export function deepOmit<\n const R extends UnknownRecord,\n const Path extends readonly (string | number)[],\n >(record: R, path: Path): DeepOmit<R, Path>;\n\n // Curried version\n export function deepOmit<const Path extends readonly (string | number)[]>(\n path: Path,\n ): <const R extends UnknownRecord>(record: R) => DeepOmit<R, Path>;\n\n export function deepOmit<\n const R extends UnknownRecord,\n const Path extends readonly (string | number)[],\n >(\n ...args: readonly [record: R, path: Path] | readonly [path: Path]\n ): DeepOmit<R, Path> | ((record: R) => DeepOmit<R, Path>) {\n switch (args.length) {\n case 2: {\n const [record, path] = args;\n\n return (\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n deepOmitImpl(record, path) as never\n );\n }\n\n case 1: {\n const [path] = args;\n\n return (record: R) => deepOmit(record, path);\n }\n }\n }\n}\n\nconst deepPickImpl = (\n record: UnknownRecord,\n path: readonly (string | number)[],\n): UnknownRecord => {\n if (!Arr.isNonEmpty(path)) return record;\n\n const head = path[0];\n\n if (!hasKey(record, head)) return {};\n\n const value = record[head];\n\n const tail = path.slice(1);\n\n if (!Arr.isNonEmpty(tail)) return { [head]: value };\n\n if (!isRecord(value)) return { [head]: {} };\n\n return {\n [head]: deepPickImpl(value, tail),\n };\n};\n\nconst deepOmitImpl = (\n record: UnknownRecord,\n path: readonly (string | number)[],\n): UnknownRecord => {\n if (!Arr.isNonEmpty(path)) return record;\n\n const head = path[0];\n\n if (!hasKey(record, head)) return record;\n\n const tail = path.slice(1);\n\n if (!Arr.isNonEmpty(tail)) {\n return Object.fromEntries(\n Object.entries(record).filter(([k]) => k !== head),\n );\n }\n\n const value = record[head];\n\n if (!isRecord(value)) return record;\n\n return {\n ...record,\n [head]: deepOmitImpl(value, tail),\n };\n};\n\n/**\n * @internal\n * Internal type utilities for the Obj module.\n */\ndeclare namespace TsDataForgeInternals {\n type RecursionLimit = 20;\n\n /** - `[['x', 1], ['y', 3]]` -> `{ x: 1, y: 3 }` */\n export type EntriesToObject<\n Entries extends readonly (readonly [PropertyKey, unknown])[],\n // eslint-disable-next-line @typescript-eslint/no-empty-object-type\n > = Readonly<EntriesToObjectImpl<{}, Entries>>;\n\n /** @internal */\n type EntriesToObjectImpl<\n R,\n Entries extends readonly (readonly [PropertyKey, unknown])[],\n > =\n TypeEq<Entries['length'], 0> extends true\n ? R\n : EntriesToObjectImpl<\n R & Readonly<Record<Entries[0][0], Entries[0][1]>>,\n List.Tail<Entries>\n >;\n\n /**\n * - `['x' | 'y' | 'z', number][]]` -> `'x' | 'y' | 'z'`\n * - `[['a' | 'b' | 'c', number], ...['x' | 'y' | 'z', number][]]` -> `'a' |\n * 'b' | 'c' | 'x' | 'y' | 'z'`\n *\n * @note To handle the second example above, recursion needs to be performed on infinite-length Entries,\n * but since the timing to stop cannot be determined, a recursion limit is set.\n */\n export type KeysOfEntries<\n Entries extends readonly (readonly [PropertyKey, unknown])[],\n > = KeysOfEntriesImpl<never, Entries, RecursionLimit>;\n\n type KeysOfEntriesImpl<\n K,\n Entries extends readonly (readonly [PropertyKey, unknown])[],\n RemainingNumRecursions extends number,\n > =\n TypeEq<RemainingNumRecursions, 0> extends true\n ? K\n : TypeEq<Entries['length'], 0> extends true\n ? K\n : KeysOfEntriesImpl<\n K | Entries[0][0],\n List.Tail<Entries>,\n Decrement<RemainingNumRecursions>\n >;\n\n export type ValuesOfEntries<\n Entries extends readonly (readonly [PropertyKey, unknown])[],\n > = ValuesOfEntriesImpl<never, Entries, RecursionLimit>;\n\n type ValuesOfEntriesImpl<\n K,\n Entries extends readonly (readonly [PropertyKey, unknown])[],\n RemainingNumRecursions extends number,\n > =\n TypeEq<RemainingNumRecursions, 0> extends true\n ? K\n : TypeEq<Entries['length'], 0> extends true\n ? K\n : ValuesOfEntriesImpl<\n K | Entries[0][1],\n List.Tail<Entries>,\n Decrement<RemainingNumRecursions>\n >;\n\n export type PartialIfKeyIsUnion<K, T> =\n IsUnion<K> extends true ? Partial<T> : T;\n\n /**\n * Merges two object types where keys in B override keys in A, preserving optional modifiers.\n *\n * This implementation uses a sophisticated technique to preserve optional property modifiers (`?`)\n * during the merge operation, which would otherwise be lost in a naive mapped type implementation.\n *\n * ## Core Technique: Optional Property Detection\n *\n * Uses `{} extends Pick<T, K>` to determine if a property is optional:\n * - **Required property**: `Pick<T, 'x'>` = `{ x: number }` → `{}` does NOT extend it (false)\n * - **Optional property**: `Pick<T, 'y'>` = `{ y?: string }` → `{}` DOES extend it (true)\n *\n * This works because `{}` (empty object) is assignable to objects with only optional properties\n * but not to objects with required properties.\n *\n * ## Implementation Strategy\n *\n * The type is constructed as an intersection of five mapped types:\n *\n * 1. **Required properties from B** - Properties in B that are required (override A completely)\n * 2. **Optional properties from B (not in A)** - New optional properties from B\n * 3. **Optional properties from B (in A)** - Union of A[K] | B[K] to preserve both possibilities\n * 4. **Required properties only in A** - Properties in A but not in B that are required\n * 5. **Optional properties only in A** - Properties in A but not in B that are optional (with `?` modifier)\n *\n * Finally, the intersection is flattened using an optional-preserving identity mapping that\n * re-applies the optional detection logic to maintain the `?` modifiers in the final type.\n *\n * @example\n * ```ts\n * type A = Readonly<{ x: number; y?: string }>;\n *\n * type B = Readonly<{ y?: number; z?: boolean }>;\n *\n * type Result = MergeTwo<A, B>;\n * // Result: Readonly<{ x: number; y?: string | number; z?: boolean }>\n * ```\n */\n // transformer-ignore-next-line\n type MergeTwo<A extends UnknownRecord, B extends UnknownRecord> = {\n // 1. Required properties from B (override A completely)\n // eslint-disable-next-line @typescript-eslint/no-empty-object-type\n readonly [K in keyof B as {} extends Pick<B, K> ? never : K]: B[K];\n } & {\n // 2. Optional properties from B that are NOT in A\n readonly [\n // eslint-disable-next-line @typescript-eslint/no-empty-object-type\n K in keyof B as {} extends Pick<B, K>\n ? K extends keyof A\n ? never\n : K\n : never\n ]?: B[K];\n } & {\n // 3. Optional properties from B that ARE in A (create union)\n readonly [\n // eslint-disable-next-line @typescript-eslint/no-empty-object-type\n K in keyof B as {} extends Pick<B, K>\n ? K extends keyof A\n ? K\n : never\n : never\n ]?: K extends keyof A ? A[K] | B[K] : never;\n } & {\n // 4. Required properties only in A (not overridden by B)\n readonly [\n // eslint-disable-next-line @typescript-eslint/no-empty-object-type\n K in Exclude<keyof A, keyof B> as {} extends Pick<A, K> ? never : K\n ]: A[K];\n } & {\n // 5. Optional properties only in A (not overridden by B)\n readonly [\n // eslint-disable-next-line @typescript-eslint/no-empty-object-type\n K in Exclude<keyof A, keyof B> as {} extends Pick<A, K> ? K : never\n ]?: A[K];\n } extends infer O\n ? Readonly<\n {\n // eslint-disable-next-line @typescript-eslint/no-empty-object-type\n [K in keyof O as {} extends Pick<O, K> ? never : K]: O[K];\n } & {\n // eslint-disable-next-line @typescript-eslint/no-empty-object-type\n [K in keyof O as {} extends Pick<O, K> ? K : never]?: O[K];\n }\n >\n : never;\n\n /** Sequentially merges a tuple of object types from left to right. */\n export type MergeAll<\n Records extends readonly UnknownRecord[],\n // eslint-disable-next-line @typescript-eslint/no-empty-object-type\n Acc extends UnknownRecord = {},\n > =\n IsFixedLengthList<Records> extends false\n ? ArrayElement<Records>\n : Records extends readonly [\n infer First extends UnknownRecord,\n ...infer Rest extends readonly UnknownRecord[],\n ]\n ? MergeAll<Rest, MergeTwo<Acc, First>>\n : Acc;\n}\n"],"names":["Obj","Arr.isNonEmpty"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwBO,IAAU;AAAA,CAAV,CAAUA,IAAAA,KAAV;AA2CE,EAAMA,KAAA,SAAA,GAAY,CACvB,GACA,CAAA,EACA,EAAA,GAA0C,OAAO,EAAA,KACrC;AACZ,IAAA,MAAM,QAAA,GAAW,MAAA,CAAO,OAAA,CAAQ,CAAC,CAAA;AAEjC,IAAA,MAAM,QAAA,GAAW,MAAA,CAAO,OAAA,CAAQ,CAAC,CAAA;AAEjC,IAAA,IAAI,QAAA,CAAS,MAAA,KAAW,QAAA,CAAS,MAAA,EAAQ,OAAO,KAAA;AAEhD,IAAA,OAAO,QAAA,CAAS,KAAA,CAAM,CAAC,CAAC,CAAA,EAAG,CAAC,CAAA,KAAM,EAAA,CAAG,CAAA,CAAE,CAAC,CAAA,EAAG,CAAC,CAAC,CAAA;AAAA,EAC/C,CAAA;AAyDO,EAAA,SAAS,QAIX,IAAA,EAGmD;AACtD,IAAA,QAAQ,KAAK,MAAA;AAAQ,MACnB,KAAK,CAAA,EAAG;AACN,QAAA,MAAM,CAAC,MAAA,EAAQ,IAAI,CAAA,GAAI,IAAA;AAEvB,QAAA,MAAM,OAAA,GAAU,IAAI,GAAA,CAAa,IAAI,CAAA;AAErC,QAAA;AAAA;AAAA,UAEE,MAAA,CAAO,WAAA;AAAA,YACL,MAAA,CAAO,OAAA,CAAQ,MAAM,CAAA,CAAE,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H,CAAC,IAAI,GAAG,YAAA,CAAa,OAAO,IAAI;AAAA,GAClC;AACF,CAAA;;;;"}
|
|
1
|
+
{"version":3,"file":"object.mjs","sources":["../../src/object/object.mts"],"sourcesContent":["import {\n type ArrayElement,\n type Decrement,\n type DeepOmit,\n type DeepPick,\n type IsFixedLengthList,\n type IsUnion,\n type List,\n type ReadonlyRecord,\n type RelaxedExclude,\n type RelaxedOmit,\n type RelaxedPick,\n type StrictOmit,\n type StrictPick,\n type TypeEq,\n type UnknownRecord,\n} from 'ts-type-forge';\nimport { Arr } from '../array/index.mjs';\nimport { hasKey, isRecord } from '../guard/index.mjs';\n\n/**\n * A collection of type-safe object utility functions providing functional\n * programming patterns for object manipulation, including pick, omit, shallow\n * equality checks, and more.\n *\n * All functions maintain TypeScript type safety and support both direct and\n * curried usage patterns for better composition with pipe operations.\n */\nexport namespace Obj {\n /**\n * Performs a shallow equality check on two records using a configurable\n * equality function. Verifies that both records have the same number of\n * entries and that for every key in the first record, the corresponding value\n * passes the equality test with the value in the second record.\n *\n * @example\n *\n * ```ts\n * const obj1 = { name: 'Alice', age: 30 } as const;\n *\n * const obj2 = { name: 'Alice', age: 30 } as const;\n *\n * const obj3 = { name: 'Alice', age: 31 } as const;\n *\n * assert.isTrue(Obj.shallowEq(obj1, obj2));\n *\n * assert.isFalse(Obj.shallowEq(obj1, obj3));\n *\n * // Custom equality function\n * const obj4 = { value: 1 } as const;\n *\n * const obj5 = { value: 1.00001 } as const;\n *\n * const closeEnough = (a: unknown, b: unknown): boolean => {\n * if (typeof a === 'number' && typeof b === 'number') {\n * return Math.abs(a - b) < 0.001;\n * }\n *\n * return Object.is(a, b);\n * };\n *\n * assert.isTrue(Obj.shallowEq(obj4, obj5, closeEnough));\n * ```\n *\n * @param a - The first record to compare\n * @param b - The second record to compare\n * @param eq - Optional equality function (defaults to Object.is for strict\n * equality)\n * @returns `true` if the records are shallowly equal according to the\n * equality function, `false` otherwise\n */\n export const shallowEq = (\n a: UnknownRecord,\n b: UnknownRecord,\n eq: (x: unknown, y: unknown) => boolean = Object.is,\n ): boolean => {\n const aEntries = Object.entries(a);\n\n const bEntries = Object.entries(b);\n\n if (aEntries.length !== bEntries.length) return false;\n\n return aEntries.every(([k, v]) => eq(b[k], v));\n };\n\n /**\n * Creates a new record that contains only the specified keys from the source\n * record. This function supports both direct usage and curried form for\n * functional composition.\n *\n * **Type Safety**: Only keys that exist in the source record type are\n * allowed, preventing runtime errors from accessing non-existent properties.\n *\n * @example\n *\n * ```ts\n * const user = {\n * id: 1,\n * name: 'Bob',\n * email: 'bob@example.com',\n * password: 'secret',\n * role: 'admin',\n * } as const;\n *\n * // Direct usage\n * const publicInfo = Obj.pick(user, ['id', 'name', 'role']);\n *\n * assert.deepStrictEqual(publicInfo, {\n * id: 1,\n * name: 'Bob',\n * role: 'admin',\n * });\n *\n * // Curried usage with pipe\n * const nameAndEmail = pipe(user).map(Obj.pick(['name', 'email'])).value;\n *\n * assert.deepStrictEqual(nameAndEmail, {\n * name: 'Bob',\n * email: 'bob@example.com',\n * });\n * ```\n *\n * @template R - The type of the input record\n * @template Keys - The readonly array type of keys to pick from the record\n * @param record - The source record to pick properties from\n * @param keys - A readonly array of keys to include in the result\n * @returns A new record containing only the specified keys and their values\n */\n export function pick<\n const R extends UnknownRecord,\n const Keys extends readonly (keyof R)[],\n >(record: R, keys: Keys): StrictPick<R, ArrayElement<Keys>>;\n\n // Curried version\n export function pick<const Keys extends readonly PropertyKey[]>(\n keys: Keys,\n ): <const R extends UnknownRecord>(\n record: R,\n ) => RelaxedPick<R, ArrayElement<Keys>>;\n\n export function pick<\n const R extends UnknownRecord,\n const Keys extends readonly (keyof R)[],\n >(\n ...args: readonly [record: R, keys: Keys] | readonly [keys: Keys]\n ):\n | StrictPick<R, ArrayElement<Keys>>\n | ((record: R) => RelaxedPick<R, ArrayElement<Keys>>) {\n switch (args.length) {\n case 2: {\n const [record, keys] = args;\n\n const keysSet = new Set<keyof R>(keys);\n\n return (\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n Object.fromEntries(\n Object.entries(record).filter(([k, _v]) => keysSet.has(k)),\n ) as never\n );\n }\n\n case 1: {\n const [keys] = args;\n\n return (record: R) => pick(record, keys);\n }\n }\n }\n\n /**\n * Creates a new record that excludes the specified keys from the source\n * record. This function supports both direct usage and curried form for\n * functional composition.\n *\n * **Type Safety**: Only keys that exist in the source record type are\n * allowed, and the return type precisely reflects which properties remain\n * after omission.\n *\n * @example\n *\n * ```ts\n * const user = {\n * id: 1,\n * name: 'Charlie',\n * email: 'charlie@example.com',\n * password: 'secret123',\n * internalNote: 'VIP customer',\n * } as const;\n *\n * // Direct usage - remove sensitive fields\n * const safeUser = Obj.omit(user, ['password', 'internalNote']);\n *\n * assert.deepStrictEqual(safeUser, {\n * id: 1,\n * name: 'Charlie',\n * email: 'charlie@example.com',\n * });\n *\n * // Curried usage with pipe\n * const withoutEmail = pipe(user).map(Obj.omit(['email', 'password'])).value;\n *\n * assert.deepStrictEqual(withoutEmail, {\n * id: 1,\n * name: 'Charlie',\n * internalNote: 'VIP customer',\n * });\n * ```\n *\n * @template R - The type of the input record\n * @template Keys - The readonly array type of keys to omit from the record\n * @param record - The source record to omit properties from\n * @param keys - A readonly array of keys to exclude from the result\n * @returns A new record containing all properties except the specified keys\n */\n export function omit<\n const R extends UnknownRecord,\n const Keys extends readonly (keyof R)[],\n >(record: R, keys: Keys): StrictOmit<R, ArrayElement<Keys>>;\n\n // Curried version\n export function omit<const Keys extends readonly PropertyKey[]>(\n keys: Keys,\n ): <const R extends UnknownRecord>(\n record: R,\n ) => RelaxedOmit<R, ArrayElement<Keys>>;\n\n export function omit<\n const R extends UnknownRecord,\n const Keys extends readonly (keyof R)[],\n >(\n ...args:\n readonly [record: R, keys: Keys] | readonly [keys: readonly PropertyKey[]]\n ):\n | StrictOmit<R, ArrayElement<Keys>>\n | ((record: R) => StrictOmit<R, ArrayElement<Keys>>) {\n switch (args.length) {\n case 2: {\n const [record, keys] = args;\n\n const keysSet = new Set<keyof R>(keys);\n\n return (\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n Object.fromEntries(\n Object.entries(record).filter(([k, _v]) => !keysSet.has(k)),\n ) as never\n );\n }\n\n case 1: {\n const [keys] = args;\n\n return <R2 extends UnknownRecord>(record: R2) => {\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n const result = omit(\n record,\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n keys as readonly (keyof R2)[],\n ) as RelaxedOmit<R2, ArrayElement<Keys>>;\n\n return result;\n };\n }\n }\n }\n\n /**\n * Creates an object from an array of key-value pairs with precise TypeScript\n * typing. This is a type-safe wrapper around `Object.fromEntries` that\n * provides better type inference and compile-time guarantees about the\n * resulting object structure.\n *\n * **Type Behavior**:\n *\n * - When entries is a fixed-length tuple, the exact object type is inferred\n * - When entries has dynamic length with union key types, `Partial` is applied\n * to prevent incorrect assumptions about which keys will be present\n *\n * @example\n *\n * ```ts\n * // Fixed-length tuple - exact type inferred\n * const entries1 = [\n * ['name', 'David'],\n * ['age', 25],\n * ['active', true],\n * ] as const;\n *\n * const obj1 = Obj.fromEntries(entries1);\n *\n * assert.deepStrictEqual(obj1, {\n * name: 'David',\n * age: 25,\n * active: true,\n * });\n *\n * // Dynamic length array - Partial type applied\n * const dynamicEntries: readonly (readonly ['x' | 'y', number])[] = [\n * ['x', 10],\n * ['y', 20],\n * ] as const;\n *\n * const obj2 = Obj.fromEntries(dynamicEntries);\n *\n * assert.deepStrictEqual(obj2, { x: 10, y: 20 });\n * ```\n *\n * @template Entries - The readonly array type of key-value entry tuples\n * @param entries - An array of readonly key-value entry tuples `[key, value]`\n * @returns An object created from the entries with precise typing\n */\n export const fromEntries = <\n const Entries extends readonly (readonly [PropertyKey, unknown])[],\n >(\n entries: Entries,\n ): IsFixedLengthList<Entries> extends true\n ? TsDataForgeInternals.EntriesToObject<Entries>\n : TsDataForgeInternals.PartialIfKeyIsUnion<\n TsDataForgeInternals.KeysOfEntries<Entries>,\n ReadonlyRecord<\n TsDataForgeInternals.KeysOfEntries<Entries>,\n TsDataForgeInternals.ValuesOfEntries<Entries>\n >\n > =>\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n Object.fromEntries(entries) as never;\n\n /**\n * Merges multiple records into a single record using `Object.assign`.\n * Later records override properties from earlier records with the same key.\n *\n * @example\n *\n * <!-- doc:embed:jsdoc:example:./samples/src/object/merge-example.mts -->\n *\n * ```ts\n * const a = { a: 0, b: 0 } as const;\n *\n * const b = { b: 1, c: 0 } as const;\n *\n * const result = Obj.merge(a, b);\n *\n * assert.deepStrictEqual(result, { a: 0, b: 1, c: 0 });\n * ```\n *\n * <!-- /doc:embed:jsdoc:example:./samples/src/object/merge-example.mts -->\n *\n * @param records - The records to merge\n * @returns A new record with all properties merged\n */\n export const merge = <const Records extends readonly UnknownRecord[]>(\n ...records: Records\n ): TsDataForgeInternals.MergeAll<Records> =>\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n Object.fromEntries(records.flatMap((r) => Object.entries(r))) as never;\n\n /**\n * Deeply picks a nested property from an object along the specified key path.\n * Supports both direct and curried usage.\n *\n * @example\n *\n * ```ts\n * const data = { a: { b: { c: 1, d: 2 }, e: 3 }, f: 4 } as const;\n *\n * // Direct usage\n * const result = Obj.deepPick(data, ['a', 'b', 'c']);\n *\n * assert.deepStrictEqual(result, { a: { b: { c: 1 } } });\n *\n * // Curried usage with pipe\n * const pickAB = Obj.deepPick(['a', 'b']);\n *\n * const result2 = pipe(data).map(pickAB).value;\n *\n * assert.deepStrictEqual(result2, { a: { b: { c: 1, d: 2 } } });\n * ```\n *\n * @template R - The type of the input record\n * @template Path - The key path tuple\n * @param record - The source record\n * @param path - A readonly tuple of keys representing the nested path\n * @returns A new record containing only the nested property at the path\n */\n export function deepPick<\n const R extends UnknownRecord,\n const Path extends readonly (string | number)[],\n >(record: R, path: Path): DeepPick<R, Path>;\n\n // Curried version\n export function deepPick<const Path extends readonly (string | number)[]>(\n path: Path,\n ): <const R extends UnknownRecord>(record: R) => DeepPick<R, Path>;\n\n export function deepPick<\n const R extends UnknownRecord,\n const Path extends readonly (string | number)[],\n >(\n ...args: readonly [record: R, path: Path] | readonly [path: Path]\n ): DeepPick<R, Path> | ((record: R) => DeepPick<R, Path>) {\n switch (args.length) {\n case 2: {\n const [record, path] = args;\n\n return (\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n deepPickImpl(record, path) as never\n );\n }\n\n case 1: {\n const [path] = args;\n\n return (record: R) => deepPick(record, path);\n }\n }\n }\n\n /**\n * Deeply omits a nested property from an object along the specified key path.\n * Supports both direct and curried usage.\n *\n * @example\n *\n * ```ts\n * const data = { a: { b: { c: 1, d: 2 }, e: 3 }, f: 4 } as const;\n *\n * // Direct usage\n * const result = Obj.deepOmit(data, ['a', 'b', 'c']);\n *\n * assert.deepStrictEqual(result, { a: { b: { d: 2 }, e: 3 }, f: 4 });\n *\n * // Curried usage with pipe\n * const omitAB = Obj.deepOmit(['a', 'b']);\n *\n * const result2 = pipe(data).map(omitAB).value;\n *\n * assert.deepStrictEqual(result2, { a: { e: 3 }, f: 4 });\n * ```\n *\n * @template R - The type of the input record\n * @template Path - The key path tuple\n * @param record - The source record\n * @param path - A readonly tuple of keys representing the nested path to omit\n * @returns A new record with the nested property at the path removed\n */\n export function deepOmit<\n const R extends UnknownRecord,\n const Path extends readonly (string | number)[],\n >(record: R, path: Path): DeepOmit<R, Path>;\n\n // Curried version\n export function deepOmit<const Path extends readonly (string | number)[]>(\n path: Path,\n ): <const R extends UnknownRecord>(record: R) => DeepOmit<R, Path>;\n\n export function deepOmit<\n const R extends UnknownRecord,\n const Path extends readonly (string | number)[],\n >(\n ...args: readonly [record: R, path: Path] | readonly [path: Path]\n ): DeepOmit<R, Path> | ((record: R) => DeepOmit<R, Path>) {\n switch (args.length) {\n case 2: {\n const [record, path] = args;\n\n return (\n // eslint-disable-next-line total-functions/no-unsafe-type-assertion\n deepOmitImpl(record, path) as never\n );\n }\n\n case 1: {\n const [path] = args;\n\n return (record: R) => deepOmit(record, path);\n }\n }\n }\n}\n\nconst deepPickImpl = (\n record: UnknownRecord,\n path: readonly (string | number)[],\n): UnknownRecord => {\n if (!Arr.isNonEmpty(path)) return record;\n\n const head = path[0];\n\n if (!hasKey(record, head)) return {};\n\n const value = record[head];\n\n const tail = path.slice(1);\n\n if (!Arr.isNonEmpty(tail)) return { [head]: value };\n\n if (!isRecord(value)) return { [head]: {} };\n\n return {\n [head]: deepPickImpl(value, tail),\n };\n};\n\nconst deepOmitImpl = (\n record: UnknownRecord,\n path: readonly (string | number)[],\n): UnknownRecord => {\n if (!Arr.isNonEmpty(path)) return record;\n\n const head = path[0];\n\n if (!hasKey(record, head)) return record;\n\n const tail = path.slice(1);\n\n if (!Arr.isNonEmpty(tail)) {\n return Object.fromEntries(\n Object.entries(record).filter(([k]) => k !== head),\n );\n }\n\n const value = record[head];\n\n if (!isRecord(value)) return record;\n\n return {\n ...record,\n [head]: deepOmitImpl(value, tail),\n };\n};\n\n/**\n * @internal\n * Internal type utilities for the Obj module.\n */\ndeclare namespace TsDataForgeInternals {\n type RecursionLimit = 20;\n\n /** - `[['x', 1], ['y', 3]]` -> `{ x: 1, y: 3 }` */\n export type EntriesToObject<\n Entries extends readonly (readonly [PropertyKey, unknown])[],\n // eslint-disable-next-line @typescript-eslint/no-empty-object-type\n > = Readonly<EntriesToObjectImpl<{}, Entries>>;\n\n /** @internal */\n type EntriesToObjectImpl<\n R,\n Entries extends readonly (readonly [PropertyKey, unknown])[],\n > =\n TypeEq<Entries['length'], 0> extends true\n ? R\n : EntriesToObjectImpl<\n R & ReadonlyRecord<Entries[0][0], Entries[0][1]>,\n List.Tail<Entries>\n >;\n\n /**\n * - `['x' | 'y' | 'z', number][]]` -> `'x' | 'y' | 'z'`\n * - `[['a' | 'b' | 'c', number], ...['x' | 'y' | 'z', number][]]` -> `'a' |\n * 'b' | 'c' | 'x' | 'y' | 'z'`\n *\n * @note To handle the second example above, recursion needs to be performed on infinite-length Entries,\n * but since the timing to stop cannot be determined, a recursion limit is set.\n */\n export type KeysOfEntries<\n Entries extends readonly (readonly [PropertyKey, unknown])[],\n > = KeysOfEntriesImpl<never, Entries, RecursionLimit>;\n\n type KeysOfEntriesImpl<\n K,\n Entries extends readonly (readonly [PropertyKey, unknown])[],\n RemainingNumRecursions extends number,\n > =\n TypeEq<RemainingNumRecursions, 0> extends true\n ? K\n : TypeEq<Entries['length'], 0> extends true\n ? K\n : KeysOfEntriesImpl<\n K | Entries[0][0],\n List.Tail<Entries>,\n Decrement<RemainingNumRecursions>\n >;\n\n export type ValuesOfEntries<\n Entries extends readonly (readonly [PropertyKey, unknown])[],\n > = ValuesOfEntriesImpl<never, Entries, RecursionLimit>;\n\n type ValuesOfEntriesImpl<\n K,\n Entries extends readonly (readonly [PropertyKey, unknown])[],\n RemainingNumRecursions extends number,\n > =\n TypeEq<RemainingNumRecursions, 0> extends true\n ? K\n : TypeEq<Entries['length'], 0> extends true\n ? K\n : ValuesOfEntriesImpl<\n K | Entries[0][1],\n List.Tail<Entries>,\n Decrement<RemainingNumRecursions>\n >;\n\n export type PartialIfKeyIsUnion<K, T> =\n IsUnion<K> extends true ? Partial<T> : T;\n\n /**\n * Merges two object types where keys in B override keys in A, preserving optional modifiers.\n *\n * This implementation uses a sophisticated technique to preserve optional property modifiers (`?`)\n * during the merge operation, which would otherwise be lost in a naive mapped type implementation.\n *\n * ## Core Technique: Optional Property Detection\n *\n * Uses `{} extends Pick<T, K>` to determine if a property is optional:\n * - **Required property**: `Pick<T, 'x'>` = `{ x: number }` → `{}` does NOT extend it (false)\n * - **Optional property**: `Pick<T, 'y'>` = `{ y?: string }` → `{}` DOES extend it (true)\n *\n * This works because `{}` (empty object) is assignable to objects with only optional properties\n * but not to objects with required properties.\n *\n * ## Implementation Strategy\n *\n * The type is constructed as an intersection of five mapped types:\n *\n * 1. **Required properties from B** - Properties in B that are required (override A completely)\n * 2. **Optional properties from B (not in A)** - New optional properties from B\n * 3. **Optional properties from B (in A)** - Union of A[K] | B[K] to preserve both possibilities\n * 4. **Required properties only in A** - Properties in A but not in B that are required\n * 5. **Optional properties only in A** - Properties in A but not in B that are optional (with `?` modifier)\n *\n * Finally, the intersection is flattened using an optional-preserving identity mapping that\n * re-applies the optional detection logic to maintain the `?` modifiers in the final type.\n *\n * @example\n * ```ts\n * type A = Readonly<{ x: number; y?: string }>;\n *\n * type B = Readonly<{ y?: number; z?: boolean }>;\n *\n * type Result = MergeTwo<A, B>;\n * // Result: Readonly<{ x: number; y?: string | number; z?: boolean }>\n * ```\n */\n // transformer-ignore-next-line\n type MergeTwo<A extends UnknownRecord, B extends UnknownRecord> = {\n // 1. Required properties from B (override A completely)\n // eslint-disable-next-line @typescript-eslint/no-empty-object-type\n readonly [K in keyof B as {} extends StrictPick<B, K> ? never : K]: B[K];\n } & {\n // 2. Optional properties from B that are NOT in A\n readonly [\n // eslint-disable-next-line @typescript-eslint/no-empty-object-type\n K in keyof B as {} extends StrictPick<B, K>\n ? K extends keyof A\n ? never\n : K\n : never\n ]?: B[K];\n } & {\n // 3. Optional properties from B that ARE in A (create union)\n readonly [\n // eslint-disable-next-line @typescript-eslint/no-empty-object-type\n K in keyof B as {} extends StrictPick<B, K>\n ? K extends keyof A\n ? K\n : never\n : never\n ]?: K extends keyof A ? A[K] | B[K] : never;\n } & {\n // 4. Required properties only in A (not overridden by B)\n readonly [\n // eslint-disable-next-line @typescript-eslint/no-empty-object-type\n K in RelaxedExclude<keyof A, keyof B> as {} extends StrictPick<A, K>\n ? never\n : K\n ]: A[K];\n } & {\n // 5. Optional properties only in A (not overridden by B)\n readonly [\n // eslint-disable-next-line @typescript-eslint/no-empty-object-type\n K in RelaxedExclude<keyof A, keyof B> as {} extends StrictPick<A, K>\n ? K\n : never\n ]?: A[K];\n } extends infer O\n ? Readonly<\n {\n // eslint-disable-next-line @typescript-eslint/no-empty-object-type\n [K in keyof O as {} extends StrictPick<O, K> ? never : K]: O[K];\n } & {\n // eslint-disable-next-line @typescript-eslint/no-empty-object-type\n [K in keyof O as {} extends StrictPick<O, K> ? K : never]?: O[K];\n }\n >\n : never;\n\n /** Sequentially merges a tuple of object types from left to right. */\n export type MergeAll<\n Records extends readonly UnknownRecord[],\n // eslint-disable-next-line @typescript-eslint/no-empty-object-type\n Acc extends UnknownRecord = {},\n > =\n IsFixedLengthList<Records> extends false\n ? ArrayElement<Records>\n : Records extends readonly [\n infer First extends UnknownRecord,\n ...infer Rest extends readonly UnknownRecord[],\n ]\n ? MergeAll<Rest, MergeTwo<Acc, First>>\n : Acc;\n}\n"],"names":["Obj","Arr.isNonEmpty"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4BO,IAAU;AAAA,CAAV,CAAUA,IAAAA,KAAV;AA2CE,EAAMA,KAAA,SAAA,GAAY,CACvB,GACA,CAAA,EACA,EAAA,GAA0C,OAAO,EAAA,KACrC;AACZ,IAAA,MAAM,QAAA,GAAW,MAAA,CAAO,OAAA,CAAQ,CAAC,CAAA;AAEjC,IAAA,MAAM,QAAA,GAAW,MAAA,CAAO,OAAA,CAAQ,CAAC,CAAA;AAEjC,IAAA,IAAI,QAAA,CAAS,MAAA,KAAW,QAAA,CAAS,MAAA,EAAQ,OAAO,KAAA;AAEhD,IAAA,OAAO,QAAA,CAAS,KAAA,CAAM,CAAC,CAAC,CAAA,EAAG,CAAC,CAAA,KAAM,EAAA,CAAG,CAAA,CAAE,CAAC,CAAA,EAAG,CAAC,CAAC,CAAA;AAAA,EAC/C,CAAA;AAyDO,EAAA,SAAS,QAIX,IAAA,EAGmD;AACtD,IAAA,QAAQ,KAAK,MAAA;AAAQ,MACnB,KAAK,CAAA,EAAG;AACN,QAAA,MAAM,CAAC,MAAA,EAAQ,IAAI,CAAA,GAAI,IAAA;AAEvB,QAAA,MAAM,OAAA,GAAU,IAAI,GAAA,CAAa,IAAI,CAAA;AAErC,QAAA;AAAA;AAAA,UAEE,MAAA,CAAO,WAAA;A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,OAAO,MAAA;AAE7B,EAAA,OAAO;AAAA,IACL,GAAG,MAAA;AAAA,IACH,CAAC,IAAI,GAAG,YAAA,CAAa,OAAO,IAAI;AAAA,GAClC;AACF,CAAA;;;;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fast-deep-equal.d.mts","sourceRoot":"","sources":["../../src/others/fast-deep-equal.mts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,eAAO,MAAM,aAAa,GAAI,CAAC,KAAM,CAAC,KAAK,CAAC,KAAG,
|
|
1
|
+
{"version":3,"file":"fast-deep-equal.d.mts","sourceRoot":"","sources":["../../src/others/fast-deep-equal.mts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,eAAO,MAAM,aAAa,GAAI,CAAC,KAAM,CAAC,KAAK,CAAC,KAAG,OAyF9C,CAAC"}
|
|
@@ -20,12 +20,12 @@ const fastDeepEqual = (a, b) => {
|
|
|
20
20
|
return false;
|
|
21
21
|
}
|
|
22
22
|
if (isMap(a)) {
|
|
23
|
-
return isMap(b) && a.size === b.size &&
|
|
23
|
+
return isMap(b) && a.size === b.size && a.entries().every(
|
|
24
24
|
([key, value]) => b.has(key) && fastDeepEqual(value, b.get(key))
|
|
25
25
|
);
|
|
26
26
|
}
|
|
27
27
|
if (isSet(a)) {
|
|
28
|
-
return isSet(b) && a.size === b.size &&
|
|
28
|
+
return isSet(b) && a.size === b.size && a.entries().every(([value]) => b.has(value));
|
|
29
29
|
}
|
|
30
30
|
if (isTypedArray(a)) {
|
|
31
31
|
return isTypedArray(b) && a.length === b.length && a.every((value, index) => value === b[index]);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fast-deep-equal.mjs","sources":["../../src/others/fast-deep-equal.mts"],"sourcesContent":["import { isMap, isRegExp, isSet, isTypedArray } from '@sindresorhus/is';\nimport { hasKey, isRecord } from '../guard/index.mjs';\n\n/**\n * The fastest deep equal with ES6 Map, Set and Typed arrays support.\n * Checks equality of Date and RegExp objects by value.\n *\n * Forked from https://github.com/epoberezkin/fast-deep-equal/blob/v3.1.3/src/index.jst\n */\nexport const fastDeepEqual = <T,>(a: T, b: T): boolean => {\n if (a === b) {\n return true;\n }\n\n if (typeof a !== typeof b) {\n return false;\n }\n\n // Arrays are handled before `isRecord`, which excludes them.\n if (Array.isArray(a)) {\n return (\n Array.isArray(b) &&\n a.constructor === b.constructor &&\n a.length === b.length &&\n a.every((ai, index) => fastDeepEqual(ai, b[index]))\n );\n }\n\n if (isRecord(a)) {\n if (!isRecord(b)) {\n return false;\n }\n\n if (a.constructor !== b.constructor) {\n return false;\n }\n\n if (isMap(a)) {\n return (\n isMap(b) &&\n a.size === b.size &&\n
|
|
1
|
+
{"version":3,"file":"fast-deep-equal.mjs","sources":["../../src/others/fast-deep-equal.mts"],"sourcesContent":["import { isMap, isRegExp, isSet, isTypedArray } from '@sindresorhus/is';\nimport { hasKey, isRecord } from '../guard/index.mjs';\n\n/**\n * The fastest deep equal with ES6 Map, Set and Typed arrays support.\n * Checks equality of Date and RegExp objects by value.\n *\n * Forked from https://github.com/epoberezkin/fast-deep-equal/blob/v3.1.3/src/index.jst\n */\nexport const fastDeepEqual = <T,>(a: T, b: T): boolean => {\n if (a === b) {\n return true;\n }\n\n if (typeof a !== typeof b) {\n return false;\n }\n\n // Arrays are handled before `isRecord`, which excludes them.\n if (Array.isArray(a)) {\n return (\n Array.isArray(b) &&\n a.constructor === b.constructor &&\n a.length === b.length &&\n a.every((ai, index) => fastDeepEqual(ai, b[index]))\n );\n }\n\n if (isRecord(a)) {\n if (!isRecord(b)) {\n return false;\n }\n\n if (a.constructor !== b.constructor) {\n return false;\n }\n\n if (isMap(a)) {\n return (\n isMap(b) &&\n a.size === b.size &&\n a\n .entries()\n .every(\n ([key, value]) => b.has(key) && fastDeepEqual(value, b.get(key)),\n )\n );\n }\n\n if (isSet(a)) {\n return (\n isSet(b) &&\n a.size === b.size &&\n a.entries().every(([value]) => b.has(value))\n );\n }\n\n if (isTypedArray(a)) {\n return (\n isTypedArray(b) &&\n a.length === b.length &&\n a.every((value, index) => value === b[index])\n );\n }\n\n if (isRegExp(a)) {\n return isRegExp(b) && a.source === b.source && a.flags === b.flags;\n }\n\n if (a.valueOf !== Object.prototype.valueOf) {\n return a.valueOf() === b.valueOf();\n }\n\n if (a.toString !== Object.prototype.toString) {\n return a.toString() === b.toString();\n }\n\n const aKeys: readonly string[] = Object.keys(a);\n\n const bKeys: readonly string[] = Object.keys(b);\n\n return (\n aKeys.length === bKeys.length &&\n aKeys.every(\n (key) =>\n hasKey(b, key) &&\n // React-specific: avoid traversing React elements' _owner.\n // _owner contains circular references\n // and is not needed when comparing the actual elements (and not their owners)\n // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions\n !(key === '_owner' && a['$$typeof']) &&\n fastDeepEqual(a[key], b[key]),\n )\n );\n }\n\n // true if both NaN, false otherwise\n return Number.isNaN(a) && Number.isNaN(b);\n};\n"],"names":[],"mappings":";;;;AASO,MAAM,aAAA,GAAgB,CAAK,CAAA,EAAM,CAAA,KAAkB;AACxD,EAAA,IAAI,MAAM,CAAA,EAAG;AACX,IAAA,OAAO,IAAA;AAAA,EACT;AAEA,EAAA,IAAI,OAAO,CAAA,KAAM,OAAO,CAAA,EAAG;AACzB,IAAA,OAAO,KAAA;AAAA,EACT;AAGA,EAAA,IAAI,KAAA,CAAM,OAAA,CAAQ,CAAC,CAAA,EAAG;AACpB,IAAA,OACE,KAAA,CAAM,QAAQ,CAAC,CAAA,IACf,EAAE,WAAA,KAAgB,CAAA,CAAE,WAAA,IACpB,CAAA,CAAE,MAAA,KAAW,CAAA,CAAE,UACf,CAAA,CAAE,KAAA,CAAM,CAAC,EAAA,EAAI,KAAA,KAAU,cAAc,EAAA,EAAI,CAAA,CAAE,KAAK,CAAC,CAAC,CAAA;AAAA,EAEtD;AAEA,EAAA,IAAI,QAAA,CAAS,CAAC,CAAA,EAAG;AACf,IAAA,IAAI,CAAC,QAAA,CAAS,CAAC,CAAA,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U,OAAA,EAAS;AAC1C,MAAA,OAAO,CAAA,CAAE,OAAA,EAAQ,KAAM,CAAA,CAAE,OAAA,EAAQ;AAAA,IACnC;AAEA,IAAA,IAAI,CAAA,CAAE,QAAA,KAAa,MAAA,CAAO,SAAA,CAAU,QAAA,EAAU;AAC5C,MAAA,OAAO,CAAA,CAAE,QAAA,EAAS,KAAM,CAAA,CAAE,QAAA,EAAS;AAAA,IACrC;AAEA,IAAA,MAAM,KAAA,GAA2B,MAAA,CAAO,IAAA,CAAK,CAAC,CAAA;AAE9C,IAAA,MAAM,KAAA,GAA2B,MAAA,CAAO,IAAA,CAAK,CAAC,CAAA;AAE9C,IAAA,OACE,KAAA,CAAM,MAAA,KAAW,KAAA,CAAM,MAAA,IACvB,KAAA,CAAM,KAAA;AAAA,MACJ,CAAC,GAAA,KACC,MAAA,CAAO,CAAA,EAAG,GAAG,CAAA;AAAA;AAAA;AAAA;AAAA,MAKb,EAAE,GAAA,KAAQ,QAAA,IAAY,CAAA,CAAE,UAAU,CAAA,CAAA,IAClC,aAAA,CAAc,CAAA,CAAE,GAAG,CAAA,EAAG,CAAA,CAAE,GAAG,CAAC;AAAA,KAChC;AAAA,EAEJ;AAGA,EAAA,OAAO,OAAO,KAAA,CAAM,CAAC,CAAA,IAAK,MAAA,CAAO,MAAM,CAAC,CAAA;AAC1C;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ts-data-forge",
|
|
3
|
-
"version": "14.0
|
|
3
|
+
"version": "14.2.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
],
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@sindresorhus/is": "^8.1.0",
|
|
39
|
-
"ts-type-forge": "~9.1.
|
|
39
|
+
"ts-type-forge": "~9.1.3"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@emotion/react": "11.14.0",
|
|
@@ -8,7 +8,11 @@ import {
|
|
|
8
8
|
import { expectType } from '../../expect-type.mjs';
|
|
9
9
|
import { asUint32 } from '../../number/index.mjs';
|
|
10
10
|
import { type SizeType } from '../../types.mjs';
|
|
11
|
-
import {
|
|
11
|
+
import {
|
|
12
|
+
asBoundedLengthArray,
|
|
13
|
+
asFixedLengthArray,
|
|
14
|
+
asMinLengthArray,
|
|
15
|
+
} from './array-utils-length-bounded-array-cast.mjs';
|
|
12
16
|
import {
|
|
13
17
|
set,
|
|
14
18
|
toFilled,
|
|
@@ -408,10 +412,11 @@ describe('Arr modifications', () => {
|
|
|
408
412
|
|
|
409
413
|
/* `Arr.set` / `Arr.toUpdated` across the input shapes the positional
|
|
410
414
|
`ConstrainedList.SetAt` annotation has to cope with; the suite previously
|
|
411
|
-
covered only the plain-tuple case. Written as
|
|
412
|
-
so the assertions get real overload resolution
|
|
413
|
-
|
|
414
|
-
|
|
415
|
+
covered only the plain-tuple case. Written as a function taking each shape as
|
|
416
|
+
a parameter so the assertions get real overload resolution against the
|
|
417
|
+
declared type rather than against whatever a literal happens to infer to; the
|
|
418
|
+
test below feeds it values built with the `as*` casts. */
|
|
419
|
+
const setTypeChecks = (
|
|
415
420
|
shapeTuple: readonly [1, 2, 3],
|
|
416
421
|
shapeArray: readonly number[],
|
|
417
422
|
shapeMutableArray: readonly number[],
|
|
@@ -487,3 +492,19 @@ export const setTypeChecks = (
|
|
|
487
492
|
|
|
488
493
|
expectType<typeof _updatedUnion, readonly [number, 2, number]>('=');
|
|
489
494
|
};
|
|
495
|
+
|
|
496
|
+
test('set / toUpdated type-level checks', () => {
|
|
497
|
+
const strings = ['a', 'b', 'c'] as const;
|
|
498
|
+
|
|
499
|
+
setTypeChecks(
|
|
500
|
+
[1, 2, 3],
|
|
501
|
+
[1, 2, 3],
|
|
502
|
+
[1, 2, 3],
|
|
503
|
+
asMinLengthArray(3, strings),
|
|
504
|
+
asBoundedLengthArray(2, 5, strings),
|
|
505
|
+
asFixedLengthArray(3, strings),
|
|
506
|
+
asMinLengthArray(3, [1, 2, 3, 4, 5] as const),
|
|
507
|
+
0,
|
|
508
|
+
1,
|
|
509
|
+
);
|
|
510
|
+
});
|