├── .gitignore
├── LICENSE
├── README.md
├── index.html
├── package.json
├── polyfill.js
├── spec.css
├── spec.emu
├── spec.js
└── spec.md
/.gitignore:
--------------------------------------------------------------------------------
1 | # Logs
2 | logs
3 | *.log
4 |
5 | # Runtime data
6 | pids
7 | *.pid
8 | *.seed
9 |
10 | # Directory for instrumented libs generated by jscoverage/JSCover
11 | lib-cov
12 |
13 | # Coverage directory used by tools like istanbul
14 | coverage
15 |
16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
17 | .grunt
18 |
19 | # node-waf configuration
20 | .lock-wscript
21 |
22 | # Compiled binary addons (http://nodejs.org/api/addons.html)
23 | build/Release
24 |
25 | # Dependency directory
26 | # https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
27 | node_modules
28 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 Jordan Harband
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # [String.prototype.padStart](https://github.com/es-shims/String.prototype.padStart) / [String.prototype.padEnd](https://github.com/es-shims/String.prototype.padEnd)
2 | ECMAScript proposal, specs, tests, and reference implementation for String.prototype.padStart/padEnd.
3 |
4 | This [initial](http://wiki.ecmascript.org/doku.php?id=strawman:string_padding) proposal was drafted by [@KevinGrandon](https://github.com/kevingrandon) with input from [@rwaldron](https://github.com/rwaldron) and [@dherman](https://github.com/dherman).
5 | Updated spec drafted by [@ljharb](https://github.com/ljharb) with input from [@rwaldron](https://github.com/rwaldron), [@allenwb](https://github.com/allenwb), and [@dherman](https://github.com/dherman).
6 |
7 | This proposal is currently at [stage 4](https://github.com/tc39/proposals/blob/master/finished-proposals.md) of the [process](https://tc39.github.io/process-document/).
8 |
9 | Designated TC39 reviewers: @littledan @michaelficarra
10 |
11 | ## Rationale
12 | Without a reasonable way to pad a string using native methods, working with JavaScript strings today is more painful than it should be. Without these functions, the language feels incomplete, and is a paper cut to what could be a very polished experience.
13 |
14 | Due to common use, string padding functions exist in a majority of websites and frameworks. For example, nearly every app in FirefoxOS had implemented a left pad function, because they all needed some generic string padding operation.
15 |
16 | It is highly probable that the majority of current string padding implementations are inefficient. Bringing this into the platform will improve performance of the web, and developer productivity as they no longer have to implement these common functions.
17 |
18 | ## Spec
19 | You can view the spec in [markdown format](spec.md) or rendered as [HTML](http://tc39.github.io/proposal-string-pad-start-end/).
20 |
21 | ## Naming
22 | ~~For consistency with [trimStart/trimRight](https://github.com/sebmarkbage/ecmascript-string-left-right-trim), and `reduce`/`reduceRight`, despite the existence of `startsWith`/`endsWith`, we have settled on `padLeft` and `padRight`.~~
23 | Update per November 2015 TC39 meeting: the names will be `padStart`/`padEnd`, `trimLeft`/`trimRight` will change to `trimStart`/`trimEnd`, and `trimLeft`/`trimRight` aliases will be added to Annex B for web compatibility.
24 |
25 | ## Semantics of "min length" vs "max length"
26 | While updating this proposal with spec language, we discussed at length whether the first parameter should determine the minimum length or the maximum length of the padded string. Specifically, "min length" semantics says `'foo'.padEnd(4, '12')` would output `foo12`, and "max length" semantics would output `foo1`. Since one of the primary use cases of `padStart`/`padEnd` is for formatting monospaced text in columns, and since "min length" semantics can be achieved via `String#repeat`, we decided that "max length" was the far more useful approach.
27 |
28 | ## Left padding, with respect to the fill string: consistency with other languages
29 | Per [#6](https://github.com/tc39/proposal-string-pad-start-end/issues/6), the only languages we found that support multiple character fill strings that provide both "left" and "right" functionality are Ruby and PHP. Both language’s form of “pad on the left” takes the *first* part of the fill string, not the last. The clear example of why this matters is: `"abc".padStart(10, "0123456789")` - taking the last part of the fill string gives `"3456789abc"`, whereas taking the first part gives `"0123456abc"`. In other words, `string.padStart(mask.length, mask)` should do what one expects.
30 |
--------------------------------------------------------------------------------
/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
Let truncatedStringFiller be a new String value consisting of repeated concatenations of filler truncated to length fillLen.
75 |
Return a new String value computed by the concatenation of truncatedStringFiller and S.
76 |
77 |
78 |
79 | Note 1The first argument maxLength will be clamped such that it can be no smaller than the length of the
80 | this value.
81 | Note 2The optional second argument fillString defaults to
82 | " " (a string consisting of U+0020 SPACE).
83 |
84 |
85 |
86 |
87 |
Let truncatedStringFiller be a new String value consisting of repeated concatenations of filler truncated to length fillLen.
107 |
Return a new String value computed by the concatenation of S and truncatedStringFiller.
108 |
109 |
110 |
111 | Note 1The first argument maxLength will be clamped such that it can be no smaller than the length of the
112 | this value.
113 | Note 2The optional second argument fillString defaults to
114 | " " (a string consisting of U+0020 SPACE).
115 |
116 |
117 |
All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT http://www.ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.
124 |
125 |
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
126 |
127 |
128 |
Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
129 |
Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
130 |
Neither the name of the authors nor Ecma International may be used to endorse or promote products derived from this software without specific prior written permission.
131 |
132 |
133 |
THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
When the `padStart` method is called, the following steps are taken:
14 |
15 | 1. Let _O_ be ? RequireObjectCoercible(*this* value).
16 | 1. Let _S_ be ? ToString(_O_).
17 | 1. Let _intMaxLength_ be ? ToLength(_maxLength_).
18 | 1. Let _stringLength_ be the number of elements in S.
19 | 1. If _intMaxLength_ is not greater than _stringLength_, return _S_.
20 | 1. If _fillString_ is *undefined*, let _filler_ be a string consisting solely of the code unit U+0020 (SPACE).
21 | 1. Else, let _filler_ be ? ToString(_fillString_).
22 | 1. If _filler_ is the empty String, return _S_.
23 | 1. Let _fillLen_ be _intMaxLength_ - _stringLength_.
24 | 1. Let _truncatedStringFiller_ be a new String value consisting of repeated concatenations of _filler_ truncated to length _fillLen_.
25 | 1. Return a new String value computed by the concatenation of _truncatedStringFiller_ and _S_.
26 |
27 | The first argument _maxLength_ will be clamped such that it can be no smaller than the length of the *this* value.
28 | The optional second argument _fillString_ defaults to *" "* (a string consisting of U+0020 SPACE).
29 |
30 |
31 |
32 |
33 |