Created
March 4, 2016 09:49
-
-
Save dhoko/4b0c20e66c8a3f1aa431 to your computer and use it in GitHub Desktop.
Template Strings - remove spaces
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
| /** | |
| * From @link {https://gist.github.com/zenparsing/5dffde82d9acef19e43c} | |
| * Usage: | |
| * output = dedent`Monique mange: | |
| * - une pomme | |
| * - une poire | |
| * Voilou` | |
| * | |
| * Output: | |
| * 'Monique mange: | |
| * - une pomme | |
| * - une poire | |
| * Voilou' | |
| * | |
| * Remove useless spaces | |
| */ | |
| function dedent(callSite, ...args) { | |
| function format(str) { | |
| let size = -1; | |
| return str.replace(/\n(\s+)/g, (m, m1) => { | |
| if (size < 0) { | |
| size = m1.replace(/\t/g, " ").length; | |
| } | |
| return "\n" + m1.slice(Math.min(m1.length, size)); | |
| }); | |
| } | |
| if (typeof callSite === 'string') { | |
| return format(callSite); | |
| } | |
| if (typeof callSite === 'function') { | |
| return (...args) => format(callSite(...args)); | |
| } | |
| const output = callSite | |
| .slice(0, args.length + 1) | |
| .map((text, i) => (i === 0 ? "" : args[i - 1]) + text) | |
| .join(""); | |
| return format(output); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment