# strip-final-newline

> Strip the final newline character from a string or Uint8Array

Latest version **4.0.0** (published 2023-12-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install strip-final-newline
pnpm add strip-final-newline
yarn add strip-final-newline
bun add strip-final-newline
```

## Health

**Score 45/100 (D)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 4.0.0 |
| Published | 2023-12-13 |
| First published | 2018-10-28 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=18 |
| Dependencies | 0 |
| Unpacked size | 4.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 37 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | strip, trim, remove, delete, final, last, end, file, newline, linebreak, character, string, uint8array |

## Links

- npm: https://www.npmjs.com/package/strip-final-newline
- Repository: https://github.com/sindresorhus/strip-final-newline
- Homepage: https://github.com/sindresorhus/strip-final-newline#readme
- Issues: https://github.com/sindresorhus/strip-final-newline/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/strip-final-newline

## Alternatives

- [unionfs](https://npm.io/package/unionfs.md) — 2.2M weekly downloads
- [path-starts-with](https://npm.io/package/path-starts-with.md) — 35.9K weekly downloads
- [redzip](https://npm.io/package/redzip.md) — 1.2K weekly downloads
- [vscode-anymatch](https://npm.io/package/vscode-anymatch.md) — 848 weekly downloads
- [@ledgerhq/coin-filecoin](https://npm.io/package/@ledgerhq/coin-filecoin.md) — 793 weekly downloads

## Recent versions

- 4.0.0 (latest) — 2023-12-13
- 3.0.0 — 2021-05-03
- 2.0.0 — 2018-10-28

## README

# strip-final-newline

> Strip the final [newline character](https://en.wikipedia.org/wiki/Newline) from a string or Uint8Array.

This can be useful when parsing the output of, for example, `ChildProcess#execFile()`, as [binaries usually output a newline at the end](https://stackoverflow.com/questions/729692/why-should-text-files-end-with-a-newline). You cannot use `stdout.trimEnd()` for this as it removes all trailing newlines and whitespaces at the end.

## Install

```sh
npm install strip-final-newline
```

## Usage

```js
import stripFinalNewline from 'strip-final-newline';

stripFinalNewline('foo\nbar\n\n');
//=> 'foo\nbar\n'

const uint8Array = new TextEncoder().encode('foo\nbar\n\n')
new TextDecoder().decode(stripFinalNewline(uint8Array));
//=> 'foo\nbar\n'
```

## Performance

When using an `Uint8Array`, the original value is referenced, not copied. This is much more efficient, requires almost no memory, and remains milliseconds fast even on very large inputs.

If you'd like to ensure that modifying the return value does not also modify the value passed as input, please use `.slice()`.

```js
const value = new TextDecoder().decode(stripFinalNewline(uint8Array).slice());
```

---
_Source: https://npm.io/package/strip-final-newline · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
