The lodash method _.trim exported as a module for removing leading and trailing whitespace or specified characters from strings.
npx @tessl/cli install tessl/npm-lodash--trim@4.5.0The lodash method _.trim exported as a Node.js module for removing leading and trailing whitespace or specified characters from strings with comprehensive Unicode support.
npm install lodash.trimconst trim = require('lodash.trim');For ES modules (if your environment supports it):
import trim from 'lodash.trim';const trim = require('lodash.trim');
// Trim whitespace from both ends
const result1 = trim(' hello world ');
// => 'hello world'
// Trim specific characters
const result2 = trim('-_-abc-_-', '_-');
// => 'abc'
// Works with Unicode characters
const result3 = trim(' café ');
// => 'café'
// Use as iteratee function
const inputs = [' foo ', ' bar '];
const cleaned = inputs.map(trim);
// => ['foo', 'bar']Removes leading and trailing whitespace or specified characters from strings with advanced Unicode support. When used as an iteratee function (e.g., with map), only whitespace trimming is performed regardless of the chars parameter.
/**
* Removes leading and trailing whitespace or specified characters from string.
* @param {string} string - The string to trim. Null/undefined values are converted to empty string.
* @param {string} chars - The characters to trim. When omitted, trims all whitespace characters.
* @returns {string} Returns the trimmed string
*/
function trim(string, chars);Parameters:
string (string): The string to trim. Null and undefined values are automatically converted to empty string.chars (string, optional): The characters to trim from both ends. When not provided, trims all whitespace characters including Unicode whitespace.Returns:
(string): The trimmed string with specified characters or whitespace removed from both ends.
Usage Examples:
const trim = require('lodash.trim');
// Basic whitespace trimming
trim(' hello ');
// => 'hello'
// Custom character trimming
trim('__hello__', '_');
// => 'hello'
// Multiple custom characters
trim('-_-hello-_-', '_-');
// => 'hello'
// Unicode whitespace support
trim('\u2000\u2001hello\u2002\u2003');
// => 'hello'
// Complex Unicode characters with combining marks
trim(' café\u0301 '); // é with combining acute accent
// => 'café\u0301'
// Empty string handling
trim('');
// => ''
// Null/undefined handling
trim(null);
// => ''
trim(undefined);
// => ''
// Use as iteratee (only trims whitespace when used this way)
[' foo ', ' bar ', ' baz '].map(trim);
// => ['foo', 'bar', 'baz']
// Note: When used as an iteratee function, custom chars parameter is ignored
['__foo__', '__bar__', '__baz__'].map(trim);
// => ['__foo__', '__bar__', '__baz__'] (underscores not removed)The trim function provides comprehensive Unicode support including:
The function uses different processing strategies based on string content:
The module works consistently across different JavaScript environments:
The function gracefully handles edge cases:
toString implementationInput values are automatically converted to strings using lodash's internal toString function:
42 becomes "42"true becomes "true"[1,2,3] becomes "1,2,3"Object.prototype.toString or custom toString methodsSymbol.prototype.toString""-0: Preserves the sign and becomes "-0"