Skip to content

Instantly share code, notes, and snippets.

@lucymtc
Created January 30, 2020 05:58
Show Gist options
  • Save lucymtc/276f882720d3b1dc0bad150148ace556 to your computer and use it in GitHub Desktop.
Save lucymtc/276f882720d3b1dc0bad150148ace556 to your computer and use it in GitHub Desktop.
Javascript rtrim, ltrim, trim helpers
export const ltrim = (str, char) => {
let message = str;
if (typeof message !== 'undefined') {
while (message[0] === char) {
message = message.substring(1);
}
}
return message;
};
export const rtrim = (str, char) => {
let message = str;
if (typeof message !== 'undefined') {
while (message[message.length - 1] === char) {
message = message.substring(0, message.length - 1);
}
}
return message;
};
export const trim = (str, char) => {
return ltrim(rtrim(str, char), char);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment