Skip to main content

trimEnd

A function that returns a string with trailing whitespace or specified characters removed from the end.

If the chars parameter is not provided, all trailing whitespace is removed. (Works the same as String.prototype.trimEnd.)

If chars is a string, it is split into individual characters and removed from the end of the string.

  • ex: '+-*' -> ['+', '-', '*']

If chars is an array, each string in the array is split into individual characters and removed from the end of the string.

  • ex: ['+*', '-'] -> ['+', '*', '-']

Code

🔗 View source code

Benchmark

  • hz: operations per second
  • mean: average response time (ms)
NamehzmeanPerformance
modern-kit/trimEnd5,487,666.270.0002fastest
lodash/trimEnd1,131,381.140.0009slowest
  • modern-kit/trimEnd
    • 4.85x faster than lodash/trimEnd

Interface

typescript
function trimEnd(str: string): string;

function trimEnd(str: string, chars: string | string[]): string;

Usage

typescript
import { trimEnd } from '@modern-kit/utils';

// When chars is not provided
const str1 = trimEnd('abc '); // 'abc'

// When chars is a string
const str2 = trimEnd('abc--', '-'); // 'abc'
const str3 = trimEnd('abc+-*', '+-*'); // 'abc'

// Case where no matching characters are found
const str4 = trimEnd('--abc ', '+'); // '--abc '

// When chars is an array
const str5 = trimEnd('abc-_-', ['_', '-']); // 'abc'
const str6 = trimEnd('abc+-*', ['+*', '-']); // 'abc'