Skip to content

Commit 4c5e463

Browse files
committed
Simplifying padLeft() to only allow spaces or zeros for padding. Updated padRight() to mirror padLeft() but without the option to use zeros since that would be an unlikely use case.
1 parent ce1d76c commit 4c5e463

File tree

1 file changed

+14
-28
lines changed

1 file changed

+14
-28
lines changed

src/compiler/core.ts

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2252,38 +2252,24 @@ namespace ts {
22522252

22532253

22542254
/**
2255-
* Returns 's' left-padded with copies of 'padString' until it reaches the given 'length'.
2256-
* If omitted, 'padString' defaults to a single space.
2257-
* If 'length' is less than or equal to 's.length' or 'padString' is empty, returns 's' unchanged.
2255+
* Returns string left-padded with spaces or zeros until it reaches the given length.
2256+
*
2257+
* @param s String to pad.
2258+
* @param length Final padded length. If less than or equal to 's.length', returns 's' unchanged.
2259+
* @param padString Character to use as padding (default " ").
22582260
*/
2259-
export function padLeft(s: string, length: number, padString = " ") {
2260-
if (length <= s.length || padString.length === 0) {
2261-
return s;
2262-
}
2263-
else {
2264-
length -= s.length; // how much padding we need
2265-
if (length > padString.length) { // need more than one copy of padString
2266-
padString += padString.repeat(length / padString.length);
2267-
}
2268-
return padString.slice(0, length) + s;
2269-
}
2261+
export function padLeft(s: string, length: number, padString: " " | "0" = " ") {
2262+
return length <= s.length ? s : padString.repeat(length - s.length) + s;
22702263
}
22712264

22722265
/**
2273-
* Returns 's' right-padded with copies of 'padString' until it reaches the given 'length'.
2274-
* If omitted, 'padString' defaults to a single space.
2275-
* If 'length' is less than or equal to 's.length' or 'padString' is empty, returns 's' unchanged.
2266+
* Returns string right-padded with spaces until it reaches the given length.
2267+
*
2268+
* @param s String to pad.
2269+
* @param length Final padded length. If less than or equal to 's.length', returns 's' unchanged.
2270+
* @param padString Character to use as padding (default " ").
22762271
*/
2277-
export function padRight(s: string, length: number, padString = " ") {
2278-
if (length <= s.length || padString.length === 0) {
2279-
return s;
2280-
}
2281-
else {
2282-
length -= s.length;
2283-
if (length > padString.length) { // need more than one copy of padString
2284-
padString += padString.repeat(length / padString.length);
2285-
}
2286-
return s + padString.slice(0, length);
2287-
}
2272+
export function padRight(s: string, length: number, padString: " " = " ") {
2273+
return length <= s.length ? s : s + padString.repeat(length - s.length);
22882274
}
22892275
}

0 commit comments

Comments
 (0)