Files
2025-11-28 19:45:44 -07:00

26 lines
683 B
JavaScript

function trimEnd(str, chars) {
if (chars === undefined) {
return str.trimEnd();
}
let endIndex = str.length;
switch (typeof chars) {
case 'string': {
if (chars.length !== 1) {
throw new Error(`The 'chars' parameter should be a single character string.`);
}
while (endIndex > 0 && str[endIndex - 1] === chars) {
endIndex--;
}
break;
}
case 'object': {
while (endIndex > 0 && chars.includes(str[endIndex - 1])) {
endIndex--;
}
}
}
return str.substring(0, endIndex);
}
export { trimEnd };