Implementing String.prototype.padStart
The padStart method is a powerful string manipulation tool that ensures a string reaches a specific length by padding it with a specified character (or space by default) at the beginning. This is incredibly useful for formatting data, aligning text in tables, or creating consistent string lengths for processing. Your task is to implement this method as an extension to the JavaScript String prototype.
Problem Description
You need to implement a padStart method on the String.prototype. This method should take two arguments:
targetLength: An integer representing the desired length of the resulting string.padString: A string used for padding. If not provided, the default padding character is a space (" ").
The padStart method should return a new string that is at least targetLength characters long. If the original string's length is already greater than or equal to targetLength, the original string should be returned unchanged. If the original string's length is less than targetLength, the beginning of the string should be padded with the padString until the resulting string reaches the targetLength. The padString should be repeated as necessary to achieve the desired length.
Key Requirements:
- The method must be added to the
String.prototype. - The method must handle cases where
targetLengthis less than the original string's length. - The method must handle cases where
padStringis not provided (defaulting to a space). - The method must handle cases where
padStringis an empty string. - The method must return a new string, not modify the original.
Examples
Example 1:
Input: "hello", 10, "x"
Output: "xxxxxhello"
Explanation: The original string "hello" has length 5. We need to add 5 padding characters ("x") to the beginning to reach a length of 10.
Example 2:
Input: "world", 7
Output: " world"
Explanation: The original string "world" has length 5. We need to add 2 padding characters (spaces, as no `padString` is provided) to the beginning to reach a length of 7.
Example 3:
Input: "abc", 3
Output: "abc"
Explanation: The original string "abc" has length 3, which is equal to `targetLength`. The original string is returned unchanged.
Example 4:
Input: "short", 15, "0"
Output: "000000000short"
Explanation: The original string "short" has length 5. We need to add 10 padding characters ("0") to the beginning to reach a length of 15.
Example 5:
Input: "test", 4, ""
Output: "test"
Explanation: The original string "test" has length 4, which is equal to `targetLength`. The original string is returned unchanged.
Constraints
targetLengthwill always be an integer.targetLengthwill be non-negative.padStringwill be a string.- The method should be performant for strings of reasonable length (up to a few thousand characters).
- The method should not throw errors for valid inputs.
Notes
Consider using string concatenation or other efficient string manipulation techniques to build the padded string. Think about how to handle the repetition of the padString when the targetLength is significantly larger than the original string's length. Remember that you are extending the String prototype, so your solution should modify the behavior of strings in JavaScript.