hadron-type-checker
Version:
Hadron Type Checker
93 lines • 3.91 kB
TypeScript
import { ObjectId, MinKey, MaxKey, Long, Double, Int32, Decimal128, Binary, BSONRegExp, Code, BSONSymbol, Timestamp, bsonType, type BSONTypeTag } from 'bson';
export { bsonType, type BSONTypeTag };
export declare function getBsonType(value: any): BSONTypeTag | undefined;
export type TypeCastMap = {
Array: unknown[];
Binary: Binary;
Boolean: boolean;
Code: Code;
Date: Date;
Decimal128: Decimal128;
Double: Double;
Int32: Int32;
Int64: Long;
MaxKey: MaxKey;
MinKey: MinKey;
Null: null;
Object: Record<string, unknown>;
ObjectId: ObjectId;
BSONRegExp: BSONRegExp;
String: string;
BSONSymbol: BSONSymbol;
Timestamp: Timestamp;
Undefined: undefined;
UUID: Binary;
LegacyJavaUUID: Binary;
LegacyCSharpUUID: Binary;
LegacyPythonUUID: Binary;
};
export type TypeCastTypes = keyof TypeCastMap;
export declare const UUID_TYPES: ("UUID" | "LegacyJavaUUID" | "LegacyCSharpUUID" | "LegacyPythonUUID")[];
export type UUIDType = (typeof UUID_TYPES)[number];
export declare function isUUIDType(type: string): type is UUIDType;
/**
* UUID regex pattern for validation (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx).
*/
export declare const UUID_REGEX: RegExp;
/**
* Converts a hex string (without hyphens) to UUID format with hyphens.
*/
export declare const uuidHexToString: (hex: string) => string;
/**
* Reverses byte order for Java legacy UUID format (both MSB and LSB).
* Takes a 32-char hex string and returns a 32-char hex string with reversed byte order.
*
* This function is an involution (self-inverse), meaning applying it twice returns
* the original value. It can be used both to:
* - Convert from Java legacy binary format to standard UUID hex (for display)
* - Convert from standard UUID hex to Java legacy binary format (for storage)
*/
export declare const reverseJavaUUIDBytes: (hex: string) => string;
/**
* Reverses byte order for C# legacy UUID format (first 3 groups only).
* Takes a 32-char hex string and returns a 32-char hex string with reversed byte order.
*
* This function is an involution (self-inverse), meaning applying it twice returns
* the original value. It can be used both to:
* - Convert from C# legacy binary format to standard UUID hex (for display)
* - Convert from standard UUID hex to C# legacy binary format (for storage)
*/
export declare const reverseCSharpUUIDBytes: (hex: string) => string;
/**
* Converts a Binary UUID from one encoding to another.
* This is used when changing between UUID types in the document editor.
*
* @param binary - The source Binary UUID
* @param sourceType - The source UUID type (e.g., 'LegacyCSharpUUID')
* @param targetType - The target UUID type (e.g., 'LegacyJavaUUID')
* @returns A new Binary with the same UUID value but different encoding
*/
export declare const convertBinaryUUID: (binary: Binary, sourceType: string, targetType: string) => Binary;
/**
* Checks the types of objects and returns them as readable strings.
*/
declare class TypeChecker {
/**
* Cast the provided object to the desired type.
*/
cast<O = unknown, T extends string = string>(object: O, type: T): T extends TypeCastTypes ? TypeCastMap[T] : O;
/**
* Get the type for the object.
* @param legacyUUIDEncoding - Optional encoding for legacy UUID (subtype 3).
* If provided and the object is a Binary with subtype 3, returns the specific legacy UUID type.
* Valid values: 'LegacyJavaUUID', 'LegacyCSharpUUID', 'LegacyPythonUUID'
*/
type(object: unknown, legacyUUIDEncoding?: 'LegacyJavaUUID' | 'LegacyCSharpUUID' | 'LegacyPythonUUID'): TypeCastTypes;
/**
* Get a list of types the object can be cast to.
*/
castableTypes(highPrecisionSupport?: boolean): TypeCastTypes[];
}
declare const _default: TypeChecker;
export default _default;
//# sourceMappingURL=type-checker.d.ts.map