Feature: padding text

58 views
Skip to first unread message

Ricardo “Rudxain” Fernández Serrata

unread,
May 14, 2024, 5:13:14 PMMay 14
to Automate for Android
I find it weird that there's so many `*Format` functions and even a `trim` fn, but no `pad` or `repeat`. Why don't they exist?

Ricardo “Rudxain” Fernández Serrata

unread,
May 14, 2024, 5:20:24 PMMay 14
to Automate for Android
And those fns don't have to be restricted to text strings: `repeat` would be very useful to generate arrays pre-filled with a value (although that would require the implementation to deep-`copy` the value for every iteration, which seems too expensive)

Henrik "The Developer" Lindqvist

unread,
May 15, 2024, 5:57:58 AMMay 15
to Automate for Android
Maybe so, padding is mostly useful for "console" printing, since HTML and other GUI usually don't show white space. Padding numbers can already be done with numberFormat.
I'll consider adding padStart and padEnd functions.
Could you give me a use cases?

Henrik "The Developer" Lindqvist

unread,
May 15, 2024, 6:16:46 AMMay 15
to Automate for Android
A repeat would probably only work for text, for arrays a fill, taking start/end indices or start+length, would be more appropriate.
But i doubt repeat function would see much use, can you give me a use-case?

Ricardo “Rudxain” Fernández Serrata

unread,
May 15, 2024, 9:16:04 PMMay 15
to Automate for Android
Both `pad` and `repeat` can be defined in terms of each other, but `repeat` is much simpler, so it makes sense for `pad` to be "canonically" defined in terms of `repeat`. I agree that `repeat` doesn't seem so useful, especially if `pad` exists as an alternative.

BTW, padding doesn't have to be only whitespace. Example:
```automate
pad(pad("hello world", " ", 1), "-", 3)
=
"--- hello world ---"
```
Of course, that example assumes the 2nd argument is absolute, not relative.

It seems padding is relative in most languages:
```javascript
'a'.padStart(3, '_').padEnd(5, '_') === '__a__'
```

For more insights, we should look at the tc39/proposal-string-pad-start-end: ECMAScript spec proposal

Henrik "The Developer" Lindqvist

unread,
May 16, 2024, 7:28:02 AMMay 16
to Automate for Android
Using a pad function to append/prepend a string is a bit ridiculous, "--- "++"hello world"++" ---" is far simpler, and not really what padding is used for, as your link say (i said earlier), "Since one of the primary use cases [...] is for formatting monospaced text in columns."
Your first example is incorrect, since the numeric argument would the the length the string should be padded to, not the number of pad character to add.

Ricardo “Rudxain” Fernández Serrata

unread,
May 21, 2024, 2:56:21 AMMay 21
to Automate for Android
I agree! So `repeat` must be "absolute" and `pad` must be relative to the length of the input string. In that case, it seems useful to have both. Although, array `fill` followed by `join` is enough to emulate string `repeat`, so `fill` seems more useful

Ricardo “Rudxain” Fernández Serrata

unread,
May 21, 2024, 3:01:37 AMMay 21
to Automate for Android
BTW, I forgot to clarify that I meant "3rd argument" (index 2) not "2nd argument". It was an off-by-one error 😅
Reply all
Reply to author
Forward
0 new messages