@typescript-package/descriptor-chain
Version:
A lightweight TypeScript library for property descriptor chain.
335 lines (328 loc) • 8.61 kB
JavaScript
import { Descriptor } from '@typescript-package/descriptor';
/**
* @description The core abstract class for descriptor chains to store related property descriptors.
* @export
* @abstract
* @class DescriptorChainCore
* @template [O=any] The type of the object that the property descriptors are associated with.
* @template {keyof O} [K=keyof O] The type of the property name in the object.
* @template {K extends keyof O ? O[K] : any} [V=K extends keyof O ? O[K] : any] The value type of the property in the object.
* @template {boolean} [A=boolean] The type of the active state of the descriptor.
* @template {boolean} [N=boolean] The enabled state of the descriptor.
* @template {boolean} [C=boolean] The configurable state of the descriptor.
* @template {boolean} [E=boolean] The enumerable state of the descriptor.
* @template {WrappedPropertyDescriptor<O, K, V, A, N, C, E, D>} [D=WrappedPropertyDescriptor<O, K, V, A, N, C, E, any>] The wrapped property descriptor type.
* @implements {PropertyDescriptorChain<O, K, V, A, N, C, E, D>}
*/
class DescriptorChainCore {
}
// Abstract.
/**
* @description The base abstraction class representing a chain of property descriptors.
* @export
* @abstract
* @class DescriptorChainBase
* @template [O=any] The type of the object that the property descriptors are associated with.
* @template {keyof O} [K=keyof O] The type of the property name in the object.
* @template {K extends keyof O ? O[K] : any} [V=K extends keyof O ? O[K] : any] The type of the value accessed by the property.
* @template {boolean} [A=boolean] The type of active property.
* @template {boolean} [N=boolean] The type of enabled property.
* @template {boolean} [C=boolean] The type of configurable property.
* @template {boolean} [E=boolean] The type of enumerable property.
* @template {WrappedPropertyDescriptor<O, K, V, A, N, C, E, D>} [D=WrappedPropertyDescriptor<O, K, V, A, N, C, E, any>] The type of wrapped property descriptor.
* @extends {DescriptorChainCore<O, K, V, A, N, C, E, D>}
*/
class DescriptorChainBase extends DescriptorChainCore {
/**
* @inheritdoc
*/
get active() {
return this.#active;
}
/**
* @inheritdoc
*/
get current() {
return this.#data[this.currentIndex];
}
/**
* @inheritdoc
*/
get currentIndex() {
return this.#currentIndex;
}
/**
* @inheritdoc
*/
get data() {
return this.#data;
}
/**
* @inheritdoc
*/
get descriptor() {
return this.#descriptor;
}
/**
* @inheritdoc
*/
get enabled() {
return this.#enabled;
}
/**
* @inheritdoc
*/
get key() {
return this.#key;
}
/**
* @inheritdoc
*/
get lastIndex() {
return this.#data.length - 1;
}
/**
* @inheritdoc
*/
get nextIndex() {
return typeof this.currentIndex === 'number' ? this.currentIndex + 1 : undefined;
}
/**
* @inheritdoc
*/
get object() {
return this.#object;
}
/**
* @inheritdoc
*/
get next() {
return typeof this.nextIndex === 'number' ? this.#data[this.nextIndex] : undefined;
}
/**
* @inheritdoc
*/
get previous() {
return typeof this.previousIndex === 'number' ? this.#data[this.previousIndex] : undefined;
}
/**
* @inheritdoc
*/
get previousIndex() {
return typeof this.currentIndex === 'number' ? this.currentIndex - 1 : undefined;
}
/**
* @inheritdoc
*/
get size() {
return this.#data.length;
}
/**
* @description Privately stored active state.
* @type {A}
*/
#active = true;
/**
* @description
* @type {new (
* object: O,
* key: K,
* attributes: WrappedPropertyDescriptor<O, K, V, A, N, C, E, D>,
* ) => D}
*/
#descriptor;
/**
* @description Privately stored enabled state.
* @type {N}
*/
#enabled = true;
/**
* @description Privately stored current index.
* @type {number}
*/
#currentIndex = 0;
/**
* @description Privately stored data array.
* @type {D[]}
*/
#data = new Array();
/**
* @description Privately stored key.
* @type {K}
*/
#key;
/**
* @description Privately stored object.
* @type {O}
*/
#object;
/**
* Creates an instance of `DescriptorChainBase` child class.
* @constructor
* @param {O} object The object containing the property.
* @param {K} key The key of the property.
* @param {new (
* object: O,
* key: K,
* attributes: WrappedPropertyDescriptor<O, K, V, A, N, C, E, D>,
* ) => D} descriptor
*/
constructor(object, key, descriptor) {
super();
this.#descriptor = descriptor;
this.#key = key;
this.#object = object;
}
/**
* @inheritdoc
*/
activate() {
this.#active = true;
return this;
}
/**
* @inheritdoc
*/
clear() {
this.#data.length = 0;
return this;
}
/**
* @inheritdoc
*/
delete(index) {
this.#data.splice(index, 1);
return this;
}
/**
* @inheritdoc
*/
deactivate() {
this.#active = false;
return this;
}
/**
* @inheritdoc
*/
disable() {
this.#enabled = false;
return this;
}
/**
* @inheritdoc
*/
enable() {
this.#enabled = true;
return this;
}
/**
* @inheritdoc
*/
entries() {
return this.#data.entries();
}
/**
* @inheritdoc
*/
first() {
return this.#data[0];
}
/**
* @inheritdoc
*/
get(index) {
return this.#data[index];
}
/**
* @inheritdoc
*/
has(index) {
return index >= 0 && index < this.#data.length;
}
/**
* @inheritdoc
*/
last() {
return this.#data[this.lastIndex];
}
/**
* @inheritdoc
*/
load() {
const descriptor = Descriptor.fromProperty(this.#object, this.#key);
if (descriptor) {
this.add(descriptor);
}
else {
throw new Error(`Descriptor not found for key: ${String(this.#key)}`);
}
return this;
}
/**
* @inheritdoc
*/
set(index, value) {
this.#data[index] = value;
return this;
}
/**
* @inheritdoc
*/
setCurrentIndex(index) {
this.#currentIndex = index;
return this;
}
/**
* @inheritdoc
*/
update(index, value) {
this.#data[index] = {
...this.#data[index],
...value
};
return this;
}
/**
* @inheritdoc
*/
values() {
return this.#data.values();
}
}
// Abstract.
/**
* @description The concrete class representing a chain of property descriptors.
* @export
* @abstract
* @class DescriptorChain
* @template [O=any] The type of the object that the property descriptors are associated with.
* @template {keyof O} [K=keyof O] The type of the property name in the object.
* @template {K extends keyof O ? O[K] : any} [V=K extends keyof O ? O[K] : any] The type of the value accessed by the property.
* @template {boolean} [A=boolean] The type of active property.
* @template {boolean} [N=boolean] The type of enabled property.
* @template {boolean} [C=boolean] The type of configurable property.
* @template {boolean} [E=boolean] The type of enumerable property.
* @template {WrappedPropertyDescriptor<O, K, V, A, N, C, E, D>} [D=WrappedPropertyDescriptor<O, K, V, A, N, C, E, any>] The type of wrapped property descriptor.
* @extends {DescriptorChainBase<O, K, V, A, N, C, E, D>}
*/
class DescriptorChain extends DescriptorChainBase {
/**
* @inheritdoc
*/
add(descriptor, current = true) {
// Adds the new descriptor to the chain.
super.data.push(new super.descriptor(super.object, super.key, descriptor));
// Sets the current index.
current && super.setCurrentIndex(super.lastIndex);
// Returns the instance.
return this;
}
}
/*
* Public API Surface of descriptor-chain
*/
/**
* Generated bundle index. Do not edit.
*/
export { DescriptorChain, DescriptorChainBase, DescriptorChainCore };
//# sourceMappingURL=typescript-package-descriptor-chain.mjs.map