UNPKG

@typescript-package/affix

Version:

A lightweight TypeScript library for the affix - prefix and suffix.

183 lines (176 loc) 5.81 kB
import { Value } from '@typescript-package/core'; var _a; /** * @description A class to manage affixes (prefixes or suffixes) that can be applied to strings. * @export * @abstract * @class Affix * @template {string} [Value=string] The type of affix constrained by the `string`. Defaults to, `string`. * @extends {Value<Value>} */ class Affix extends Value { /** * @description Returns the `string` tag representation of the `Affix` class when used in `Object.prototype.toString.call(instance)`. * @public * @readonly * @type {string} */ get [Symbol.toStringTag]() { return 'Affix'; } /** * @description Defines the affix sanitized by specified pattern. * @public * @static * @template {string} [Value=string] * @param {Value} value A value of generic type variable `Value` constrained by the `string` type to be sanitized with the `pattern`. * @param {RegExp} [pattern=Affix.pattern] The pattern of `RegExp` to sanitize the `affix`. Defaults to static `Affix.pattern`. * @returns {Value} The returned value is an affix of a generic type variable `Value`, optionally sanitized by the `pattern`. */ static sanitize(value, pattern = _a.pattern) { return value.replace(pattern, ''); } /** * @description The default pattern pattern used to sanitize the affix, which removes characters that are not part of the valid characters for the affix. * @public * @static * @type {RegExp} */ static pattern = /[^a-zA-Z0-9$_]/g; /** * @description Returns the privately stored pattern of `RegExp` type to sanitize the affix. * @public * @readonly * @type {RegExp} */ get pattern() { return this.#pattern; } /** * @description Privately stored pattern of `RegExp` to sanitize the affix. * @type {RegExp} */ #pattern = _a.pattern; /** * Creates an instance of child class. * @constructor * @param {?Value} [value] An optional initial affix of generic type variable `Value` constrained by `string` type. * @param {?RegExp} [pattern] The pattern of `RegExp` to sanitize the affix. */ constructor(value, pattern) { super(value || ''); pattern instanceof RegExp && (this.#pattern = pattern); typeof value !== 'undefined' && this.set(value); } /** * @description Returns the affix, optionally sanitized by the `pattern`. * @public * @param {?RegExp} [pattern] The pattern of `RegExp` to sanitize privately stored affix. * @returns {string} Returns privately stored `#affix` of `string` type optionally sanitized by the `pattern`. */ get(pattern) { return _a.sanitize(this.value, pattern); } /** * @description Sets and stores privately sanitized affix of generic type variable `Value` constrained by `string` type. * @private * @param {Value} value The `affix` of generic type variable `Value`. * @param {RegExp} [pattern=this.#pattern] The pattern of `RegExp` to sanitize the `affix`. Defaults to privately stored `#pattern`. * @returns {this} The returned value is current instance for method chaining. */ set(value, pattern = this.#pattern) { typeof value === 'string' && (super.setValue(_a.sanitize(value, pattern))); return this; } /** * @description Sets the pattern to sanitize the affix. * @public * @param {RegExp} pattern The pattern of `RegExp` to sanitize the affix. * @returns {this} */ setPattern(pattern) { pattern instanceof RegExp && (this.#pattern = pattern); return this; } } _a = Affix; // Class. /** * @description A class to manage prefixes that can be applied to strings. * @export * @class Prefix * @template {string} [Value=string] The type of prefix constrained by the `string`. * @extends {Affix<Value>} */ class Prefix extends Affix { /** * @description Returns the `string` tag representation of the `Prefix` class when used in `Object.prototype.toString.call(instance)`. * @public * @readonly * @type {string} */ get [Symbol.toStringTag]() { return 'Prefix'; } /** * @description Sanitizes the prefix with a `pattern`. * @public * @param {string} value * @param {RegExp} [pattern=Prefix.pattern] * @returns {string} */ static sanitize(value, pattern = Prefix.pattern) { return value.replace(pattern, ''); } /** * @inheritdoc * @public * @static * @type {RegExp} */ static pattern = super.pattern; } // Class. /** * @description A class to manage suffixes that can be applied to strings. * @export * @class Suffix * @template {string} [Value=string] The type of suffix constrained by the `string`. * @extends {Affix<Value>} */ class Suffix extends Affix { /** * @description Returns the `string` tag representation of the `Suffix` class when used in `Object.prototype.toString.call(instance)`. * @public * @readonly * @type {string} */ get [Symbol.toStringTag]() { return 'Suffix'; } /** * @description Sanitizes the suffix with a `pattern`. * @public * @param {string} value * @param {RegExp} [pattern=Suffix.pattern] * @returns {string} */ static sanitize(value, pattern = Suffix.pattern) { return value.replace(pattern, ''); } /** * @inheritdoc * @public * @static * @type {RegExp} */ static pattern = super.pattern; } /* * Public API Surface of affix */ /** * Generated bundle index. Do not edit. */ export { Affix, Prefix, Suffix }; //# sourceMappingURL=typescript-package-affix.mjs.map