Last active
November 2, 2020 22:43
-
-
Save mistercoffee66/6bfdaf438e8434ab05750279529bc5d9 to your computer and use it in GitHub Desktop.
trim anything
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const trimAnything = (str, trimChar, opts = { start: false, end: true }) => { | |
if (str.length > 1 || opts.allowEmpty) { | |
let pattern; | |
if (opts.start && !opts.end) { | |
pattern = `^\\${trimChar}+`; | |
} else if (!opts.start && opts.end) { | |
pattern = `\\${trimChar}+$`; | |
} else { | |
pattern = `^\\${trimChar}+|\\${trimChar}+$`; | |
} | |
return str.replace(new RegExp(pattern, 'g'), ''); | |
} | |
return str; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment