UNPKG

@typescript-package/affix

Version:

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

1 lines 8.51 kB
{"version":3,"file":"typescript-package-affix.mjs","sources":["../../../package/affix/src/lib/affix.abstract.ts","../../../package/affix/src/lib/prefix.class.ts","../../../package/affix/src/lib/suffix.class.ts","../../../package/affix/src/public-api.ts","../../../package/affix/src/typescript-package-affix.ts"],"sourcesContent":["// Class.\nimport { Value } from '@typescript-package/core';\n/**\n * @description A class to manage affixes (prefixes or suffixes) that can be applied to strings.\n * @export\n * @abstract\n * @class Affix\n * @template {string} [Value=string] The type of affix constrained by the `string`. Defaults to, `string`.\n * @extends {Value<Value>}\n */\nexport abstract class Affix<Value extends string = string> extends Value<Value> {\n /**\n * @description Returns the `string` tag representation of the `Affix` class when used in `Object.prototype.toString.call(instance)`.\n * @public\n * @readonly\n * @type {string}\n */\n public override get [Symbol.toStringTag]() {\n return 'Affix';\n }\n\n /**\n * @description Defines the affix sanitized by specified pattern.\n * @public\n * @static\n * @template {string} [Value=string] \n * @param {Value} value A value of generic type variable `Value` constrained by the `string` type to be sanitized with the `pattern`.\n * @param {RegExp} [pattern=Affix.pattern] The pattern of `RegExp` to sanitize the `affix`. Defaults to static `Affix.pattern`.\n * @returns {Value} The returned value is an affix of a generic type variable `Value`, optionally sanitized by the `pattern`.\n */\n public static sanitize<Value extends string = string>(\n value: Value,\n pattern: RegExp = Affix.pattern,\n ): Value {\n return value.replace(pattern, '') as Value;\n }\n\n /**\n * @description The default pattern pattern used to sanitize the affix, which removes characters that are not part of the valid characters for the affix.\n * @public\n * @static\n * @type {RegExp}\n */\n public static pattern: RegExp = /[^a-zA-Z0-9$_]/g;\n\n /**\n * @description Returns the privately stored pattern of `RegExp` type to sanitize the affix.\n * @public\n * @readonly\n * @type {RegExp}\n */\n public get pattern() {\n return this.#pattern;\n }\n\n /**\n * @description Privately stored pattern of `RegExp` to sanitize the affix.\n * @type {RegExp}\n */\n #pattern = Affix.pattern;\n\n /**\n * Creates an instance of child class.\n * @constructor\n * @param {?Value} [value] An optional initial affix of generic type variable `Value` constrained by `string` type.\n * @param {?RegExp} [pattern] The pattern of `RegExp` to sanitize the affix.\n */\n constructor(value?: Value, pattern?: RegExp) {\n super(value || '' as Value);\n pattern instanceof RegExp && (this.#pattern = pattern);\n typeof value !== 'undefined' && this.set(value);\n }\n\n /**\n * @description Returns the affix, optionally sanitized by the `pattern`.\n * @public\n * @param {?RegExp} [pattern] The pattern of `RegExp` to sanitize privately stored affix.\n * @returns {string} Returns privately stored `#affix` of `string` type optionally sanitized by the `pattern`.\n */\n public get(pattern?: RegExp) {\n return Affix.sanitize(this.value, pattern);\n }\n\n /**\n * @description Sets and stores privately sanitized affix of generic type variable `Value` constrained by `string` type.\n * @private\n * @param {Value} value The `affix` of generic type variable `Value`.\n * @param {RegExp} [pattern=this.#pattern] The pattern of `RegExp` to sanitize the `affix`. Defaults to privately stored `#pattern`.\n * @returns {this} The returned value is current instance for method chaining.\n */\n public set(value: Value, pattern: RegExp = this.#pattern): this {\n typeof value === 'string' && (super.setValue(Affix.sanitize(value, pattern) as Value));\n return this;\n }\n\n /**\n * @description Sets the pattern to sanitize the affix.\n * @public\n * @param {RegExp} pattern The pattern of `RegExp` to sanitize the affix.\n * @returns {this} \n */\n public setPattern(pattern: RegExp): this {\n pattern instanceof RegExp && (this.#pattern = pattern);\n return this;\n }\n}\n","// Class.\nimport { Affix } from \"./affix.abstract\";\n/**\n * @description A class to manage prefixes that can be applied to strings.\n * @export\n * @class Prefix\n * @template {string} [Value=string] The type of prefix constrained by the `string`.\n * @extends {Affix<Value>}\n */\nexport class Prefix<Value extends string = string> extends Affix<Value> {\n /**\n * @description Returns the `string` tag representation of the `Prefix` class when used in `Object.prototype.toString.call(instance)`.\n * @public\n * @readonly\n * @type {string}\n */\n public override get [Symbol.toStringTag]() {\n return 'Prefix';\n }\n\n /**\n * @description Sanitizes the prefix with a `pattern`.\n * @public\n * @param {string} value \n * @param {RegExp} [pattern=Prefix.pattern] \n * @returns {string} \n */\n public static override sanitize<Value extends string = string>(\n value: Value,\n pattern: RegExp = Prefix.pattern\n ): Value {\n return value.replace(pattern, '') as Value;\n }\n\n /**\n * @inheritdoc\n * @public\n * @static\n * @type {RegExp}\n */\n public static override pattern: RegExp = super.pattern;\n}\n","// Class.\nimport { Affix } from \"./affix.abstract\";\n/**\n * @description A class to manage suffixes that can be applied to strings.\n * @export\n * @class Suffix\n * @template {string} [Value=string] The type of suffix constrained by the `string`.\n * @extends {Affix<Value>}\n */\nexport class Suffix<Value extends string = string> extends Affix<Value> {\n /**\n * @description Returns the `string` tag representation of the `Suffix` class when used in `Object.prototype.toString.call(instance)`.\n * @public\n * @readonly\n * @type {string}\n */\n public override get [Symbol.toStringTag]() {\n return 'Suffix';\n }\n\n /**\n * @description Sanitizes the suffix with a `pattern`.\n * @public\n * @param {string} value \n * @param {RegExp} [pattern=Suffix.pattern] \n * @returns {string} \n */\n public static override sanitize<Value extends string = string>(\n value: Value,\n pattern: RegExp = Suffix.pattern\n ): Value {\n return value.replace(pattern, '') as Value;\n }\n\n /**\n * @inheritdoc\n * @public\n * @static\n * @type {RegExp}\n */\n public static override pattern: RegExp = super.pattern;\n}\n","/*\n * Public API Surface of affix\n */\n\nexport * from './lib';\n","/**\n * Generated bundle index. Do not edit.\n */\n\nexport * from './public-api';\n"],"names":[],"mappings":";;;AAEA;;;;;;;AAOG;AACG,MAAgB,KAAqC,SAAQ,KAAY,CAAA;AAC7E;;;;;AAKG;AACH,IAAA,KAAqB,MAAM,CAAC,WAAW,CAAC,GAAA;AACtC,QAAA,OAAO,OAAO;;AAGhB;;;;;;;;AAQG;IACI,OAAO,QAAQ,CACpB,KAAY,EACZ,OAAkB,GAAA,EAAK,CAAC,OAAO,EAAA;QAE/B,OAAO,KAAK,CAAC,OAAO,CAAC,OAAO,EAAE,EAAE,CAAU;;AAG5C;;;;;AAKG;AACI,IAAA,OAAO,OAAO,GAAW,iBAAiB;AAEjD;;;;;AAKG;AACH,IAAA,IAAW,OA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