UNPKG

@typescript-package/wrapper

Version:

A lightweight TypeScript library to wrap the text with the opening and closing chars.

494 lines (489 loc) 27.5 kB
/** * The `Wrap` object is based on the `String` object and represents the immutable primitive value of the text wrapped by the opening and * closing chars. It is designed to preserve the type names of the supplied opening, text, and closing chars by using the generic type * variables. */ class Wrap extends String { // #region instance public accessors. /** * The `get` accessor gets the closing of the wrap by returning the `#closing` property of a specified object. * @returns The return value is closing of the wrap of a generic type variable `Closing`. */ get closing() { return this.#closing; } /** * The `get` accessor gets the opening of the wrap by returning the `#opening` property of a specified object. * @returns The return value is the opening of the wrap of a generic type variable `Opening`. */ get opening() { return this.#opening; } /** * The `get` accessor gets the text of the `Wrap` by returning the `#text` property of a specified object. * @returns The return value is the text of a generic type variable `Text`. */ get text() { return this.#text; } /** * The `get` accessor, with the help of `toStringTag`, changes the default tag to `Wrap` for an instance of `Wrap`. It can be read by * the `typeOf()` function of `@typescript-package/type`. * @returns The return value is the word 'Wrap` of a `string`. */ get [Symbol.toStringTag]() { return 'Wrap'; } //#endregion instance public accessors. //#region instance private properties. /** * Private property of the closing chars of a generic type variable `Closing`. */ #closing; /** * Private property of the opening chars of a generic type variable `Opening`. */ #opening; /** * Private property of text of a generic type variable `Text`. */ #text; //#endregion instance private properties. //#region static public methods. /** * The method checks whether the text has given `closing` chars at the end. * @param text The text of `string` type, to check whether it contains given `closing` chars. * @param closing The closing chars of `string` type to check if a given `text` contains. * @returns The return value is a `boolean` indicating whether the `text` contains `closing` chars at the end. */ static hasClosing(text, closing) { return (typeof text === 'string' && text.length >= 1 && typeof closing === 'string' && closing.length >= 1 && text.slice(-closing.length) === closing); } /** * Checks whether the text has `opening` chars at the beginning. * @param text The text of `string`, to check whether it contains given `opening` chars. * @param opening The opening chars of `string` to check if a given `text` contains. * @returns The return value is a `boolean` indicating whether the `text` contains `opening` chars at the beginning. */ static hasOpening(text, opening) { return (typeof text === 'string' && text.length >= 1 && typeof opening === 'string' && opening.length >= 1 && text.slice(0, opening.length) === opening); } /** * The method checks whether the `value` of any type is the `Wrap` instance of any or given `opening` and `closing` chars. * @param value The value of any type to test against the `Wrap` instance of any or given opening and closing. * @param opening Optional opening chars of a generic type variable `Opening` to check if the given `value` contains. * @param closing Optional closing chars of a generic type variable `Closing` to check if the given `value` contains. * @param text An optional text of a generic type variable `Text` to check if the given `value` contains. * @returns The return value is a `boolean` type indicating whether the value is an instance of `Wrap` of any, or the given opening, * closing, and text. */ static isWrap(value, opening, closing, text) { return typeof value === 'object' && value instanceof this ? (typeof opening === 'string' ? opening === value.opening : true) && (typeof closing === 'string' ? closing === value.closing : true) && (typeof text === 'string' ? text === value.text : true) : false; } //#endregion static public methods. //#region constructor. /** * Creates a new `Wrap` instance of the opening and closing chars and optional text to wrap. * @param opening Opening characters of the generic type variable `Opening` placed before the given `text`. An empty `string` indicates * that for the `hasOpening()` and `isWrapped()` methods, the opening chars are `undefined`, returning `false`. * @param closing Closing characters of the generic type variable `Closing` placed after the given `text`. An empty `string` indicates * that for the `hasClosing()` and `isWrapped()` methods, the closing chars are `undefined`, returning `false`. * @param text An optional text placed between the given `opening` and `closing` chars on the template `${Opening}${Text}${Closing}`. */ constructor(opening, closing, text = '') { super(`${opening}${text}${closing}`); this.#closing = String(closing); this.#text = String(text); this.#opening = String(opening); } //#endregion constructor. //#region instance public methods. /** * Gets the closing chars of the wrap by returning the `#closing` property of a specified object. * @returns The return value is closing chars of a generic type variable `Closing`. */ getClosing() { return this.#closing; } /** * Gets the opening chars of the wrap by returning the `#opening` property of a specified object. * @returns The return value is opening chars of a generic type variable `Opening`. */ getOpening() { return this.#opening; } /** * Gets the text of the wrap by returning the `#text` property of a specified object, without the opening and closing chars of the `Wrap`. * @returns The return value is the text of a generic type variable `Text`. */ getText() { return this.#text; } /** * Checks whether the primitive value of a specified object has the closing chars or given `closing` chars. If given `closing` chars in * the constructor are the empty `string`, the method returns `false`. * @param closing Optional closing chars of a `string` type to check whether the primitive value contains them at the **end**. * @returns The return value is a `boolean` indicating whether the primitive value has the closing chars or given closing chars. */ hasClosing(closing) { return (this.#closing.length >= 1 && (typeof closing === 'string' ? this.#closing === closing : true)); } /** * Checks whether the primitive value of a specified object has the opening chars or given `opening` chars. If given `opening` chars in * the constructor are the empty `string`, the method returns `false`. * @param opening Optional opening chars of a `string` type to check if the primitive value contains them at the **beginning**. * @returns The return value is a `boolean` indicating whether the primitive value has the opening chars or given `opening` chars. */ hasOpening(opening) { return (this.#opening.length >= 1 && (typeof opening === 'string' ? this.#opening === opening : true)); } /** * The method checks whether the text of a specified `Wrap` object is defined, which means it's a `string` of at least one char and * optionally equal to the given `text`. * @param text Optional text of `string` type to check whether it's equal to the text of the `Wrap` object. * @returns The return value is a `boolean` indicating whether the text is defined and optionally equal to the given text. */ hasText(text) { return (this.#text.length >= 1 && (typeof text === 'string' ? this.#text === text : true)); } /** * The method checks whether the primitive value of the specified `object` is wrapped by the opening and closing chars of an instance or * given `opening` and `closing` chars. If given `opening` or `closing` chars in the constructor are the empty `string`, the method * returns `false`. * @param opening Optional opening chars of a `string` type to check if the primitive value contains them at the beginning. The default * value is picked from the private `#opening` property of an instance. * @param closing Optional closing chars of a `string` type to check if the primitive value contains them at the end. The default value is * picked from the private `#closing` property of an instance. * @returns The return value is a `boolean` indicating whether the object has both opening and closing chars or given `opening` and * `closing` chars. */ isWrapped(opening = this.#opening, closing = this.#closing) { return this.hasOpening(opening) && this.hasClosing(closing); } /** * Returns the primitive value with replaced closing chars. * @param closing The closing chars of a generic type variable `ReplaceClosing` to replace the closing chars in the primitive value. * @returns The return value is the primitive value with replaced closing chars of a generic type variables in order `Opening`, `Text` and * `ReplaceClosing` on the template `${Opening}${Text}${ReplaceClosing}`. */ replaceClosing(closing) { return `${this.#opening}${this.#text}${closing}`; } /** * Returns the primitive value with replaced opening chars. * @param opening The opening chars of a generic type variable `ReplaceOpening` to replace the opening chars in the primitive value. * @returns The return value is the primitive value with replaced opening chars of a generic type variables in order `ReplaceOpening`, * `Text` and `Closing` on the template `${ReplaceOpening}${Text}${Closing}`. */ replaceOpening(opening) { return `${opening}${this.#text}${this.#closing}`; } /** * Returns the primitive value with replaced text. * @param text The text of a generic type variable `ReplaceText` to replace the text in the primitive value. * @returns The return value is the primitive value with replaced text of a generic type variables in order `Opening`, `ReplaceText` * and `Closing` on the template `${Opening}${ReplaceText}${Closing}`. */ replaceText(text) { return `${this.#opening}${text}${this.#closing}`; } /** * Checks whether the wrapped text starts with the specified opening characters. * @param opening Optional opening characters to check against. Defaults to the instance's `#opening`. * @returns The returned value is a `boolean` indicating whether the wrapped text starts with the given or default opening characters. */ startsWith(opening = this.#opening) { return this.toString().startsWith(opening); } /** * Checks whether the wrapped text ends with the specified closing characters. * @param closing Optional closing characters to check against. Defaults to the instance's `#closing`. * @returns The returned value is a `boolean` indicating whether the wrapped text ends with the given or default closing characters. */ endsWith(closing = this.#closing) { return this.toString().endsWith(closing); } /** * Gets the wrap, the primitive value of a specified `Wrap` object. * @returns The return value is the wrap of generic type variables in order `Opening`, `Text`, and `Closing` on the template * `${Opening}${Text}${Closing}`. */ toString() { return super.toString(); } /** * Returns the wrap, primitive value of a specified `Wrap` object. * @returns The return value is the wrap of generic type variables in order `Opening`, `Text`, and `Closing` on the template * `${Opening}${Text}${Closing}`. */ valueOf() { return super.valueOf(); } } // Class. /** * The `Wrapper` is an extension of the `Wrap` object, which means it represents the immutable wrap of the opening and closing with the * additional ability to use it to wrap strings. */ class Wrapper extends Wrap { //#region instance accessors. /** * The property, with the help of `toStringTag`, changes the default tag to `Wrapper` in the `Wrapper` instance. It can be read by the * `typeOf()` function of `@angular-package/type`. */ get [Symbol.toStringTag]() { return 'Wrapper'; } //#endregion instance accessors. //#region static public methods. /** * Defines a new `Wrapper` instance with the provided `opening`, `closing` chars, and optional `text`. * @param opening The opening chars of generic type variable `Opening` for new `Wrapper` instance. * @param closing The closing chars of generic type variable `Closing` for new `Wrapper` instance. * @param text An optional text of generic type variable `Text` for new `Wrapper` instance. * @returns The return value is the `Wrapper` instance of given `opening`, `closing` chars, and optional `text`. */ static define(opening, closing, text) { return new this(opening, closing, text); } /** * The method checks if the value of any type is an instance of the `Wrapper` of any, or the given `opening`, `closing` chars, and `text`. * @param value The value of any type to test against the `Wrapper` instance. * @param opening Optional opening chars of generic type variable `Opening` to check if the given `value` contains. * @param closing Optional closing chars of generic type variable `Closing` to check if the given `value` contains. * @param text An optional text of generic type variable `Text` to check if the given `value` contains. * @returns The return value is a `boolean` type indicating whether the value is an instance of `Wrapper` of any, or the given opening, * closing chars, and text. */ static isWrapper(value, opening, closing, text) { return (typeof value === 'object' && value instanceof this && super.isWrap(value, opening, closing, text)); } /** * Replaces given `closing` chars with a given replacement value at the end of the given `text`. * @param text The text of `string` type in which given `closing` characters are replaced by a given replacement value. * @param closing The closing chars of the `string` to replace by a given replacement value at the end of the given `text`. * @param replacement The replacement value of a string type for the given `closing` characters in the given `text`. * @returns The return value is the given `text` of `string` type with a replaced `closing` chars by a given replacement value or the * specified `text` unchanged if it does not contain the given `closing` chars. */ static replaceClosing(text, closing, replacement) { return this.hasClosing(text, closing) ? text.slice(0, -closing.length) + replacement : text; } /** * Replaces given `opening` chars with a given replacement value at the beginning of the given `text`. * @param text The text of `string` type in which the given `opening` chars are replaced by a given replacement value. * @param opening The opening chars of the `string` to replace by a given replacement value at the beginning of the given `text`. * @param replacement The replacement value of a string type for the given `opening` characters in the given `text`. * @returns The return value is the given `text` of `string` type with a replaced `opening` chars by a given replacement value or the * specified `text` unchanged if it does not contain the given `opening` chars. */ static replaceOpening(text, opening, replacement) { return this.hasOpening(text, opening) ? text.replace(opening, String(replacement)) : text; } /** * The method returns the given `text` without the given `opening` and `closing` chars. * @param text The text of the `string` from which given opening and closing chars are removed. * @param opening The opening chars of the `string` to be removed in the given `text`. * @param closing The closing chars of the `string` to be removed in the given `text`. * @returns The return value is the given `text` of `string` type without the given `opening` and `closing` chars or unchanged `text` if * it does not contain the given `opening` and `closing` chars. */ static unwrap(text, opening, closing) { return ((text = this.replaceClosing(text, closing, '')), (text = this.replaceOpening(text, opening, '')), text); } //#endregion static public methods. //#region constructor. /** * Creates a new `Wrapper` instance with the opening and closing chars and optional text. * @param opening The opening chars of a generic type variable `Opening` placed before the given `text`. * @param closing The closing chars of a generic type variable `Closing` placed after the given `text`. * @param text Optional text of a generic type variable `Text` to wrap by given `opening` and `closing` chars. * @returns The return value is a new `Wrapper` instance. */ constructor(opening, closing, text = '') { super(opening, closing, text); } //#endregion constructor. //#region instance public methods. /** * Determines whether the provided `text` has the closing chars of the specified `Wrapper` object at the end. * @param text The text of `string` to test for the existence of the closing chars at the end of it. * @returns The return value is a `boolean` indicating whether the given `text` has the closing chars of the wrap. */ isClosingIn(text) { return Wrapper.hasClosing(text, this.closing); } /** * Checks whether the provided `text` has the opening chars of a specified `Wrapper` object at the beginning. * @param text The text of `string` to test for the existence of the opening chars at the beginning of it. * @returns The return value is a `boolean` indicating whether the given `text` has the opening chars of the wrap. */ isOpeningIn(text) { return Wrapper.hasOpening(text, this.opening); } /** * Replaces the closing chars of the `Wrapper` object in the given `text` with a given replacement value. * The replacement succeeds if the closing characters exist at the end of the text. * @param text The text of `string` type in which the closing chars are replaced by given replacement value. * @param replaceValue The value of `string` type as a replacement for the closing chars at the end of the given `text`. * @returns The return value is the given `text` of `string` type with replaced closing chars by given replacement value. */ replaceClosingIn(text, replaceValue) { return Wrapper.replaceClosing(text, this.closing, replaceValue); } /** * Replaces the opening chars of the `Wrapper` object in the given `text` with a given replacement value. * The replacement succeeds if the opening characters exist at the beginning of the text. * @param text The text of `string` type in which the opening chars are replaced by given replacement value. * @param replaceValue The value of `string` type as a replacement for the opening chars at the beginning of the given `text`. * @returns The return value is the given `text` of `string` type with replaced opening chars by given replacement value. */ replaceOpeningIn(text, replaceValue) { return Wrapper.replaceOpening(text, this.opening, replaceValue); } /** * Returns given `text` without the opening and closing chars of the `Wrapper` object. * @param text The text of a `string` type to unwrap from the opening and closing chars of the `Wrapper` object. * @returns The return value is the text of `string` type unwrapped from the opening and closing chars of the `Wrapper` object. */ removeWrapIn(text) { return ((text = this.replaceClosingIn(text, '')), (text = this.replaceOpeningIn(text, '')), text); } /** * The method returns the text of the `Wrapper` object wrapped by the given `opening` and `closing` chars. * @param opening The opening chars of a generic type variable `TextOpening` to wrap the text of the `Wrapper` instance. * @param closing The closing chars of a generic type variable `TextClosing` to wrap the text of the `Wrapper` instance. * @returns The return value is the text wrapped by given `opening` and closing `chars` of generic type `Wrapped`. */ rewrap(opening, closing) { return new Wrap(opening, closing, this.text).valueOf(); } /** * Replaces the closing chars of the `Wrapper` object in the text of the `Wrapper` object with the given `closing` chars. * The replacement succeeds if the closing characters exist at the end of the text. * @param closing The closing chars of `string` to replace in the text(part of the primitive value). * @returns The return value is the text of `string` type with replaced closing chars. */ textReplaceClosing(closing) { return Wrapper.replaceClosing(this.text, this.closing, closing); } /** * Replaces the opening chars of the `Wrapper` object in the text of the `Wrapper` object with the given `opening` chars. The replacement * succeeds if the opening characters exist at the beginning of the text. * @param opening The opening chars of `string` to replace in the text(part of the primitive value). * @returns The return value is the text of `string` type with replaced opening chars. */ textReplaceOpening(opening) { return Wrapper.replaceOpening(this.text, this.opening, opening); } /** * The method returns the text of the `Wrapper` object without its opening and closing chars or the given `opening` and `closing` chars. * @param opening Optional opening chars of `string` type to remove from the beginning of the text of the `Wrapper` instance. By default, * its value is equal to the opening chars of the `Wrapper` instance. * @param closing Optional closing chars of `string` type to remove from the end of the text of the `Wrapper` instance. By default, its * value is equal to the closing chars of the `Wrapper` instance. * @returns The return value is the text of `string` type without the opening and closing chars of the `Wrapper` object or given `opening` * and `closing` chars. */ textUnwrap(opening = this.opening, closing = this.closing) { return Wrapper.unwrap(this.text, opening, closing); } /** * Returns an `array` consisting of the opening chars, text, and closing chars. * @returns The return value is a read-only `array` consisting of the opening chars, text, and closing chars. */ toArray() { return [this.opening, this.text, this.closing]; } /** * Returns the `Wrap` instance consists of the text, opening and closing chars of the `Wrapper` object. * @returns The return value is an instance of `Wrap` consisting of the text, opening, and closing chars of the `Wrapper` object. */ toWrap() { return new Wrap(this.opening, this.closing, this.text); } /** * Returns the text without the opening and closing chars. * @returns The return value is the text of a generic type variable `Text`. */ unwrap() { return this.text; } /** * The method returns the primitive value of a specified `Wrapper` object with text unwrapped from the opening and closing chars of the * `Wrapper` instance or given `opening` and `closing` chars. * @param opening Optional opening chars of `string` type to remove from the beginning of the text of the `Wrapper` instance. By default, * its value is equal to the opening chars of the `Wrapper` instance. * @param closing Optional closing chars of `string` type to remove from the end of the text of the `Wrapper` instance. By default, its * value is equal to the closing chars of the `Wrapper` instance. * @returns The return value is the primitive value of `string` type with text unwrapped from the opening and closing chars of the * `Wrapper` object or the given `opening` and `closing` chars. */ unwrapText(opening = this.opening, closing = this.closing) { return `${this.opening}${Wrapper.unwrap(this.text, opening, closing)}${this.closing}`; } /** * The method wraps the primitive value of a specified `Wrapper` object by its opening and closing chars or given `opening` and `closing` * chars. * @param opening Optional opening chars of a generic type variable `CustomOpening` to wrap the primitive value of the `Wrapper` instance. * By default, its value is equal to the opening chars of the `Wrapper` instance. * @param closing Optional closing chars of a generic type variable `CustomClosing` to wrap the primitive value of the `Wrapper` instance. * By default, its value is equal to the closing chars of the `Wrapper` instance. * @returns The return value is a primitive value wrapped by the opening and closing chars of the `Wrapper` instance or the given * `opening` and `closing` chars. */ wrap(opening = this.opening, closing = this.closing) { return new Wrap(opening, closing, this.valueOf()).valueOf(); } /** * The method wraps the given `text` with the wrap, the `opening`, and `closing` chars of the `Wrapper` object. * @param text The text of generic type variable `CustomText` to wrap by the opening and closing chars of the `Wrapper` instance. * @returns The return value is the given `text` wrapped by the opening and closing chars of the `Wrapper` object of the generic type * `Wrapped`. */ wrapOn(text) { return new Wrap(this.opening, this.closing, text).valueOf(); } /** * The method returns the primitive value of the `Wrapper` object with text wrapped by given `opening` and `closing` chars. * @param opening The opening chars of a generic type variable `TextOpening` to wrap the text of the `Wrapper` instance. * @param closing The closing chars of a generic type variable `TextClosing` to wrap the text of the `Wrapper` instance. * @returns The return value is the primitive value with wrapped text by given opening and closing characters of generic type `Wrapped`. */ wrapText(opening, closing) { return `${this.opening}${this.rewrap(opening, closing)}${this.closing}`; } } /* * Public API Surface of wrapper */ /** * Generated bundle index. Do not edit. */ export { Wrap, Wrapper }; //# sourceMappingURL=typescript-package-wrapper.mjs.map