proposal: String.prototype.padCenter

Claude Pache claude.pache at gmail.com
Wed Aug 22 06:49:21 UTC 2018


```js
x.padStart((x.length + 32)/2).padEnd(32)
```

(BTW, I don’t like this habit of top-posting the reply. Please look below for the question I’m answering, and read the mail backwards for more context.)

—Claude

> Le 22 août 2018 à 05:47, Naveen Chawla <naveen.chwl at gmail.com> a écrit :
> 
> How would you combine padStart and padEnd to get the desired result? Just curious, because the idea would be to create a minimum of 32 chars with the original string centered inside the "padding" (if there is any)
> 
> On Wed, 22 Aug 2018 at 07:26 Isiah Meadows <isiahmeadows at gmail.com <mailto:isiahmeadows at gmail.com>> wrote:
> What's wrong with combining `padStart` with `padEnd`? I'm not sure this is worth it, since you can easily emulate it anyways (as you stated).
> On Tue, Aug 21, 2018 at 16:44 Ricardo Zorzal <ricardozorzal at hotmail.com <mailto:ricardozorzal at hotmail.com>> wrote:
> Hello guys, I here to talk about padStart, padEnd and my new proposal "padCenter", I think it is not hard to develop because we already had padStart and padEnd.
> 
> I thought that we need to add one more feature to String's prototype, and it is not a hard work. So the proposal is to create the String.prototype.padCenter that centralize a string with a max character lenght, equals we do when call the "pad" functions.
> 
> For exemple, when we call padStart
> 
> ```javascript
> "MY-TEST".padStart(32)
> //result: "                         MY-TEST"
> ```
> 
> and when we call padEnd:
> 
> ```javascript
> "MY-TEST".padEnd(32)
> //result: "MY-TEST                         "
> ```
> 
> how about that:
> 
> ```javascript
> "MY-TEST".padCenter(32)
> //result: "            MY-TEST            "
> ```
> 
> I'm searching it for a long time and I seen people talking about it in different use cases. But for me it will be usefull when I need to send some text data to my bluetooth printer, becouse the length of his line is 32 and I need to centralize the data and adjust the line to right and left.
> 
> I think this feature can improve the language and it will be useful for other developers like me.
> 
> Although we has libs that has an implementation of it
> https://lodash.com/docs#pad <https://lodash.com/docs#pad>
> http://gabceb.github.io/underscore.string.site/#pad <http://gabceb.github.io/underscore.string.site/#pad>
> https://vocajs.com/#pad <https://vocajs.com/#pad>
> I think it can be done for native functions too
> 
> And it is native for other languages, such as:
>     1. PHP (https://www.w3schools.com/php/showphp.asp?filename=demo_func_string_str_pad3 <https://www.w3schools.com/php/showphp.asp?filename=demo_func_string_str_pad3>)
>     2. Python (https://docs.python.org/2/library/string.html#string.center <https://docs.python.org/2/library/string.html#string.center>)
>     3. Ruby (https://ruby-doc.org/core-2.2.0/String.html#center-method <https://ruby-doc.org/core-2.2.0/String.html#center-method>)
>     4. R (https://www.rdocumentation.org/packages/stringr/versions/1.3.1/topics/str_pad <https://www.rdocumentation.org/packages/stringr/versions/1.3.1/topics/str_pad>)
>     5. etc...
> 
> 
> What do you guys think ?
> 
> ( We can call it "pad" instead of "padCenter"  haha )
> 
> 
> Att, 
> 
> Ricardo Zorzal Davila 
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org <mailto:es-discuss at mozilla.org>
> https://mail.mozilla.org/listinfo/es-discuss <https://mail.mozilla.org/listinfo/es-discuss>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org <mailto:es-discuss at mozilla.org>
> https://mail.mozilla.org/listinfo/es-discuss <https://mail.mozilla.org/listinfo/es-discuss>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180822/797b18d0/attachment-0001.html>


More information about the es-discuss mailing list