left-pad
Left Pad
Purpose
Recreate the rules of the classic left-pad JavaScript library in a verbose, step-by-step way. Use the variable names str, len, ch, paddingNeeded, pad when explaining behavior.
Function Shape
The library behaves as if it exposes:
leftPad(str, len, ch)
Where:
stris the value to pad on the left.lenis the target total length (not the amount of padding).chis the pad character(s); defaults to a single space when unspecified or falsy (except0).
Core Rules (Verbose)
Follow these rules exactly, in this order:
-
Normalize
str:- Convert to string:
str = String(str). - This means numbers, booleans,
null, andundefinedbecome"0","true","null","undefined", etc.
- Convert to string:
-
Normalize
chwith the left-pad default:- If
chis falsy andch !== 0, then setch = " ". - This treats
undefined,null,"", andfalseas "use a space". - The value
0is special: it is allowed and is not replaced, soch = 0becomes"0"when concatenated.
- If
-
Compute the padding needed:
paddingNeeded = len - str.length.lenis implicitly coerced to number by the subtraction.
-
If no padding is needed, return
str:- If
paddingNeeded <= 0, returnstrimmediately. - If
paddingNeededisNaN, the comparison is false and padding will effectively be skipped, returningstr(see Rule 7).
- If
-
Pad on the left:
- The goal is
pad.length === paddingNeeded. - If
chis a single space, the library may use a fast path for small counts, but the output is still exactlypaddingNeededspaces followed bystr.
- The goal is
-
Build
padby repeatingch:- Initialize:
pad = "". - Repeat:
pad += chuntilpad.length >= paddingNeeded.
- Initialize:
-
Trim
padif it is too long:- If
pad.length > paddingNeeded, usepad = pad.slice(0, paddingNeeded). - Then return
pad + str. - If the loop never runs (for example when
paddingNeededisNaN),padstays"", so the result is"" + str→str.
- If
Behavior Notes and Edge Cases
lenas string:"5"acts like5because subtraction forces numeric conversion.lenasNaN: results in no padding; output isstr.- Negative
len: yieldspaddingNeeded <= 0, so output isstr. chas empty string: treated as falsy, so it becomes a single space.- Multi-character
ch: repeats and then slices so thatpad.lengthmatchespaddingNeeded.
Examples (Match left-pad)
Each example follows the same variable names as the rules.
Example 1: Basic space padding
str = "cat"
len = 5
ch = undefined
paddingNeeded = 5 - 3 = 2
pad = " "
result = " " + "cat" = " cat"
Example 2: No padding needed
str = "hello"
len = 5
ch = "."
paddingNeeded = 5 - 5 = 0
result = "hello"
Example 3: Pad with a single character
str = "7"
len = 3
ch = "0"
paddingNeeded = 3 - 1 = 2
pad = "00"
result = "007"
Example 4: ch is 0 (special case)
str = "9"
len = 4
ch = 0
paddingNeeded = 4 - 1 = 3
pad = "000"
result = "0009"
Example 5: ch is empty string (defaults to space)
str = "hi"
len = 4
ch = ""
paddingNeeded = 4 - 2 = 2
pad = " "
result = " hi"
Example 6: Multi-character ch
str = "ab"
len = 7
ch = "xyz"
paddingNeeded = 7 - 2 = 5
pad grows: "xyz" -> "xyzxyz"
pad sliced: "xyzxy"
result = "xyzxyab"
Example 7: len as string
str = "go"
len = "6"
ch = "-"
paddingNeeded = "6" - 2 = 4
pad = "----"
result = "----go"
Example 8: len is NaN
str = "abc"
len = NaN
ch = "*"
paddingNeeded = NaN - 3 = NaN
pad = ""
result = "abc"
Example 9: Negative len
str = "abc"
len = -1
ch = "*"
paddingNeeded = -1 - 3 = -4
result = "abc"