join-newlines
TypeScript icon, indicating that this package has built-in type declarations

2.0.2 • Public • Published

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

Package Sidebar

Install

npm i join-newlines

Weekly Downloads

44

Version

2.0.2

License

MIT

Unpacked Size

4.45 kB

Total Files

8

Last publish

Collaborators

  • bconnorwhite