Skip to content

bconnorwhite/join-newlines

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

join-newlines

NPM TypeScript

Join an array of strings with newlines.

If I should maintain this repo, please ⭐️ GitHub stars

DM me on Twitter if you have questions or suggestions. Twitter


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.

Dev Dependencies

  • autorepo: Autorepo abstracts away your dev dependencies, providing a single command to run all of your scripts.

License license

MIT

About

Join an array of strings with newlines

Resources

Stars

Watchers

Forks

Packages

No packages published