Skip to content

unacy

Unacy Core - Type-safe unit and format conversion library

Branding

Type AliasDescription
RelaxRelax a branded unit type to accept either the branded form or its raw unwrapped value. Useful for APIs that should accept both WithUnits<T, M> and plain T interchangeably.
WithFormatBrand a value with a format identifier for compile-time format safety.
WithTypedUnitsResolve a branded unit type from a TypedMetadata object.
WithUnitsBrand a value with a unit identifier for compile-time unit safety.

Converters

Type AliasDescription
BidirectionalConverterBidirectional converter with forward and reverse transformations.
ConverterUnidirectional converter from one unit to another.
RelaxedBidirectionalConverterA bidirectional converter with relaxed (unwrapped) output types. Input remains branded for full autocompletion and type safety; return values are plain base types without branding.
RelaxedConverterA converter that accepts the branded input type but returns unwrapped (plain) output. This eliminates the need to cast return values to branded types inside converter functions, while preserving full autocompletion on the input parameter.

Errors

ClassDescription
ConversionErrorError thrown when a conversion cannot be performed.
CycleErrorError thrown when a cycle is detected during BFS path-finding.
MaxDepthErrorError thrown when BFS path-finding exceeds the maximum conversion depth.
ParseErrorError thrown when parsing a string into a format-tagged value fails.
UnacyErrorBase error class for all unacy errors.

Formatters

Type AliasDescription
FormatterFormatter converts a format-tagged value to a string representation.
FormatterParserPaired formatter/parser for round-trip format transformations.
ParserParser converts a plain string into a format-tagged value with validation.

Metadata

NameDescription
UnitMetadataDisplay and descriptive metadata that can be attached to units in the registry.
BaseMetadataBase metadata type that all unit metadata must extend. Requires a name property and allows arbitrary additional properties.
TypedMetadataMetadata type for units with explicit type information.

Other

Type AliasDescription
InferFromRecordSchemaInfer TypeScript type from a RecordSchema. Recursively processes nested schemas.
InferFromTupleSchemaInfer TypeScript type from a TupleSchema. Handles optional (?) and rest (...) elements.
PrimitiveTypeFromNameMap type name strings to TypeScript primitive types.
ToPrimitiveTypeNameMap a primitive TypeScript type to its corresponding type name string. For example, number'number', string'string', boolean'boolean', bigint'bigint'. Returns never for non-primitive types.

Registry

NameDescription
UnitRegistryRegistry for managing and composing unit converters.
UnitAccessorCallable accessor object returned per unit from the registry.
UnitMapA map of unit name → UnitAccessor for all registered source units.
createRegistryCreate a new, empty converter registry.

Types

Type AliasDescription
ClassTypeA class constructor (including abstract classes) that can serve as a unit's type identity. At runtime, the constructor itself is stored in the metadata type field.
EnumTypeA TypeScript enum object at runtime — an object whose values are all strings (string enum) or all numbers (numeric enum). Mixed enums (both string and number values) are rejected at validation.
PrimitiveTypePrimitive JavaScript types that can be used as unit base types.
RecordSchemaA schema describing an object shape. Keys are property names; values are primitive type name strings ('number', 'string', etc.) or nested RecordSchema objects.
SupportedTypeUnion of all types that can be used as a unit's base type. Includes primitives and non-primitive categories (enum, class, record, tuple).
TupleSchemaA schema describing a tuple as an array of primitive type name strings. Supports optional ('number?') and rest ('...number') modifiers.

Validation

FunctionDescription
createParserWithSchemaCreate a Parser<WithFormat<T, F>> backed by a Zod-compatible schema.
detectMetadataKindDetect the kind of a metadata object by inspecting its type field.
isClassMetadataType guard: returns true when meta.type is a class constructor.
isEnumMetadataType guard: returns true when meta.type is an enum object.
isRecordMetadataType guard: returns true when meta.type is a record schema object.
isTupleMetadataType guard: returns true when meta.type is a tuple schema array.
validateClassValidate that a runtime value is a valid class constructor.
validateEnumValidate that a runtime value is a valid TypeScript enum object.
validateRecordSchemaValidate that a runtime value is a valid record schema.
validateTupleSchemaValidate that a runtime value is a valid tuple schema.

Released under the MIT License.