Pad methods?

4
closed
levithomason
levithomason
Posted 4 years ago

Pad methods? #16

Curious if it is in the interest of this lib to support methods for padding strings.

davezuko
davezuko
Created 4 years ago

I think some simple ones (padLeft, padRight, etc.) would be a welcome addition :D.

levithomason
levithomason
Created 4 years ago

How about?:

pad(num: number, char: string, str: string): string
padLeft(num: number, char: string, str: string): string
padRight(num: number, char: string, str: string): string

num - the total minimum width of the returned string
char - the character to pad with
str - the input string to pad

---

pad(4, ' ', 'yo')          // => ' yo '
padLeft(16, 'X', '1234')   // => 'XXXXXXXXXXXX1234'
padRight(10, '-', '567')   // => '573-------'
davezuko
davezuko
Created 4 years ago

Those all look good to me. I think there are some oddities with handling foreign characters, but I'd be satisfied with an implementation that works with the majority use case and then address issues should they arise.

davezuko
davezuko
Created 4 years ago

All 3 of these have been added :).