or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-lodash--trim

The lodash method _.trim exported as a module for removing leading and trailing whitespace or specified characters from strings.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/lodash.trim@4.5.x

To install, run

npx @tessl/cli install tessl/npm-lodash--trim@4.5.0

index.mddocs/

lodash.trim

The lodash method _.trim exported as a Node.js module for removing leading and trailing whitespace or specified characters from strings with comprehensive Unicode support.

Package Information

  • Package Name: lodash.trim
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install lodash.trim

Core Imports

const trim = require('lodash.trim');

For ES modules (if your environment supports it):

import trim from 'lodash.trim';

Basic Usage

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']

Capabilities

String Trimming

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)

Advanced Features

Unicode Support

The trim function provides comprehensive Unicode support including:

  • Astral Plane Characters: Properly handles characters beyond the Basic Multilingual Plane (BMP)
  • Combining Characters: Correctly processes combining marks and diacritical marks
  • Zero-Width Joiners: Supports complex emoji and character sequences with ZWJ
  • Surrogate Pairs: Handles high and low surrogate pairs for extended Unicode characters
  • Regional Indicators: Properly processes flag emoji and regional sequences
  • Fitzpatrick Modifiers: Supports skin tone modifiers for emoji characters

Performance Optimization

The function uses different processing strategies based on string content:

  • ASCII-only strings: Uses faster character-by-character processing
  • Unicode strings: Employs sophisticated Unicode-aware parsing to maintain character integrity
  • Whitespace-only trimming: Optimized regex-based approach for default behavior

Cross-Platform Compatibility

The module works consistently across different JavaScript environments:

  • Node.js: Full compatibility with all Node.js versions
  • Browsers: Works in modern browsers with proper Unicode support
  • Global Detection: Automatically detects and adapts to different global object patterns

Error Handling

The function gracefully handles edge cases:

  • Null/undefined inputs: Converts to empty string
  • Non-string inputs: Converts to string using internal toString implementation
  • Symbol inputs: Properly converts Symbol values to string representation
  • Invalid Unicode sequences: Handles malformed Unicode gracefully

Type Conversion

Input values are automatically converted to strings using lodash's internal toString function:

  • Numbers: 42 becomes "42"
  • Booleans: true becomes "true"
  • Arrays: [1,2,3] becomes "1,2,3"
  • Objects: Uses Object.prototype.toString or custom toString methods
  • Symbols: Converts to string representation using Symbol.prototype.toString
  • Null/undefined: Becomes empty string ""
  • -0: Preserves the sign and becomes "-0"