@digitaldefiance/branded-enum 0.0.4 → 0.0.6

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 +1 @@
1
- {"version":3,"sources":["../../../src/lib/registry.ts"],"sourcesContent":["/**\n * Global registry for branded enums.\n *\n * Uses globalThis to ensure cross-bundle compatibility - all instances\n * of the library share the same registry regardless of how they're bundled.\n */\n\nimport {\n BrandedEnum,\n BrandedEnumRegistry,\n RegistryEntry,\n REGISTRY_KEY,\n ENUM_ID,\n ENUM_VALUES,\n} from './types.js';\n\n/**\n * Gets the global registry, initializing it lazily if needed.\n * Uses globalThis for cross-bundle compatibility.\n *\n * The registry is shared across all instances of the library, even when\n * bundled separately or loaded as different module formats (ESM/CJS).\n *\n * @returns The global branded enum registry containing all registered enums\n * and a value index for reverse lookups.\n *\n * @example\n * const registry = getRegistry();\n * console.log(registry.enums.size); // Number of registered enums\n */\nexport function getRegistry(): BrandedEnumRegistry {\n const global = globalThis as typeof globalThis & {\n [REGISTRY_KEY]?: BrandedEnumRegistry;\n };\n\n if (!(REGISTRY_KEY in global) || !global[REGISTRY_KEY]) {\n global[REGISTRY_KEY] = {\n enums: new Map<string, RegistryEntry>(),\n valueIndex: new Map<string, Set<string>>(),\n };\n }\n\n return global[REGISTRY_KEY];\n}\n\n/**\n * Registers a branded enum in the global registry.\n * Also updates the value index for reverse lookups.\n *\n * @param enumObj - The branded enum to register\n * @throws Error if an enum with the same ID is already registered\n */\nexport function registerEnum<T extends Record<string, string>>(\n enumObj: BrandedEnum<T>\n): void {\n const registry = getRegistry();\n const enumId = enumObj[ENUM_ID];\n const values = enumObj[ENUM_VALUES];\n\n // Check for duplicate ID\n if (registry.enums.has(enumId)) {\n throw new Error(`Branded enum with ID \"${enumId}\" already exists`);\n }\n\n // Create registry entry\n const entry: RegistryEntry = {\n enumId,\n enumObj: enumObj as BrandedEnum<Record<string, string>>,\n values,\n };\n\n // Add to enums map\n registry.enums.set(enumId, entry);\n\n // Update value index for reverse lookups\n for (const value of values) {\n let enumIds = registry.valueIndex.get(value);\n if (!enumIds) {\n enumIds = new Set<string>();\n registry.valueIndex.set(value, enumIds);\n }\n enumIds.add(enumId);\n }\n}\n\n/**\n * Gets all registered enum IDs.\n *\n * Returns an array of all enum IDs that have been registered via\n * `createBrandedEnum`. Useful for debugging or introspection.\n *\n * @returns Array of all registered enum IDs. Returns empty array if no\n * enums have been registered.\n *\n * @example\n * createBrandedEnum('colors', { Red: 'red' } as const);\n * createBrandedEnum('sizes', { Small: 'small' } as const);\n *\n * getAllEnumIds(); // ['colors', 'sizes']\n */\nexport function getAllEnumIds(): string[] {\n const registry = getRegistry();\n return Array.from(registry.enums.keys());\n}\n\n/**\n * Gets a branded enum by its ID.\n *\n * Retrieves a previously registered branded enum from the global registry.\n * Useful when you need to access an enum dynamically by its ID.\n *\n * @param enumId - The enum ID to look up\n * @returns The branded enum object if found, or `undefined` if no enum\n * with the given ID has been registered.\n *\n * @example\n * const Status = createBrandedEnum('status', { Active: 'active' } as const);\n *\n * const retrieved = getEnumById('status');\n * console.log(retrieved === Status); // true\n *\n * const notFound = getEnumById('nonexistent');\n * console.log(notFound); // undefined\n */\nexport function getEnumById(\n enumId: string\n): BrandedEnum<Record<string, string>> | undefined {\n const registry = getRegistry();\n const entry = registry.enums.get(enumId);\n return entry?.enumObj;\n}\n\n/**\n * Finds all enum IDs that contain a given value.\n *\n * Performs a reverse lookup to find which enums contain a specific value.\n * Useful for debugging value collisions or routing values to handlers.\n *\n * @param value - The string value to search for\n * @returns Array of enum IDs that contain the value. Returns empty array\n * if no enums contain the value.\n *\n * @example\n * // Single enum containing value\n * createBrandedEnum('colors', { Red: 'red', Blue: 'blue' } as const);\n * findEnumSources('red'); // ['colors']\n *\n * @example\n * // Multiple enums with same value (collision detection)\n * createBrandedEnum('status1', { Active: 'active' } as const);\n * createBrandedEnum('status2', { Enabled: 'active' } as const);\n * findEnumSources('active'); // ['status1', 'status2']\n *\n * @example\n * // Value not found\n * findEnumSources('nonexistent'); // []\n */\nexport function findEnumSources(value: string): string[] {\n const registry = getRegistry();\n const enumIds = registry.valueIndex.get(value);\n return enumIds ? Array.from(enumIds) : [];\n}\n"],"names":["findEnumSources","getAllEnumIds","getEnumById","getRegistry","registerEnum","global","globalThis","REGISTRY_KEY","enums","Map","valueIndex","enumObj","registry","enumId","ENUM_ID","values","ENUM_VALUES","has","Error","entry","set","value","enumIds","get","Set","add","Array","from","keys"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA;;;;;CAKC;;;;;;;;;;;IAwJeA,eAAe;eAAfA;;IAzDAC,aAAa;eAAbA;;IAwBAC,WAAW;eAAXA;;IA9FAC,WAAW;eAAXA;;IAsBAC,YAAY;eAAZA;;;uBAtCT;AAgBA,SAASD;IACd,MAAME,SAASC;IAIf,IAAI,CAAEC,CAAAA,mBAAY,IAAIF,MAAK,KAAM,CAACA,MAAM,CAACE,mBAAY,CAAC,EAAE;QACtDF,MAAM,CAACE,mBAAY,CAAC,GAAG;YACrBC,OAAO,IAAIC;YACXC,YAAY,IAAID;QAClB;IACF;IAEA,OAAOJ,MAAM,CAACE,mBAAY,CAAC;AAC7B;AASO,SAASH,aACdO,OAAuB;IAEvB,MAAMC,WAAWT;IACjB,MAAMU,SAASF,OAAO,CAACG,cAAO,CAAC;IAC/B,MAAMC,SAASJ,OAAO,CAACK,kBAAW,CAAC;IAEnC,yBAAyB;IACzB,IAAIJ,SAASJ,KAAK,CAACS,GAAG,CAACJ,SAAS;QAC9B,MAAM,IAAIK,MAAM,CAAC,sBAAsB,EAAEL,OAAO,gBAAgB,CAAC;IACnE;IAEA,wBAAwB;IACxB,MAAMM,QAAuB;QAC3BN;QACAF,SAASA;QACTI;IACF;IAEA,mBAAmB;IACnBH,SAASJ,KAAK,CAACY,GAAG,CAACP,QAAQM;IAE3B,yCAAyC;IACzC,KAAK,MAAME,SAASN,OAAQ;QAC1B,IAAIO,UAAUV,SAASF,UAAU,CAACa,GAAG,CAACF;QACtC,IAAI,CAACC,SAAS;YACZA,UAAU,IAAIE;YACdZ,SAASF,UAAU,CAACU,GAAG,CAACC,OAAOC;QACjC;QACAA,QAAQG,GAAG,CAACZ;IACd;AACF;AAiBO,SAASZ;IACd,MAAMW,WAAWT;IACjB,OAAOuB,MAAMC,IAAI,CAACf,SAASJ,KAAK,CAACoB,IAAI;AACvC;AAqBO,SAAS1B,YACdW,MAAc;IAEd,MAAMD,WAAWT;IACjB,MAAMgB,QAAQP,SAASJ,KAAK,CAACe,GAAG,CAACV;IACjC,OAAOM,OAAOR;AAChB;AA2BO,SAASX,gBAAgBqB,KAAa;IAC3C,MAAMT,WAAWT;IACjB,MAAMmB,UAAUV,SAASF,UAAU,CAACa,GAAG,CAACF;IACxC,OAAOC,UAAUI,MAAMC,IAAI,CAACL,WAAW,EAAE;AAC3C"}
1
+ {"version":3,"sources":["../../../src/lib/registry.ts"],"sourcesContent":["/**\n * Global registry for branded enums.\n *\n * Uses globalThis to ensure cross-bundle compatibility - all instances\n * of the library share the same registry regardless of how they're bundled.\n */\n\nimport {\n BrandedEnum,\n BrandedEnumRegistry,\n RegistryEntry,\n REGISTRY_KEY,\n ENUM_ID,\n ENUM_VALUES,\n} from './types.js';\n\n/**\n * Gets the global registry, initializing it lazily if needed.\n * Uses globalThis for cross-bundle compatibility.\n *\n * The registry is shared across all instances of the library, even when\n * bundled separately or loaded as different module formats (ESM/CJS).\n *\n * @returns The global branded enum registry containing all registered enums\n * and a value index for reverse lookups.\n *\n * @example\n * const registry = getRegistry();\n * console.log(registry.enums.size); // Number of registered enums\n */\nexport function getRegistry(): BrandedEnumRegistry {\n const global = globalThis as typeof globalThis & {\n [REGISTRY_KEY]?: BrandedEnumRegistry;\n };\n\n if (!(REGISTRY_KEY in global) || !global[REGISTRY_KEY]) {\n global[REGISTRY_KEY] = {\n enums: new Map<string, RegistryEntry>(),\n valueIndex: new Map<string, Set<string>>(),\n };\n }\n\n return global[REGISTRY_KEY];\n}\n\n/**\n * Registers a branded enum in the global registry.\n * Also updates the value index for reverse lookups.\n *\n * @param enumObj - The branded enum to register\n * @throws Error if an enum with the same ID is already registered\n */\nexport function registerEnum<T extends Record<string, string>>(\n enumObj: BrandedEnum<T>\n): void {\n const registry = getRegistry();\n const enumId = enumObj[ENUM_ID];\n const values = enumObj[ENUM_VALUES];\n\n // Check for duplicate ID\n if (registry.enums.has(enumId)) {\n throw new Error(`Branded enum with ID \"${enumId}\" already exists`);\n }\n\n // Create registry entry\n const entry: RegistryEntry = {\n enumId,\n enumObj: enumObj as BrandedEnum<Record<string, string>>,\n values,\n };\n\n // Add to enums map\n registry.enums.set(enumId, entry);\n\n // Update value index for reverse lookups\n for (const value of values) {\n let enumIds = registry.valueIndex.get(value);\n if (!enumIds) {\n enumIds = new Set<string>();\n registry.valueIndex.set(value, enumIds);\n }\n enumIds.add(enumId);\n }\n}\n\n/**\n * Gets all registered enum IDs.\n *\n * Returns an array of all enum IDs that have been registered via\n * `createBrandedEnum`. Useful for debugging or introspection.\n *\n * @returns Array of all registered enum IDs. Returns empty array if no\n * enums have been registered.\n *\n * @example\n * createBrandedEnum('colors', { Red: 'red' } as const);\n * createBrandedEnum('sizes', { Small: 'small' } as const);\n *\n * getAllEnumIds(); // ['colors', 'sizes']\n */\nexport function getAllEnumIds(): string[] {\n const registry = getRegistry();\n return Array.from(registry.enums.keys());\n}\n\n/**\n * Gets a branded enum by its ID.\n *\n * Retrieves a previously registered branded enum from the global registry.\n * Useful when you need to access an enum dynamically by its ID.\n *\n * @param enumId - The enum ID to look up\n * @returns The branded enum object if found, or `undefined` if no enum\n * with the given ID has been registered.\n *\n * @example\n * const Status = createBrandedEnum('status', { Active: 'active' } as const);\n *\n * const retrieved = getEnumById('status');\n * console.log(retrieved === Status); // true\n *\n * const notFound = getEnumById('nonexistent');\n * console.log(notFound); // undefined\n */\nexport function getEnumById(\n enumId: string\n): BrandedEnum<Record<string, string>> | undefined {\n const registry = getRegistry();\n const entry = registry.enums.get(enumId);\n return entry?.enumObj;\n}\n\n/**\n * Finds all enum IDs that contain a given value.\n *\n * Performs a reverse lookup to find which enums contain a specific value.\n * Useful for debugging value collisions or routing values to handlers.\n *\n * @param value - The string value to search for\n * @returns Array of enum IDs that contain the value. Returns empty array\n * if no enums contain the value.\n *\n * @example\n * // Single enum containing value\n * createBrandedEnum('colors', { Red: 'red', Blue: 'blue' } as const);\n * findEnumSources('red'); // ['colors']\n *\n * @example\n * // Multiple enums with same value (collision detection)\n * createBrandedEnum('status1', { Active: 'active' } as const);\n * createBrandedEnum('status2', { Enabled: 'active' } as const);\n * findEnumSources('active'); // ['status1', 'status2']\n *\n * @example\n * // Value not found\n * findEnumSources('nonexistent'); // []\n */\nexport function findEnumSources(value: string): string[] {\n const registry = getRegistry();\n const enumIds = registry.valueIndex.get(value);\n return enumIds ? Array.from(enumIds) : [];\n}\n\n/**\n * Resets the global branded enum registry, clearing all registered enums.\n *\n * **WARNING**: This function is intended for testing purposes only.\n * Using it in production code will break any code that depends on\n * previously registered enums.\n *\n * Clears all entries from:\n * - The enums map (ID -> enum object)\n * - The value index (value -> set of enum IDs)\n *\n * @example\n * // In test setup/teardown\n * beforeEach(() => {\n * resetRegistry();\n * });\n *\n * @example\n * // Clear and verify\n * resetRegistry();\n * getAllEnumIds(); // []\n */\nexport function resetRegistry(): void {\n const registry = getRegistry();\n registry.enums.clear();\n registry.valueIndex.clear();\n}\n"],"names":["findEnumSources","getAllEnumIds","getEnumById","getRegistry","registerEnum","resetRegistry","global","globalThis","REGISTRY_KEY","enums","Map","valueIndex","enumObj","registry","enumId","ENUM_ID","values","ENUM_VALUES","has","Error","entry","set","value","enumIds","get","Set","add","Array","from","keys","clear"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;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,WAAWV;IACjB,MAAMiB,QAAQP,SAASJ,KAAK,CAACe,GAAG,CAACV;IACjC,OAAOM,OAAOR;AAChB;AA2BO,SAASZ,gBAAgBsB,KAAa;IAC3C,MAAMT,WAAWV;IACjB,MAAMoB,UAAUV,SAASF,UAAU,CAACa,GAAG,CAACF;IACxC,OAAOC,UAAUI,MAAMC,IAAI,CAACL,WAAW,EAAE;AAC3C;AAwBO,SAASlB;IACd,MAAMQ,WAAWV;IACjBU,SAASJ,KAAK,CAACqB,KAAK;IACpBjB,SAASF,UAAU,CAACmB,KAAK;AAC3B"}
@@ -8,7 +8,7 @@
8
8
  */
9
9
  export type { BrandedEnumMetadata, BrandedEnum, BrandedEnumValue, RegistryEntry, BrandedEnumRegistry, EnumConsumerEntry, EnumConsumerRegistry, AnyBrandedEnum, EnumKeys, EnumValues, ValidEnumValue, StrictEnumParam, } from './lib/types.js';
10
10
  export { createBrandedEnum } from './lib/factory.js';
11
- export { getRegistry, getAllEnumIds, getEnumById, findEnumSources, } from './lib/registry.js';
11
+ export { getRegistry, getAllEnumIds, getEnumById, findEnumSources, resetRegistry, } from './lib/registry.js';
12
12
  export { isFromEnum, assertFromEnum, parseEnum, safeParseEnum } from './lib/guards.js';
13
13
  export type { SafeParseSuccess, SafeParseFailure, SafeParseError, SafeParseErrorCode, SafeParseResult } from './lib/guards.js';
14
14
  export { getEnumId, getEnumValues, enumSize } from './lib/accessors.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAMH,YAAY,EACV,mBAAmB,EACnB,WAAW,EACX,gBAAgB,EAChB,aAAa,EACb,mBAAmB,EACnB,iBAAiB,EACjB,oBAAoB,EACpB,cAAc,EACd,QAAQ,EACR,UAAU,EACV,cAAc,EACd,eAAe,GAChB,MAAM,gBAAgB,CAAC;AAMxB,OAAO,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC;AAMrD,OAA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AO,EACL,SAAS,EACT,SAAS,EACT,gBAAgB,EAChB,gBAAgB,EAChB,mBAAmB,GACpB,MAAM,qBAAqB,CAAC;AAC7B,YAAY,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC"}
package/dist/esm/index.js CHANGED
@@ -15,7 +15,7 @@ export { createBrandedEnum } from './lib/factory.js';
15
15
  // =============================================================================
16
16
  // Registry
17
17
  // =============================================================================
18
- export { getRegistry, getAllEnumIds, getEnumById, findEnumSources } from './lib/registry.js';
18
+ export { getRegistry, getAllEnumIds, getEnumById, findEnumSources, resetRegistry } from './lib/registry.js';
19
19
  // =============================================================================
20
20
  // Type Guards
21
21
  // =============================================================================
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["/**\n * branded-enum - Runtime-identifiable enum-like types for TypeScript\n *\n * This library provides enum-like objects with embedded metadata for runtime\n * identification, enabling you to determine which enum a string value belongs to.\n *\n * @packageDocumentation\n */\n\n// =============================================================================\n// Types\n// =============================================================================\n\nexport type {\n BrandedEnumMetadata,\n BrandedEnum,\n BrandedEnumValue,\n RegistryEntry,\n BrandedEnumRegistry,\n EnumConsumerEntry,\n EnumConsumerRegistry,\n AnyBrandedEnum,\n EnumKeys,\n EnumValues,\n ValidEnumValue,\n StrictEnumParam,\n} from './lib/types.js';\n\n// =============================================================================\n// Factory\n// =============================================================================\n\nexport { createBrandedEnum } from './lib/factory.js';\n\n// =============================================================================\n// Registry\n// =============================================================================\n\nexport {\n getRegistry,\n getAllEnumIds,\n getEnumById,\n findEnumSources,\n} from './lib/registry.js';\n\n// =============================================================================\n// Type Guards\n// =============================================================================\n\nexport { isFromEnum, assertFromEnum, parseEnum, safeParseEnum } from './lib/guards.js';\nexport type { SafeParseSuccess, SafeParseFailure, SafeParseError, SafeParseErrorCode, SafeParseResult } from './lib/guards.js';\n\n// =============================================================================\n// Metadata Accessors\n// =============================================================================\n\nexport { getEnumId, getEnumValues, enumSize } from './lib/accessors.js';\n\n// =============================================================================\n// Utility Functions\n// =============================================================================\n\nexport {\n hasValue,\n getKeyForValue,\n isValidKey,\n enumEntries,\n} from './lib/utils.js';\n\n// =============================================================================\n// Composition\n// =============================================================================\n\nexport { mergeEnums } from './lib/merge.js';\n\n// =============================================================================\n// Advanced Operations\n// =============================================================================\n\nexport { enumSubset, enumExclude, enumMap, enumFromKeys, enumDiff, enumIntersect, enumToRecord, watchEnum, watchAllEnums, clearAllEnumWatchers, getEnumWatcherCount, getGlobalWatcherCount, exhaustive, exhaustiveGuard, toJsonSchema, toZodSchema, enumSerializer } from './lib/advanced.js';\nexport type { EnumDiffResult, EnumIntersectEntry, EnumAccessType, EnumAccessEvent, EnumWatchCallback, WatchEnumResult, ToJsonSchemaOptions, EnumJsonSchema, ToZodSchemaOptions, ZodEnumSchemaDefinition, EnumSerializerOptions, DeserializeSuccess, DeserializeFailure, DeserializeResult, EnumSerializer } from './lib/advanced.js';\n\n// =============================================================================\n// Decorators\n// =============================================================================\n\nexport {\n EnumValue,\n EnumClass,\n getEnumConsumers,\n getConsumedEnums,\n getAllEnumConsumers,\n} from './lib/decorators.js';\nexport type { EnumValueOptions } from './lib/decorators.js';\n"],"names":["createBrandedEnum","getRegistry","getAllEnumIds","getEnumById","findEnumSources","isFromEnum","assertFromEnum","parseEnum","safeParseEnum","getEnumId","getEnumValues","enumSize","hasValue","getKeyForValue","isValidKey","enumEntries","mergeEnums","enumSubset","enumExclude","enumMap","enumFromKeys","enumDiff","enumIntersect","enumToRecord","watchEnum","watchAllEnums","clearAllEnumWatchers","getEnumWatcherCount","getGlobalWatcherCount","exhaustive","exhaustiveGuard","toJsonSchema","toZodSchema","enumSerializer","EnumValue","EnumClass","getEnumConsumers","getConsumedEnums","getAllEnumConsumers"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA;;;;;;;CAOC,GAED,gFAAgF;AAChF,QAAQ;AACR,gFAAgF;AAiBhF,gFAAgF;AAChF,UAAU;AACV,gFAAgF;AAEhF,SAASA,iBAAiB,QAAQ,mBAAmB;AAErD,gFAAgF;AAChF,WAAW;AACX,gFAAgF;AAEhF,SACEC,WAAW,EACXC,aAAa,EACbC,WAAW,EACXC,eAAe,QACV,oBAAoB;AAE3B,gFAAgF;AAChF,cAAc;AACd,gFAAgF;AAEhF,SAASC,UAAU,EAAEC,cAAc,EAAEC,SAAS,EAAEC,aAAa,QAAQ,kBAAkB;AAGvF,gFAAgF;AAChF,qBAAqB;AACrB,gFAAgF;AAEhF,SAASC,SAAS,EAAEC,aAAa,EAAEC,QAAQ,QAAQ,qBAAqB;AAExE,gFAAgF;AAChF,oBAAoB;AACpB,gFAAgF;AAEhF,SACEC,QAAQ,EACRC,cAAc,EACdC,UAAU,EACVC,WAAW,QACN,iBAAiB;AAExB,gFAAgF;AAChF,cAAc;AACd,gFAAgF;AAEhF,SAASC,UAAU,QAAQ,iBAAiB;AAE5C,gFAAgF;AAChF,sBAAsB;AACtB,gFAAgF;AAEhF,SAASC,UAAU,EAAEC,WAAW,EAAEC,OAAO,EAAEC,YAAY,EAAEC,QAAQ,EAAEC,aAAa,EAAEC,YAAY,EAAEC,SAAS,EAAEC,aAAa,EAAEC,oBAAoB,EAAEC,mBAAmB,EAAEC,qBAAqB,EAAEC,UAAU,EAAEC,eAAe,EAAEC,YAAY,EAAEC,WAAW,EAAEC,cAAc,QAAQ,oBAAoB;AAG9R,gFAAgF;AAChF,aAAa;AACb,gFAAgF;AAEhF,SACEC,SAAS,EACTC,SAAS,EACTC,gBAAgB,EAChBC,gBAAgB,EAChBC,mBAAmB,QACd,sBAAsB"}
1
+ {"version":3,"sources":["../../src/index.ts"],"sourcesContent":["/**\n * branded-enum - Runtime-identifiable enum-like types for TypeScript\n *\n * This library provides enum-like objects with embedded metadata for runtime\n * identification, enabling you to determine which enum a string value belongs to.\n *\n * @packageDocumentation\n */\n\n// =============================================================================\n// Types\n// =============================================================================\n\nexport type {\n BrandedEnumMetadata,\n BrandedEnum,\n BrandedEnumValue,\n RegistryEntry,\n BrandedEnumRegistry,\n EnumConsumerEntry,\n EnumConsumerRegistry,\n AnyBrandedEnum,\n EnumKeys,\n EnumValues,\n ValidEnumValue,\n StrictEnumParam,\n} from './lib/types.js';\n\n// =============================================================================\n// Factory\n// =============================================================================\n\nexport { createBrandedEnum } from './lib/factory.js';\n\n// =============================================================================\n// Registry\n// =============================================================================\n\nexport {\n getRegistry,\n getAllEnumIds,\n getEnumById,\n findEnumSources,\n resetRegistry,\n} from './lib/registry.js';\n\n// =============================================================================\n// Type Guards\n// =============================================================================\n\nexport { isFromEnum, assertFromEnum, parseEnum, safeParseEnum } from './lib/guards.js';\nexport type { SafeParseSuccess, SafeParseFailure, SafeParseError, SafeParseErrorCode, SafeParseResult } from './lib/guards.js';\n\n// =============================================================================\n// Metadata Accessors\n// =============================================================================\n\nexport { getEnumId, getEnumValues, enumSize } from './lib/accessors.js';\n\n// =============================================================================\n// Utility Functions\n// =============================================================================\n\nexport {\n hasValue,\n getKeyForValue,\n isValidKey,\n enumEntries,\n} from './lib/utils.js';\n\n// =============================================================================\n// Composition\n// =============================================================================\n\nexport { mergeEnums } from './lib/merge.js';\n\n// =============================================================================\n// Advanced Operations\n// =============================================================================\n\nexport { enumSubset, enumExclude, enumMap, enumFromKeys, enumDiff, enumIntersect, enumToRecord, watchEnum, watchAllEnums, clearAllEnumWatchers, getEnumWatcherCount, getGlobalWatcherCount, exhaustive, exhaustiveGuard, toJsonSchema, toZodSchema, enumSerializer } from './lib/advanced.js';\nexport type { EnumDiffResult, EnumIntersectEntry, EnumAccessType, EnumAccessEvent, EnumWatchCallback, WatchEnumResult, ToJsonSchemaOptions, EnumJsonSchema, ToZodSchemaOptions, ZodEnumSchemaDefinition, EnumSerializerOptions, DeserializeSuccess, DeserializeFailure, DeserializeResult, EnumSerializer } from './lib/advanced.js';\n\n// =============================================================================\n// Decorators\n// =============================================================================\n\nexport {\n EnumValue,\n EnumClass,\n getEnumConsumers,\n getConsumedEnums,\n getAllEnumConsumers,\n} from './lib/decorators.js';\nexport type { EnumValueOptions } from './lib/decorators.js';\n"],"names":["createBrandedEnum","getRegistry","getAllEnumIds","getEnumById","findEnumSources","resetRegistry","isFromEnum","assertFromEnum","parseEnum","safeParseEnum","getEnumId","getEnumValues","enumSize","hasValue","getKeyForValue","isValidKey","enumEntries","mergeEnums","enumSubset","enumExclude","enumMap","enumFromKeys","enumDiff","enumIntersect","enumToRecord","watchEnum","watchAllEnums","clearAllEnumWatchers","getEnumWatcherCount","getGlobalWatcherCount","exhaustive","exhaustiveGuard","toJsonSchema","toZodSchema","enumSerializer","EnumValue","EnumClass","getEnumConsumers","getConsumedEnums","getAllEnumConsumers"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA;;;;;;;CAOC,GAED,gFAAgF;AAChF,QAAQ;AACR,gFAAgF;AAiBhF,gFAAgF;AAChF,UAAU;AACV,gFAAgF;AAEhF,SAASA,iBAAiB,QAAQ,mBAAmB;AAErD,gFAAgF;AAChF,WAAW;AACX,gFAAgF;AAEhF,SACEC,WAAW,EACXC,aAAa,EACbC,WAAW,EACXC,eAAe,EACfC,aAAa,QACR,oBAAoB;AAE3B,gFAAgF;AAChF,cAAc;AACd,gFAAgF;AAEhF,SAASC,UAAU,EAAEC,cAAc,EAAEC,SAAS,EAAEC,aAAa,QAAQ,kBAAkB;AAGvF,gFAAgF;AAChF,qBAAqB;AACrB,gFAAgF;AAEhF,SAASC,SAAS,EAAEC,aAAa,EAAEC,QAAQ,QAAQ,qBAAqB;AAExE,gFAAgF;AAChF,oBAAoB;AACpB,gFAAgF;AAEhF,SACEC,QAAQ,EACRC,cAAc,EACdC,UAAU,EACVC,WAAW,QACN,iBAAiB;AAExB,gFAAgF;AAChF,cAAc;AACd,gFAAgF;AAEhF,SAASC,UAAU,QAAQ,iBAAiB;AAE5C,gFAAgF;AAChF,sBAAsB;AACtB,gFAAgF;AAEhF,SAASC,UAAU,EAAEC,WAAW,EAAEC,OAAO,EAAEC,YAAY,EAAEC,QAAQ,EAAEC,aAAa,EAAEC,YAAY,EAAEC,SAAS,EAAEC,aAAa,EAAEC,oBAAoB,EAAEC,mBAAmB,EAAEC,qBAAqB,EAAEC,UAAU,EAAEC,eAAe,EAAEC,YAAY,EAAEC,WAAW,EAAEC,cAAc,QAAQ,oBAAoB;AAG9R,gFAAgF;AAChF,aAAa;AACb,gFAAgF;AAEhF,SACEC,SAAS,EACTC,SAAS,EACTC,gBAAgB,EAChBC,gBAAgB,EAChBC,mBAAmB,QACd,sBAAsB"}
@@ -15,18 +15,18 @@ import { AnyBrandedEnum, BrandedEnum, EnumValues } from './types.js';
15
15
  *
16
16
  * @template E - The source branded enum type
17
17
  * @template K - The keys to include in the subset (must be keys of E)
18
- * @param newId - Unique identifier for the new subset enum. Must not already be registered.
18
+ * @param newId - Unique identifier for the new subset enum. If already registered,
19
+ * returns the existing enum (idempotent behavior).
19
20
  * @param sourceEnum - The branded enum to derive the subset from
20
21
  * @param keys - Array of keys to include in the subset. All keys must exist in sourceEnum.
21
- * @returns A new branded enum containing only the specified key-value pairs
22
+ * @returns A new branded enum containing only the specified key-value pairs,
23
+ * or the existing enum if newId is already registered
22
24
  * @throws {Error} Throws `Error` with message `enumSubset requires a branded enum as the source`
23
25
  * if sourceEnum is not a valid branded enum.
24
26
  * @throws {Error} Throws `Error` with message `enumSubset requires at least one key`
25
27
  * if keys array is empty.
26
28
  * @throws {Error} Throws `Error` with message `Key "${key}" does not exist in enum "${enumId}"`
27
29
  * if any specified key does not exist in the source enum.
28
- * @throws {Error} Throws `Error` with message `Branded enum with ID "${newId}" already exists`
29
- * if newId is already registered.
30
30
  *
31
31
  * @example
32
32
  * // Basic usage - create a subset of colors
@@ -79,18 +79,18 @@ export declare function enumSubset<E extends AnyBrandedEnum, K extends keyof E &
79
79
  *
80
80
  * @template E - The source branded enum type
81
81
  * @template K - The keys to exclude from the result (must be keys of E)
82
- * @param newId - Unique identifier for the new enum. Must not already be registered.
82
+ * @param newId - Unique identifier for the new enum. If already registered,
83
+ * returns the existing enum (idempotent behavior).
83
84
  * @param sourceEnum - The branded enum to derive from
84
85
  * @param keysToExclude - Array of keys to exclude. All keys must exist in sourceEnum.
85
- * @returns A new branded enum containing all key-value pairs except the excluded ones
86
+ * @returns A new branded enum containing all key-value pairs except the excluded ones,
87
+ * or the existing enum if newId is already registered
86
88
  * @throws {Error} Throws `Error` with message `enumExclude requires a branded enum as the source`
87
89
  * if sourceEnum is not a valid branded enum.
88
90
  * @throws {Error} Throws `Error` with message `enumExclude: excluding all keys would result in an empty enum`
89
91
  * if excluding all keys would leave no keys remaining.
90
92
  * @throws {Error} Throws `Error` with message `Key "${key}" does not exist in enum "${enumId}"`
91
93
  * if any specified key to exclude does not exist in the source enum.
92
- * @throws {Error} Throws `Error` with message `Branded enum with ID "${newId}" already exists`
93
- * if newId is already registered.
94
94
  *
95
95
  * @example
96
96
  * // Basic usage - exclude specific colors
@@ -153,17 +153,17 @@ type MappedEnumValues<E extends Record<string, string>> = {
153
153
  * The resulting enum is registered as an independent enum in the global registry.
154
154
  *
155
155
  * @template E - The source branded enum type
156
- * @param newId - Unique identifier for the new enum. Must not already be registered.
156
+ * @param newId - Unique identifier for the new enum. If already registered,
157
+ * returns the existing enum (idempotent behavior).
157
158
  * @param sourceEnum - The branded enum to derive from
158
159
  * @param mapper - Function that transforms each value. Receives the original value
159
160
  * and the key, and returns the transformed value.
160
- * @returns A new branded enum with transformed values
161
+ * @returns A new branded enum with transformed values,
162
+ * or the existing enum if newId is already registered
161
163
  * @throws {Error} Throws `Error` with message `enumMap requires a branded enum as the source`
162
164
  * if sourceEnum is not a valid branded enum.
163
165
  * @throws {Error} Throws `Error` with message `enumMap mapper must return a string`
164
166
  * if the mapper function returns a non-string value.
165
- * @throws {Error} Throws `Error` with message `Branded enum with ID "${newId}" already exists`
166
- * if newId is already registered.
167
167
  *
168
168
  * @example
169
169
  * // Prefix all values with a namespace
@@ -220,18 +220,18 @@ type KeysAsValues<K extends readonly string[]> = {
220
220
  * types of the provided keys.
221
221
  *
222
222
  * @template K - The array of string keys (use `as const` for literal types)
223
- * @param enumId - Unique identifier for the new enum. Must not already be registered.
223
+ * @param enumId - Unique identifier for the new enum. If already registered,
224
+ * returns the existing enum (idempotent behavior).
224
225
  * @param keys - Array of strings that will become both keys and values.
225
226
  * Use `as const` for literal type inference.
226
- * @returns A new branded enum where each key maps to itself
227
+ * @returns A new branded enum where each key maps to itself,
228
+ * or the existing enum if enumId is already registered
227
229
  * @throws {Error} Throws `Error` with message `enumFromKeys requires at least one key`
228
230
  * if keys array is empty.
229
231
  * @throws {Error} Throws `Error` with message `enumFromKeys requires all keys to be non-empty strings`
230
232
  * if any key is not a non-empty string.
231
233
  * @throws {Error} Throws `Error` with message `enumFromKeys: duplicate key "${key}" found`
232
234
  * if the keys array contains duplicates.
233
- * @throws {Error} Throws `Error` with message `Branded enum with ID "${enumId}" already exists`
234
- * if enumId is already registered.
235
235
  *
236
236
  * @example
237
237
  * // Basic usage - create enum from string array
@@ -23,18 +23,18 @@ import { createBrandedEnum } from './factory.js';
23
23
  *
24
24
  * @template E - The source branded enum type
25
25
  * @template K - The keys to include in the subset (must be keys of E)
26
- * @param newId - Unique identifier for the new subset enum. Must not already be registered.
26
+ * @param newId - Unique identifier for the new subset enum. If already registered,
27
+ * returns the existing enum (idempotent behavior).
27
28
  * @param sourceEnum - The branded enum to derive the subset from
28
29
  * @param keys - Array of keys to include in the subset. All keys must exist in sourceEnum.
29
- * @returns A new branded enum containing only the specified key-value pairs
30
+ * @returns A new branded enum containing only the specified key-value pairs,
31
+ * or the existing enum if newId is already registered
30
32
  * @throws {Error} Throws `Error` with message `enumSubset requires a branded enum as the source`
31
33
  * if sourceEnum is not a valid branded enum.
32
34
  * @throws {Error} Throws `Error` with message `enumSubset requires at least one key`
33
35
  * if keys array is empty.
34
36
  * @throws {Error} Throws `Error` with message `Key "${key}" does not exist in enum "${enumId}"`
35
37
  * if any specified key does not exist in the source enum.
36
- * @throws {Error} Throws `Error` with message `Branded enum with ID "${newId}" already exists`
37
- * if newId is already registered.
38
38
  *
39
39
  * @example
40
40
  * // Basic usage - create a subset of colors
@@ -108,18 +108,18 @@ import { createBrandedEnum } from './factory.js';
108
108
  *
109
109
  * @template E - The source branded enum type
110
110
  * @template K - The keys to exclude from the result (must be keys of E)
111
- * @param newId - Unique identifier for the new enum. Must not already be registered.
111
+ * @param newId - Unique identifier for the new enum. If already registered,
112
+ * returns the existing enum (idempotent behavior).
112
113
  * @param sourceEnum - The branded enum to derive from
113
114
  * @param keysToExclude - Array of keys to exclude. All keys must exist in sourceEnum.
114
- * @returns A new branded enum containing all key-value pairs except the excluded ones
115
+ * @returns A new branded enum containing all key-value pairs except the excluded ones,
116
+ * or the existing enum if newId is already registered
115
117
  * @throws {Error} Throws `Error` with message `enumExclude requires a branded enum as the source`
116
118
  * if sourceEnum is not a valid branded enum.
117
119
  * @throws {Error} Throws `Error` with message `enumExclude: excluding all keys would result in an empty enum`
118
120
  * if excluding all keys would leave no keys remaining.
119
121
  * @throws {Error} Throws `Error` with message `Key "${key}" does not exist in enum "${enumId}"`
120
122
  * if any specified key to exclude does not exist in the source enum.
121
- * @throws {Error} Throws `Error` with message `Branded enum with ID "${newId}" already exists`
122
- * if newId is already registered.
123
123
  *
124
124
  * @example
125
125
  * // Basic usage - exclude specific colors
@@ -203,17 +203,17 @@ import { createBrandedEnum } from './factory.js';
203
203
  * The resulting enum is registered as an independent enum in the global registry.
204
204
  *
205
205
  * @template E - The source branded enum type
206
- * @param newId - Unique identifier for the new enum. Must not already be registered.
206
+ * @param newId - Unique identifier for the new enum. If already registered,
207
+ * returns the existing enum (idempotent behavior).
207
208
  * @param sourceEnum - The branded enum to derive from
208
209
  * @param mapper - Function that transforms each value. Receives the original value
209
210
  * and the key, and returns the transformed value.
210
- * @returns A new branded enum with transformed values
211
+ * @returns A new branded enum with transformed values,
212
+ * or the existing enum if newId is already registered
211
213
  * @throws {Error} Throws `Error` with message `enumMap requires a branded enum as the source`
212
214
  * if sourceEnum is not a valid branded enum.
213
215
  * @throws {Error} Throws `Error` with message `enumMap mapper must return a string`
214
216
  * if the mapper function returns a non-string value.
215
- * @throws {Error} Throws `Error` with message `Branded enum with ID "${newId}" already exists`
216
- * if newId is already registered.
217
217
  *
218
218
  * @example
219
219
  * // Prefix all values with a namespace
@@ -283,18 +283,18 @@ import { createBrandedEnum } from './factory.js';
283
283
  * types of the provided keys.
284
284
  *
285
285
  * @template K - The array of string keys (use `as const` for literal types)
286
- * @param enumId - Unique identifier for the new enum. Must not already be registered.
286
+ * @param enumId - Unique identifier for the new enum. If already registered,
287
+ * returns the existing enum (idempotent behavior).
287
288
  * @param keys - Array of strings that will become both keys and values.
288
289
  * Use `as const` for literal type inference.
289
- * @returns A new branded enum where each key maps to itself
290
+ * @returns A new branded enum where each key maps to itself,
291
+ * or the existing enum if enumId is already registered
290
292
  * @throws {Error} Throws `Error` with message `enumFromKeys requires at least one key`
291
293
  * if keys array is empty.
292
294
  * @throws {Error} Throws `Error` with message `enumFromKeys requires all keys to be non-empty strings`
293
295
  * if any key is not a non-empty string.
294
296
  * @throws {Error} Throws `Error` with message `enumFromKeys: duplicate key "${key}" found`
295
297
  * if the keys array contains duplicates.
296
- * @throws {Error} Throws `Error` with message `Branded enum with ID "${enumId}" already exists`
297
- * if enumId is already registered.
298
298
  *
299
299
  * @example
300
300
  * // Basic usage - create enum from string array