ok-tools
Version:
My personal JS tools and utilities
76 lines (75 loc) • 2.5 kB
TypeScript
export declare function number(input: any): number;
export declare function string(input: any): string;
export declare function boolean(input: any): boolean;
export declare function date(input: any, returnNullOnError?: boolean): Date | null;
export interface DefinitionForFactory {
default?: any;
number?: string[];
string?: string[];
date?: string[];
boolean?: string[];
/**
* Use data from original key in source for another key in created object
*
* from: { "target": "source" } means that source's "source" property will be copied into returned object's "target" property.
*
* Also, you can supply a function that receives the source object and returns expected value.
*/
from?: {
[key: string]: (string | ((from: any) => any));
};
/**
* Property should be object. Supply their own DefinitionForFactory.
* Similar to subItem, but here, give just a definition of the subobject.
*/
object?: {
[key: string]: DefinitionForFactory;
};
/**
* Property should be array of objects. Supply their own DefinitionForFactory.
* Similar to subItem, but here, give just a definition of the subobjects.
*/
objects?: {
[key: string]: DefinitionForFactory;
};
/**
* Property should be object. Apply a function for every its property to normalize it.
*/
objectMap?: {
[key: string]: (from: any) => any;
};
/**
* Property should be object. Apply a factory function for it.
* Similar to object, but here, give a factory function.
*/
subItem?: {
[key: string]: (from: any) => any;
};
/**
* Property should be an array of objects. Apply a factory function to each item.
*/
subItems?: {
[key: string]: (from: any) => any;
};
/**
* After applying all other rules and checks, apply a map function to resulting values.
*/
map?: {
[key: string]: (from: any) => any;
};
/**
* The value must be in array of given options. It also normalizes strings/numbers.
*/
enum?: {
[key: string]: (string | number)[];
};
/**
* Anything can be in there properties. Do not check anything.
*/
any?: string[];
/**
* Parent factory function, useful when extending parent interfaces
*/
extends?: (from: any) => any;
}
export declare function factory<T = any>(input: any, definitions: DefinitionForFactory): T;