Join an array of strings with newlines.
If I should maintain this repo, please
DM me on Twitter if you have questions or suggestions.
Installation
yarn add join-newlines
npm install join-newlines
pnpm add join-newlines
API
The provided array of strings is joined with newlines. Any undefined
values are filterd out.
Additionally, you can pass true
as a second argument to append a newline to the end of the string.
import join from "join-newlines";
join([
"This is the first line.",
"Then I want this on a second line.",
"I also want this on a third line."
]);
// "This is the first line.\nThen I want this on a second line.\nI also want this on a third line."
join([
"This is the first line.",
"Then I want this on a second line.",
undefined,
"I also want this on a third line."
]);
// "This is the first line.\nThen I want this on a second line.\nI also want this on a third line."
join([
"This is the first line.",
"Then I want this on a second line.",
"I also want a newline at the end."
], true); // Add true to append a newline to the end as well.
// "This is the first line.\nThen I want this on a second line.\nI also want a newline at the end.\n"
FAQ
\n
in your string?
Why not just type - Stacking each line in an array aligns nicely, which fixes issues with indentation, especially when using template strings.
.join("\n")
after the array?
Why not just type - Think this looks a little cleaner / more readable, and filters out
undefined
values.
Dev Dependencies
- autorepo: Autorepo abstracts away your dev dependencies, providing a single command to run all of your scripts.