@typescript-package/length
Version:
A lightweight TypeScript library for the length.
159 lines (155 loc) • 5.05 kB
JavaScript
/**
* @description Represents a length value with optional minimum and maximum constraints.
* @export
* @class Length
* @template {number | undefined} [Value=number | undefined] The type of the length value, which can be a number or undefined.
* @template {number | undefined} [Min=number | undefined] The type of the minimum length value, which can be a number or undefined.
* @template {number | undefined} [Max=number | undefined] The type of the maximum length value, which can be a number or undefined.
*/
class Length {
/**
* @description The length value of generic type variable `Value`.
* @public
* @readonly
* @type {Value }
*/
get length() {
return this.#value;
}
/**
* @description The maximum length value of generic type variable `Max`.
* @public
* @readonly
* @type {Max}
*/
get max() {
return this.#max;
}
/**
* @description The minimum length value of generic type variable `Min`.
* @public
* @readonly
* @type {Min}
*/
get min() {
return this.#min;
}
/**
* @description The configuration object containing the length, minimum, and maximum values.
* @public
* @readonly
* @type {LengthSettings<Value, Min, Max>}
*/
get config() {
return {
value: this.#value,
min: this.#min,
max: this.#max,
};
}
/**
* @description Privately stored maximum length value of generic type variable `Max`.
* @type {Max}
*/
#max;
/**
* @description Privately stored minimum length value of generic type variable `Min`.
* @type {Min}
*/
#min;
/**
* @description Privately stored length value of generic type variable `Value`.
* @type {Value}
*/
#value;
/**
* Creates an instance of `Length`.
* @constructor
* @param {?(Value | LengthOptions<Value, Min, Max>)} [length] The initial length value, which can be a number or an object containing min, max, and value properties.
*/
constructor(length) {
this.#value = typeof length === 'number'
? length
: typeof length === 'object' ? length.value : undefined;
this.#max = typeof length === 'object' && 'max' in length ? length.max : undefined;
this.#min = typeof length === 'object' && 'min' in length ? length.min : undefined;
}
/**
* @description Checks whether the length is valid based on the defined minimum and maximum constraints.
* @public
* @returns {boolean} Returns the true if the length is valid, otherwise false.
*/
isValid() {
if (typeof this.length === 'number') {
if (typeof this.min === 'number' && this.length < this.min)
return false;
if (typeof this.max === 'number' && this.length > this.max)
return false;
}
return true;
}
/**
* @description Sets the length, minimum, and maximum values.
* @public
* @param {LengthOptions<Value, Min, Max>} [param0={}]
* @param {LengthOptions<Value, Min, Max>} param0.max Optional maximum length value.
* @param {LengthOptions<Value, Min, Max>} param0.min Optional minimum length value.
* @param {LengthOptions<Value, Min, Max>} param0.value Optional length value.
* @returns {this} The current instance of Length.
*/
set({ max, min, value } = {}) {
'max' in arguments[0] && this.setMax(max);
'min' in arguments[0] && this.setMin(min);
'value' in arguments[0] && this.setLength(value);
return this;
}
/**
* @description Sets the length value.
* @public
* @param {(Value | undefined)} value The length value to set.
* @returns {this} The current instance of Length.
*/
setLength(value) {
this.#value = value;
return this;
}
/**
* @description Sets the maximum length value.
* @public
* @param {(Max | undefined)} max The maximum length value to set.
* @returns {this} The current instance of Length.
*/
setMax(max) {
this.#max = max;
return this;
}
/**
* @description Sets the minimum length value.
* @public
* @param {(Min | undefined)} min The minimum length value to set.
* @returns {this} The current instance of Length.
*/
setMin(min) {
this.#min = min;
return this;
}
/**
* @description Sets both the minimum and maximum length values.
* @public
* @param {Min | undefined} min The minimum length value to set.
* @param {Max | undefined} max The maximum length value to set.
* @returns {this} The current instance of Length.
*/
setMinMax(min, max) {
this.setMax(max).setMin(min);
return this;
}
}
/*
* Public API Surface of length
*/
/**
* Generated bundle index. Do not edit.
*/
export { Length };
//# sourceMappingURL=typescript-package-length.mjs.map