idea-toolbox
Version:
IDEA's utility functions
92 lines (91 loc) • 3.87 kB
TypeScript
import { isEmptyFieldTypes } from './utils';
/**
* An abstract class to inherit to manage a resource model.
*/
export declare abstract class Resource {
/**
* Object initialization, setting all the default values.
* @param newData the data to load, optional
* @param options custom options to apply; they will depend on the child resource
*
* Usually, there is no need to implement the constructor; implicitly, it will call the `load` of the child resource
* and therefore loading all the resources with default values.
* If needed, this is the suggested implementation:
* ```
* super();
* this.load(x);
* // ...
* ```
*/
constructor(newData?: any, options?: any);
/**
* Load the attributes from an already existing resource.
* @param newData the data to load
* @param options custom options to apply; they will depend on the child resource
*
* Typical implementation:
* ```
* super.load(newData);
* this.attr = this.clean(newData.attr, String);
* this.attr2 = this.clean(newData.attr2, Number, 0);
* this.attr3 = this.clean(newData.attr3, a => new Date(a), Date.now());
* this.arr = this.cleanArray(arr, String);
* // ...
* ```
*/
load(newData: any, options?: any): void;
/**
* Load the attributes from an already existing resource and then force some attributes to assume _safeData_ values.
* The function is usually used in the back-end to mix together db data with new data, without the risk of changing
* ids and other attributes which are managed in appositely curated scenario.
* @param newData the data to load
* @param safeData the attributes to force to specific values
* @param options custom options to apply; they will depend on the child resource
*
* Typical implementation:
* ```
* super.safeLoad(newData, safeData);
* this.keyAttr = safeData.keyAttr;
* this.importantAttr = safeData.importantAttr;
* this.isDraft = safeData.isDraft;
* // ...
* ```
* _Note well_: there is no need to call `this.load()`, since it's implicitly called from `super.safeLoad()`,
* which will anyway use the child version of the method.
*/
safeLoad(newData: any, safeData: any, options?: any): void;
/**
* Valide the object's attributes, performing all the checkings.
* @param options custom options to apply; they will depend on the implementations
* @return errors if empty, the checkings are successfully passed.
*
* Typical implementation:
* ```
* const e = super.validate();
* if(this.iE(this.attr)) e.push(`attr`);
* // ...
* return e;
* ```
*/
validate(options?: any): string[];
/**
* Shortcut to Utils.isEmpty to check the emptiness of a field.
*/
iE(field: any, type?: isEmptyFieldTypes): boolean;
/**
* Return an attribute in a cleaned standard that force-cast the element.
* @param origin the origin attribute, to cast
* @param castFunction the cast function, e.g. `Boolean`, `Number`, `String`, `x => new CustomClass(x)`, etc.
* @param defaultVal if set, the fallback value instead of `null`
* @return cleaned attribute
*/
clean(origin: any, castFunction: (x: any) => any, defaultVal?: any): any;
/**
* Return an array in a cleaned standard that force-cast each element, keeping only the valid ones.
* @param origin the origin array, to cast and check
* @param castFunction the cast function, e.g. `x => String(x)` or `x => new CustomClass(x)`
* @param defaultVal if set, the fallback value instead of `null`
* @return cleaned array
*/
cleanArray(origin: any[], castFunction: (x: any) => any, defaultVal?: any): any[];
}