castium
Version:
A lightweight and chainable utility for effortless data type conversation in JavaScript and Node.js
34 lines (32 loc) • 1.29 kB
TypeScript
declare class Castium<T> {
private value;
constructor(value: T);
number(): Castium<number | null>;
string(): Castium<string>;
boolean(): Castium<boolean>;
isEqual(expected: T): Castium<boolean>;
booleanString(): Castium<boolean | null>;
date(): Castium<Date | null>;
isoDate(): Castium<string | null>;
fromDate(): Castium<Date | null>;
toDate(): Castium<Date | null>;
dateTime(): Castium<number | null>;
array(): Castium<any[] | null>;
object(): Castium<object | null>;
nullable(): Castium<T | null>;
undefined(): Castium<T | undefined>;
default<U>(defaultValue: U): Castium<U | Exclude<T, null | undefined>>;
transform<U, D = null>(fn: (value: T) => U, defaultValue?: D): Castium<U | D>;
json<R extends object>(): Castium<R | null>;
match(regex: RegExp): Castium<boolean>;
oneOf(...values: (T | T[])[]): Castium<boolean>;
clamp(min: number, max: number): Castium<number>;
enum<E>(enumObj: Record<string, E>): Castium<E | null>;
shape<R extends object>(schema: {
[K in keyof R]: ((value: T[K]) => R[K]) | R[K];
}): Castium<R>;
when(predicate: boolean | ((value: T) => boolean)): Castium<T | null>;
get(): T;
}
declare const c: <T>(value: T) => Castium<T>;
export { c };