@@ -2252,38 +2252,24 @@ namespace ts {
2252
2252
2253
2253
2254
2254
/**
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 " ").
2258
2260
*/
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 ;
2270
2263
}
2271
2264
2272
2265
/**
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 " ").
2276
2271
*/
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 ) ;
2288
2274
}
2289
2275
}
0 commit comments