What is trim-newlines?
The trim-newlines npm package is used to remove newlines from the start and/or end of a string. It provides a simple way to trim newline characters from the beginning, end, or both ends of a string, which can be useful when processing text data.
What are trim-newlines's main functionalities?
Start
Removes newline characters from the start of a string.
const trimNewlines = require('trim-newlines');
console.log(trimNewlines.start('\n\nUnicorn\n'));
//=> 'Unicorn\n'
End
Removes newline characters from the end of a string.
const trimNewlines = require('trim-newlines');
console.log(trimNewlines.end('Unicorn\n\n'));
//=> 'Unicorn'
Start and End
Removes newline characters from both the start and end of a string.
const trimNewlines = require('trim-newlines');
console.log(trimNewlines('\n\nUnicorn\n\n'));
//=> 'Unicorn'
Other packages similar to trim-newlines
trim
The 'trim' package is similar to 'trim-newlines' but it trims all whitespace characters from the beginning and end of a string, not just newlines.
lodash.trim
The 'lodash.trim' function from the Lodash library is another alternative that trims whitespace from the beginning and end of a string. It is part of a larger utility library and offers more customization options than 'trim-newlines'.
string.prototype.trim
The 'string.prototype.trim' package is a polyfill for the native JavaScript String.prototype.trim method, which removes whitespace from both ends of a string. It is similar to 'trim-newlines' but targets all whitespace, not just newlines.
trim-newlines
Trim newlines from the start and/or end of a string
Looking to trim all whitespace, not just newlines? Use String#trim()
, String#trimStart()
, or String#trimEnd()
.
Install
npm install trim-newlines
Usage
import {trimNewlines, trimNewlinesStart, trimNewlinesEnd} from 'trim-newlines';
trimNewlines('\n🦄\n🦄\r\n');
trimNewlinesStart('\n🦄\r\n');
trimNewlinesEnd('\n🦄\r\n');
API
trimNewlines(string)
Trim from the start and end of a string.
trimNewlinesStart(string)
Trim from the start of a string.
trimNewlinesEnd(string)
Trim from the end of a string.