UNPKG

@typescript-package/descriptor

Version:

A lightweight TypeScript library for property descriptor.

105 lines (104 loc) 8.17 kB
import { CommonDescriptor } from './common-descriptor.abstract'; import { DataPropertyDescriptor } from '@typedly/descriptor'; import { ValidationCallback } from '@typedly/callback'; /** * @description The `DataDescriptor` class is a concrete implementation of the `CommonDescriptor` class that represents a data property descriptor. * @export * @class DataDescriptor * @template [O=any] The type of the object. * @template {PropertyKey | keyof O} [K=keyof O] The type of property key. * @template [V=K extends keyof O ? O[K] : any] The type of value captured from the object. * @template {boolean} [C=boolean] The type of the configurable property. * @template {boolean} [E=boolean] The type of the enumerable property. * @template {boolean} [W=boolean] The type of the writable property. * @extends {CommonDescriptor<C, E>} */ export declare class DataDescriptor<O = any, K extends PropertyKey | keyof O = keyof O, V = K extends keyof O ? O[K] : any, C extends boolean = boolean, E extends boolean = boolean, W extends boolean = boolean> extends CommonDescriptor<C, E> implements DataPropertyDescriptor<V, C, E, W> { /** * @description Creates an instance of `DataDescriptor`. * @public * @static * @template [O=any] The type of the object. * @template {PropertyKey | keyof O} [K=keyof O] The type of the property key. * @template [V=K extends keyof O ? O[K] : any] The type of the value captured from the object. * @template {boolean} [C=boolean] The type of configurable. * @template {boolean} [E=boolean] The type of enumerable. * @template {boolean} [W=boolean] The type of writable. * @param {DataPropertyDescriptor<V, C, E, W>} param0 Data descriptor properties. * @param {DataPropertyDescriptor<V, C, E, W>} param0.configurable The configurable property. * @param {DataPropertyDescriptor<V, C, E, W>} param0.enumerable The enumerable property. * @param {DataPropertyDescriptor<V, C, E, W>} param0.value The value for data descriptor. * @param {DataPropertyDescriptor<V, C, E, W>} param0.writable The writable property. * @param {?O} [object] The object (non-stored) to define the descriptor on. The object is optional, if not provided the descriptor will be created without an object. * @param {?K} [key] The property key to define the descriptor on. The key is optional, if not provided the descriptor will be created without a key. * @param {?ValidationCallback<DataPropertyDescriptor<V, C, E, W>>} [onValidate] An optional validation callback to validate the descriptor. * @returns {(DataDescriptor<O, K, V, C, E, W> | undefined)} The returned */ static create<O = any, K extends PropertyKey | keyof O = keyof O, V = K extends keyof O ? O[K] : any, C extends boolean = boolean, E extends boolean = boolean, W extends boolean = boolean>({ configurable, enumerable, value, writable }: DataPropertyDescriptor<V, C, E, W>, object?: O, key?: K, onValidate?: ValidationCallback<DataPropertyDescriptor<V, C, E, W>>): DataDescriptor<O, K, V, C, E, W> | undefined; /** * @description Returns strictly defined data descriptor of a `DataPropertyDescriptor<V, C, E, W>` interface on `writable` or `value` property detected. * @public * @static * @template [O=any] The type of the object. * @template {PropertyKey | keyof O} [K=keyof O] The type of the object key. * @template [V=K extends keyof O ? O[K] : any] The type of value. * @template {boolean} [C=boolean] The type of configurable. * @template {boolean} [E=boolean] The type of enumerable. * @template {boolean} [W=boolean] The type of writable. * @param {DataPropertyDescriptor<V, C, E, W>} param0 An `object` of a `DataPropertyDescriptor<V, C, E, W>` interface, to set with the default values of the `CommonDescriptor`. * @param {DataPropertyDescriptor<V, C, E, W>} param0.configurable The descriptor configurable. * @param {DataPropertyDescriptor<V, C, E, W>} param0.enumerable The descriptor enumerable. * @param {DataPropertyDescriptor<V, C, E, W>} param0.value The descriptor value. * @param {DataPropertyDescriptor<V, C, E, W>} param0.writable The descriptor writable. * @param {?O} [object] The object (non-stored) to define the descriptor on. The object is optional, if not provided the descriptor will be created without an object. * @param {?K} [key] The (non-stored) key to define the descriptor on. * @param {?ValidationCallback<DataPropertyDescriptor<V, C, E, W>>} [onValidate] An optional `ResultCallback` function to handle the result of the check whether or not the `descriptor` is an `object` with the `writable` or `value` property, by default it uses `dataCallback()` function from the static `guard()` method. * @returns {(DataPropertyDescriptor<V, C, E, W> | undefined)} The return value is an `object` of a `DataPropertyDescriptor<V, C, E, W>` interface. */ static define<O = any, K extends PropertyKey | keyof O = keyof O, V = K extends keyof O ? O[K] : any, C extends boolean = boolean, E extends boolean = boolean, W extends boolean = boolean>({ configurable, enumerable, value, writable }: DataPropertyDescriptor<V, C, E, W>, object?: O, key?: K, onValidate?: ValidationCallback<DataPropertyDescriptor<V, C, E, W>>): DataPropertyDescriptor<V, C, E, W> | undefined; /** * @description Guards the `descriptor` to be an `object` of a `DataPropertyDescriptor<Value>` interface. * @public * @static * @template V The type of the value. * @template {boolean} [C=boolean] The type of configurable. * @template {boolean} [E=boolean] The type of enumerable. * @template {boolean} [W=boolean] The type of writable. * @param {DataPropertyDescriptor<V, C, E, W>} descriptor Object of a `DataPropertyDescriptor<Value>` interface to guard. * @param {?ValidationCallback<DataPropertyDescriptor<V, C, E, W>>} [callbackFn] A `ResultCallback` function to handle the result of the check whether or not the `descriptor` is an `object` with the `writable` or `value` property, by default it uses `dataCallback()` function. * @returns {descriptor is DataPropertyDescriptor<V, C, E, W>} The return value is a `boolean` indicating whether the `descriptor` is an `object` with the `writable` or `value` property. */ static guard<V, C extends boolean = boolean, E extends boolean = boolean, W extends boolean = boolean>(descriptor: DataPropertyDescriptor<V, C, E, W>, callbackFn?: ValidationCallback<DataPropertyDescriptor<V, C, E, W>>): descriptor is DataPropertyDescriptor<V, C, E, W>; /** * @description Defaults to writable. * @public * @static * @type {?boolean} */ static writable?: boolean; /** * @description The value of the descriptor. * @public * @type {?V} */ value?: V; /** * @description The writable of the descriptor. * @public * @type {?W} */ writable?: W; /** * Creates an instance of `DataDescriptor`. * @constructor * @param {DataPropertyDescriptor<V, C, E, W>} param0 Data descriptor properties. * @param {DataPropertyDescriptor<V, C, E, W>} param0.configurable The configurable of the descriptor. * @param {DataPropertyDescriptor<V, C, E, W>} param0.enumerable Enumerable of the descriptor. * @param {DataPropertyDescriptor<V, C, E, W>} param0.value The value for data descriptor. * @param {DataPropertyDescriptor<V, C, E, W>} param0.writable The writable for data descriptor. * @param {?O} [object] The object (non-stored) to define the descriptor on. The object is optional, if not provided the descriptor will be created without an object. * @param {?K} [key] The (non-stored) key to define the descriptor on. * @param {?ValidationCallback<DataPropertyDescriptor<V, C, E, W>>} [onValidate] Optional callback function to determine the validity of the descriptor. */ constructor({ configurable, enumerable, value, writable }: DataPropertyDescriptor<V, C, E, W>, object?: O, key?: K, onValidate?: ValidationCallback<DataPropertyDescriptor<V, C, E, W>>); }