← Back to utilities
string

pad-start

Pads the start of a string with another string until it reaches the target length. If padString is '', the original string is returned (mirrors native behavior).

Installation

npx fragmen add string/pad-start

Source Code

/**
 * Pads the start of a string with another string until it reaches the target length.
 *
 * @tags pure, string-manipulation, formatting
 * @param {string} str The string to pad.
 * @param {number} targetLength The desired length of the resulting string.
 * @param {string} padString The string to pad with. Defaults to a space ' '.
 *   If padString is '', the original string is returned (mirrors native behavior).
 * @returns {string} The padded string. Returns '' for non-string input.
 * @example
 * padStart('abc', 5, '0')
 * // => '00abc'
 */
export function padStart(
  str: string,
  targetLength: number,
  padString = ' '
): string {
  if (typeof str !== 'string') {
    return '';
  }
  const needed = targetLength - str.length;
  if (needed <= 0) {
    return str;
  }

  if (padString === '') {
    return str;
  }

  if (padString.length < needed) {
    const repeatCount = Math.ceil(needed / padString.length);
    padString = padString.repeat(repeatCount);
  }

  return padString.slice(0, needed) + str;
}

Examples

padStart('abc', 5, '0')
// => '00abc'

Related Utilities

camel-case

string

Converts a string to camelCase. Transforms a string by removing spaces, underscores, and hyphens, then capitalizing the first letter of each word except the first one. Commonly used for JavaScript variable names, object properties, and function names.

#pure#string-manipulation#formatting

capitalize

string

Capitalizes the first letter of a string. Converts the first character to uppercase while leaving the rest unchanged. Safely handles edge cases like empty strings and non-string inputs.

#pure#string-manipulation#formatting

kebab-case

string

Converts a string to kebab-case. Transforms a string by converting it to lowercase and replacing spaces, underscores, and camelCase boundaries with hyphens. Useful for creating URL slugs, CSS class names, and file names.

#pure#string-manipulation#formatting

pad-end

string

Pads the end of a string with another string until it reaches the target length. If padString is '', the original string is returned (mirrors native behavior).

#pure#string-manipulation#formatting

pascal-case

string

Converts a string to PascalCase. Transforms a string by removing spaces, underscores, and hyphens, then capitalizing the first letter of each word including the first one. Commonly used for class names, interface names, type names, and component names.

#pure#string-manipulation#formatting

snake-case

string

Converts a string to snake_case. Transforms a string by converting it to lowercase and replacing spaces, hyphens, and camelCase boundaries with underscores. Commonly used for database column names, Python variables, and configuration keys.

#pure#string-manipulation#formatting

Quick Actions

Estimated size:1.00 KB

Tags

Parameters

strstring

The string to pad.

targetLengthnumber

The desired length of the resulting string.

padStringstring

The string to pad with. Defaults to a space ' '.

Returns

string

The padded string. Returns '' for non-string input.