@typescript-package/name
Version:
A lightweight TypeScript library for the name with prefix and suffix.
246 lines (237 loc) • 5.22 kB
JavaScript
import { Wrap } from '@typescript-package/wrapper';
var _a;
/**
* @description
* @export
* @abstract
* @class Affix
*/
class Affix {
/**
* @description Define affix for the name.
* @public
* @static
* @param {string} value A `string` type value.
* @param {RegExp} [filter=Affix.filter]
* @returns {string} The return value is a `string` type affix.
*/
static define(value, filter = _a.filter) {
return value.replace(filter, '');
}
/**
* @description
* @public
* @static
* @type {RegExp}
*/
static filter = /[^a-zA-Z0-9$_]/g;
/**
* @description Returns privately stored affix.
* @public
* @readonly
* @type {string}
*/
get get() {
return this.#affix;
}
/**
* @description
* @type {RegExp}
*/
#filter = _a.filter;
/**
* @description Initialize default affix.
* @type {string}
*/
#affix = '';
/**
* Creates an instance of child class.
* @constructor
* @param {?string} [affix] An optional initial `string` type value as affix.
* @param {?RegExp} [filter] The filter of `RegExp` for affix.
*/
constructor(affix, filter) {
typeof affix === 'string' && this.set(affix);
typeof filter === 'string' && (this.#filter = filter);
}
/**
* @description Sets and stores privately affix of a string type for the name.
* @public
* @param {string} affix A `string` type value.
* @returns {this}
*/
set(affix) {
typeof affix === 'string' && (this.#affix = _a.define(affix, this.#filter));
return this;
}
}
_a = Affix;
// Class.
/**
* @description
* @export
* @class Prefix
* @extends {Affix}
*/
class Prefix extends Affix {
}
// Class.
/**
* @description
* @export
* @class Suffix
* @extends {Affix}
*/
class Suffix extends Affix {
}
// Class.
/**
* @description
* @export
* @abstract
* @class CommonName
*/
class CommonName {
/**
* @description
* @public
* @readonly
* @type {string}
*/
get prefix() {
return this.#prefix.get;
}
/**
* @description
* @public
* @readonly
* @type {string}
*/
get suffix() {
return this.#suffix.get;
}
/**
* @description Private namespace for prefix.
* @type {Prefix}
*/
#prefix = new Prefix();
/**
* @description Private namespace for suffix.
* @type {Suffix}
*/
#suffix = new Suffix();
/**
* Creates an instance of child class.
* @constructor
* @param {{ prefix?: string, suffix?: string }} [param0={}]
* @param {string} param0.prefix
* @param {string} param0.suffix
*/
constructor({ prefix, suffix } = {}) {
typeof prefix === 'string' && this.setPrefix(prefix);
typeof suffix === 'string' && this.setSuffix(suffix);
}
/**
* @description Set prefix for the name.
* @public
* @param {string} prefix A `string` type value as prefix.
* @returns {this}
*/
setPrefix(prefix) {
this.#prefix.set(prefix);
return this;
}
/**
* @description Sets suffix for the name.
* @public
* @param {string} suffix A `string` type value as suffix.
* @returns {this}
*/
setSuffix(suffix) {
this.#suffix.set(suffix);
return this;
}
}
// Class.
/**
* @description
* @export
* @class Name
* @template {string} [Prefix='']
* @template {string} [Name=string]
* @template {string} [Suffix='']
* @extends {CommonName}
*/
class Name extends CommonName {
/**
* @description Returns privately stored name.
* @public
* @readonly
* @type {Name}
*/
get get() {
return this.#name;
}
/**
* @inheritdoc
* @public
* @readonly
* @type {Prefix}
*/
get prefix() {
return super.prefix;
}
/**
* @inheritdoc
* @public
* @readonly
* @type {Suffix}
*/
get suffix() {
return super.suffix;
}
/**
* @description Generates the name with prefix and suffix.
* @public
* @readonly
* @type {`${Prefix}${Name}${Suffix}`}
*/
get generate() {
return new Wrap(this.prefix, this.suffix, this.#name).valueOf();
}
/**
* @description
* @type {Name}
*/
#name;
/**
* Creates an instance of `Name`.
* @constructor
* @param {Name} name
* @param {{ prefix?: Prefix, suffix?: Suffix }} [param0={}]
* @param {Prefix} param0.prefix
* @param {Suffix} param0.suffix
*/
constructor(name, { prefix, suffix } = {}) {
super({ prefix, suffix });
this.#name = name;
}
/**
* @description Set the name.
* @public
* @param {string} name A `string` type value.
* @returns {this}
*/
set(name) {
typeof name === 'string' && (this.#name = name);
return this;
}
}
/*
* Public API Surface of name
*/
/**
* Generated bundle index. Do not edit.
*/
export { Affix, CommonName, Name, Prefix, Suffix };
//# sourceMappingURL=typescript-package-name.mjs.map