UNPKG

@rxap/utilities

Version:

A collection of utility functions, types and interfaces.

33 lines 1.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CoerceSuffix = CoerceSuffix; /** * Coerces a suffix onto a string if it does not already end with that suffix. * * @export * @function CoerceSuffix * @param {string} str - The string to which the suffix will be appended. * @param {string} suffix - The suffix to append to the string. * @param {RegExp} [regexp] - Optional. A regular expression to match against the string. If not provided, a new RegExp will be created that matches the suffix at the end of the string. * * @returns {string} - Returns the original string if it matches the regular expression or if it already ends with the suffix. Otherwise, returns the original string with the suffix appended. * * @example * // returns 'Hello World!' * CoerceSuffix('Hello World', '!', /!$/) * * @example * // returns 'Hello World!' * CoerceSuffix('Hello World', '!') * * @example * // returns 'Hello World!' * CoerceSuffix('Hello World!', '!') */ function CoerceSuffix(str, suffix, regexp) { if (!str.match(regexp !== null && regexp !== void 0 ? regexp : new RegExp(`${suffix}$`))) { return str + suffix; } return str; } //# sourceMappingURL=coerce-suffix.js.map