Security News
Internet Archive Hacked, 31 Million Record Compromised
The Internet Archive's "Wayback Machine" has been hacked and defaced, with 31 millions records compromised.
strip-final-newline
Advanced tools
Strip the final newline character from a string or Uint8Array
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.
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!'
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.
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 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.
npm install strip-final-newline
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'
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());
FAQs
Strip the final newline character from a string or Uint8Array
The npm package strip-final-newline receives a total of 30,573,764 weekly downloads. As such, strip-final-newline popularity was classified as popular.
We found that strip-final-newline demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The Internet Archive's "Wayback Machine" has been hacked and defaced, with 31 millions records compromised.
Security News
TC39 is meeting in Tokyo this week and they have approved nearly a dozen proposals to advance to the next stages.
Security News
Our threat research team breaks down two malicious npm packages designed to exploit developer trust, steal your data, and destroy data on your machine.