Skip to main content

Validation, Time, Events, and Security

Date and Time

Built on a pre-configured dayjs (zh-CN locale, localizedFormat / customParseFormat / duration / relativeTime plugins).

ExportSignatureDescription
Dayjstype Dayjs = dayjs.DayjsRe-export of dayjs's instance type.
TemporalMode"minute" | "hour" | "time" | "date" | "datetime" | "week" | "month" | "quarter" | "year"Temporal-picker granularity.
DEFAULT_DATE_FORMAT"YYYY-MM-DD"Default date format constant.
DEFAULT_TIME_FORMAT"HH:mm:ss"Default time format constant.
DEFAULT_DATETIME_FORMAT"YYYY-MM-DD HH:mm:ss"Default datetime format constant.
LOCALIZED_DATE_FORMAT"LLdddd"dayjs localized-format token for a full date + weekday.
LOCALIZED_DATETIME_FORMAT"LLLL"dayjs localized-format token for a full datetime.
formatDuration(value: number, unit?: DurationUnitType) => stringFormats a duration (default unit "seconds") as a human-readable Chinese string, e.g. formatDuration(3600)"1小时0分钟".
parseDate(date: string | Date, format?: string) => DayjsParses a date string or Date into a Dayjs instance; format is ignored for Date inputs, and without it dayjs's built-in parser is used (ISO 8601 and common shapes). May return an invalid Dayjs on bad input — use tryParseDate when the shape is unknown.
tryParseDate(date: string) => Dayjs | nullParses a date string by strict-matching every known datetime format, then every date format ("YYYY-MM-DD", "YYYY/MM/DD", "YYYYMMDD", two-digit-year variants, …), finally falling back to dayjs's default parser; returns null instead of an invalid Dayjs when everything fails.
tryParseTime(time: string) => Dayjs | nullParses a bare time-of-day string that dayjs's default parser would reject, strict-matching the time ("HH:mm:ss", "HH.mm.ss", "HHmmss"), minute ("HH:mm", "HH.mm", "HHmm"), and hour ("HH") formats; returns null on failure.
formatDate(date: Dayjs, format?: string) => stringFormats a Dayjs instance (default format DEFAULT_DATETIME_FORMAT).
getNow() => DayjsReturns the current time as a Dayjs instance.
getNowDateString() => stringCurrent date as "YYYY-MM-DD".
getNowTimeString() => stringCurrent time as "HH:mm:ss".
getNowDateTimeString() => stringCurrent datetime as "YYYY-MM-DD HH:mm:ss".
getLocalizedDateTime(includeTime?: boolean) => stringCurrent datetime in the zh-CN localized full format (default includeTime: true).
getTemporalFormats<T extends TemporalMode>(mode: T) => readonly string[]Returns the accepted parse-format patterns for a TemporalMode (e.g. getTemporalFormats("date")["YYYY-MM-DD", "YYYY/MM/DD", …]).

Formatting

ExportSignatureDescription
formatBytes(bytes: number, decimals?: number) => stringFormats a byte count with a binary (1024-based) B/KB/MB/GB/TB/PB unit suffix, e.g. formatBytes(1536, 1)"1.5 KB" (default decimals: 2; trailing zeros trimmed; 0"0 B").
formatNumber(num: number, decimals?: number) => stringFormats a large number with a decimal (1000-based) K/M/B/T unit suffix, e.g. formatNumber(1234567)"1.23 M" (default decimals: 2). Numbers below 1000 are returned as-is without a suffix.

Equality

ExportSignatureDescription
isShallowEqual(value1: unknown, value2: unknown) => booleanShallow equality: primitives via Object.is, one level deep for Map/Set/plain objects/arrays.
isDeepEqual(value1: unknown, value2: unknown) => booleanDeep equality with circular-reference support; special-cases Date, RegExp, Set, Map, ArrayBuffer, typed arrays, and objects with custom valueOf/toString.

Error and Stack Helpers

Built on stacktrace-js.

ExportSignatureDescription
StackFrametype StackFrame = stackTrace.StackFrameRe-export of stacktrace-js's frame type.
parseErrorStack(error: Error, filter?: (frame: StackFrame) => boolean) => Promise<StackFrame[]>Parses an Error's stack into frames, optionally filtered.
filterUserFrame(stackFrame: StackFrame) => booleanFrame filter that excludes node_modules frames.
getSanitizedErrorStack(error: Error) => Promise<string>Formats an error's stack as user-code-only text (filterUserFrame applied), one at fn (file:line:col) line per frame.
getCurrentStack(filter?: (frame: StackFrame) => boolean) => Promise<StackFrame[]>Captures the current call stack.
getCurrentStackSync(filter?: (frame: StackFrame) => boolean) => StackFrame[]Synchronous version of getCurrentStack.

Event Bus

Built on mitt.

ExportSignatureDescription
EventEmitter<TEvents>class EventEmitter<TEvents extends Record<EventType, any>>Type-safe event emitter (see the member table below).
createEventEmitter<TEvents>() => EventEmitter<TEvents>Factory for a new EventEmitter instance.
EventHandlertype EventHandler<T> = Handler<T> (from mitt)The listener function type accepted by EventEmitter.on.
EventTypetype EventType (from mitt)The event-key constraint (string | symbol) used by EventEmitter.

EventEmitter members:

MemberSignatureDescription
on(eventType, eventListener) => () => voidSubscribes; returns the unsubscribe function.
emit(eventType) / (eventType, eventPayload)Emits an event. The payload-less overload is only accepted when TEvents[eventType] allows undefined.
off(eventType, eventListener?) => voidRemoves one listener — or all listeners of that type when eventListener is omitted.
clear() => voidRemoves every listener from every event.
getAllListeners() => EventHandlerMap<TEvents>Returns the underlying mitt handler map (for debugging).

Security Helpers

ExportSignatureDescription
obfuscateEncode(plainText: string) => Uint8ArrayXOR-obfuscates a string with a fixed 16-byte key; not cryptographic security, only casual obfuscation.
obfuscateDecode(encoded: Uint8Array | number[]) => stringReverses obfuscateEncode.
obfuscateEncodeToHex(plainText: string) => stringobfuscateEncode, hex-encoded.
obfuscateDecodeFromHex(hexString: string) => stringReverses obfuscateEncodeToHex.
obfuscateEncodeToBase64(plainText: string) => stringobfuscateEncode, base64-encoded.
obfuscateDecodeFromBase64(base64String: string) => stringReverses obfuscateEncodeToBase64.
encryptUsingRSA(value: string, publicKey: string) => stringRSA-encrypts (via jsencrypt) a value with a PEM public key; throws on invalid input or encryption failure.
decryptUsingRSA(value: string, privateKey: string) => stringRSA-decrypts a value with a PEM private key; throws on invalid input or decryption failure.

Zod Re-Exports

A single project-wide zod instance, pre-configured with the zh-CN locale (z.config(zhCN())), re-exported so every package shares the same configured instance.

ExportDescription
zThe configured Zod namespace/factory.
ZodErrorZod's validation error type.
ZodIssueA single validation issue within a ZodError.
ZodSchemaThe base schema type.
ZodTypeThe base type for all Zod types.