Feature request: support `join` option
scottrippey opened this issue · 7 comments
I would like a "join" option, so that multi-line strings could be both outdented and joined into a single line.
Examples:
example = outdent({ join: " " })`
This is a
multiline string
that is joined with spaces instead of newlines.
`; // "This is a multiline string that is joined with spaces instead of newlines."
Or even better:
example = outdent(" ")`
Just specify a string
to use 'join'.
`; // "Just specify a string to use 'join'."
A little backstory, I actually have my own outdent
function implemented in several projects, and use it constantly! I found this repo when thinking about publishing my outdent
. I'm glad to see it already exists!
This 'join' feature is something I use quite often, too, so if that's something you supported, I'd probably stop working on my own and "make the switch" to this one!
Let's discuss?
I do like the newline
option, which makes this feature "fit" better with this module.
My primary use-case is simply to break long string templates over multiple lines.
One of my projects is a chat-bot, so I've got to compose lengthy sentences. Eg:
robot.send(outdent(" ")`
The current version is ${currentVersion},
and you can update it by saying
"${robot.name} deploy ${newVersion} to prod".
`);
Being able to split up this single-line across 3 lines makes the code FAR more readable!
YAML actually has 9 different ways to write multi-line strings, all designed for maximizing readability.
All of them "strip" the indentation, and then join the strings in different ways.
https://stackoverflow.com/questions/3790454/in-yaml-how-do-i-break-a-string-over-multiple-lines
Relative indentation is a great feature, but I have no scenarios where it would be used together with single-line join.
I'd expect, if you use both features, you'd simply end up with the extra space inline.
Ok cool. The reason I asked is, if you don't need relative indentation then something as simple as the following will do what you want:
inputString.replace(/\s*[\r\n]\s*/g, ' ').trim();
However, if someone writes a PR implementing the newline
feature we discussed, I'm happy to merge and publish it. For what it's worth, the PR doesn't need to update any of the TypeScript stuff. It's easy for me to fix the type annotations given a working JS implementation.