-
Notifications
You must be signed in to change notification settings - Fork 15
Should padLeft pad "outwardly", or "left"? #6
Comments
I call this "alignment". |
Things with the ability to specify both a length and a fillStrRuby has 'abc'.rjust(5, 'def') => 'deabc'
'abc'.rjust(6, 'def') => 'defabc'
'abc'.ljust(5, 'def') => 'abcde'
'abc'.ljust(6, 'def') => 'abcdef' PHP has echo str_pad('abc', 5, 'de', STR_PAD_LEFT); => "deabc"
echo str_pad('abc', 6, 'de', STR_PAD_LEFT); => "dedabc"
echo str_pad('abc', 6, 'de', STR_PAD_RIGHT); => "abcde"
echo str_pad('abc', 6, 'de', STR_PAD_RIGHT); => "abcded" Things that don't truncate
echo 'abc' | sed -e :a -e 's/^.\{1,5\}$/&de/;ta' => abcdede
echo 'abc' | sed -e :a -e 's/^.\{1,5\}$/de&/;ta' => dedeabc
echo 'abc' | sed -e :a -e 's/^.\{1,5\}$/ed&/;ta' => ededabc Scala doesn't let me truncate or pad on the opposite side, and the number argument seems to be "number of occurrences of the fillStr": "abc".padTo(6, "de") => "abcdedede" Languages that only allow a single space, and thus don't applyJava has String.format("%6s", "abc") => " abc"
String.format("%-6s", "abc") => "abc " Smalltalk and Perl have MiscPython also has 'abc'.ljust(6, 'de') => TypeError: The fill character must be exactly one character long
'abc'.rjust(6, 'de') => TypeError: The fill character must be exactly one character long ConclusionI've found only two languages that both accept a multiple character fill string, and decide the alignment for you. One, PHP, follows the current proposal (outward alignment). The other, Ruby, deviates from the current proposal (fillString-order alignment, ie, left-to-right). I don't feel strongly about one over the other, so unless more languages are suggested/discovered that help move the needle, I'd prefer to keep the proposal as-is. Thoughts? |
Ruby still appears to differ from this proposal. Ruby:
This proposal:
|
Ah yes, you're correct - my Ruby example didn't correctly indicate what was going on. I'll update my comment. |
@michaelficarra Given this sole example of alignment with the fill string, how do you want to proceed? Are you comfortable following PHP's example? Or would you prefer to follow Ruby's example? Alternatively, do you have any other languages to recommend investigating that would help decide this? As it is, I'm aware of one applicable language on either side, which means we can choose whichever we want. |
Can you update the PHP example in the same way? I think the new test is much better. |
Done! |
Okay, so they both are aligning padding left, which is what I would have intuited and not the way that is described by these proposals. Are you going to update them to sync with Ruby/PHP? |
Given the evidence, that seems reasonable. I'll update that shortly. To illustrate the difference: |
@bterlson came up with a clear example of why this matters: With this change, |
Strange logic. |
|
I had no motivating example, I just thought that Regardless, the consistency with both Ruby and PHP (and the lack of any other example language that differs) completely trumps it in my mind, even without the very compelling "mask" use case. |
I was referring to @zloirock. I'm curious how the current behavior makes more sense to him (presumably because it's useful for some purpose I haven't thought of!). |
Per tc39/proposal-string-pad-start-end#6, and to be consistent with Ruby and PHP. Fixes #1.
@bterlson yes, with |
Even though 'abc' isn't symmetrical already? |
For |
Investigate use cases, and what other langs do. From @michaelficarra
The text was updated successfully, but these errors were encountered: