1. strip-final-newline
Strip the final newline character from a string or Uint8Array
strip-final-newline
Package: strip-final-newline
Created by: sindresorhus
Last modified: Wed, 13 Dec 2023 19:31:50 GMT
Version: 4.0.0
License: MIT
Downloads: 191,295,856
Repository: https://github.com/sindresorhus/strip-final-newline

Install

npm install strip-final-newline
yarn add strip-final-newline

strip-final-newline

Strip the final newline character 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. You cannot use stdout.trimEnd() for this as it removes all trailing newlines and whitespaces at the end.

Install

 npm install strip-final-newline

Usage

 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().

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

RELATED POST

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Enhancing Vue.js Development: Harnessing the Potential of Vue-Loader

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Simplify Data Validation in Vue.js: A Step-by-Step Guide to Using Regex

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Troubleshooting Made Easy: Common Issues and Solutions with vue-loader Without vue-cli

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Optimizing Webpack 4 with Vue CLI 3: Disabling the Cache-Loader

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin

Step-by-Step Guide: How to Add a Function to Your Vuex Plugin