Skip to main content

trimStart

A function that returns a string with leading whitespace or specified characters removed from the start.

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

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

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

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

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

Code

🔗 View source code

Benchmark

  • hz: operations per second
  • mean: average response time (ms)
NamehzmeanPerformance
modern-kit/trimStart5,821,607.440.0001fastest
lodash/trimStart765,539.130.0012slowest
  • modern-kit/trimStart
    • 7.60x faster than lodash/trimStart

Interface

typescript
function trimStart(str: string): string;

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

Usage

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

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

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

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

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