What is strip-final-newline?
The strip-final-newline npm package is a simple utility that removes the final newline character from a string. This can be useful when you want to process text data and ensure that it does not end with a newline character, which might affect the handling of the text in certain situations.
What are strip-final-newline's main functionalities?
Strip final newline from a string
This feature allows you to remove the final newline character from a given string. The code sample demonstrates how to use the strip-final-newline package to strip the newline from the end of a string.
const stripFinalNewline = require('strip-final-newline');
const stringWithNewline = 'Hello World!\n';
const stringWithoutNewline = stripFinalNewline(stringWithNewline);
console.log(stringWithoutNewline); // 'Hello World!'
Other packages similar to strip-final-newline
newline-remove
The newline-remove package is similar to strip-final-newline in that it is designed to remove newline characters from strings. However, it may offer different options or behaviors, such as removing all newline characters, not just the final one.
trim-newlines
The trim-newlines package offers functionality to trim newline characters from the start and/or end of a string. It provides more options than strip-final-newline, which only removes the final newline character.
strip-final-newline
Strip the final newline character from a string/buffer
Can be useful when parsing the output of, for example, ChildProcess#execFile
, as binaries usually output a newline at the end. Normally, you would use stdout.trim()
, but that would also remove newlines at the start and whitespace.
Install
$ npm install strip-final-newline
Usage
const stripFinalNewline = require('strip-final-newline');
stripFinalNewline('foo\nbar\n\n');
stripFinalNewline(Buffer.from('foo\nbar\n\n')).toString();
License
MIT © Sindre Sorhus