join-newlines
v2.0.2
Published
Join an array of strings with newlines
Downloads
360
Maintainers
Readme
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
Why not just type \n
in your string?
- Stacking each line in an array aligns nicely, which fixes issues with indentation, especially when using template strings.
Why not just type .join("\n")
after the array?
- Think this looks a little cleaner / more readable, and filters out
undefined
values.
- autorepo: Autorepo abstracts away your dev dependencies, providing a single command to run all of your scripts.