t0n
Version:
Collection of elegant typescript resources for web artisans
86 lines (79 loc) • 2.93 kB
TypeScript
type VALUE<T = any> = T | null | undefined | Symbol;
declare class Attempt<T = any> {
static readonly UNDEFINED: unique symbol;
static resolve<T>(callback: () => T, defaultValue?: T): Attempt<T>;
static defined(value: any): boolean;
static undefined(value: any): boolean;
static isUnset(value: any): boolean;
private value;
private _default?;
constructor(value: VALUE<T>, defaultValue?: T);
or(callback: () => T): Attempt<T>;
default(defaultValue: T): this;
tap(callback?: (value: T | null | undefined) => void): this;
get(): T;
getOrThrow(error?: Error): T;
}
declare class Base64 {
static encode(data: any): string;
static decode(data: string): string;
static toString(data: any, type?: string): string | Uint8Array | ArrayBuffer;
}
declare class UrlSafeBase64 {
static encode(data: any): string;
static decode(data: string): string;
}
interface Baggable<T = any> {
all(): Record<string, T>;
keys(): string[];
has(key: string): boolean;
add(data: Record<string, T>): void;
replace(data: Record<string, T>): void;
get(key: string, defaultValue?: T): T;
remove(key: string): void;
set(key: string, value: T): void;
clear(): void;
values(): T[];
toArray(): [string, T][];
toJson(options?: number): string;
}
declare class DataBag<T = any> implements Baggable<T> {
protected data: Record<string, T>;
constructor(data?: Record<string, T>);
all(): Record<string, T>;
has(key: string): boolean;
add(data?: Record<string, T>): void;
replace(data?: Record<string, T>): void;
get(key: string, defaultValue?: T): T;
set(key: string, value: T): void;
remove(key: string): void;
clear(): void;
get length(): number;
get size(): number;
keys(): string[];
values(): T[];
toArray(): [string, T][];
jsonSerialize(): Record<string, T>;
toJson(options?: number): string;
[Symbol.iterator](): IterableIterator<[string, T]>;
}
declare class Timestamp {
static timestamp(date?: number | Date | null): number;
static minuteToSecond(date?: number | Date | null): number;
static strToDate(str: string): Date;
static now(): number;
static addSeconds(timestamp: number, seconds: number): number;
static addMinutes(timestamp: number, minutes: number): number;
static addHours(timestamp: number, hours: number): number;
static addDays(timestamp: number, days: number): number;
}
declare class Envir {
private static _memory;
private static memory;
static has(key: string): boolean;
static get<T = any>(key: string, defaultValue?: T): T | undefined;
static set<T = any>(key: string, value: T): void;
static remove(key: string): void;
}
declare function toInt(data: any[] | string | number): number;
export { Attempt, type Baggable, Base64, DataBag, Envir, Timestamp, UrlSafeBase64, type VALUE, toInt };