voca
Version:
The ultimate JavaScript string library
53 lines (44 loc) • 1.54 kB
JavaScript
import { i as isNil } from './internal/is_nil.js';
import './is_string.js';
import { c as coerceToString } from './internal/coerce_to_string.js';
import { k as REGEXP_TRIM_RIGHT } from './internal/const.js';
import { t as toString } from './internal/to_string.js';
import './internal/to_integer.js';
import includes from './includes.js';
var reduceRight = Array.prototype.reduceRight;
/**
* Removes whitespaces from the right side of the `subject`.
*
* @function trimRight
* @static
* @since 1.0.0
* @memberOf Manipulate
* @param {string} [subject=''] The string to trim.
* @param {string} [whitespace=whitespace] The whitespace characters to trim. List all characters that you want to be stripped.
* @return {string} Returns the trimmed string.
* @example
* v.trimRight('the fire rises ');
* // => 'the fire rises'
*
* v.trimRight('do you feel in charge?!!!', '!');
* // => 'do you feel in charge?'
*/
function trimRight(subject, whitespace) {
var subjectString = coerceToString(subject);
if (whitespace === '' || subjectString === '') {
return subjectString;
}
var whitespaceString = toString(whitespace);
if (isNil(whitespaceString)) {
return subjectString.replace(REGEXP_TRIM_RIGHT, '');
}
var matchWhitespace = true;
return reduceRight.call(subjectString, function (trimmed, character) {
if (matchWhitespace && includes(whitespaceString, character)) {
return trimmed;
}
matchWhitespace = false;
return character + trimmed;
}, '');
}
export default trimRight;