UNPKG

protoobject

Version:

A universal class for creating any JSON objects and simple manipulations with them.

144 lines (143 loc) 5.43 kB
import { UnknownObject } from "../types/unknown-object"; import { ProtoObjectDynamicMethods } from "../types/dynamic-methods"; import { ValidatorFunction } from "../types/validator-function"; /** * A universal class for creating any JSON objects and simple manipulations with them. */ export declare class ProtoObject<T extends ProtoObjectDynamicMethods<T>> implements ProtoObjectDynamicMethods<T> { /** * * @param data - ProtoObject or its heir properties * @returns - the ProtoObject or its heir */ constructor(data?: Partial<T>); /** * Get all properties of an object and its prototypes * * @param data - any non-null object, such as an instance of the ProtoObject class or its heir * @returns - an array of the properties information */ static getProperties(data: { [key: PropertyKey]: unknown; }): { key: PropertyKey; value: unknown; descriptor: PropertyDescriptor; }[]; /** * Get all enumerable properties of an object and its prototypes * * @param data - any non-null object, such as an instance of the ProtoObject class or its heir * @param options - options for the returned array of properties * @param options.onlyWritable - return only writable properties * @returns - an array of the properties information */ static getEnumerableProperties<T extends ProtoObject<T>>(data: { [key: PropertyKey]: unknown; }, options?: { onlyWritable: boolean; }): { key: PropertyKey; value: unknown; descriptor: PropertyDescriptor; }[]; /** * A recursive function for assigning properties to an object or returning a property * if it is not interchangeable * * WARN: The first transferred object will be changed. * * @param obj - an object such as a ProtoObject or its heir or an object property * @param data - an object such as a ProtoObject or its heir or an object property * @returns - an object such as a ProtoObject or its heir or an object property */ static recursiveAssign<T extends ProtoObject<T>, K>(obj: unknown, data?: unknown): K; /** * Deep assign data to an instance of the ProtoObject class or its heir * * @param obj - a ProtoObject class or its heir * @param data - a ProtoObject class or its heir or any other object * @returns - a assigned ProtoObject class or its heir */ static deepAssign<T extends ProtoObject<T>>(obj: T, data?: Partial<T>): T; /** * The converter of values into simple types * * @param data - a value to convert to a simple type * @returns - a simple type */ static valueToJSON(data: unknown): unknown; /** * The converter of simple types into values * * @param data - a simple type to convert to a value * @returns - a value */ static valueFromJSON(data: unknown): unknown; /** * A method for converting a simple json to ProtoObject class or its heir * * @param data - a simple json data * @returns - a ProtoObject class or its heir */ static fromJSON<T extends ProtoObject<T>>(data: { [key: string]: unknown; }): T; /** * A method for converting a ProtoObject class or its heir to simple json * * @returns - a simple json */ toJSON(): { [key: string]: any; }; /** * A method for converting a ProtoObject class or its heir to a string * * @returns - string */ toString(): string; /** * Copying a ProtoObject class or its heirs * * @returns - a deep copy of the ProtoObject object or its heir */ copy(): T; /** * Deep assign data to an instance of the ProtoObject class or its heir * * @param data - a ProtoObject class or its heir or any other object * @returns - a assigned ProtoObject class or its heir */ assign(data: Partial<T>): T; /** * Factory for creating a data transformer for the ProtoObject class or its heir * * @param param0 - data validators * @param param0.validatorTo - data validator when converting to a simple JSON object * @param param0.validatorFrom - data validator when converting to a class * @returns data transformer for the ProtoObject class or its heir */ static recordTransformer<T extends ProtoObject<T>>({ validatorTo, validatorFrom, }: { validatorTo?: ValidatorFunction<T>; validatorFrom?: ValidatorFunction<UnknownObject>; }): { to(obj: unknown): UnknownObject | undefined; from(json: unknown): T | undefined; }; /** * Factory for creating a data transformer for the array of ProtoObject classes or its heirs * * @param param0 - data validators * @param param0.validatorTo - data validator when converting to a simple JSON object * @param param0.validatorFrom - data validator when converting to a class * @returns data transformer for the array of the ProtoObject classes or its heirs */ static collectionTransformer<T extends ProtoObject<T>>({ validatorTo, validatorFrom, }: { validatorTo?: ValidatorFunction<T>; validatorFrom?: ValidatorFunction<UnknownObject>; }): { to(objArr: unknown): UnknownObject[] | undefined; from(jsonArr: unknown): T[] | undefined; }; }