@reharik/smart-enum 0.0.1 → 0.0.3

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.
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  isSerializedSmartEnumItem,
3
3
  isSmartEnumItem
4
- } from "./chunk-NASPJET6.js";
4
+ } from "./chunk-E3FO4SL3.js";
5
5
 
6
6
  // src/utilities/logger.ts
7
7
  var consoleLogger = {
@@ -176,4 +176,4 @@ export {
176
176
  reviveAfterTransport,
177
177
  serializeForTransport
178
178
  };
179
- //# sourceMappingURL=chunk-7HX2FHYM.js.map
179
+ //# sourceMappingURL=chunk-3GONOJ54.js.map
@@ -142,4 +142,4 @@ export {
142
142
  isSmartEnum,
143
143
  isSerializedSmartEnumItem
144
144
  };
145
- //# sourceMappingURL=chunk-NASPJET6.js.map
145
+ //# sourceMappingURL=chunk-E3FO4SL3.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/enumerations.ts","../src/extensionMethods.ts","../src/types.ts","../src/utilities/typeGuards.ts"],"sourcesContent":["import { capitalCase, constantCase } from 'case-anything';\n\nimport { addExtensionMethods } from './extensionMethods.js';\nimport {\n SMART_ENUM,\n SMART_ENUM_ID,\n SMART_ENUM_ITEM,\n StandardEnumItem,\n} from './types.js';\n\ntype BuiltInOverrideKeys =\n | 'key'\n | 'value'\n | 'display'\n | 'deprecated'\n | 'index'\n | '__smart_enum_brand'\n | '__smart_enum_type';\n\nexport type EnumInputItem = Partial<{\n key: string;\n value: string;\n display: string;\n deprecated: boolean;\n}> &\n object;\n\nexport type ObjectEnumInput = Record<string, EnumInputItem>;\n\ntype EmptyEnumInputItem = Record<never, never>;\n\ntype Separator = '-' | '_' | ' ' | '.';\n\ntype IsUpperChar<C extends string> =\n C extends Uppercase<C> ? (C extends Lowercase<C> ? false : true) : false;\n\ntype IsLowerChar<C extends string> =\n C extends Lowercase<C> ? (C extends Uppercase<C> ? false : true) : false;\n\ntype PushUnderscore<S extends string> = S extends '' | `${string}_`\n ? S\n : `${S}_`;\n\ntype TrimUnderscore<S extends string> = S extends `_${infer R}`\n ? TrimUnderscore<R>\n : S extends `${infer R}_`\n ? TrimUnderscore<R>\n : S;\n\ntype CollapseUnderscores<S extends string> = S extends `${infer A}__${infer B}`\n ? CollapseUnderscores<`${A}_${B}`>\n : S;\n\ntype ConstantCaseInternal<\n S extends string,\n Prev extends string = '',\n Out extends string = '',\n> = S extends `${infer C}${infer Rest}`\n ? C extends Separator\n ? ConstantCaseInternal<Rest, C, PushUnderscore<Out>>\n : IsUpperChar<C> extends true\n ? IsLowerChar<Prev> extends true\n ? ConstantCaseInternal<Rest, C, `${PushUnderscore<Out>}${Uppercase<C>}`>\n : ConstantCaseInternal<Rest, C, `${Out}${Uppercase<C>}`>\n : ConstantCaseInternal<Rest, C, `${Out}${Uppercase<C>}`>\n : CollapseUnderscores<TrimUnderscore<Out>>;\n\ntype ConstantCase<S extends string> = string extends S\n ? string\n : ConstantCaseInternal<S>;\n\ntype ArrayToObjectType<T extends readonly string[]> = {\n [K in T[number]]: EmptyEnumInputItem & { readonly value?: ConstantCase<K> };\n};\n\ntype NormalizedInputType<TInput> = TInput extends readonly string[]\n ? ArrayToObjectType<TInput>\n : TInput extends ObjectEnumInput\n ? TInput\n : never;\n\ntype UnionKeys<T> = T extends T ? keyof T : never;\n\ntype MergeUnionToObject<T> = {\n [K in UnionKeys<T>]: T extends Record<K, infer V> ? V : undefined;\n};\n\ntype ExtraShapeUnion<TObj extends ObjectEnumInput> = {\n [K in keyof TObj]: Omit<TObj[K], BuiltInOverrideKeys>;\n}[keyof TObj];\n\nexport type InferredExtraFields<TObj extends ObjectEnumInput> =\n MergeUnionToObject<ExtraShapeUnion<TObj>>;\n\nexport type EnumItemFromNormalizedObject<\n TObj extends ObjectEnumInput,\n K extends keyof TObj = keyof TObj,\n> = Omit<StandardEnumItem, 'key' | 'value'> &\n InferredExtraFields<TObj> & {\n readonly key: Extract<K, string>;\n readonly value: TObj[K] extends { value?: infer V }\n ? Extract<V, string> extends never\n ? string\n : Extract<V, string>\n : string;\n };\n\nexport type EnumMemberUnionFromNormalizedObject<TObj extends ObjectEnumInput> =\n {\n [K in keyof TObj]: EnumItemFromNormalizedObject<TObj, K>;\n }[keyof TObj];\n\nexport type CoreEnumMethods<TItem extends StandardEnumItem> = {\n fromValue(value: string): TItem;\n tryFromValue(value?: string | null): TItem | undefined;\n fromKey(key: string): TItem;\n tryFromKey(key?: string | null): TItem | undefined;\n items(): readonly TItem[];\n values(): readonly string[];\n keys(): readonly string[];\n};\n\nexport type EnumFromNormalizedObject<TObj extends ObjectEnumInput> = {\n [K in keyof TObj]: EnumItemFromNormalizedObject<TObj, K>;\n} & CoreEnumMethods<EnumMemberUnionFromNormalizedObject<TObj>>;\n\nexport type EnumerationProps<TInput> = {\n input: TInput;\n propertyAutoFormatters?: PropertyAutoFormatter[];\n};\n\nexport type EnumItem<TEnum> = {\n [K in keyof TEnum]: TEnum[K] extends { __smart_enum_brand: true }\n ? TEnum[K]\n : never;\n}[keyof TEnum];\n\nexport type PropertyAutoFormatter = {\n key: string;\n format: (k: string) => string;\n};\n\ntype FinalizedEnumFields = Pick<\n StandardEnumItem,\n '__smart_enum_brand' | '__smart_enum_type'\n>;\n\ntype FinalizableEnumItem = {\n key: string;\n value: string;\n display: string;\n index: number;\n deprecated?: boolean;\n};\n\nfunction normalizeInput<TInput extends readonly string[] | ObjectEnumInput>(\n input: TInput,\n): NormalizedInputType<TInput> {\n if (Array.isArray(input)) {\n return Object.fromEntries(\n input.map(k => [k, {}]),\n ) as NormalizedInputType<TInput>;\n }\n\n return input as NormalizedInputType<TInput>;\n}\n\nconst finalizeEnumItem = <T extends { value: string }>(\n item: T,\n enumType: string,\n enumInstanceId: symbol,\n): T & FinalizedEnumFields => {\n Object.defineProperty(item, SMART_ENUM_ITEM, {\n value: true,\n enumerable: false,\n });\n\n Object.defineProperty(item, SMART_ENUM_ID, {\n value: enumInstanceId,\n enumerable: false,\n });\n\n Object.defineProperty(item, '__smart_enum_brand', {\n value: true,\n enumerable: false,\n });\n\n Object.defineProperty(item, '__smart_enum_type', {\n value: enumType,\n enumerable: false,\n });\n\n Object.defineProperty(item, 'toJSON', {\n value: () => ({ __smart_enum_type: enumType, value: item.value }),\n enumerable: false,\n });\n\n Object.defineProperty(item, 'toPostgres', {\n value: () => item.value,\n enumerable: false,\n });\n\n return item as T & FinalizedEnumFields;\n};\n\nconst formatProperties = (\n k: string,\n formatters: PropertyAutoFormatter[],\n): { value: string; display: string } & Record<string, string> =>\n formatters.reduce(\n (acc, formatter) => {\n acc[formatter.key] = formatter.format(k);\n return acc;\n },\n {\n value: constantCase(k),\n display: capitalCase(k),\n } as { value: string; display: string } & Record<string, string>,\n );\n\nfunction buildEnumFromObject<TObj extends ObjectEnumInput>(\n enumType: string,\n input: TObj,\n propertyAutoFormatters?: PropertyAutoFormatter[],\n): EnumFromNormalizedObject<TObj> {\n const formattersWithDefaults: PropertyAutoFormatter[] = [\n { key: 'value', format: constantCase },\n { key: 'display', format: capitalCase },\n ...(propertyAutoFormatters ?? []),\n ];\n\n type TItem = EnumItemFromNormalizedObject<TObj>;\n\n const rawEnumItems = {} as {\n [K in keyof TObj]: EnumItemFromNormalizedObject<TObj, K>;\n };\n\n const enumInstanceId = Symbol('smart-enum-instance');\n let index = 0;\n\n for (const key in input) {\n if (Object.prototype.hasOwnProperty.call(input, key)) {\n const typedKey = key;\n const value = input[typedKey];\n\n const enumItemBase = {\n index,\n key: typedKey,\n ...formatProperties(typedKey, formattersWithDefaults),\n ...value,\n } as FinalizableEnumItem & TObj[typeof typedKey];\n\n const enumItem = finalizeEnumItem(\n enumItemBase,\n enumType,\n enumInstanceId,\n ) as unknown as TItem;\n\n Object.freeze(enumItem);\n rawEnumItems[typedKey] = enumItem;\n index++;\n }\n }\n\n const extensionMethods = addExtensionMethods<TItem>(\n Object.values(rawEnumItems) as TItem[],\n );\n\n const enumObject = {\n ...rawEnumItems,\n ...extensionMethods,\n } as EnumFromNormalizedObject<TObj>;\n\n Object.defineProperty(enumObject, SMART_ENUM, {\n value: true,\n enumerable: false,\n });\n\n Object.freeze(enumObject);\n\n return enumObject;\n}\n\nexport function enumeration<const TArr extends readonly string[]>(\n enumType: string,\n props: EnumerationProps<TArr>,\n): EnumFromNormalizedObject<NormalizedInputType<TArr>>;\n\nexport function enumeration<const TObj extends ObjectEnumInput>(\n enumType: string,\n props: EnumerationProps<TObj>,\n): EnumFromNormalizedObject<NormalizedInputType<TObj>>;\n\nexport function enumeration(\n enumType: string,\n props: EnumerationProps<readonly string[] | ObjectEnumInput>,\n) {\n const normalized = normalizeInput(props.input);\n return buildEnumFromObject(\n enumType,\n normalized,\n props.propertyAutoFormatters,\n );\n}\n","import type { CoreEnumMethods, StandardEnumItem } from './types.js';\nexport const addExtensionMethods = <TItem extends StandardEnumItem>(\n enumItems: readonly TItem[],\n): CoreEnumMethods<TItem> => {\n const findBy = <K extends keyof TItem>(field: K, target: TItem[K]) =>\n enumItems.find(item => item[field] === target);\n\n const requireBy = <K extends keyof TItem>(\n field: K,\n target: TItem[K],\n label: string,\n ) => {\n const item = findBy(field, target);\n if (!item) {\n throw new Error(`No enum ${label} found for '${String(target)}'`);\n }\n return item;\n };\n\n return {\n fromValue: value => requireBy('value', value as TItem['value'], 'value'),\n tryFromValue: value =>\n value ? findBy('value', value as TItem['value']) : undefined,\n\n fromKey: key => requireBy('key', key as TItem['key'], 'key'),\n tryFromKey: key => (key ? findBy('key', key as TItem['key']) : undefined),\n\n items: () => [...enumItems],\n values: () => enumItems.map(item => item.value),\n keys: () => enumItems.map(item => item.key),\n };\n};\n","/**\n * Type guard to check if a value is not null or undefined\n * @param value - The value to check\n * @returns True if the value is defined and not null\n */\nexport const notEmpty = <X>(\n value: X | null | undefined,\n): value is NonNullable<X> => {\n // eslint-disable-next-line unicorn/no-null\n return value != null;\n};\n\n// Public symbols used at runtime for detection/identity (not used in type keys)\nexport const SMART_ENUM_ITEM = Symbol('smart-enum-item');\nexport const SMART_ENUM_ID = Symbol('smart-enum-id');\nexport const SMART_ENUM = Symbol('smart-enum');\n\n/**\n * Options for filtering enum items in various methods\n */\nexport type EnumFilterOptions = {\n /** Include items with null/undefined values (default: false) */\n showEmpty?: boolean;\n /** Include deprecated items (default: false) */\n showDeprecated?: boolean;\n};\n\n// export type EnumInput = readonly string[] | ObjectEnumInput;\n\n/**\n * Compile-time transformer: replaces Smart Enum items with string values,\n * recursively over arrays and objects. Structural detection checks for\n * presence of `key` and `value`.\n */\nexport type SerializedSmartEnums<T> = T extends { value: string; key: unknown }\n ? string\n : T extends ReadonlyArray<infer U>\n ? ReadonlyArray<SerializedSmartEnums<U>>\n : T extends Array<infer U>\n ? SerializedSmartEnums<U>[]\n : T extends object\n ? { [K in keyof T]: SerializedSmartEnums<T[K]> }\n : T;\n\n/**\n * Revived shape: for keys present in mapping M, the string becomes the\n * corresponding enum item type (derived from the provided enum object);\n * other fields recurse.\n */\nexport type EnumItemFromEnum<TEnum> =\n TEnum extends Record<string, infer V>\n ? V extends { __smart_enum_brand: true }\n ? V\n : never\n : never;\n\n// Structural constraint for enum objects passed to reviveSmartEnums mapping\nexport type AnyEnumLike = {\n tryFromValue: (value?: string | null) => unknown;\n tryFromKey: (key?: string | null) => unknown;\n} & Record<string, unknown>;\n\nexport type SmartEnumLike<T = unknown> = {\n tryFromValue: (value: string) => T | undefined;\n};\n\nexport type FieldEnumMapping = Record<string, SmartEnumLike>;\n\nexport type ReviveRowOptions = {\n fieldEnumMapping: FieldEnumMapping;\n strict?: boolean;\n};\n\nexport type PathEnumMapping = Record<string, SmartEnumLike>;\n\nexport type RevivePayloadOptions = {\n pathEnumMapping: PathEnumMapping;\n strict?: boolean;\n};\n\nexport type RevivedSmartEnums<T, M extends Record<string, AnyEnumLike>> =\n T extends ReadonlyArray<infer U>\n ? RevivedSmartEnums<U, M>[]\n : T extends Array<infer U>\n ? RevivedSmartEnums<U, M>[]\n : T extends object\n ? {\n [K in keyof T]: K extends Extract<keyof M, string>\n ? Enumeration<M[K]>\n : RevivedSmartEnums<T[K], M>;\n }\n : T;\n\nexport type SmartEnumItemSerialized = {\n __smart_enum_type: string;\n value: string;\n};\n\n/** Example shape for transport tests (`reviveAfterTransport` registry). */\nexport type SmartApiHelperConfig = {\n enumRegistry: Record<string, AnyEnumLike>;\n};\n\n/**\n * Compile-time transformer: replaces Smart Enum items with string values,\n * recursively over arrays and objects. Used for database storage.\n */\nexport type DatabaseFormat<T> = T extends { value: string; key: unknown }\n ? string\n : T extends ReadonlyArray<infer U>\n ? ReadonlyArray<DatabaseFormat<U>>\n : T extends Array<infer U>\n ? DatabaseFormat<U>[]\n : T extends object\n ? { [K in keyof T]: DatabaseFormat<T[K]> }\n : T;\n\n/**\n * Log levels for smart enum mappings\n */\nexport type LogLevel = 'debug' | 'info' | 'warn' | 'error';\n\n/**\n * Configuration for smart enum mappings initialization\n */\nexport type SmartEnumMappingsConfig = {\n enumRegistry: Record<string, AnyEnumLike>;\n logLevel?: LogLevel;\n logger?: import('./utilities/logger.js').Logger;\n};\n\ntype BuiltInOverrideKeys =\n | 'key'\n | 'value'\n | 'display'\n | 'deprecated'\n | 'index'\n | '__smart_enum_brand'\n | '__smart_enum_type';\n\nexport type StandardEnumItemBase = {\n readonly key: string;\n readonly value: string;\n readonly display: string;\n readonly index: number;\n readonly deprecated?: boolean;\n};\n\nexport type StandardEnumItem = StandardEnumItemBase & {\n readonly __smart_enum_brand: true;\n readonly __smart_enum_type: string;\n readonly toPostgres: () => string;\n};\n\nexport type EnumInputItem = Partial<{\n key: string;\n value: string;\n display: string;\n deprecated: boolean;\n}> &\n Record<string, unknown>;\n\nexport type ObjectEnumInput = Record<string, EnumInputItem>;\n\nexport type EmptyEnumInputItem = Record<never, never>;\n\nexport type ArrayToObjectType<T extends readonly string[]> = {\n [K in T[number]]: EmptyEnumInputItem;\n};\n\nexport type NormalizedInputType<TInput> = TInput extends readonly string[]\n ? ArrayToObjectType<TInput>\n : TInput extends ObjectEnumInput\n ? TInput\n : never;\n\nexport type EnumItemFromNormalizedObject<\n TObj extends ObjectEnumInput,\n K extends keyof TObj = keyof TObj,\n> = Omit<StandardEnumItem, 'key'> &\n InferredExtraFields<TObj> & {\n readonly key: Extract<K, string>;\n };\n\nexport type EnumMemberUnionFromNormalizedObject<TObj extends ObjectEnumInput> =\n {\n [K in keyof TObj]: EnumItemFromNormalizedObject<TObj, K>;\n }[keyof TObj];\n\nexport type EnumFromNormalizedObject<TObj extends ObjectEnumInput> = {\n [K in keyof TObj]: EnumItemFromNormalizedObject<TObj, K>;\n} & CoreEnumMethods<EnumMemberUnionFromNormalizedObject<TObj>>;\n\nexport type UnionKeys<T> = T extends T ? keyof T : never;\n\n/*\nTo widen the type of the OptionalObject from literals to their actual types,\ne.g.\nshape?: \"round\" | \"square\" | \"long\";\nslices?: true;\nlayers?: 2;\n--turns into:--\nshape: string | undefined;\nslices: boolean| undefined;\nlayers: number| undefined;\n\nwe can use the following code:\ntype Widen<T> = T extends string\n ? string\n : T extends number\n ? number\n : T extends boolean\n ? boolean\n : T;\n\ntype MergeUnionToObject<T> = {\n [K in UnionKeys<T>]: T extends Record<K, infer V> ? Widen<V> : undefined;\n};\n*/\n\ntype MergeUnionToObject<T> = {\n [K in UnionKeys<T>]: T extends Record<K, infer V> ? V : undefined;\n};\n\ntype ExtraShapeUnion<TObj extends ObjectEnumInput> = {\n [K in keyof TObj]: Omit<TObj[K], BuiltInOverrideKeys>;\n}[keyof TObj];\n\nexport type InferredExtraFields<TObj extends ObjectEnumInput> =\n MergeUnionToObject<ExtraShapeUnion<TObj>>;\n\nexport type PropertyAutoFormatter = {\n /** The property name to generate */\n key: string;\n /** Function to transform the key into the property value */\n format: (k: string) => string;\n};\n\nexport type CoreEnumMethods<TItem extends StandardEnumItem> = {\n fromValue(value: string): TItem;\n tryFromValue(value?: string | null): TItem | undefined;\n fromKey(key: string): TItem;\n tryFromKey(key?: string | null): TItem | undefined;\n items(): readonly TItem[];\n values(): readonly string[];\n keys(): readonly string[];\n};\n\nexport type EnumerationProps<TInput> = {\n input: TInput;\n propertyAutoFormatters?: PropertyAutoFormatter[];\n};\n\nexport type Enumeration<TEnum> =\n TEnum extends Record<string, infer V>\n ? V extends { __smart_enum_brand: true }\n ? V\n : never\n : never;\n\nexport type FinalizedEnumFields = Pick<\n StandardEnumItem,\n '__smart_enum_brand' | '__smart_enum_type'\n>;\nexport type FinalizableEnumItem = {\n key: string;\n value: string;\n display: string;\n index: number;\n deprecated?: boolean;\n};\n","import {\n SMART_ENUM_ITEM,\n SMART_ENUM,\n SmartEnumItemSerialized,\n} from '../types.js';\n\n/**\n * Runtime type guard to detect Smart Enum items created by this library.\n * Returns true if the value has the SMART_ENUM_ITEM symbol.\n *\n * @example\n * ```typescript\n * import { Status } from './status';\n * const item = Status.active;\n * isSmartEnumItem(item); // true\n * isSmartEnumItem({ key: 'active', value: 'ACTIVE' }); // false (plain object)\n * if (isSmartEnumItem(x)) {\n * console.log(x.value, x.__smart_enum_type); // narrowed to enum item\n * }\n * ```\n */\nexport const isSmartEnumItem = (\n x: unknown,\n): x is {\n key: string;\n value: string;\n index?: number;\n __smart_enum_type?: string;\n} => {\n return (\n !!x && typeof x === 'object' && Reflect.get(x, SMART_ENUM_ITEM) === true\n );\n};\n\n/**\n * Runtime type guard to detect a full Smart Enum object created by this library.\n * Returns true if the object has the SMART_ENUM property.\n *\n * @example\n * ```typescript\n * import { MyEnum } from './blah';\n * isSmartEnum(MyEnum) === true; // true\n * isSmartEnum(MyEnum.one) === false; // false (this is an item, not the enum)\n * ```\n */\nexport const isSmartEnum = (x: unknown): boolean => {\n return !!x && typeof x === 'object' && Reflect.get(x, SMART_ENUM) === true;\n};\n\n/**\n * Runtime type guard to detect a serialized Smart Enum item created by this library.\n * Returns true if the value has `__smart_enum_type` and `value` (wire/database shape).\n *\n * @example\n * ```typescript\n * const wire = { __smart_enum_type: 'Status', value: 'ACTIVE' };\n * isSerializedSmartEnumItem(wire); // true\n * isSerializedSmartEnumItem(Status.active); // false (live enum item)\n * if (isSerializedSmartEnumItem(x)) {\n * reviveItem(x.__smart_enum_type, x.value); // narrowed to SmartEnumItemSerialized\n * }\n * ```\n */\nexport const isSerializedSmartEnumItem = (\n x: unknown,\n): x is SmartEnumItemSerialized => {\n return (\n !!x &&\n typeof x === 'object' &&\n Reflect.has(x, '__smart_enum_type') &&\n Reflect.has(x, 'value')\n );\n};\n"],"mappings":";AAAA,SAAS,aAAa,oBAAoB;;;ACCnC,IAAM,sBAAsB,CACjC,cAC2B;AAC3B,QAAM,SAAS,CAAwB,OAAU,WAC/C,UAAU,KAAK,UAAQ,KAAK,KAAK,MAAM,MAAM;AAE/C,QAAM,YAAY,CAChB,OACA,QACA,UACG;AACH,UAAM,OAAO,OAAO,OAAO,MAAM;AACjC,QAAI,CAAC,MAAM;AACT,YAAM,IAAI,MAAM,WAAW,KAAK,eAAe,OAAO,MAAM,CAAC,GAAG;AAAA,IAClE;AACA,WAAO;AAAA,EACT;AAEA,SAAO;AAAA,IACL,WAAW,WAAS,UAAU,SAAS,OAAyB,OAAO;AAAA,IACvE,cAAc,WACZ,QAAQ,OAAO,SAAS,KAAuB,IAAI;AAAA,IAErD,SAAS,SAAO,UAAU,OAAO,KAAqB,KAAK;AAAA,IAC3D,YAAY,SAAQ,MAAM,OAAO,OAAO,GAAmB,IAAI;AAAA,IAE/D,OAAO,MAAM,CAAC,GAAG,SAAS;AAAA,IAC1B,QAAQ,MAAM,UAAU,IAAI,UAAQ,KAAK,KAAK;AAAA,IAC9C,MAAM,MAAM,UAAU,IAAI,UAAQ,KAAK,GAAG;AAAA,EAC5C;AACF;;;AClBO,IAAM,kBAAkB,OAAO,iBAAiB;AAChD,IAAM,gBAAgB,OAAO,eAAe;AAC5C,IAAM,aAAa,OAAO,YAAY;;;AF4I7C,SAAS,eACP,OAC6B;AAC7B,MAAI,MAAM,QAAQ,KAAK,GAAG;AACxB,WAAO,OAAO;AAAA,MACZ,MAAM,IAAI,OAAK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atabase.ts
6
6
  var isPlainObject = (x) => typeof x === "object" && x !== null && Object.getPrototypeOf(x) === Object.prototype;
@@ -181,4 +181,4 @@ export {
181
181
  reviveRowFromDatabase,
182
182
  revivePayloadFromDatabase
183
183
  };
184
- //# sourceMappingURL=chunk-YGXDKW6W.js.map
184
+ //# sourceMappingURL=chunk-X2DQHWNN.js.map
@@ -1,76 +1,3 @@
1
- type BuiltInOverrideKeys = 'key' | 'value' | 'display' | 'deprecated' | 'index' | '__smart_enum_brand' | '__smart_enum_type';
2
- type StandardEnumItem = {
3
- readonly __smart_enum_brand: true;
4
- readonly __smart_enum_type: string;
5
- readonly key: string;
6
- readonly value: string;
7
- readonly display: string;
8
- readonly index: number;
9
- readonly deprecated?: boolean;
10
- readonly toPostgres: () => string;
11
- };
12
- type EnumInputItem = Partial<{
13
- key: string;
14
- value: string;
15
- display: string;
16
- deprecated: boolean;
17
- }> & object;
18
- type ObjectEnumInput = Record<string, EnumInputItem>;
19
- type EmptyEnumInputItem = Record<never, never>;
20
- type Separator = '-' | '_' | ' ' | '.';
21
- type IsUpperChar<C extends string> = C extends Uppercase<C> ? (C extends Lowercase<C> ? false : true) : false;
22
- type IsLowerChar<C extends string> = C extends Lowercase<C> ? (C extends Uppercase<C> ? false : true) : false;
23
- type PushUnderscore<S extends string> = S extends '' | `${string}_` ? S : `${S}_`;
24
- type TrimUnderscore<S extends string> = S extends `_${infer R}` ? TrimUnderscore<R> : S extends `${infer R}_` ? TrimUnderscore<R> : S;
25
- type CollapseUnderscores<S extends string> = S extends `${infer A}__${infer B}` ? CollapseUnderscores<`${A}_${B}`> : S;
26
- type ConstantCaseInternal<S extends string, Prev extends string = '', Out extends string = ''> = S extends `${infer C}${infer Rest}` ? C extends Separator ? ConstantCaseInternal<Rest, C, PushUnderscore<Out>> : IsUpperChar<C> extends true ? IsLowerChar<Prev> extends true ? ConstantCaseInternal<Rest, C, `${PushUnderscore<Out>}${Uppercase<C>}`> : ConstantCaseInternal<Rest, C, `${Out}${Uppercase<C>}`> : ConstantCaseInternal<Rest, C, `${Out}${Uppercase<C>}`> : CollapseUnderscores<TrimUnderscore<Out>>;
27
- type ConstantCase<S extends string> = string extends S ? string : ConstantCaseInternal<S>;
28
- type ArrayToObjectType<T extends readonly string[]> = {
29
- [K in T[number]]: EmptyEnumInputItem & {
30
- readonly value?: ConstantCase<K>;
31
- };
32
- };
33
- type NormalizedInputType<TInput> = TInput extends readonly string[] ? ArrayToObjectType<TInput> : TInput extends ObjectEnumInput ? TInput : never;
34
- type UnionKeys<T> = T extends T ? keyof T : never;
35
- type MergeUnionToObject<T> = {
36
- [K in UnionKeys<T>]: T extends Record<K, infer V> ? V : undefined;
37
- };
38
- type ExtraShapeUnion<TObj extends ObjectEnumInput> = {
39
- [K in keyof TObj]: Omit<TObj[K], BuiltInOverrideKeys>;
40
- }[keyof TObj];
41
- type InferredExtraFields<TObj extends ObjectEnumInput> = MergeUnionToObject<ExtraShapeUnion<TObj>>;
42
- type EnumItemFromNormalizedObject<TObj extends ObjectEnumInput, K extends keyof TObj = keyof TObj> = Omit<StandardEnumItem, 'key' | 'value'> & InferredExtraFields<TObj> & {
43
- readonly key: Extract<K, string>;
44
- readonly value: TObj[K] extends {
45
- value?: infer V;
46
- } ? Extract<V, string> extends never ? string : Extract<V, string> : string;
47
- };
48
- type EnumMemberUnionFromNormalizedObject<TObj extends ObjectEnumInput> = {
49
- [K in keyof TObj]: EnumItemFromNormalizedObject<TObj, K>;
50
- }[keyof TObj];
51
- type CoreEnumMethods<TItem extends StandardEnumItem> = {
52
- fromValue(value: string): TItem;
53
- tryFromValue(value?: string | null): TItem | undefined;
54
- fromKey(key: string): TItem;
55
- tryFromKey(key?: string | null): TItem | undefined;
56
- items(): readonly TItem[];
57
- values(): readonly string[];
58
- keys(): readonly string[];
59
- };
60
- type EnumFromNormalizedObject<TObj extends ObjectEnumInput> = {
61
- [K in keyof TObj]: EnumItemFromNormalizedObject<TObj, K>;
62
- } & CoreEnumMethods<EnumMemberUnionFromNormalizedObject<TObj>>;
63
- type EnumerationProps<TInput> = {
64
- input: TInput;
65
- propertyAutoFormatters?: PropertyAutoFormatter[];
66
- };
67
- type PropertyAutoFormatter = {
68
- key: string;
69
- format: (k: string) => string;
70
- };
71
- declare function enumeration<const TArr extends readonly string[]>(enumType: string, props: EnumerationProps<TArr>): EnumFromNormalizedObject<NormalizedInputType<TArr>>;
72
- declare function enumeration<const TObj extends ObjectEnumInput>(enumType: string, props: EnumerationProps<TObj>): EnumFromNormalizedObject<NormalizedInputType<TObj>>;
73
-
74
1
  /**
75
2
  * Logger interface for ts-smart-enum library
76
3
  *
@@ -145,10 +72,85 @@ type SmartEnumMappingsConfig = {
145
72
  logLevel?: LogLevel;
146
73
  logger?: Logger;
147
74
  };
75
+ type StandardEnumItemBase = {
76
+ readonly key: string;
77
+ readonly value: string;
78
+ readonly display: string;
79
+ readonly index: number;
80
+ readonly deprecated?: boolean;
81
+ };
82
+ type StandardEnumItem = StandardEnumItemBase & {
83
+ readonly __smart_enum_brand: true;
84
+ readonly __smart_enum_type: string;
85
+ readonly toPostgres: () => string;
86
+ };
148
87
  type Enumeration<TEnum> = TEnum extends Record<string, infer V> ? V extends {
149
88
  __smart_enum_brand: true;
150
89
  } ? V : never : never;
151
90
 
91
+ type BuiltInOverrideKeys = 'key' | 'value' | 'display' | 'deprecated' | 'index' | '__smart_enum_brand' | '__smart_enum_type';
92
+ type EnumInputItem = Partial<{
93
+ key: string;
94
+ value: string;
95
+ display: string;
96
+ deprecated: boolean;
97
+ }> & object;
98
+ type ObjectEnumInput = Record<string, EnumInputItem>;
99
+ type EmptyEnumInputItem = Record<never, never>;
100
+ type Separator = '-' | '_' | ' ' | '.';
101
+ type IsUpperChar<C extends string> = C extends Uppercase<C> ? (C extends Lowercase<C> ? false : true) : false;
102
+ type IsLowerChar<C extends string> = C extends Lowercase<C> ? (C extends Uppercase<C> ? false : true) : false;
103
+ type PushUnderscore<S extends string> = S extends '' | `${string}_` ? S : `${S}_`;
104
+ type TrimUnderscore<S extends string> = S extends `_${infer R}` ? TrimUnderscore<R> : S extends `${infer R}_` ? TrimUnderscore<R> : S;
105
+ type CollapseUnderscores<S extends string> = S extends `${infer A}__${infer B}` ? CollapseUnderscores<`${A}_${B}`> : S;
106
+ type ConstantCaseInternal<S extends string, Prev extends string = '', Out extends string = ''> = S extends `${infer C}${infer Rest}` ? C extends Separator ? ConstantCaseInternal<Rest, C, PushUnderscore<Out>> : IsUpperChar<C> extends true ? IsLowerChar<Prev> extends true ? ConstantCaseInternal<Rest, C, `${PushUnderscore<Out>}${Uppercase<C>}`> : ConstantCaseInternal<Rest, C, `${Out}${Uppercase<C>}`> : ConstantCaseInternal<Rest, C, `${Out}${Uppercase<C>}`> : CollapseUnderscores<TrimUnderscore<Out>>;
107
+ type ConstantCase<S extends string> = string extends S ? string : ConstantCaseInternal<S>;
108
+ type ArrayToObjectType<T extends readonly string[]> = {
109
+ [K in T[number]]: EmptyEnumInputItem & {
110
+ readonly value?: ConstantCase<K>;
111
+ };
112
+ };
113
+ type NormalizedInputType<TInput> = TInput extends readonly string[] ? ArrayToObjectType<TInput> : TInput extends ObjectEnumInput ? TInput : never;
114
+ type UnionKeys<T> = T extends T ? keyof T : never;
115
+ type MergeUnionToObject<T> = {
116
+ [K in UnionKeys<T>]: T extends Record<K, infer V> ? V : undefined;
117
+ };
118
+ type ExtraShapeUnion<TObj extends ObjectEnumInput> = {
119
+ [K in keyof TObj]: Omit<TObj[K], BuiltInOverrideKeys>;
120
+ }[keyof TObj];
121
+ type InferredExtraFields<TObj extends ObjectEnumInput> = MergeUnionToObject<ExtraShapeUnion<TObj>>;
122
+ type EnumItemFromNormalizedObject<TObj extends ObjectEnumInput, K extends keyof TObj = keyof TObj> = Omit<StandardEnumItem, 'key' | 'value'> & InferredExtraFields<TObj> & {
123
+ readonly key: Extract<K, string>;
124
+ readonly value: TObj[K] extends {
125
+ value?: infer V;
126
+ } ? Extract<V, string> extends never ? string : Extract<V, string> : string;
127
+ };
128
+ type EnumMemberUnionFromNormalizedObject<TObj extends ObjectEnumInput> = {
129
+ [K in keyof TObj]: EnumItemFromNormalizedObject<TObj, K>;
130
+ }[keyof TObj];
131
+ type CoreEnumMethods<TItem extends StandardEnumItem> = {
132
+ fromValue(value: string): TItem;
133
+ tryFromValue(value?: string | null): TItem | undefined;
134
+ fromKey(key: string): TItem;
135
+ tryFromKey(key?: string | null): TItem | undefined;
136
+ items(): readonly TItem[];
137
+ values(): readonly string[];
138
+ keys(): readonly string[];
139
+ };
140
+ type EnumFromNormalizedObject<TObj extends ObjectEnumInput> = {
141
+ [K in keyof TObj]: EnumItemFromNormalizedObject<TObj, K>;
142
+ } & CoreEnumMethods<EnumMemberUnionFromNormalizedObject<TObj>>;
143
+ type EnumerationProps<TInput> = {
144
+ input: TInput;
145
+ propertyAutoFormatters?: PropertyAutoFormatter[];
146
+ };
147
+ type PropertyAutoFormatter = {
148
+ key: string;
149
+ format: (k: string) => string;
150
+ };
151
+ declare function enumeration<const TArr extends readonly string[]>(enumType: string, props: EnumerationProps<TArr>): EnumFromNormalizedObject<NormalizedInputType<TArr>>;
152
+ declare function enumeration<const TObj extends ObjectEnumInput>(enumType: string, props: EnumerationProps<TObj>): EnumFromNormalizedObject<NormalizedInputType<TObj>>;
153
+
152
154
  /**
153
155
  * Runtime type guard to detect Smart Enum items created by this library.
154
156
  * Returns true if the value has the SMART_ENUM_ITEM symbol.
@@ -183,4 +185,4 @@ declare const isSmartEnumItem: (x: unknown) => x is {
183
185
  */
184
186
  declare const isSmartEnum: (x: unknown) => boolean;
185
187
 
186
- export { type AnyEnumLike as A, type DatabaseFormat as D, type Enumeration as E, type FieldEnumMapping as F, type Logger as L, type PathEnumMapping as P, type RevivedSmartEnums as R, type SmartApiHelperConfig as S, isSmartEnum as a, type LogLevel as b, type SmartEnumMappingsConfig as c, type SerializedSmartEnums as d, enumeration as e, type SmartEnumItemSerialized as f, type SmartEnumLike as g, type ReviveRowOptions as h, isSmartEnumItem as i, type RevivePayloadOptions as j };
188
+ export { type AnyEnumLike as A, type DatabaseFormat as D, type Enumeration as E, type FieldEnumMapping as F, type Logger as L, type PathEnumMapping as P, type RevivedSmartEnums as R, type SmartApiHelperConfig as S, isSmartEnum as a, type LogLevel as b, type SmartEnumMappingsConfig as c, type SerializedSmartEnums as d, enumeration as e, type SmartEnumItemSerialized as f, type SmartEnumLike as g, type ReviveRowOptions as h, isSmartEnumItem as i, type RevivePayloadOptions as j, type StandardEnumItemBase as k };
@@ -1,76 +1,3 @@
1
- type BuiltInOverrideKeys = 'key' | 'value' | 'display' | 'deprecated' | 'index' | '__smart_enum_brand' | '__smart_enum_type';
2
- type StandardEnumItem = {
3
- readonly __smart_enum_brand: true;
4
- readonly __smart_enum_type: string;
5
- readonly key: string;
6
- readonly value: string;
7
- readonly display: string;
8
- readonly index: number;
9
- readonly deprecated?: boolean;
10
- readonly toPostgres: () => string;
11
- };
12
- type EnumInputItem = Partial<{
13
- key: string;
14
- value: string;
15
- display: string;
16
- deprecated: boolean;
17
- }> & object;
18
- type ObjectEnumInput = Record<string, EnumInputItem>;
19
- type EmptyEnumInputItem = Record<never, never>;
20
- type Separator = '-' | '_' | ' ' | '.';
21
- type IsUpperChar<C extends string> = C extends Uppercase<C> ? (C extends Lowercase<C> ? false : true) : false;
22
- type IsLowerChar<C extends string> = C extends Lowercase<C> ? (C extends Uppercase<C> ? false : true) : false;
23
- type PushUnderscore<S extends string> = S extends '' | `${string}_` ? S : `${S}_`;
24
- type TrimUnderscore<S extends string> = S extends `_${infer R}` ? TrimUnderscore<R> : S extends `${infer R}_` ? TrimUnderscore<R> : S;
25
- type CollapseUnderscores<S extends string> = S extends `${infer A}__${infer B}` ? CollapseUnderscores<`${A}_${B}`> : S;
26
- type ConstantCaseInternal<S extends string, Prev extends string = '', Out extends string = ''> = S extends `${infer C}${infer Rest}` ? C extends Separator ? ConstantCaseInternal<Rest, C, PushUnderscore<Out>> : IsUpperChar<C> extends true ? IsLowerChar<Prev> extends true ? ConstantCaseInternal<Rest, C, `${PushUnderscore<Out>}${Uppercase<C>}`> : ConstantCaseInternal<Rest, C, `${Out}${Uppercase<C>}`> : ConstantCaseInternal<Rest, C, `${Out}${Uppercase<C>}`> : CollapseUnderscores<TrimUnderscore<Out>>;
27
- type ConstantCase<S extends string> = string extends S ? string : ConstantCaseInternal<S>;
28
- type ArrayToObjectType<T extends readonly string[]> = {
29
- [K in T[number]]: EmptyEnumInputItem & {
30
- readonly value?: ConstantCase<K>;
31
- };
32
- };
33
- type NormalizedInputType<TInput> = TInput extends readonly string[] ? ArrayToObjectType<TInput> : TInput extends ObjectEnumInput ? TInput : never;
34
- type UnionKeys<T> = T extends T ? keyof T : never;
35
- type MergeUnionToObject<T> = {
36
- [K in UnionKeys<T>]: T extends Record<K, infer V> ? V : undefined;
37
- };
38
- type ExtraShapeUnion<TObj extends ObjectEnumInput> = {
39
- [K in keyof TObj]: Omit<TObj[K], BuiltInOverrideKeys>;
40
- }[keyof TObj];
41
- type InferredExtraFields<TObj extends ObjectEnumInput> = MergeUnionToObject<ExtraShapeUnion<TObj>>;
42
- type EnumItemFromNormalizedObject<TObj extends ObjectEnumInput, K extends keyof TObj = keyof TObj> = Omit<StandardEnumItem, 'key' | 'value'> & InferredExtraFields<TObj> & {
43
- readonly key: Extract<K, string>;
44
- readonly value: TObj[K] extends {
45
- value?: infer V;
46
- } ? Extract<V, string> extends never ? string : Extract<V, string> : string;
47
- };
48
- type EnumMemberUnionFromNormalizedObject<TObj extends ObjectEnumInput> = {
49
- [K in keyof TObj]: EnumItemFromNormalizedObject<TObj, K>;
50
- }[keyof TObj];
51
- type CoreEnumMethods<TItem extends StandardEnumItem> = {
52
- fromValue(value: string): TItem;
53
- tryFromValue(value?: string | null): TItem | undefined;
54
- fromKey(key: string): TItem;
55
- tryFromKey(key?: string | null): TItem | undefined;
56
- items(): readonly TItem[];
57
- values(): readonly string[];
58
- keys(): readonly string[];
59
- };
60
- type EnumFromNormalizedObject<TObj extends ObjectEnumInput> = {
61
- [K in keyof TObj]: EnumItemFromNormalizedObject<TObj, K>;
62
- } & CoreEnumMethods<EnumMemberUnionFromNormalizedObject<TObj>>;
63
- type EnumerationProps<TInput> = {
64
- input: TInput;
65
- propertyAutoFormatters?: PropertyAutoFormatter[];
66
- };
67
- type PropertyAutoFormatter = {
68
- key: string;
69
- format: (k: string) => string;
70
- };
71
- declare function enumeration<const TArr extends readonly string[]>(enumType: string, props: EnumerationProps<TArr>): EnumFromNormalizedObject<NormalizedInputType<TArr>>;
72
- declare function enumeration<const TObj extends ObjectEnumInput>(enumType: string, props: EnumerationProps<TObj>): EnumFromNormalizedObject<NormalizedInputType<TObj>>;
73
-
74
1
  /**
75
2
  * Logger interface for ts-smart-enum library
76
3
  *
@@ -145,10 +72,85 @@ type SmartEnumMappingsConfig = {
145
72
  logLevel?: LogLevel;
146
73
  logger?: Logger;
147
74
  };
75
+ type StandardEnumItemBase = {
76
+ readonly key: string;
77
+ readonly value: string;
78
+ readonly display: string;
79
+ readonly index: number;
80
+ readonly deprecated?: boolean;
81
+ };
82
+ type StandardEnumItem = StandardEnumItemBase & {
83
+ readonly __smart_enum_brand: true;
84
+ readonly __smart_enum_type: string;
85
+ readonly toPostgres: () => string;
86
+ };
148
87
  type Enumeration<TEnum> = TEnum extends Record<string, infer V> ? V extends {
149
88
  __smart_enum_brand: true;
150
89
  } ? V : never : never;
151
90
 
91
+ type BuiltInOverrideKeys = 'key' | 'value' | 'display' | 'deprecated' | 'index' | '__smart_enum_brand' | '__smart_enum_type';
92
+ type EnumInputItem = Partial<{
93
+ key: string;
94
+ value: string;
95
+ display: string;
96
+ deprecated: boolean;
97
+ }> & object;
98
+ type ObjectEnumInput = Record<string, EnumInputItem>;
99
+ type EmptyEnumInputItem = Record<never, never>;
100
+ type Separator = '-' | '_' | ' ' | '.';
101
+ type IsUpperChar<C extends string> = C extends Uppercase<C> ? (C extends Lowercase<C> ? false : true) : false;
102
+ type IsLowerChar<C extends string> = C extends Lowercase<C> ? (C extends Uppercase<C> ? false : true) : false;
103
+ type PushUnderscore<S extends string> = S extends '' | `${string}_` ? S : `${S}_`;
104
+ type TrimUnderscore<S extends string> = S extends `_${infer R}` ? TrimUnderscore<R> : S extends `${infer R}_` ? TrimUnderscore<R> : S;
105
+ type CollapseUnderscores<S extends string> = S extends `${infer A}__${infer B}` ? CollapseUnderscores<`${A}_${B}`> : S;
106
+ type ConstantCaseInternal<S extends string, Prev extends string = '', Out extends string = ''> = S extends `${infer C}${infer Rest}` ? C extends Separator ? ConstantCaseInternal<Rest, C, PushUnderscore<Out>> : IsUpperChar<C> extends true ? IsLowerChar<Prev> extends true ? ConstantCaseInternal<Rest, C, `${PushUnderscore<Out>}${Uppercase<C>}`> : ConstantCaseInternal<Rest, C, `${Out}${Uppercase<C>}`> : ConstantCaseInternal<Rest, C, `${Out}${Uppercase<C>}`> : CollapseUnderscores<TrimUnderscore<Out>>;
107
+ type ConstantCase<S extends string> = string extends S ? string : ConstantCaseInternal<S>;
108
+ type ArrayToObjectType<T extends readonly string[]> = {
109
+ [K in T[number]]: EmptyEnumInputItem & {
110
+ readonly value?: ConstantCase<K>;
111
+ };
112
+ };
113
+ type NormalizedInputType<TInput> = TInput extends readonly string[] ? ArrayToObjectType<TInput> : TInput extends ObjectEnumInput ? TInput : never;
114
+ type UnionKeys<T> = T extends T ? keyof T : never;
115
+ type MergeUnionToObject<T> = {
116
+ [K in UnionKeys<T>]: T extends Record<K, infer V> ? V : undefined;
117
+ };
118
+ type ExtraShapeUnion<TObj extends ObjectEnumInput> = {
119
+ [K in keyof TObj]: Omit<TObj[K], BuiltInOverrideKeys>;
120
+ }[keyof TObj];
121
+ type InferredExtraFields<TObj extends ObjectEnumInput> = MergeUnionToObject<ExtraShapeUnion<TObj>>;
122
+ type EnumItemFromNormalizedObject<TObj extends ObjectEnumInput, K extends keyof TObj = keyof TObj> = Omit<StandardEnumItem, 'key' | 'value'> & InferredExtraFields<TObj> & {
123
+ readonly key: Extract<K, string>;
124
+ readonly value: TObj[K] extends {
125
+ value?: infer V;
126
+ } ? Extract<V, string> extends never ? string : Extract<V, string> : string;
127
+ };
128
+ type EnumMemberUnionFromNormalizedObject<TObj extends ObjectEnumInput> = {
129
+ [K in keyof TObj]: EnumItemFromNormalizedObject<TObj, K>;
130
+ }[keyof TObj];
131
+ type CoreEnumMethods<TItem extends StandardEnumItem> = {
132
+ fromValue(value: string): TItem;
133
+ tryFromValue(value?: string | null): TItem | undefined;
134
+ fromKey(key: string): TItem;
135
+ tryFromKey(key?: string | null): TItem | undefined;
136
+ items(): readonly TItem[];
137
+ values(): readonly string[];
138
+ keys(): readonly string[];
139
+ };
140
+ type EnumFromNormalizedObject<TObj extends ObjectEnumInput> = {
141
+ [K in keyof TObj]: EnumItemFromNormalizedObject<TObj, K>;
142
+ } & CoreEnumMethods<EnumMemberUnionFromNormalizedObject<TObj>>;
143
+ type EnumerationProps<TInput> = {
144
+ input: TInput;
145
+ propertyAutoFormatters?: PropertyAutoFormatter[];
146
+ };
147
+ type PropertyAutoFormatter = {
148
+ key: string;
149
+ format: (k: string) => string;
150
+ };
151
+ declare function enumeration<const TArr extends readonly string[]>(enumType: string, props: EnumerationProps<TArr>): EnumFromNormalizedObject<NormalizedInputType<TArr>>;
152
+ declare function enumeration<const TObj extends ObjectEnumInput>(enumType: string, props: EnumerationProps<TObj>): EnumFromNormalizedObject<NormalizedInputType<TObj>>;
153
+
152
154
  /**
153
155
  * Runtime type guard to detect Smart Enum items created by this library.
154
156
  * Returns true if the value has the SMART_ENUM_ITEM symbol.
@@ -183,4 +185,4 @@ declare const isSmartEnumItem: (x: unknown) => x is {
183
185
  */
184
186
  declare const isSmartEnum: (x: unknown) => boolean;
185
187
 
186
- export { type AnyEnumLike as A, type DatabaseFormat as D, type Enumeration as E, type FieldEnumMapping as F, type Logger as L, type PathEnumMapping as P, type RevivedSmartEnums as R, type SmartApiHelperConfig as S, isSmartEnum as a, type LogLevel as b, type SmartEnumMappingsConfig as c, type SerializedSmartEnums as d, enumeration as e, type SmartEnumItemSerialized as f, type SmartEnumLike as g, type ReviveRowOptions as h, isSmartEnumItem as i, type RevivePayloadOptions as j };
188
+ export { type AnyEnumLike as A, type DatabaseFormat as D, type Enumeration as E, type FieldEnumMapping as F, type Logger as L, type PathEnumMapping as P, type RevivedSmartEnums as R, type SmartApiHelperConfig as S, isSmartEnum as a, type LogLevel as b, type SmartEnumMappingsConfig as c, type SerializedSmartEnums as d, enumeration as e, type SmartEnumItemSerialized as f, type SmartEnumLike as g, type ReviveRowOptions as h, isSmartEnumItem as i, type RevivePayloadOptions as j, type StandardEnumItemBase as k };
package/dist/core.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/core.ts","../src/enumerations.ts","../src/extensionMethods.ts","../src/types.ts","../src/utilities/typeGuards.ts"],"sourcesContent":["/**\n * Core Smart Enums functionality\n *\n * This is the minimal entry point containing only the essential enumeration\n * functionality. Import this for tree-shaking when you only need basic enums.\n *\n * @example\n * ```typescript\n * import { enumeration, isSmartEnumItem, isSmartEnum } from 'ts-smart-enum/core';\n * ```\n */\n\nexport { enumeration } from './enumerations.js';\nexport { isSmartEnumItem, isSmartEnum } from './utilities/typeGuards.js';\nexport type { Enumeration, AnyEnumLike } from './types.js';\n","import { capitalCase, constantCase } from 'case-anything';\n\nimport { addExtensionMethods } from './extensionMethods.js';\nimport { SMART_ENUM, SMART_ENUM_ID, SMART_ENUM_ITEM } from './types.js';\n\ntype BuiltInOverrideKeys =\n | 'key'\n | 'value'\n | 'display'\n | 'deprecated'\n | 'index'\n | '__smart_enum_brand'\n | '__smart_enum_type';\n\nexport type StandardEnumItem = {\n readonly __smart_enum_brand: true;\n readonly __smart_enum_type: string;\n readonly key: string;\n readonly value: string;\n readonly display: string;\n readonly index: number;\n readonly deprecated?: boolean;\n readonly toPostgres: () => string;\n};\n\nexport type EnumInputItem = Partial<{\n key: string;\n value: string;\n display: string;\n deprecated: boolean;\n}> &\n object;\n\nexport type ObjectEnumInput = Record<string, EnumInputItem>;\n\ntype EmptyEnumInputItem = Record<never, never>;\n\ntype Separator = '-' | '_' | ' ' | '.';\n\ntype IsUpperChar<C extends string> =\n C extends Uppercase<C> ? (C extends Lowercase<C> ? false : true) : false;\n\ntype IsLowerChar<C extends string> =\n C extends Lowercase<C> ? (C extends Uppercase<C> ? false : true) : false;\n\ntype PushUnderscore<S extends string> = S extends '' | `${string}_`\n ? S\n : `${S}_`;\n\ntype TrimUnderscore<S extends string> = S extends `_${infer R}`\n ? TrimUnderscore<R>\n : S extends `${infer R}_`\n ? TrimUnderscore<R>\n : S;\n\ntype CollapseUnderscores<S extends string> = S extends `${infer A}__${infer B}`\n ? CollapseUnderscores<`${A}_${B}`>\n : S;\n\ntype ConstantCaseInternal<\n S extends string,\n Prev extends string = '',\n Out extends string = '',\n> = S extends `${infer C}${infer Rest}`\n ? C extends Separator\n ? ConstantCaseInternal<Rest, C, PushUnderscore<Out>>\n : IsUpperChar<C> extends true\n ? IsLowerChar<Prev> extends true\n ? ConstantCaseInternal<Rest, C, `${PushUnderscore<Out>}${Uppercase<C>}`>\n : ConstantCaseInternal<Rest, C, `${Out}${Uppercase<C>}`>\n : ConstantCaseInternal<Rest, C, `${Out}${Uppercase<C>}`>\n : CollapseUnderscores<TrimUnderscore<Out>>;\n\ntype ConstantCase<S extends string> = string extends S\n ? string\n : ConstantCaseInternal<S>;\n\ntype ArrayToObjectType<T extends readonly string[]> = {\n [K in T[number]]: EmptyEnumInputItem & { readonly value?: ConstantCase<K> };\n};\n\ntype NormalizedInputType<TInput> = TInput extends readonly string[]\n ? ArrayToObjectType<TInput>\n : TInput extends ObjectEnumInput\n ? TInput\n : never;\n\ntype UnionKeys<T> = T extends T ? keyof T : never;\n\ntype MergeUnionToObject<T> = {\n [K in UnionKeys<T>]: T extends Record<K, infer V> ? V : undefined;\n};\n\ntype ExtraShapeUnion<TObj extends ObjectEnumInput> = {\n [K in keyof TObj]: Omit<TObj[K], BuiltInOverrideKeys>;\n}[keyof TObj];\n\nexport type InferredExtraFields<TObj extends ObjectEnumInput> =\n MergeUnionToObject<ExtraShapeUnion<TObj>>;\n\nexport type EnumItemFromNormalizedObject<\n TObj extends ObjectEnumInput,\n K extends keyof TObj = keyof TObj,\n> = Omit<StandardEnumItem, 'key' | 'value'> &\n InferredExtraFields<TObj> & {\n readonly key: Extract<K, string>;\n readonly value: TObj[K] extends { value?: infer V }\n ? Extract<V, string> extends never\n ? string\n : Extract<V, string>\n : string;\n };\n\nexport type EnumMemberUnionFromNormalizedObject<TObj extends ObjectEnumInput> =\n {\n [K in keyof TObj]: EnumItemFromNormalizedObject<TObj, K>;\n }[keyof TObj];\n\nexport type CoreEnumMethods<TItem extends StandardEnumItem> = {\n fromValue(value: string): TItem;\n tryFromValue(value?: string | null): TItem | undefined;\n fromKey(key: string): TItem;\n tryFromKey(key?: string | null): TItem | undefined;\n items(): readonly TItem[];\n values(): readonly string[];\n keys(): readonly string[];\n};\n\nexport type EnumFromNormalizedObject<TObj extends ObjectEnumInput> = {\n [K in keyof TObj]: EnumItemFromNormalizedObject<TObj, K>;\n} & CoreEnumMethods<EnumMemberUnionFromNormalizedObject<TObj>>;\n\nexport type EnumerationProps<TInput> = {\n input: TInput;\n propertyAutoFormatters?: PropertyAutoFormatter[];\n};\n\nexport type EnumItem<TEnum> = {\n [K in keyof TEnum]: TEnum[K] extends { __smart_enum_brand: true }\n ? TEnum[K]\n : never;\n}[keyof TEnum];\n\nexport type PropertyAutoFormatter = {\n key: string;\n format: (k: string) => string;\n};\n\ntype FinalizedEnumFields = Pick<\n StandardEnumItem,\n '__smart_enum_brand' | '__smart_enum_type'\n>;\n\ntype FinalizableEnumItem = {\n key: string;\n value: string;\n display: string;\n index: number;\n deprecated?: boolean;\n};\n\nfunction normalizeInput<TInput extends readonly string[] | ObjectEnumInput>(\n input: TInput,\n): NormalizedInputType<TInput> {\n if (Array.isArray(input)) {\n return Object.fromEntries(\n input.map(k => [k, {}]),\n ) as NormalizedInputType<TInput>;\n }\n\n return input as NormalizedInputType<TInput>;\n}\n\nconst finalizeEnumItem = <T extends { value: string }>(\n item: T,\n enumType: string,\n enumInstanceId: symbol,\n): T & FinalizedEnumFields => {\n Object.defineProperty(item, SMART_ENUM_ITEM, {\n value: true,\n enumerable: false,\n });\n\n Object.defineProperty(item, SMART_ENUM_ID, {\n value: enumInstanceId,\n enumerable: false,\n });\n\n Object.defineProperty(item, '__smart_enum_brand', {\n value: true,\n enumerable: false,\n });\n\n Object.defineProperty(item, '__smart_enum_type', {\n value: enumType,\n enumerable: false,\n });\n\n Object.defineProperty(item, 'toJSON', {\n value: () => ({ __smart_enum_type: enumType, value: item.value }),\n enumerable: false,\n });\n\n Object.defineProperty(item, 'toPostgres', {\n value: () => item.value,\n enumerable: false,\n });\n\n return item as T & FinalizedEnumFields;\n};\n\nconst formatProperties = (\n k: string,\n formatters: PropertyAutoFormatter[],\n): { value: string; display: string } & Record<string, string> =>\n formatters.reduce(\n (acc, formatter) => {\n acc[formatter.key] = formatter.format(k);\n return acc;\n },\n {\n value: constantCase(k),\n display: capitalCase(k),\n } as { value: string; display: string } & Record<string, string>,\n );\n\nfunction buildEnumFromObject<TObj extends ObjectEnumInput>(\n enumType: string,\n input: TObj,\n propertyAutoFormatters?: PropertyAutoFormatter[],\n): EnumFromNormalizedObject<TObj> {\n const formattersWithDefaults: PropertyAutoFormatter[] = [\n { key: 'value', format: constantCase },\n { key: 'display', format: capitalCase },\n ...(propertyAutoFormatters ?? []),\n ];\n\n type TItem = EnumItemFromNormalizedObject<TObj>;\n\n const rawEnumItems = {} as {\n [K in keyof TObj]: EnumItemFromNormalizedObject<TObj, K>;\n };\n\n const enumInstanceId = Symbol('smart-enum-instance');\n let index = 0;\n\n for (const key in input) {\n if (Object.prototype.hasOwnProperty.call(input, key)) {\n const typedKey = key;\n const value = input[typedKey];\n\n const enumItemBase = {\n index,\n key: typedKey,\n ...formatProperties(typedKey, formattersWithDefaults),\n ...value,\n } as FinalizableEnumItem & TObj[typeof typedKey];\n\n const enumItem = finalizeEnumItem(\n enumItemBase,\n enumType,\n enumInstanceId,\n ) as unknown as TItem;\n\n Object.freeze(enumItem);\n rawEnumItems[typedKey] = enumItem;\n index++;\n }\n }\n\n const extensionMethods = addExtensionMethods<TItem>(\n Object.values(rawEnumItems) as TItem[],\n );\n\n const enumObject = {\n ...rawEnumItems,\n ...extensionMethods,\n } as EnumFromNormalizedObject<TObj>;\n\n Object.defineProperty(enumObject, SMART_ENUM, {\n value: true,\n enumerable: false,\n });\n\n Object.freeze(enumObject);\n\n return enumObject;\n}\n\nexport function enumeration<const TArr extends readonly string[]>(\n enumType: string,\n props: EnumerationProps<TArr>,\n): EnumFromNormalizedObject<NormalizedInputType<TArr>>;\n\nexport function enumeration<const TObj extends ObjectEnumInput>(\n enumType: string,\n props: EnumerationProps<TObj>,\n): EnumFromNormalizedObject<NormalizedInputType<TObj>>;\n\nexport function enumeration(\n enumType: string,\n props: EnumerationProps<readonly string[] | ObjectEnumInput>,\n) {\n const normalized = normalizeInput(props.input);\n return buildEnumFromObject(\n enumType,\n normalized,\n props.propertyAutoFormatters,\n );\n}\n","import type { CoreEnumMethods, StandardEnumItem } from './types.js';\nexport const addExtensionMethods = <TItem extends StandardEnumItem>(\n enumItems: readonly TItem[],\n): CoreEnumMethods<TItem> => {\n const findBy = <K extends keyof TItem>(field: K, target: TItem[K]) =>\n enumItems.find(item => item[field] === target);\n\n const requireBy = <K extends keyof TItem>(\n field: K,\n target: TItem[K],\n label: string,\n ) => {\n const item = findBy(field, target);\n if (!item) {\n throw new Error(`No enum ${label} found for '${String(target)}'`);\n }\n return item;\n };\n\n return {\n fromValue: value => requireBy('value', value as TItem['value'], 'value'),\n tryFromValue: value =>\n value ? findBy('value', value as TItem['value']) : undefined,\n\n fromKey: key => requireBy('key', key as TItem['key'], 'key'),\n tryFromKey: key => (key ? findBy('key', key as TItem['key']) : undefined),\n\n items: () => [...enumItems],\n values: () => enumItems.map(item => item.value),\n keys: () => enumItems.map(item => item.key),\n };\n};\n","/**\n * Type guard to check if a value is not null or undefined\n * @param value - The value to check\n * @returns True if the value is defined and not null\n */\nexport const notEmpty = <X>(\n value: X | null | undefined,\n): value is NonNullable<X> => {\n // eslint-disable-next-line unicorn/no-null\n return value != null;\n};\n\n// Public symbols used at runtime for detection/identity (not used in type keys)\nexport const SMART_ENUM_ITEM = Symbol('smart-enum-item');\nexport const SMART_ENUM_ID = Symbol('smart-enum-id');\nexport const SMART_ENUM = Symbol('smart-enum');\n\n/**\n * Options for filtering enum items in various methods\n */\nexport type EnumFilterOptions = {\n /** Include items with null/undefined values (default: false) */\n showEmpty?: boolean;\n /** Include deprecated items (default: false) */\n showDeprecated?: boolean;\n};\n\n// export type EnumInput = readonly string[] | ObjectEnumInput;\n\n/**\n * Compile-time transformer: replaces Smart Enum items with string values,\n * recursively over arrays and objects. Structural detection checks for\n * presence of `key` and `value`.\n */\nexport type SerializedSmartEnums<T> = T extends { value: string; key: unknown }\n ? string\n : T extends ReadonlyArray<infer U>\n ? ReadonlyArray<SerializedSmartEnums<U>>\n : T extends Array<infer U>\n ? SerializedSmartEnums<U>[]\n : T extends object\n ? { [K in keyof T]: SerializedSmartEnums<T[K]> }\n : T;\n\n/**\n * Revived shape: for keys present in mapping M, the string becomes the\n * corresponding enum item type (derived from the provided enum object);\n * other fields recurse.\n */\nexport type EnumItemFromEnum<TEnum> =\n TEnum extends Record<string, infer V>\n ? V extends { __smart_enum_brand: true }\n ? V\n : never\n : never;\n\n// Structural constraint for enum objects passed to reviveSmartEnums mapping\nexport type AnyEnumLike = {\n tryFromValue: (value?: string | null) => unknown;\n tryFromKey: (key?: string | null) => unknown;\n} & Record<string, unknown>;\n\nexport type SmartEnumLike<T = unknown> = {\n tryFromValue: (value: string) => T | undefined;\n};\n\nexport type FieldEnumMapping = Record<string, SmartEnumLike>;\n\nexport type ReviveRowOptions = {\n fieldEnumMapping: FieldEnumMapping;\n strict?: boolean;\n};\n\nexport type PathEnumMapping = Record<string, SmartEnumLike>;\n\nexport type RevivePayloadOptions = {\n pathEnumMapping: PathEnumMapping;\n strict?: boolean;\n};\n\nexport type RevivedSmartEnums<T, M extends Record<string, AnyEnumLike>> =\n T extends ReadonlyArray<infer U>\n ? RevivedSmartEnums<U, M>[]\n : T extends Array<infer U>\n ? RevivedSmartEnums<U, M>[]\n : T extends object\n ? {\n [K in keyof T]: K extends Extract<keyof M, string>\n ? Enumeration<M[K]>\n : RevivedSmartEnums<T[K], M>;\n }\n : T;\n\nexport type SmartEnumItemSerialized = {\n __smart_enum_type: string;\n value: string;\n};\n\n/** Example shape for transport tests (`reviveAfterTransport` registry). */\nexport type SmartApiHelperConfig = {\n enumRegistry: Record<string, AnyEnumLike>;\n};\n\n/**\n * Compile-time transformer: replaces Smart Enum items with string values,\n * recursively over arrays and objects. Used for database storage.\n */\nexport type DatabaseFormat<T> = T extends { value: string; key: unknown }\n ? string\n : T extends ReadonlyArray<infer U>\n ? ReadonlyArray<DatabaseFormat<U>>\n : T extends Array<infer U>\n ? DatabaseFormat<U>[]\n : T extends object\n ? { [K in keyof T]: DatabaseFormat<T[K]> }\n : T;\n\n/**\n * Log levels for smart enum mappings\n */\nexport type LogLevel = 'debug' | 'info' | 'warn' | 'error';\n\n/**\n * Configuration for smart enum mappings initialization\n */\nexport type SmartEnumMappingsConfig = {\n enumRegistry: Record<string, AnyEnumLike>;\n logLevel?: LogLevel;\n logger?: import('./utilities/logger.js').Logger;\n};\n\ntype BuiltInOverrideKeys =\n | 'key'\n | 'value'\n | 'display'\n | 'deprecated'\n | 'index'\n | '__smart_enum_brand'\n | '__smart_enum_type';\n\nexport type StandardEnumItem = {\n readonly __smart_enum_brand: true;\n readonly __smart_enum_type: string;\n readonly key: string;\n readonly value: string;\n readonly display: string;\n readonly index: number;\n readonly deprecated?: boolean;\n readonly toPostgres: () => string;\n};\nexport type EnumInputItem = Partial<{\n key: string;\n value: string;\n display: string;\n deprecated: boolean;\n}> &\n Record<string, unknown>;\n\nexport type ObjectEnumInput = Record<string, EnumInputItem>;\n\nexport type EmptyEnumInputItem = Record<never, never>;\n\nexport type ArrayToObjectType<T extends readonly string[]> = {\n [K in T[number]]: EmptyEnumInputItem;\n};\n\nexport type NormalizedInputType<TInput> = TInput extends readonly string[]\n ? ArrayToObjectType<TInput>\n : TInput extends ObjectEnumInput\n ? TInput\n : never;\n\nexport type EnumItemFromNormalizedObject<\n TObj extends ObjectEnumInput,\n K extends keyof TObj = keyof TObj,\n> = Omit<StandardEnumItem, 'key'> &\n InferredExtraFields<TObj> & {\n readonly key: Extract<K, string>;\n };\n\nexport type EnumMemberUnionFromNormalizedObject<TObj extends ObjectEnumInput> =\n {\n [K in keyof TObj]: EnumItemFromNormalizedObject<TObj, K>;\n }[keyof TObj];\n\nexport type EnumFromNormalizedObject<TObj extends ObjectEnumInput> = {\n [K in keyof TObj]: EnumItemFromNormalizedObject<TObj, K>;\n} & CoreEnumMethods<EnumMemberUnionFromNormalizedObject<TObj>>;\n\nexport type UnionKeys<T> = T extends T ? keyof T : never;\n\n/*\nTo widen the type of the OptionalObject from literals to their actual types,\ne.g.\nshape?: \"round\" | \"square\" | \"long\";\nslices?: true;\nlayers?: 2;\n--turns into:--\nshape: string | undefined;\nslices: boolean| undefined;\nlayers: number| undefined;\n\nwe can use the following code:\ntype Widen<T> = T extends string\n ? string\n : T extends number\n ? number\n : T extends boolean\n ? boolean\n : T;\n\ntype MergeUnionToObject<T> = {\n [K in UnionKeys<T>]: T extends Record<K, infer V> ? Widen<V> : undefined;\n};\n*/\n\ntype MergeUnionToObject<T> = {\n [K in UnionKeys<T>]: T extends Record<K, infer V> ? V : undefined;\n};\n\ntype ExtraShapeUnion<TObj extends ObjectEnumInput> = {\n [K in keyof TObj]: Omit<TObj[K], BuiltInOverrideKeys>;\n}[keyof TObj];\n\nexport type InferredExtraFields<TObj extends ObjectEnumInput> =\n MergeUnionToObject<ExtraShapeUnion<TObj>>;\n\nexport type PropertyAutoFormatter = {\n /** The property name to generate */\n key: string;\n /** Function to transform the key into the property value */\n format: (k: string) => string;\n};\n\nexport type CoreEnumMethods<TItem extends StandardEnumItem> = {\n fromValue(value: string): TItem;\n tryFromValue(value?: string | null): TItem | undefined;\n fromKey(key: string): TItem;\n tryFromKey(key?: string | null): TItem | undefined;\n items(): readonly TItem[];\n values(): readonly string[];\n keys(): readonly string[];\n};\n\nexport type EnumerationProps<TInput> = {\n input: TInput;\n propertyAutoFormatters?: PropertyAutoFormatter[];\n};\n\nexport type Enumeration<TEnum> =\n TEnum extends Record<string, infer V>\n ? V extends { __smart_enum_brand: true }\n ? V\n : never\n : never;\n\nexport type FinalizedEnumFields = Pick<\n StandardEnumItem,\n '__smart_enum_brand' | '__smart_enum_type'\n>;\nexport type FinalizableEnumItem = {\n key: string;\n value: string;\n display: string;\n index: number;\n deprecated?: boolean;\n};\n","import {\n SMART_ENUM_ITEM,\n SMART_ENUM,\n SmartEnumItemSerialized,\n} from '../types.js';\n\n/**\n * Runtime type guard to detect Smart Enum items created by this library.\n * Returns true if the value has the SMART_ENUM_ITEM symbol.\n *\n * @example\n * ```typescript\n * import { Status } from './status';\n * const item = Status.active;\n * isSmartEnumItem(item); // true\n * isSmartEnumItem({ key: 'active', value: 'ACTIVE' }); // false (plain object)\n * if (isSmartEnumItem(x)) {\n * console.log(x.value, x.__smart_enum_type); // narrowed to enum item\n * }\n * ```\n */\nexport const isSmartEnumItem = (\n x: unknown,\n): x is {\n key: string;\n value: string;\n index?: number;\n __smart_enum_type?: string;\n} => {\n return (\n !!x && typeof x === 'object' && Reflect.get(x, SMART_ENUM_ITEM) === true\n );\n};\n\n/**\n * Runtime type guard to detect a full Smart Enum object created by this library.\n * Returns true if the object has the SMART_ENUM property.\n *\n * @example\n * ```typescript\n * import { MyEnum } from './blah';\n * isSmartEnum(MyEnum) === true; // true\n * isSmartEnum(MyEnum.one) === false; // false (this is an item, not the enum)\n * ```\n */\nexport const isSmartEnum = (x: unknown): boolean => {\n return !!x && typeof x === 'object' && Reflect.get(x, SMART_ENUM) === true;\n};\n\n/**\n * Runtime type guard to detect a serialized Smart Enum item created by this library.\n * Returns true if the value has `__smart_enum_type` and `value` (wire/database shape).\n *\n * @example\n * ```typescript\n * const wire = { __smart_enum_type: 'Status', value: 'ACTIVE' };\n * isSerializedSmartEnumItem(wire); // true\n * isSerializedSmartEnumItem(Status.active); // false (live enum item)\n * if (isSerializedSmartEnumItem(x)) {\n * reviveItem(x.__smart_enum_type, x.value); // narrowed to SmartEnumItemSerialized\n * }\n * ```\n */\nexport const isSerializedSmartEnumItem = (\n x: unknown,\n): x is SmartEnumItemSerialized => {\n return (\n !!x &&\n typeof x === 'object' &&\n Reflect.has(x, '__smart_enum_type') &&\n Reflect.has(x, 'value')\n );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,2BAA0C;;;ACCnC,IAAM,sBAAsB,CACjC,cAC2B;AAC3B,QAAM,SAAS,CAAwB,OAAU,WAC/C,UAAU,KAAK,UAAQ,KAAK,KAAK,MAAM,MAAM;AAE/C,QAAM,YAAY,CAChB,OACA,QACA,UACG;AACH,UAAM,OAAO,OAAO,OAAO,MAAM;AACjC,QAAI,CAAC,MAAM;AACT,YAAM,IAAI,MAAM,WAAW,KAAK,eAAe,OAAO,MAAM,CAAC,GAAG;AAAA,IAClE;AACA,WAAO;AAAA,EACT;AAEA,SAAO;AAAA,IACL,WAAW,WAAS,UAAU,SAAS,OAAyB,OAAO;AAAA,IACvE,cAAc,WACZ,QAAQ,OAAO,SAAS,KAAuB,IAAI;AAAA,IAErD,SAAS,SAAO,UAAU,OAAO,KAAqB,KAAK;AAAA,IAC3D,YAAY,SAAQ,MAAM,OAAO,OAAO,GAAmB,IAAI;AAAA,IAE/D,OAAO,MAAM,CAAC,GAAG,SAAS;AAAA,IAC1B,QAAQ,MAAM,UAAU,IAAI,UAAQ,KAAK,KAAK;AAAA,IAC9C,MAAM,MAAM,UAAU,IAAI,UAAQ,KAAK,GAA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src/enumerations.ts","../src/extensionMethods.ts","../src/types.ts","../src/utilities/typeGuards.ts"],"sourcesContent":["/**\n * Core Smart Enums functionality\n *\n * This is the minimal entry point containing only the essential enumeration\n * functionality. Import this for tree-shaking when you only need basic enums.\n *\n * @example\n * ```typescript\n * import { enumeration, isSmartEnumItem, isSmartEnum } from 'ts-smart-enum/core';\n * ```\n */\n\nexport { enumeration } from './enumerations.js';\nexport { isSmartEnumItem, isSmartEnum } from './utilities/typeGuards.js';\nexport type {\n Enumeration,\n AnyEnumLike,\n StandardEnumItemBase as StandardEnumItem,\n} from './types.js';\n","import { capitalCase, constantCase } from 'case-anything';\n\nimport { addExtensionMethods } from './extensionMethods.js';\nimport {\n SMART_ENUM,\n SMART_ENUM_ID,\n SMART_ENUM_ITEM,\n StandardEnumItem,\n} from './types.js';\n\ntype BuiltInOverrideKeys =\n | 'key'\n | 'value'\n | 'display'\n | 'deprecated'\n | 'index'\n | '__smart_enum_brand'\n | '__smart_enum_type';\n\nexport type EnumInputItem = Partial<{\n key: string;\n value: string;\n display: string;\n deprecated: boolean;\n}> &\n object;\n\nexport type ObjectEnumInput = Record<string, EnumInputItem>;\n\ntype EmptyEnumInputItem = Record<never, never>;\n\ntype Separator = '-' | '_' | ' ' | '.';\n\ntype IsUpperChar<C extends string> =\n C extends Uppercase<C> ? (C extends Lowercase<C> ? false : true) : false;\n\ntype IsLowerChar<C extends string> =\n C extends Lowercase<C> ? (C extends Uppercase<C> ? false : true) : false;\n\ntype PushUnderscore<S extends string> = S extends '' | `${string}_`\n ? S\n : `${S}_`;\n\ntype TrimUnderscore<S extends string> = S extends `_${infer R}`\n ? TrimUnderscore<R>\n : S extends `${infer R}_`\n ? TrimUnderscore<R>\n : S;\n\ntype CollapseUnderscores<S extends string> = S extends `${infer A}__${infer B}`\n ? CollapseUnderscores<`${A}_${B}`>\n : S;\n\ntype ConstantCaseInternal<\n S extends string,\n Prev extends string = '',\n Out extends string = '',\n> = S extends `${infer C}${infer Rest}`\n ? C extends Separator\n ? ConstantCaseInternal<Rest, C, PushUnderscore<Out>>\n : IsUpperChar<C> extends true\n ? IsLowerChar<Prev> extends true\n ? ConstantCaseInternal<Rest, C, `${PushUnderscore<Out>}${Uppercase<C>}`>\n : ConstantCaseInternal<Rest, C, `${Out}${Uppercase<C>}`>\n : ConstantCaseInternal<Rest, C, `${Out}${Uppercase<C>}`>\n : CollapseUnderscores<TrimUnderscore<Out>>;\n\ntype ConstantCase<S extends string> = string extends S\n ? string\n : ConstantCaseInternal<S>;\n\ntype ArrayToObjectType<T extends readonly string[]> = {\n [K in T[number]]: EmptyEnumInputItem & { readonly value?: ConstantCase<K> };\n};\n\ntype NormalizedInputType<TInput> = TInput extends readonly string[]\n ? ArrayToObjectType<TInput>\n : TInput extends ObjectEnumInput\n ? TInput\n : never;\n\ntype UnionKeys<T> = T extends T ? keyof T : never;\n\ntype MergeUnionToObject<T> = {\n [K in UnionKeys<T>]: T extends Record<K, infer V> ? V : undefined;\n};\n\ntype ExtraShapeUnion<TObj extends ObjectEnumInput> = {\n [K in keyof TObj]: Omit<TObj[K], BuiltInOverrideKeys>;\n}[keyof TObj];\n\nexport type InferredExtraFields<TObj extends ObjectEnumInput> =\n MergeUnionToObject<ExtraShapeUnion<TObj>>;\n\nexport type EnumItemFromNormalizedObject<\n TObj extends ObjectEnumInput,\n K extends keyof TObj = keyof TObj,\n> = Omit<StandardEnumItem, 'key' | 'value'> &\n InferredExtraFields<TObj> & {\n readonly key: Extract<K, string>;\n readonly value: TObj[K] extends { value?: infer V }\n ? Extract<V, string> extends never\n ? string\n : Extract<V, string>\n : string;\n };\n\nexport type EnumMemberUnionFromNormalizedObject<TObj extends ObjectEnumInput> =\n {\n [K in keyof TObj]: EnumItemFromNormalizedObject<TObj, K>;\n }[keyof TObj];\n\nexport type CoreEnumMethods<TItem extends StandardEnumItem> = {\n fromValue(value: string): TItem;\n tryFromValue(value?: string | null): TItem | undefined;\n fromKey(key: string): TItem;\n tryFromKey(key?: string | null): TItem | undefined;\n items(): readonly TItem[];\n values(): readonly string[];\n keys(): readonly string[];\n};\n\nexport type EnumFromNormalizedObject<TObj extends ObjectEnumInput> = {\n [K in keyof TObj]: EnumItemFromNormalizedObject<TObj, K>;\n} & CoreEnumMethods<EnumMemberUnionFromNormalizedObject<TObj>>;\n\nexport type EnumerationProps<TInput> = {\n input: TInput;\n propertyAutoFormatters?: PropertyAutoFormatter[];\n};\n\nexport type EnumItem<TEnum> = {\n [K in keyof TEnum]: TEnum[K] extends { __smart_enum_brand: true }\n ? TEnum[K]\n : never;\n}[keyof TEnum];\n\nexport type PropertyAutoFormatter = {\n key: string;\n format: (k: string) => string;\n};\n\ntype FinalizedEnumFields = Pick<\n StandardEnumItem,\n '__smart_enum_brand' | '__smart_enum_type'\n>;\n\ntype FinalizableEnumItem = {\n key: string;\n value: string;\n display: string;\n index: number;\n deprecated?: boolean;\n};\n\nfunction normalizeInput<TInput extends readonly string[] | ObjectEnumInput>(\n input: TInput,\n): NormalizedInputType<TInput> {\n if (Array.isArray(input)) {\n return Object.fromEntries(\n input.map(k => [k, {}]),\n ) as NormalizedInputType<TInput>;\n }\n\n return input as NormalizedInputType<TInput>;\n}\n\nconst finalizeEnumItem = <T extends { value: string }>(\n item: T,\n enumType: string,\n enumInstanceId: symbol,\n): T & FinalizedEnumFields => {\n Object.defineProperty(item, SMART_ENUM_ITEM, {\n value: true,\n enumerable: false,\n });\n\n Object.defineProperty(item, SMART_ENUM_ID, {\n value: enumInstanceId,\n enumerable: false,\n });\n\n Object.defineProperty(item, '__smart_enum_brand', {\n value: true,\n enumerable: false,\n });\n\n Object.defineProperty(item, '__smart_enum_type', {\n value: enumType,\n enumerable: false,\n });\n\n Object.defineProperty(item, 'toJSON', {\n value: () => ({ __smart_enum_type: enumType, value: item.value }),\n enumerable: false,\n });\n\n Object.defineProperty(item, 'toPostgres', {\n value: () => item.value,\n enumerable: false,\n });\n\n return item as T & FinalizedEnumFields;\n};\n\nconst formatProperties = (\n k: string,\n formatters: PropertyAutoFormatter[],\n): { value: string; display: string } & Record<string, string> =>\n formatters.reduce(\n (acc, formatter) => {\n acc[formatter.key] = formatter.format(k);\n return acc;\n },\n {\n value: constantCase(k),\n display: capitalCase(k),\n } as { value: string; display: string } & Record<string, string>,\n );\n\nfunction buildEnumFromObject<TObj extends ObjectEnumInput>(\n enumType: string,\n input: TObj,\n propertyAutoFormatters?: PropertyAutoFormatter[],\n): EnumFromNormalizedObject<TObj> {\n const formattersWithDefaults: PropertyAutoFormatter[] = [\n { key: 'value', format: constantCase },\n { key: 'display', format: capitalCase },\n ...(propertyAutoFormatters ?? []),\n ];\n\n type TItem = EnumItemFromNormalizedObject<TObj>;\n\n const rawEnumItems = {} as {\n [K in keyof TObj]: EnumItemFromNormalizedObject<TObj, K>;\n };\n\n const enumInstanceId = Symbol('smart-enum-instance');\n let index = 0;\n\n for (const key in input) {\n if (Object.prototype.hasOwnProperty.call(input, key)) {\n const typedKey = key;\n const value = input[typedKey];\n\n const enumItemBase = {\n index,\n key: typedKey,\n ...formatProperties(typedKey, formattersWithDefaults),\n ...value,\n } as FinalizableEnumItem & TObj[typeof typedKey];\n\n const enumItem = finalizeEnumItem(\n enumItemBase,\n enumType,\n enumInstanceId,\n ) as unknown as TItem;\n\n Object.freeze(enumItem);\n rawEnumItems[typedKey] = enumItem;\n index++;\n }\n }\n\n const extensionMethods = addExtensionMethods<TItem>(\n Object.values(rawEnumItems) as TItem[],\n );\n\n const enumObject = {\n ...rawEnumItems,\n ...extensionMethods,\n } as EnumFromNormalizedObject<TObj>;\n\n Object.defineProperty(enumObject, SMART_ENUM, {\n value: true,\n enumerable: false,\n });\n\n Object.freeze(enumObject);\n\n return enumObject;\n}\n\nexport function enumeration<const TArr extends readonly string[]>(\n enumType: string,\n props: EnumerationProps<TArr>,\n): EnumFromNormalizedObject<NormalizedInputType<TArr>>;\n\nexport function enumeration<const TObj extends ObjectEnumInput>(\n enumType: string,\n props: EnumerationProps<TObj>,\n): EnumFromNormalizedObject<NormalizedInputType<TObj>>;\n\nexport function enumeration(\n enumType: string,\n props: EnumerationProps<readonly string[] | ObjectEnumInput>,\n) {\n const normalized = normalizeInput(props.input);\n return buildEnumFromObject(\n enumType,\n normalized,\n props.propertyAutoFormatters,\n );\n}\n","import type { CoreEnumMethods, StandardEnumItem } from './types.js';\nexport const addExtensionMethods = <TItem extends StandardEnumItem>(\n enumItems: readonly TItem[],\n): CoreEnumMethods<TItem> => {\n const findBy = <K extends keyof TItem>(field: K, target: TItem[K]) =>\n enumItems.find(item => item[field] === target);\n\n const requireBy = <K extends keyof TItem>(\n field: K,\n target: TItem[K],\n label: string,\n ) => {\n const item = findBy(field, target);\n if (!item) {\n throw new Error(`No enum ${label} found for '${String(target)}'`);\n }\n return item;\n };\n\n return {\n fromValue: value => requireBy('value', value as TItem['value'], 'value'),\n tryFromValue: value =>\n value ? findBy('value', value as TItem['value']) : undefined,\n\n fromKey: key => requireBy('key', key as TItem['key'], 'key'),\n tryFromKey: key => (key ? findBy('key', key as TItem['key']) : undefined),\n\n items: () => [...enumItems],\n values: () => enumItems.map(item => item.value),\n keys: () => enumItems.map(item => item.key),\n };\n};\n","/**\n * Type guard to check if a value is not null or undefined\n * @param value - The value to check\n * @returns True if the value is defined and not null\n */\nexport const notEmpty = <X>(\n value: X | null | undefined,\n): value is NonNullable<X> => {\n // eslint-disable-next-line unicorn/no-null\n return value != null;\n};\n\n// Public symbols used at runtime for detection/identity (not used in type keys)\nexport const SMART_ENUM_ITEM = Symbol('smart-enum-item');\nexport const SMART_ENUM_ID = Symbol('smart-enum-id');\nexport const SMART_ENUM = Symbol('smart-enum');\n\n/**\n * Options for filtering enum items in various methods\n */\nexport type EnumFilterOptions = {\n /** Include items with null/undefined values (default: false) */\n showEmpty?: boolean;\n /** Include deprecated items (default: false) */\n showDeprecated?: boolean;\n};\n\n// export type EnumInput = readonly string[] | ObjectEnumInput;\n\n/**\n * Compile-time transformer: replaces Smart Enum items with string values,\n * recursively over arrays and objects. Structural detection checks for\n * presence of `key` and `value`.\n */\nexport type SerializedSmartEnums<T> = T extends { value: string; key: unknown }\n ? string\n : T extends ReadonlyArray<infer U>\n ? ReadonlyArray<SerializedSmartEnums<U>>\n : T extends Array<infer U>\n ? SerializedSmartEnums<U>[]\n : T extends object\n ? { [K in keyof T]: SerializedSmartEnums<T[K]> }\n : T;\n\n/**\n * Revived shape: for keys present in mapping M, the string becomes the\n * corresponding enum item type (derived from the provided enum object);\n * other fields recurse.\n */\nexport type EnumItemFromEnum<TEnum> =\n TEnum extends Record<string, infer V>\n ? V extends { __smart_enum_brand: true }\n ? V\n : never\n : never;\n\n// Structural constraint for enum objects passed to reviveSmartEnums mapping\nexport type AnyEnumLike = {\n tryFromValue: (value?: string | null) => unknown;\n tryFromKey: (key?: string | null) => unknown;\n} & Record<string, unknown>;\n\nexport type SmartEnumLike<T = unknown> = {\n tryFromValue: (value: string) => T | undefined;\n};\n\nexport type FieldEnumMapping = Record<string, SmartEnumLike>;\n\nexport type ReviveRowOptions = {\n fieldEnumMapping: FieldEnumMapping;\n strict?: boolean;\n};\n\nexport type PathEnumMapping = Record<string, SmartEnumLike>;\n\nexport type RevivePayloadOptions = {\n pathEnumMapping: PathEnumMapping;\n strict?: boolean;\n};\n\nexport type RevivedSmartEnums<T, M extends Record<string, AnyEnumLike>> =\n T extends ReadonlyArray<infer U>\n ? RevivedSmartEnums<U, M>[]\n : T extends Array<infer U>\n ? RevivedSmartEnums<U, M>[]\n : T extends object\n ? {\n [K in keyof T]: K extends Extract<keyof M, string>\n ? Enumeration<M[K]>\n : RevivedSmartEnums<T[K], M>;\n }\n : T;\n\nexport type SmartEnumItemSerialized = {\n __smart_enum_type: string;\n value: string;\n};\n\n/** Example shape for transport tests (`reviveAfterTransport` registry). */\nexport type SmartApiHelperConfig = {\n enumRegistry: Record<string, AnyEnumLike>;\n};\n\n/**\n * Compile-time transformer: replaces Smart Enum items with string values,\n * recursively over arrays and objects. Used for database storage.\n */\nexport type DatabaseFormat<T> = T extends { value: string; key: unknown }\n ? string\n : T extends ReadonlyArray<infer U>\n ? ReadonlyArray<DatabaseFormat<U>>\n : T extends Array<infer U>\n ? DatabaseFormat<U>[]\n : T extends object\n ? { [K in keyof T]: DatabaseFormat<T[K]> }\n : T;\n\n/**\n * Log levels for smart enum mappings\n */\nexport type LogLevel = 'debug' | 'info' | 'warn' | 'error';\n\n/**\n * Configuration for smart enum mappings initialization\n */\nexport type SmartEnumMappingsConfig = {\n enumRegistry: Record<string, AnyEnumLike>;\n logLevel?: LogLevel;\n logger?: import('./utilities/logger.js').Logger;\n};\n\ntype BuiltInOverrideKeys =\n | 'key'\n | 'value'\n | 'display'\n | 'deprecated'\n | 'index'\n | '__smart_enum_brand'\n | '__smart_enum_type';\n\nexport type StandardEnumItemBase = {\n readonly key: string;\n readonly value: string;\n readonly display: string;\n readonly index: number;\n readonly deprecated?: boolean;\n};\n\nexport type StandardEnumItem = StandardEnumItemBase & {\n readonly __smart_enum_brand: true;\n readonly __smart_enum_type: string;\n readonly toPostgres: () => string;\n};\n\nexport type EnumInputItem = Partial<{\n key: string;\n value: string;\n display: string;\n deprecated: boolean;\n}> &\n Record<string, unknown>;\n\nexport type ObjectEnumInput = Record<string, EnumInputItem>;\n\nexport type EmptyEnumInputItem = Record<never, never>;\n\nexport type ArrayToObjectType<T extends readonly string[]> = {\n [K in T[number]]: EmptyEnumInputItem;\n};\n\nexport type NormalizedInputType<TInput> = TInput extends readonly string[]\n ? ArrayToObjectType<TInput>\n : TInput extends ObjectEnumInput\n ? TInput\n : never;\n\nexport type EnumItemFromNormalizedObject<\n TObj extends ObjectEnumInput,\n K extends keyof TObj = keyof TObj,\n> = Omit<StandardEnumItem, 'key'> &\n InferredExtraFields<TObj> & {\n readonly key: Extract<K, string>;\n };\n\nexport type EnumMemberUnionFromNormalizedObject<TObj extends ObjectEnumInput> =\n {\n [K in keyof TObj]: EnumItemFromNormalizedObject<TObj, K>;\n }[keyof TObj];\n\nexport type EnumFromNormalizedObject<TObj extends ObjectEnumInput> = {\n [K in keyof TObj]: EnumItemFromNormalizedObject<TObj, K>;\n} & CoreEnumMethods<EnumMemberUnionFromNormalizedObject<TObj>>;\n\nexport type UnionKeys<T> = T extends T ? keyof T : never;\n\n/*\nTo widen the type of the OptionalObject from literals to their actual types,\ne.g.\nshape?: \"round\" | \"square\" | \"long\";\nslices?: true;\nlayers?: 2;\n--turns into:--\nshape: string | undefined;\nslices: boolean| undefined;\nlayers: number| undefined;\n\nwe can use the following code:\ntype Widen<T> = T extends string\n ? string\n : T extends number\n ? number\n : T extends boolean\n ? boolean\n : T;\n\ntype MergeUnionToObject<T> = {\n [K in UnionKeys<T>]: T extends Record<K, infer V> ? Widen<V> : undefined;\n};\n*/\n\ntype MergeUnionToObject<T> = {\n [K in UnionKeys<T>]: T extends Record<K, infer V> ? V : undefined;\n};\n\ntype ExtraShapeUnion<TObj extends ObjectEnumInput> = {\n [K in keyof TObj]: Omit<TObj[K], BuiltInOverrideKeys>;\n}[keyof TObj];\n\nexport type InferredExtraFields<TObj extends ObjectEnumInput> =\n MergeUnionToObject<ExtraShapeUnion<TObj>>;\n\nexport type PropertyAutoFormatter = {\n /** The property name to generate */\n key: string;\n /** Function to transform the key into the property value */\n format: (k: string) => string;\n};\n\nexport type CoreEnumMethods<TItem extends StandardEnumItem> = {\n fromValue(value: string): TItem;\n tryFromValue(value?: string | null): TItem | undefined;\n fromKey(key: string): TItem;\n tryFromKey(key?: string | null): TItem | undefined;\n items(): readonly TItem[];\n values(): readonly string[];\n keys(): readonly string[];\n};\n\nexport type EnumerationProps<TInput> = {\n input: TInput;\n propertyAutoFormatters?: PropertyAutoFormatter[];\n};\n\nexport type Enumeration<TEnum> =\n TEnum extends Record<string, infer V>\n ? V extends { __smart_enum_brand: true }\n ? V\n : never\n : never;\n\nexport type FinalizedEnumFields = Pick<\n StandardEnumItem,\n '__smart_enum_brand' | '__smart_enum_type'\n>;\nexport type FinalizableEnumItem = {\n key: string;\n value: string;\n display: string;\n index: number;\n deprecated?: boolean;\n};\n","import {\n SMART_ENUM_ITEM,\n SMART_ENUM,\n SmartEnumItemSerialized,\n} from '../types.js';\n\n/**\n * Runtime type guard to detect Smart Enum items created by this library.\n * Returns true if the value has the SMART_ENUM_ITEM symbol.\n *\n * @example\n * ```typescript\n * import { Status } from './status';\n * const item = Status.active;\n * isSmartEnumItem(item); // true\n * isSmartEnumItem({ key: 'active', value: 'ACTIVE' }); // false (plain object)\n * if (isSmartEnumItem(x)) {\n * console.log(x.value, x.__smart_enum_type); // narrowed to enum item\n * }\n * ```\n */\nexport const isSmartEnumItem = (\n x: unknown,\n): x is {\n key: string;\n value: string;\n index?: number;\n __smart_enum_type?: string;\n} => {\n return (\n !!x && typeof x === 'object' && Reflect.get(x, SMART_ENUM_ITEM) === true\n );\n};\n\n/**\n * Runtime type guard to detect a full Smart Enum object created by this library.\n * Returns true if the object has the SMART_ENUM property.\n *\n * @example\n * ```typescript\n * import { MyEnum } from './blah';\n * isSmartEnum(MyEnum) === true; // true\n * isSmartEnum(MyEnum.one) === false; // false (this is an item, not the enum)\n * ```\n */\nexport const isSmartEnum = (x: unknown): boolean => {\n return !!x && typeof x === 'object' && Reflect.get(x, SMART_ENUM) === true;\n};\n\n/**\n * Runtime type guard to detect a serialized Smart Enum item created by this library.\n * Returns true if the value has `__smart_enum_type` and `value` (wire/database shape).\n *\n * @example\n * ```typescript\n * const wire = { __smart_enum_type: 'Status', value: 'ACTIVE' };\n * isSerializedSmartEnumItem(wire); // true\n * isSerializedSmartEnumItem(Status.active); // false (live enum item)\n * if (isSerializedSmartEnumItem(x)) {\n * reviveItem(x.__smart_enum_type, x.value); // narrowed to SmartEnumItemSerialized\n * }\n * ```\n */\nexport const isSerializedSmartEnumItem = (\n x: unknown,\n): x is SmartEnumItemSerialized => {\n return (\n !!x &&\n typeof x === 'object' &&\n Reflect.has(x, '__smart_enum_type') &&\n Reflect.has(x, 'value')\n );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAA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;AAAA,MACF;AAEA,aAAO,OAAO,QAAQ;AACtB,mBAAa,QAAQ,IAAI;AACzB;AAAA,IACF;AAAA,EACF;AAEA,QAAM,mBAAmB;AAAA,IACvB,OAAO,OAAO,YAAY;AAAA,EAC5B;AAEA,QAAM,aAAa;AAAA,IACjB,GAAG;AAAA,IACH,GAAG;AAAA,EACL;AAEA,SAAO,eAAe,YAAY,YAAY;AAAA,IAC5C,OAAO;AAAA,IACP,YAAY;AAAA,EACd,CAAC;AAED,SAAO,OAAO,UAAU;AAExB,SAAO;AACT;AAYO,SAAS,YACd,UACA,OACA;AACA,QAAM,aAAa,eAAe,MAAM,KAAK;AAC7C,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA,MAAM;AAAA,EACR;AACF;;;AG1RO,IAAM,kBAAkB,CAC7B,MAMG;AACH,SACE,CAAC,CAAC,KAAK,OAAO,MAAM,YAAY,QAAQ,IAAI,GAAG,eAAe,MAAM;AAExE;AAaO,IAAM,cAAc,CAAC,MAAwB;AAClD,SAAO,CAAC,CAAC,KAAK,OAAO,MAAM,YAAY,QAAQ,IAAI,GAAG,UAAU,MAAM;AACxE;","names":[]}
package/dist/core.d.cts CHANGED
@@ -1 +1 @@
1
- export { A as AnyEnumLike, E as Enumeration, e as enumeration, a as isSmartEnum, i as isSmartEnumItem } from './core-DgjOZ8Bg.cjs';
1
+ export { A as AnyEnumLike, E as Enumeration, k as StandardEnumItem, e as enumeration, a as isSmartEnum, i as isSmartEnumItem } from './core-CDE80kvy.cjs';
package/dist/core.d.ts CHANGED
@@ -1 +1 @@
1
- export { A as AnyEnumLike, E as Enumeration, e as enumeration, a as isSmartEnum, i as isSmartEnumItem } from './core-DgjOZ8Bg.js';
1
+ export { A as AnyEnumLike, E as Enumeration, k as StandardEnumItem, e as enumeration, a as isSmartEnum, i as isSmartEnumItem } from './core-CDE80kvy.js';
package/dist/core.js CHANGED
@@ -2,7 +2,7 @@ import {
2
2
  enumeration,
3
3
  isSmartEnum,
4
4
  isSmartEnumItem
5
- } from "./chunk-NASPJET6.js";
5
+ } from "./chunk-E3FO4SL3.js";
6
6
  export {
7
7
  enumeration,
8
8
  isSmartEnum,