What is lodash.trim?
The lodash.trim package is a utility library that provides a simple function to trim whitespace from the beginning and end of a string. It is part of the larger Lodash library, which is known for its utility functions for common programming tasks.
What are lodash.trim's main functionalities?
Trim whitespace from both ends of a string
This feature removes whitespace from both the beginning and the end of the input string.
const _ = require('lodash.trim');
const trimmedString = _.trim(' Hello World ');
console.log(trimmedString); // 'Hello World'
Trim specific characters from both ends of a string
This feature allows you to specify characters to be trimmed from both ends of the input string.
const _ = require('lodash.trim');
const trimmedString = _.trim('-_-Hello World-_-', '_-');
console.log(trimmedString); // 'Hello World'
Other packages similar to lodash.trim
trim
The 'trim' package is a simple utility to trim whitespace from both ends of a string. It is similar to lodash.trim but does not offer the ability to trim specific characters.
string.prototype.trim
The 'string.prototype.trim' package is a polyfill for the native String.prototype.trim method. It provides similar functionality to lodash.trim but is focused on ensuring compatibility with older JavaScript environments.
underscore.string
The 'underscore.string' package is a string manipulation library that includes a trim function among many other utilities. It is similar to lodash.trim but offers a broader range of string manipulation functions.
lodash.trim v4.3.0
The lodash method _.trim
exported as a Node.js module.
Installation
Using npm:
$ {sudo -H} npm i -g npm
$ npm i --save lodash.trim
In Node.js:
var trim = require('lodash.trim');
See the documentation or package source for more details.