├── .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 | 4 | 5 | 6 | 7 | 8 | String.prototype.padStart / padEnd 9 | 36 | 37 | 38 | 39 | 40 | 52 |

Stage 3 Draft / March 29, 2016

53 |

String.prototype.padStart / padEnd

54 | 55 |

1String.prototype.padStart( maxLength [ , fillString ] )#

56 |

When the padStart method is called, the following steps are taken:

57 | 58 |
    59 |
  1. Let O be ? 60 | RequireObjectCoercible( 61 | this value).
  2. 62 |
  3. Let S be ? 63 | ToString(O).
  4. 64 |
  5. Let intMaxLength be ? 65 | ToLength(maxLength).
  6. 66 |
  7. Let stringLength be the number of elements in S.
  8. 67 |
  9. If intMaxLength is not greater than stringLength, return S.
  10. 68 |
  11. If fillString is 69 | undefined, let filler be a string consisting solely of the code unit U+0020 (SPACE).
  12. 70 |
  13. Else, let filler be ? 71 | ToString(fillString).
  14. 72 |
  15. If filler is the empty String, return S.
  16. 73 |
  17. Let fillLen be intMaxLength - stringLength.
  18. 74 |
  19. Let truncatedStringFiller be a new String value consisting of repeated concatenations of filler truncated to length fillLen.
  20. 75 |
  21. Return a new String value computed by the concatenation of truncatedStringFiller and S. 76 |
  22. 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 |

2String.prototype.padEnd( maxLength [ , fillString ] )#

88 |

When the padEnd method is called, the following steps are taken:

89 | 90 |
    91 |
  1. Let O be ? 92 | RequireObjectCoercible( 93 | this value).
  2. 94 |
  3. Let S be ? 95 | ToString(O).
  4. 96 |
  5. Let intMaxLength be ? 97 | ToLength(maxLength).
  6. 98 |
  7. Let stringLength be the number of elements in S.
  8. 99 |
  9. If intMaxLength is not greater than stringLength, return S.
  10. 100 |
  11. If fillString is 101 | undefined, let filler be a string consisting solely of the code unit U+0020 (SPACE).
  12. 102 |
  13. Else, let filler be ? 103 | ToString(fillString).
  14. 104 |
  15. If filler is the empty String, return S.
  16. 105 |
  17. Let fillLen be intMaxLength - stringLength.
  18. 106 |
  19. Let truncatedStringFiller be a new String value consisting of repeated concatenations of filler truncated to length fillLen.
  20. 107 |
  21. Return a new String value computed by the concatenation of S and truncatedStringFiller. 108 |
  22. 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 |

ACopyright & Software License#

118 | 119 |

Copyright Notice

120 |

© 2016 Jordan Harband

121 | 122 |

Software License

123 |

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 |
  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. 129 |
  3. 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.
  4. 130 |
  5. 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.
  6. 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.

134 | 135 |
136 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ecma-proposal-string-pad-start-eng", 3 | "version": "0.0.0", 4 | "description": "ECMAScript spec proposal for String.prototype.{padStart,padEnd}", 5 | "main": "index.js", 6 | "scripts": { 7 | "build": "ecmarkup spec.emu --js=spec.js --css=spec.css | js-beautify -f - --type=html -t > index.html", 8 | "prepublish": "npm run build && echo >&2 'no publishing' && exit 255" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "git+https://github.com/ljharb/proposal-string-pad-start-end.git" 13 | }, 14 | "keywords": [ 15 | "String.prototype.padLeft", 16 | "String.prototype.padRight", 17 | "String.prototype.padStart", 18 | "String.prototype.padEnd", 19 | "padLeft", 20 | "padRight", 21 | "padStart", 22 | "padEnd", 23 | "pad", 24 | "padding", 25 | "ECMAScript", 26 | "ESNext", 27 | "spec" 28 | ], 29 | "author": "Jordan Harband ", 30 | "license": "MIT", 31 | "bugs": { 32 | "url": "https://github.com/tc39/proposal-string-pad-start-end/issues" 33 | }, 34 | "homepage": "https://github.com/tc39/proposal-string-pad-start-end#readme", 35 | "dependencies": {}, 36 | "devDependencies": { 37 | "ecmarkup": "^3.0.1", 38 | "js-beautify": "^1.6.2", 39 | "tape": "^4.4.0" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /polyfill.js: -------------------------------------------------------------------------------- 1 | const RequireObjectCoercible = O => { 2 | if (O === null || typeof O === 'undefined') { 3 | throw new TypeError('"this" value must not be null or undefined'); 4 | } 5 | return O; 6 | }; 7 | const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || Math.pow(2, 53) - 1; 8 | const ToLength = argument => { 9 | const len = Number(argument); 10 | if (Number.isNaN(len) || len <= 0) { return 0; } 11 | if (len > MAX_SAFE_INTEGER) { return MAX_SAFE_INTEGER; } 12 | return len; 13 | }; 14 | 15 | if (!String.prototype.padStart) { 16 | String.prototype.padStart = function padStart(maxLength, fillString = ' ') { 17 | const O = RequireObjectCoercible(this); 18 | const S = String(O); 19 | const intMaxLength = ToLength(maxLength); 20 | const stringLength = ToLength(S.length); 21 | if (intMaxLength <= stringLength) { return S; } 22 | let filler = typeof fillString === 'undefined' ? ' ' : String(fillString); 23 | if (filler === '') { return S; } 24 | const fillLen = intMaxLength - stringLength; 25 | while (filler.length < fillLen) { 26 | const fLen = filler.length; 27 | const remainingCodeUnits = fillLen - fLen; 28 | if (fLen > remainingCodeUnits) { 29 | filler += filler.slice(0, remainingCodeUnits); 30 | } else { 31 | filler += filler; 32 | } 33 | } 34 | const truncatedStringFiller = filler.slice(0, fillLen); 35 | return truncatedStringFiller + S; 36 | }; 37 | } 38 | 39 | if (!String.prototype.padEnd) { 40 | String.prototype.padEnd = function padEnd(maxLength, fillString = ' ') { 41 | const O = RequireObjectCoercible(this); 42 | const S = String(O); 43 | const intMaxLength = ToLength(maxLength); 44 | const stringLength = ToLength(S.length); 45 | if (intMaxLength <= stringLength) { return S; } 46 | let filler = typeof fillString === 'undefined' ? ' ' : String(fillString); 47 | if (filler === '') { return S; } 48 | const fillLen = intMaxLength - stringLength; 49 | while (filler.length < fillLen) { 50 | const fLen = filler.length; 51 | const remainingCodeUnits = fillLen - fLen; 52 | if (fLen > remainingCodeUnits) { 53 | filler += filler.slice(0, remainingCodeUnits); 54 | } else { 55 | filler += filler; 56 | } 57 | } 58 | const truncatedStringFiller = filler.slice(0, fillLen); 59 | return S + truncatedStringFiller; 60 | }; 61 | } 62 | -------------------------------------------------------------------------------- /spec.css: -------------------------------------------------------------------------------- 1 | body { 2 | font-size: 18px; 3 | line-height: 1.5; 4 | font-family: Cambria, Palatino Linotype, Palatino, Liberation Serif, serif; 5 | padding: 0; 6 | color: #333; 7 | margin: 0 2% 0 31%; 8 | } 9 | 10 | body.oldtoc { 11 | margin: 0 auto; 12 | } 13 | 14 | a { 15 | text-decoration: none; 16 | color: #206ca7; 17 | } 18 | 19 | a:visited { 20 | color: #206ca7; 21 | } 22 | 23 | a:hover { 24 | text-decoration: underline; 25 | color: #239dee; 26 | } 27 | 28 | 29 | code { 30 | font-weight: bold; 31 | font-family: Consolas, Monaco, monospace; 32 | white-space: pre; 33 | } 34 | 35 | pre code { 36 | font-weight: inherit; 37 | } 38 | 39 | pre code.hljs { 40 | background-color: #fff; 41 | margin: 0; 42 | padding: 0; 43 | } 44 | 45 | ol.toc { 46 | list-style: none; 47 | padding-left: 0; 48 | } 49 | 50 | ol.toc ol.toc { 51 | padding-left: 2ex; 52 | list-style: none; 53 | } 54 | 55 | var { 56 | color: #2aa198; 57 | transition: background-color 0.25s ease; 58 | cursor: pointer; 59 | } 60 | 61 | var.referenced { 62 | background-color: #ffff33; 63 | } 64 | 65 | emu-const { 66 | font-family: sans-serif; 67 | } 68 | 69 | emu-val { 70 | font-weight: bold; 71 | } 72 | emu-alg ol, emu-alg ol ol ol ol { 73 | list-style-type: decimal; 74 | } 75 | 76 | emu-alg ol ol, emu-alg ol ol ol ol ol { 77 | list-style-type: lower-alpha; 78 | } 79 | 80 | emu-alg ol ol ol, ol ol ol ol ol ol { 81 | list-style-type: lower-roman; 82 | } 83 | 84 | emu-eqn { 85 | display: block; 86 | margin-left: 4em; 87 | } 88 | 89 | emu-eqn div:first-child { 90 | margin-left: -2em; 91 | } 92 | 93 | emu-eqn.inline { 94 | display: inline; 95 | margin: 0; 96 | white-space: nowrap; 97 | } 98 | 99 | emu-note { 100 | display: block; 101 | margin: 1em 0 1em 6em; 102 | color: #666; 103 | } 104 | 105 | emu-note span.note { 106 | text-transform: uppercase; 107 | margin-left: -6em; 108 | display: block; 109 | float: left; 110 | } 111 | 112 | emu-example { 113 | display: block; 114 | margin: 1em 3em; 115 | } 116 | 117 | emu-example figure figcaption { 118 | margin-top: 0.5em; 119 | text-align: left; 120 | } 121 | 122 | emu-production { 123 | display: block; 124 | margin-top: 1em; 125 | margin-bottom: 1em; 126 | margin-left: 5ex; 127 | } 128 | 129 | 130 | emu-grammar.inline, emu-production.inline, 131 | emu-grammar.inline emu-production emu-rhs, emu-production.inline emu-rhs { 132 | display: inline; 133 | } 134 | 135 | emu-grammar[collapsed] emu-production, emu-production[collapsed] { 136 | margin: 0; 137 | } 138 | 139 | emu-grammar[collapsed] emu-production emu-rhs, emu-production[collapsed] emu-rhs { 140 | display: inline; 141 | padding-left: 1ex; 142 | } 143 | 144 | emu-constraints { 145 | font-size: .75em; 146 | margin-right: 1ex; 147 | } 148 | 149 | emu-gann { 150 | margin-right: 1ex; 151 | } 152 | 153 | emu-gann emu-t:last-child, 154 | emu-gann emu-nt:last-child { 155 | margin-right: 0; 156 | } 157 | 158 | emu-geq { 159 | margin-left: 1ex; 160 | font-weight: bold; 161 | } 162 | 163 | emu-oneof { 164 | font-weight: bold; 165 | margin-left: 1ex; 166 | } 167 | 168 | emu-nt { 169 | display: inline-block; 170 | font-style: italic; 171 | white-space: nowrap; 172 | text-indent: 0; 173 | } 174 | 175 | emu-nt a, emu-nt a:visited { 176 | color: #333; 177 | } 178 | 179 | emu-rhs emu-nt { 180 | margin-right: 1ex; 181 | } 182 | 183 | emu-t { 184 | display: inline-block; 185 | font-family: monospace; 186 | font-weight: bold; 187 | white-space: nowrap; 188 | text-indent: 0; 189 | } 190 | 191 | emu-production emu-t { 192 | margin-right: 1ex; 193 | } 194 | 195 | emu-rhs { 196 | display: block; 197 | padding-left: 75px; 198 | text-indent: -25px; 199 | } 200 | 201 | emu-mods { 202 | font-size: .85em; 203 | vertical-align: sub; 204 | font-style: normal; 205 | font-weight: normal; 206 | } 207 | 208 | emu-production[collapsed] emu-mods { 209 | display: none; 210 | } 211 | 212 | emu-params, emu-opt { 213 | margin-right: 1ex; 214 | font-family: monospace; 215 | } 216 | 217 | emu-params, emu-constraints { 218 | color: #2aa198; 219 | } 220 | 221 | emu-opt { 222 | color: #b58900; 223 | } 224 | 225 | emu-gprose { 226 | font-size: 0.9em; 227 | font-family: Helvetica, Arial, sans-serif; 228 | } 229 | 230 | h1.shortname { 231 | color: #f60; 232 | font-size: 1.5em; 233 | margin: 0; 234 | } 235 | h1.version { 236 | color: #f60; 237 | font-size: 1.5em; 238 | margin: 0; 239 | } 240 | h1.title { 241 | margin-top: 0; 242 | color: #f60; 243 | } 244 | h1, h2, h3, h4, h5, h6 { 245 | position: relative; 246 | } 247 | h1 .secnum { 248 | position: absolute; 249 | text-align: right; 250 | right: 100%; 251 | margin-right: 1ex; 252 | white-space: nowrap; 253 | } 254 | 255 | h1 { font-size: 2.67em; } 256 | h2 { font-size: 2em; } 257 | h3 { font-size: 1.56em; } 258 | h4 { font-size: 1.25em; } 259 | h5 { font-size: 1.11em; } 260 | h6 { font-size: 1em; } 261 | 262 | h1 span.utils, 263 | h2 span.utils, 264 | h3 span.utils, 265 | h4 span.utils, 266 | h5 span.utils, 267 | h6 span.utils { 268 | padding-left: 1em; 269 | } 270 | 271 | h1 span.utils span.anchor a, 272 | h2 span.utils span.anchor a, 273 | h3 span.utils span.anchor a, 274 | h4 span.utils span.anchor a, 275 | h5 span.utils span.anchor a, 276 | h6 span.utils span.anchor a { 277 | color: #ccc; 278 | text-decoration: none; 279 | } 280 | 281 | h1 span.utils span.anchor a:hover, 282 | h2 span.utils span.anchor a:hover, 283 | h3 span.utils span.anchor a:hover, 284 | h4 span.utils span.anchor a:hover, 285 | h5 span.utils span.anchor a:hover, 286 | h6 span.utils span.anchor a:hover { 287 | color: #333; 288 | } 289 | 290 | emu-intro h1, emu-clause h1, emu-annex h1 { font-size: 2em; } 291 | emu-intro h2, emu-clause h2, emu-annex h2 { font-size: 1.56em; } 292 | emu-intro h3, emu-clause h3, emu-annex h3 { font-size: 1.25em; } 293 | emu-intro h4, emu-clause h4, emu-annex h4 { font-size: 1.11em; } 294 | emu-intro h5, emu-clause h5, emu-annex h5 { font-size: 1em; } 295 | emu-intro h6, emu-clause h6, emu-annex h6 { font-size: 0.9em; } 296 | emu-intro emu-intro h1, emu-clause emu-clause h1, emu-annex emu-annex h1 { font-size: 1.56em; } 297 | emu-intro emu-intro h2, emu-clause emu-clause h2, emu-annex emu-annex h2 { font-size: 1.25em; } 298 | emu-intro emu-intro h3, emu-clause emu-clause h3, emu-annex emu-annex h3 { font-size: 1.11em; } 299 | emu-intro emu-intro h4, emu-clause emu-clause h4, emu-annex emu-annex h4 { font-size: 1em; } 300 | emu-intro emu-intro h5, emu-clause emu-clause h5, emu-annex emu-annex h5 { font-size: 0.9em; } 301 | emu-intro emu-intro emu-intro h1, emu-clause emu-clause emu-clause h1, emu-annex emu-annex emu-annex h1 { font-size: 1.25em; } 302 | emu-intro emu-intro emu-intro h2, emu-clause emu-clause emu-clause h2, emu-annex emu-annex emu-annex h2 { font-size: 1.11em; } 303 | emu-intro emu-intro emu-intro h3, emu-clause emu-clause emu-clause h3, emu-annex emu-annex emu-annex h3 { font-size: 1em; } 304 | emu-intro emu-intro emu-intro h4, emu-clause emu-clause emu-clause h4, emu-annex emu-annex emu-annex h4 { font-size: 0.9em; } 305 | emu-intro emu-intro emu-intro emu-intro h1, emu-clause emu-clause emu-clause emu-clause h1, emu-annex emu-annex emu-annex emu-annex h1 { font-size: 1.11em; } 306 | emu-intro emu-intro emu-intro emu-intro h2, emu-clause emu-clause emu-clause emu-clause h2, emu-annex emu-annex emu-annex emu-annex h2 { font-size: 1em; } 307 | emu-intro emu-intro emu-intro emu-intro h3, emu-clause emu-clause emu-clause emu-clause h3, emu-annex emu-annex emu-annex emu-annex h3 { font-size: 0.9em; } 308 | emu-intro emu-intro emu-intro emu-intro emu-intro h1, emu-clause emu-clause emu-clause emu-clause emu-clause h1, emu-annex emu-annex emu-annex emu-annex emu-annex h1 { font-size: 1em; } 309 | emu-intro emu-intro emu-intro emu-intro emu-intro h2, emu-clause emu-clause emu-clause emu-clause emu-clause h2, emu-annex emu-annex emu-annex emu-annex emu-annex h2 { font-size: 0.9em; } 310 | emu-intro emu-intro emu-intro emu-intro emu-intro emu-intro h1, emu-clause emu-clause emu-clause emu-clause emu-clause emu-clause h1, emu-annex emu-annex emu-annex emu-annex emu-annex emu-annex h1 { font-size: 0.9em } 311 | 312 | emu-clause { 313 | display: block; 314 | } 315 | 316 | /* Figures and tables */ 317 | figure { display: block; margin: 1em 0 3em 0; } 318 | figure object { display: block; margin: 0 auto; } 319 | figure table.real-table { margin: 0 auto; } 320 | figure figcaption { 321 | display: block; 322 | color: #555555; 323 | font-weight: bold; 324 | text-align: center; 325 | } 326 | 327 | emu-table table { 328 | margin: 0 auto; 329 | } 330 | 331 | emu-table table, table.real-table { 332 | border-collapse: collapse; 333 | } 334 | 335 | emu-table td, emu-table th, table.real-table td, table.real-table th { 336 | border: 1px solid black; 337 | padding: 0.4em; 338 | vertical-align: baseline; 339 | } 340 | emu-table th, emu-table thead td, table.real-table th { 341 | background-color: #eeeeee; 342 | } 343 | 344 | /* Note: the left content edges of table.lightweight-table >tbody >tr >td 345 | and div.display line up. */ 346 | table.lightweight-table { 347 | border-collapse: collapse; 348 | margin: 0 0 0 1.5em; 349 | } 350 | table.lightweight-table td, table.lightweight-table th { 351 | border: none; 352 | padding: 0 0.5em; 353 | vertical-align: baseline; 354 | } 355 | 356 | /* diff styles */ 357 | ins { 358 | background-color: #e0f8e0; 359 | text-decoration: none; 360 | border-bottom: 1px solid #396; 361 | } 362 | 363 | ins.block { 364 | display: block; 365 | } 366 | 367 | del { 368 | background-color: #fee; 369 | } 370 | 371 | del.block { 372 | display: block; 373 | } 374 | 375 | /* Menu Styles */ 376 | #menu-toggle { 377 | font-size: 2em; 378 | 379 | position: fixed; 380 | top: 0; 381 | left: 0; 382 | width: 1.5em; 383 | height: 1.5em; 384 | z-index: 3; 385 | visibility: hidden; 386 | 387 | background-color: #111; 388 | color: #B6C8E4; 389 | 390 | line-height: 1.5em; 391 | text-align: center; 392 | -webkit-touch-callout: none; 393 | -webkit-user-select: none; 394 | -khtml-user-select: none; 395 | -moz-user-select: none; 396 | -ms-user-select: none; 397 | user-select: none;; 398 | 399 | cursor: pointer; 400 | } 401 | 402 | #menu { 403 | position: fixed; 404 | left: 0; 405 | top: 0; 406 | height: 100%; 407 | width: 24%; 408 | z-index: 2; 409 | overflow-x: hidden; 410 | overflow-y: auto; 411 | box-sizing: border-box; 412 | 413 | background-color: #111; 414 | 415 | transition: opacity 0.1s linear; 416 | } 417 | 418 | #menu.active { 419 | display: block; 420 | opacity: 1; 421 | } 422 | 423 | #menu-toc > ol { 424 | padding: 0; 425 | } 426 | 427 | #menu-toc > ol , #menu-toc > ol ol { 428 | list-style-type: none; 429 | } 430 | 431 | #menu-toc > ol ol { 432 | padding-left: 0.75em; 433 | } 434 | 435 | #menu-toc li { 436 | text-overflow: ellipsis; 437 | overflow: hidden; 438 | white-space: nowrap; 439 | } 440 | 441 | #menu-toc .item-toggle { 442 | display: inline-block; 443 | transform: rotate(-45deg) translate(-5px, -5px); 444 | transition: transform 0.1s ease; 445 | width: 1em; 446 | 447 | color: #555F6E; 448 | 449 | -webkit-touch-callout: none; 450 | -webkit-user-select: none; 451 | -khtml-user-select: none; 452 | -moz-user-select: none; 453 | -ms-user-select: none; 454 | user-select: none;; 455 | 456 | cursor: pointer; 457 | } 458 | 459 | #menu-toc .item-toggle-none { 460 | display: inline-block; 461 | width: 1em; 462 | } 463 | 464 | #menu-toc li.active > .item-toggle { 465 | transform: rotate(45deg) translate(-5px, -5px); 466 | } 467 | 468 | #menu-toc li > ol { 469 | display: none; 470 | } 471 | 472 | #menu-toc li.active > ol { 473 | display: block; 474 | } 475 | 476 | #menu-toc li > a { 477 | padding-left: 0.25em; 478 | color: #B6C8E4; 479 | } 480 | 481 | #menu-search { 482 | color: #B6C8E4; 483 | } 484 | 485 | #menu-search-box { 486 | display: block; 487 | width: 90%; 488 | margin: 5px auto; 489 | font-size: 1em; 490 | padding: 2px; 491 | } 492 | 493 | #menu-search-results.inactive { 494 | display: none; 495 | } 496 | 497 | #menu-search-results ul { 498 | list-style-type: square; 499 | padding: 0 0 0 35px; 500 | margin: 0; 501 | } 502 | 503 | #menu-search-results li { 504 | white-space: nowrap; 505 | } 506 | 507 | #menu-search-results a { 508 | color: #b6c8e4; 509 | } 510 | 511 | @media (max-width: 1366px) { 512 | body { 513 | margin: 0 0 0 150px; 514 | } 515 | 516 | #menu { 517 | display: none; 518 | padding-top: 3em; 519 | width: 323px; 520 | } 521 | 522 | #menu-toggle { 523 | visibility: visible; 524 | } 525 | } 526 | 527 | @media only screen and (max-width: 800px) { 528 | body { 529 | margin: 2em 10px 0 10px; 530 | } 531 | 532 | #menu { 533 | width: 100%; 534 | } 535 | 536 | h1 .secnum { 537 | display: inline; 538 | position: inherit; 539 | left: 0; 540 | right: 0; 541 | } 542 | 543 | h1 .secnum:empty { 544 | margin: 0; padding: 0; 545 | } 546 | } 547 | -------------------------------------------------------------------------------- /spec.emu: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |
 7 | title: String.prototype.padStart / padEnd
 8 | stage: 3
 9 | contributors: Jordan Harband
10 | 
11 | 12 |

String.prototype.padStart( maxLength [ , fillString ] )

13 |

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 |

String.prototype.padEnd( maxLength [ , fillString ] )

34 |

When the `padEnd` method is called, the following steps are taken:

35 | 36 | 1. Let _O_ be ? RequireObjectCoercible(*this* value). 37 | 1. Let _S_ be ? ToString(_O_). 38 | 1. Let _intMaxLength_ be ? ToLength(_maxLength_). 39 | 1. Let _stringLength_ be the number of elements in S. 40 | 1. If _intMaxLength_ is not greater than _stringLength_, return _S_. 41 | 1. If _fillString_ is *undefined*, let _filler_ be a string consisting solely of the code unit U+0020 (SPACE). 42 | 1. Else, let _filler_ be ? ToString(_fillString_). 43 | 1. If _filler_ is the empty String, return _S_. 44 | 1. Let _fillLen_ be _intMaxLength_ - _stringLength_. 45 | 1. Let _truncatedStringFiller_ be a new String value consisting of repeated concatenations of _filler_ truncated to length _fillLen_. 46 | 1. Return a new String value computed by the concatenation of _S_ and _truncatedStringFiller_. 47 | 48 | The first argument _maxLength_ will be clamped such that it can be no smaller than the length of the *this* value. 49 | The optional second argument _fillString_ defaults to *" "* (a string consisting of U+0020 SPACE). 50 |
51 | -------------------------------------------------------------------------------- /spec.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | 3 | function Menu() { 4 | this.$toggle = document.getElementById('menu-toggle'); 5 | this.$menu = document.getElementById('menu'); 6 | this.$searchBox = document.getElementById('menu-search-box'); 7 | this.$searchResults = document.getElementById('menu-search-results'); 8 | this.initSearch(); 9 | 10 | this.$toggle.addEventListener('click', this.toggle.bind(this)); 11 | 12 | this.$searchBox.addEventListener('keydown', function (e) { 13 | if (e.keyCode === 191 && e.target.value.length === 0) { 14 | e.preventDefault(); 15 | e.stopPropagation(); 16 | } else if (e.keyCode === 13) { 17 | e.preventDefault(); 18 | e.stopPropagation(); 19 | this.selectResult(); 20 | } 21 | }.bind(this)); 22 | 23 | this.$searchBox.addEventListener('keyup', debounce(function (e) { 24 | e.stopPropagation(); 25 | this.search(e.target.value); 26 | }.bind(this))); 27 | 28 | 29 | var tocItems = this.$menu.querySelectorAll('#menu-toc li'); 30 | for (var i = 0; i < tocItems.length; i++) { 31 | var $item = tocItems[i]; 32 | $item.addEventListener('click', function($item, event) { 33 | $item.classList.toggle('active'); 34 | event.stopPropagation(); 35 | }.bind(null, $item)); 36 | } 37 | 38 | var tocLinks = this.$menu.querySelectorAll('#menu-toc li > a'); 39 | for (var i = 0; i < tocLinks.length; i++) { 40 | var $link = tocLinks[i]; 41 | $link.addEventListener('click', function(event) { 42 | this.toggle(); 43 | event.stopPropagation(); 44 | }.bind(this)); 45 | } 46 | } 47 | 48 | Menu.prototype.toggle = function () { 49 | this.$menu.classList.toggle('active'); 50 | } 51 | 52 | Menu.prototype.show = function () { 53 | this.$menu.classList.add('active'); 54 | } 55 | 56 | Menu.prototype.hide = function () { 57 | this.$menu.classList.remove('active'); 58 | } 59 | 60 | Menu.prototype.isVisible = function() { 61 | return this.$menu.classList.contains('active'); 62 | } 63 | 64 | Menu.prototype.initSearch = function () { 65 | var $biblio = document.getElementById('menu-search-biblio'); 66 | if (!$biblio) { 67 | this.biblio = {}; 68 | } else { 69 | this.biblio = JSON.parse($biblio.textContent); 70 | } 71 | 72 | this.biblio.ops = this.biblio.filter(function (e) { return e.type === 'op' }); 73 | this.biblio.clauses = this.biblio.filter(function (e) { return e.type === 'clause' }); 74 | this.biblio.productions = this.biblio.filter(function (e) { return e.type === 'production' }); 75 | 76 | document.addEventListener('keydown', function (e) { 77 | if (e.keyCode === 191) { 78 | e.preventDefault(); 79 | e.stopPropagation(); 80 | 81 | if(this.isVisible()) { 82 | this._closeAfterSearch = false; 83 | } else { 84 | this._closeAfterSearch = true; 85 | this.show(); 86 | } 87 | 88 | this.show(); 89 | this.$searchBox.focus(); 90 | } 91 | }.bind(this)) 92 | } 93 | 94 | Menu.prototype.search = function (needle) { 95 | if (needle.length < 2) { 96 | this.hideSearch(); 97 | } else { 98 | this.showSearch(); 99 | } 100 | 101 | needle = needle.toLowerCase(); 102 | 103 | var results = {}; 104 | var seenClauses = {}; 105 | 106 | results.ops = this.biblio.ops.filter(function(op) { 107 | return fuzzysearch(needle, op.aoid.toLowerCase()); 108 | }); 109 | 110 | results.ops.forEach(function(op) { 111 | seenClauses[op.refId] = true; 112 | }); 113 | 114 | results.productions = this.biblio.productions.filter(function(prod) { 115 | return fuzzysearch(needle, prod.name.toLowerCase()); 116 | }); 117 | 118 | results.clauses = this.biblio.clauses.filter(function(clause) { 119 | return !seenClauses[clause.id] && (clause.number.indexOf(needle) === 0 || fuzzysearch(needle, clause.title.toLowerCase())); 120 | }); 121 | 122 | if (results.length > 50) { 123 | results = results.slice(0, 50); 124 | } 125 | 126 | this.displayResults(results); 127 | } 128 | 129 | Menu.prototype.displayResults = function (results) { 130 | var totalResults = Object.keys(results).reduce(function (sum, record) { return sum + record.length }, 0); 131 | 132 | if (totalResults > 0) { 133 | this.$searchResults.classList.remove('no-results'); 134 | 135 | var html = '' 150 | 151 | this.$searchResults.innerHTML = html; 152 | } else { 153 | this.$searchResults.classList.add('no-results'); 154 | } 155 | } 156 | 157 | Menu.prototype.hideSearch = function () { 158 | this.$searchResults.classList.add('inactive'); 159 | } 160 | 161 | Menu.prototype.showSearch = function () { 162 | this.$searchResults.classList.remove('inactive'); 163 | } 164 | 165 | Menu.prototype.selectResult = function () { 166 | var $first = this.$searchResults.querySelector('li:first-child a'); 167 | 168 | if ($first) { 169 | document.location = $first.getAttribute('href'); 170 | } 171 | 172 | this.$searchBox.value = ''; 173 | this.$searchBox.blur(); 174 | this.hideSearch(); 175 | 176 | if (this._closeAfterSearch) { 177 | this.hide(); 178 | } 179 | } 180 | 181 | function init() { 182 | var menu = new Menu(); 183 | } 184 | 185 | document.addEventListener('DOMContentLoaded', init); 186 | 187 | function debounce(fn) { 188 | var timeout; 189 | return function() { 190 | var args = arguments; 191 | if (timeout) { 192 | clearTimeout(timeout); 193 | } 194 | timeout = setTimeout(function() { 195 | timeout = null; 196 | fn.apply(this, args); 197 | }.bind(this), 150); 198 | } 199 | } 200 | 201 | // The following license applies to the fuzzysearch function 202 | // The MIT License (MIT) 203 | // Copyright © 2015 Nicolas Bevacqua 204 | // Permission is hereby granted, free of charge, to any person obtaining a copy of 205 | // this software and associated documentation files (the "Software"), to deal in 206 | // the Software without restriction, including without limitation the rights to 207 | // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 208 | // the Software, and to permit persons to whom the Software is furnished to do so, 209 | // subject to the following conditions: 210 | 211 | // The above copyright notice and this permission notice shall be included in all 212 | // copies or substantial portions of the Software. 213 | 214 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 215 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 216 | // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 217 | // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 218 | // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 219 | // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 220 | function fuzzysearch (needle, haystack) { 221 | var tlen = haystack.length; 222 | var qlen = needle.length; 223 | if (qlen > tlen) { 224 | return false; 225 | } 226 | if (qlen === tlen) { 227 | return needle === haystack; 228 | } 229 | outer: for (var i = 0, j = 0; i < qlen; i++) { 230 | var nch = needle.charCodeAt(i); 231 | while (j < tlen) { 232 | if (haystack.charCodeAt(j++) === nch) { 233 | continue outer; 234 | } 235 | } 236 | return false; 237 | } 238 | return true; 239 | } 240 | var CLAUSE_NODES = ['EMU-CLAUSE', 'EMU-INTRO', 'EMU-ANNEX']; 241 | function findLocalReferences ($elem) { 242 | var name = $elem.innerHTML; 243 | var references = []; 244 | 245 | var parentClause = $elem.parentNode; 246 | while (parentClause && CLAUSE_NODES.indexOf(parentClause.nodeName) === -1) { 247 | parentClause = parentClause.parentNode; 248 | } 249 | 250 | if(!parentClause) return; 251 | 252 | var vars = parentClause.querySelectorAll('var'); 253 | 254 | for (var i = 0; i < vars.length; i++) { 255 | var $var = vars[i]; 256 | 257 | if ($var.innerHTML === name) { 258 | references.push($var); 259 | } 260 | } 261 | 262 | return references; 263 | } 264 | 265 | function toggleFindLocalReferences($elem) { 266 | var references = findLocalReferences($elem); 267 | if ($elem.classList.contains('referenced')) { 268 | references.forEach(function ($reference) { 269 | $reference.classList.remove('referenced'); 270 | }); 271 | } else { 272 | references.forEach(function ($reference) { 273 | $reference.classList.add('referenced'); 274 | }); 275 | } 276 | } 277 | 278 | function installFindLocalReferences () { 279 | document.addEventListener('click', function (e) { 280 | if (e.target.nodeName === 'VAR') { 281 | toggleFindLocalReferences(e.target); 282 | } 283 | }); 284 | } 285 | 286 | document.addEventListener('DOMContentLoaded', installFindLocalReferences); 287 | -------------------------------------------------------------------------------- /spec.md: -------------------------------------------------------------------------------- 1 | # String.prototype.padStart( maxLength [ , fillString ] ) 2 | 3 | When the _padStart_ method is called, the following steps are taken: 4 | 1. Let _O_ be ? RequireObjectCoercible(*this* value). 5 | 1. Let _S_ be ? ToString(_O_). 6 | 1. Let _intMaxLength_ be ? ToLength(_maxLength_). 7 | 1. Let _stringLength_ be the number of elements in S. 8 | 1. If _intMaxLength_ is not greater than _stringLength_, return _S_. 9 | 1. If _fillString_ is *undefined*, let _filler_ be a string consisting solely of the code unit U+0020 (SPACE). 10 | 1. Else, let _filler_ be ? ToString(_fillString_). 11 | 1. If _filler_ is the empty String, return _S_. 12 | 1. Let _fillLen_ be _intMaxLength_ - _stringLength_. 13 | 1. Let _truncatedStringFiller_ be a new String value consisting of repeated concatenations of _filler_ truncated to length _fillLen_. 14 | 1. Return a new String value computed by the concatenation of _truncatedStringFiller_ and _S_. 15 | 16 | Note: the first argument _maxLength_ will be clamped such that it can be no smaller than the length of the *this* value. 17 | Note: The optional second argument _fillString_ defaults to *" "* (a string consisting of U+0020 SPACE). 18 | 19 | # String.prototype.padEnd( maxLength [ , fillString ] ) 20 | 21 | When the _padEnd_ method is called, the following steps are taken: 22 | 1. Let _O_ be ? RequireObjectCoercible(*this* value). 23 | 1. Let _S_ be ? ToString(_O_). 24 | 1. Let _intMaxLength_ be ? ToLength(_maxLength_). 25 | 1. Let _stringLength_ be the number of elements in S. 26 | 1. If _intMaxLength_ is not greater than _stringLength_, return _S_. 27 | 1. If _fillString_ is *undefined*, let _filler_ be a string consisting solely of the code unit U+0020 (SPACE). 28 | 1. Else, let _filler_ be ? ToString(_fillString_). 29 | 1. If _filler_ is the empty String, return _S_. 30 | 1. Let _fillLen_ be _intMaxLength_ - _stringLength_. 31 | 1. Let _truncatedStringFiller_ be a new String value consisting of repeated concatenations of _filler_ truncated to length _fillLen_. 32 | 1. Return a new String value computed by the concatenation of _S_ and _truncatedStringFiller_. 33 | 34 | Note: the first argument _maxLength_ will be clamped such that it can be no smaller than the length of the *this* value. 35 | Note: The optional second argument _fillString_ defaults to *" "* (a string consisting of U+0020 SPACE). 36 | --------------------------------------------------------------------------------