├── .gitignore ├── README.md ├── index.html ├── package.json ├── spec.css ├── spec.emu ├── spec.js └── spec.md /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Map-Set.prototype.toJSON 2 | 3 | An [ECMAScript](https://github.com/tc39/ecma262) proposal. ~~Currently at **Stage 0**.~~ 4 | 5 | This proposal was brought before the committee in the March 2016 meeting, and thoroughly rejected. [Details](https://github.com/DavidBruant/Map-Set.prototype.toJSON/issues/16) 6 | 7 | ## Problem 8 | 9 | Here is the current situation: 10 | 11 | ````js 12 | var s = new Set(['yo', 'ya', true, 8]); 13 | 14 | console.log(JSON.stringify(s)); // '{}' 15 | ```` 16 | 17 | This result is unhelpful. Same goes for Map. 18 | 19 | 20 | ## Proposal 21 | 22 | This proposal is about providing a sensible default to the common operation of JSON serialization via default `toJSON` implementations on `Set.prototype` and `Map.prototype`. Of course, userland code can always shadow this value on specific instances. 23 | 24 | ### Map.prototype.toJSON 25 | 26 | The essence of the proposal is captured in this snippet (spec in [markdown](spec.md#mapprototypetojson--) or [HTML](http://davidbruant.github.io/Map-Set.prototype.toJSON/#Map.prototype.toJSON)): 27 | 28 | ````js 29 | Map.prototype.toJSON = function toJSON() { 30 | return [...Map.prototype.entries.call(this)]; 31 | } 32 | ```` 33 | 34 | ### Set.prototype.toJSON 35 | 36 | The essence of the proposal is captured in this snippet (spec in [markdown](spec.md#setprototypetojson--) or [HTML](http://davidbruant.github.io/Map-Set.prototype.toJSON/#Set.prototype.toJSON)): 37 | 38 | ````js 39 | Set.prototype.toJSON = function toJSON() { 40 | return [...Set.prototype.values.call(this)]; 41 | } 42 | ```` 43 | 44 | ## Discussion 45 | 46 | There might be a web compat concern if code using native Map and Set or polyfill relies on the current JSON.stringify behavior. 47 | 48 | 49 | # Licence 50 | 51 | This work is dedicated to the public domain. It is [CC0 licenced](https://creativecommons.org/publicdomain/zero/1.0/). 52 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Map.prototype.toJSON / Set.prototype.toJSON 9 | 39 | 40 | 41 | 42 | 43 | 55 |

Stage 0 Draft / March 28, 2016

56 |

Map.prototype.toJSON / Set.prototype.toJSON

57 | 58 |

1Map.prototype.toJSON ( )#

59 |

This function provides an Array representation of an iterable object for use by JSON.stringify.

60 |

61 |

62 |

When the 63 | toJSON method is called, the following steps are taken:

64 | 65 |
    66 |
  1. Let M be the 67 | this value.
  2. 68 |
  3. Let iterator be ? 69 | GetIterator(M)
  4. 70 |
  5. Let A be ! 71 | ArrayCreate( 72 | 0).
  6. 73 |
  7. Let k be 74 | 0.
  8. 75 |
  9. Repeat 76 |
      77 |
    1. Let Pk be ! 78 | ToString(k).
    2. 79 |
    3. Let next be ? 80 | IteratorStep(iterator).
    4. 81 |
    5. If next is 82 | false, then 83 |
        84 |
      1. Perform ? 85 | Set(A, "length", k, 86 | true).
      2. 87 |
      3. Return A.
      4. 88 |
      89 |
    6. 90 |
    7. Let nextValue be ? 91 | IteratorValue(next).
    8. 92 |
    9. Let defineStatus be 93 | CreateDataPropertyOrThrow(A, Pk, nextValue).
    10. 94 |
    11. If defineStatus is an 95 | abrupt completion, return 96 | IteratorClose(iterator, defineStatus).
    12. 97 |
    13. Increase k by 1. 98 |
    14. 99 |
    100 |
  10. 101 |
102 |
103 | NoteThe 104 | length property of the 105 | toJSON method is 106 | 0. 107 |
108 | 109 | 110 |

2Set.prototype.toJSON ( )#

111 |

The initial value of the 112 | toJSON property is the same function object as the initial value of the 113 | Map.prototype.toJSON property.

114 |
115 | 116 |

ACopyright & Software License#

117 | 118 |

Copyright Notice

119 |

© 2016 David Bruant, Jordan Harband

120 | 121 |

Software License

122 |

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.

123 | 124 |

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

125 | 126 |
    127 |
  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. 128 |
  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. 129 |
  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. 130 |
131 | 132 |

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.

133 | 134 |
135 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ecma-proposal-map-set-prototype-tojson", 3 | "version": "0.0.0", 4 | "description": "ES7 spec proposal for Map#toJSON / Set#toJSON", 5 | "scripts": { 6 | "build": "ecmarkup spec.emu --js=spec.js --css=spec.css | js-beautify -f - --type=html -t > index.html", 7 | "prepublish": "npm run build && echo >&2 'no publishing' && exit 255" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/DavidBruant/Map-Set.prototype.toJSON.git" 12 | }, 13 | "keywords": [ 14 | "Map", 15 | "Set", 16 | "values", 17 | "entries", 18 | "toJSON", 19 | "JSON", 20 | "stringify", 21 | "spec" 22 | ], 23 | "license": "MIT", 24 | "bugs": { 25 | "url": "https://github.com/DavidBruant/Map-Set.prototype.toJSON/issues" 26 | }, 27 | "homepage": "https://github.com/DavidBruant/Map-Set.prototype.toJSON#readme", 28 | "dependencies": {}, 29 | "devDependencies": { 30 | "ecmarkup": "^3.1.1", 31 | "js-beautify": "^1.6.2", 32 | "tape": "^4.5.1" 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /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: Map.prototype.toJSON / Set.prototype.toJSON
 8 | stage: 0
 9 | contributors: David Bruant, Jordan Harband
10 | 
11 | 12 |

Map.prototype.toJSON ( )

13 |

This function provides an Array representation of an iterable object for use by JSON.stringify.

14 |

When the *toJSON* method is called, the following steps are taken:

15 | 16 | 1. Let _M_ be the *this* value. 17 | 1. Let _iterator_ be ? GetIterator(_M_) 18 | 1. Let _A_ be ! ArrayCreate(*0*). 19 | 1. Let _k_ be *0*. 20 | 1. Repeat 21 | 1. Let _Pk_ be ! ToString(_k_). 22 | 1. Let _next_ be ? IteratorStep(_iterator_). 23 | 1. If _next_ is *false*, then 24 | 1. Perform ? Set(_A_, "length", _k_, *true*). 25 | 1. Return _A_. 26 | 1. Let _nextValue_ be ? IteratorValue(_next_). 27 | 1. Let _defineStatus_ be CreateDataPropertyOrThrow(_A_, _Pk_, _nextValue_). 28 | 1. If _defineStatus_ is an abrupt completion, return IteratorClose(_iterator_, _defineStatus_). 29 | 1. Increase _k_ by 1. 30 | 31 | The *length* property of the *toJSON* method is *0*. 32 |
33 | 34 | 35 |

Set.prototype.toJSON ( )

36 |

The initial value of the *toJSON* property is the same function object as the initial value of the *Map.prototype.toJSON* property.

37 |
38 | -------------------------------------------------------------------------------- /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 | # Map.prototype.toJSON ( ) 2 | 3 | This function provides an Array representation of an iterable object for use by [JSON.stringify][json-stringify] ([24.3.2][json-stringify]). 4 | 5 | When the **toJSON** method is called, the following steps are taken: 6 | 7 | 1. Let *M* be the **this** value. 8 | 1. Let *iterator* be ? GetIterator(*M*) 9 | 1. Let *A* be ! ArrayCreate(**0**). 10 | 1. Let *k* be **0**. 11 | 1. Repeat 12 | 1. Let *Pk* be ! ToString(*k*). 13 | 1. Let *next* be ? IteratorStep(*iterator*). 14 | 1. If *next* is **false**, then 15 | 1. Perform ? Set(*A*, "length", *k*, **true**). 16 | 1. Return *A*. 17 | 1. Let *nextValue* be ? IteratorValue(*next*). 18 | 1. Let *defineStatus* be CreateDataPropertyOrThrow(*A*, *Pk*, *nextValue*). 19 | 1. If *defineStatus* is an abrupt completion, return IteratorClose(*iterator*, *defineStatus*). 20 | 1. Increase *k* by 1. 21 | 22 | The **length** property of the **toJSON** method is **0**. 23 | 24 | # Set.prototype.toJSON ( ) 25 | 26 | The initial value of the **toJSON** property is the same function object as the initial value of the **Map.prototype.toJSON** property. 27 | 28 | [json-stringify]: http://www.ecma-international.org/ecma-262/6.0/#sec-json.stringify 29 | --------------------------------------------------------------------------------