@grain/stdlib 0.5.13 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (155) hide show
  1. package/CHANGELOG.md +193 -0
  2. package/LICENSE +1 -1
  3. package/README.md +25 -2
  4. package/array.gr +1512 -199
  5. package/array.md +2032 -94
  6. package/bigint.gr +239 -140
  7. package/bigint.md +450 -106
  8. package/buffer.gr +595 -102
  9. package/buffer.md +903 -145
  10. package/bytes.gr +401 -110
  11. package/bytes.md +551 -63
  12. package/char.gr +228 -49
  13. package/char.md +373 -7
  14. package/exception.gr +26 -12
  15. package/exception.md +29 -5
  16. package/float32.gr +130 -109
  17. package/float32.md +185 -57
  18. package/float64.gr +112 -99
  19. package/float64.md +185 -57
  20. package/hash.gr +47 -37
  21. package/hash.md +21 -3
  22. package/int16.gr +430 -0
  23. package/int16.md +618 -0
  24. package/int32.gr +200 -269
  25. package/int32.md +254 -289
  26. package/int64.gr +142 -225
  27. package/int64.md +254 -289
  28. package/int8.gr +511 -0
  29. package/int8.md +786 -0
  30. package/json.gr +2084 -0
  31. package/json.md +608 -0
  32. package/list.gr +120 -68
  33. package/list.md +125 -80
  34. package/map.gr +560 -57
  35. package/map.md +672 -56
  36. package/marshal.gr +239 -227
  37. package/marshal.md +36 -4
  38. package/number.gr +626 -676
  39. package/number.md +738 -153
  40. package/option.gr +33 -35
  41. package/option.md +58 -42
  42. package/package.json +2 -2
  43. package/path.gr +148 -187
  44. package/path.md +47 -96
  45. package/pervasives.gr +75 -416
  46. package/pervasives.md +85 -180
  47. package/priorityqueue.gr +433 -74
  48. package/priorityqueue.md +422 -54
  49. package/queue.gr +362 -80
  50. package/queue.md +433 -38
  51. package/random.gr +67 -75
  52. package/random.md +68 -40
  53. package/range.gr +135 -63
  54. package/range.md +198 -43
  55. package/rational.gr +284 -0
  56. package/rational.md +545 -0
  57. package/regex.gr +933 -1066
  58. package/regex.md +59 -60
  59. package/result.gr +23 -25
  60. package/result.md +54 -39
  61. package/runtime/atof/common.gr +78 -82
  62. package/runtime/atof/common.md +22 -10
  63. package/runtime/atof/decimal.gr +102 -127
  64. package/runtime/atof/decimal.md +28 -7
  65. package/runtime/atof/lemire.gr +56 -71
  66. package/runtime/atof/lemire.md +9 -1
  67. package/runtime/atof/parse.gr +83 -110
  68. package/runtime/atof/parse.md +12 -2
  69. package/runtime/atof/slow.gr +28 -35
  70. package/runtime/atof/slow.md +9 -1
  71. package/runtime/atof/table.gr +19 -18
  72. package/runtime/atof/table.md +10 -2
  73. package/runtime/atoi/parse.gr +153 -136
  74. package/runtime/atoi/parse.md +50 -1
  75. package/runtime/bigint.gr +410 -517
  76. package/runtime/bigint.md +71 -57
  77. package/runtime/compare.gr +176 -85
  78. package/runtime/compare.md +31 -1
  79. package/runtime/dataStructures.gr +144 -32
  80. package/runtime/dataStructures.md +267 -31
  81. package/runtime/debugPrint.gr +34 -15
  82. package/runtime/debugPrint.md +37 -5
  83. package/runtime/equal.gr +53 -52
  84. package/runtime/equal.md +30 -1
  85. package/runtime/exception.gr +38 -47
  86. package/runtime/exception.md +10 -8
  87. package/runtime/gc.gr +23 -152
  88. package/runtime/gc.md +13 -17
  89. package/runtime/malloc.gr +31 -31
  90. package/runtime/malloc.md +11 -3
  91. package/runtime/numberUtils.gr +191 -172
  92. package/runtime/numberUtils.md +17 -9
  93. package/runtime/numbers.gr +1695 -1021
  94. package/runtime/numbers.md +1098 -134
  95. package/runtime/string.gr +540 -242
  96. package/runtime/string.md +76 -6
  97. package/runtime/unsafe/constants.gr +30 -13
  98. package/runtime/unsafe/constants.md +80 -0
  99. package/runtime/unsafe/conv.gr +55 -28
  100. package/runtime/unsafe/conv.md +41 -9
  101. package/runtime/unsafe/memory.gr +10 -30
  102. package/runtime/unsafe/memory.md +15 -19
  103. package/runtime/unsafe/tags.gr +37 -21
  104. package/runtime/unsafe/tags.md +88 -8
  105. package/runtime/unsafe/wasmf32.gr +30 -36
  106. package/runtime/unsafe/wasmf32.md +64 -56
  107. package/runtime/unsafe/wasmf64.gr +30 -36
  108. package/runtime/unsafe/wasmf64.md +64 -56
  109. package/runtime/unsafe/wasmi32.gr +49 -66
  110. package/runtime/unsafe/wasmi32.md +102 -94
  111. package/runtime/unsafe/wasmi64.gr +52 -79
  112. package/runtime/unsafe/wasmi64.md +108 -100
  113. package/runtime/utils/printing.gr +13 -15
  114. package/runtime/utils/printing.md +11 -3
  115. package/runtime/wasi.gr +294 -295
  116. package/runtime/wasi.md +62 -42
  117. package/set.gr +574 -64
  118. package/set.md +634 -54
  119. package/stack.gr +181 -64
  120. package/stack.md +271 -42
  121. package/string.gr +453 -533
  122. package/string.md +241 -151
  123. package/uint16.gr +369 -0
  124. package/uint16.md +585 -0
  125. package/uint32.gr +470 -0
  126. package/uint32.md +737 -0
  127. package/uint64.gr +471 -0
  128. package/uint64.md +737 -0
  129. package/uint8.gr +369 -0
  130. package/uint8.md +585 -0
  131. package/uri.gr +1093 -0
  132. package/uri.md +477 -0
  133. package/{sys → wasi}/file.gr +914 -500
  134. package/{sys → wasi}/file.md +454 -50
  135. package/wasi/process.gr +292 -0
  136. package/{sys → wasi}/process.md +164 -6
  137. package/wasi/random.gr +77 -0
  138. package/wasi/random.md +80 -0
  139. package/{sys → wasi}/time.gr +15 -22
  140. package/{sys → wasi}/time.md +5 -5
  141. package/immutablearray.gr +0 -929
  142. package/immutablearray.md +0 -1038
  143. package/immutablemap.gr +0 -493
  144. package/immutablemap.md +0 -479
  145. package/immutablepriorityqueue.gr +0 -360
  146. package/immutablepriorityqueue.md +0 -291
  147. package/immutableset.gr +0 -498
  148. package/immutableset.md +0 -449
  149. package/runtime/debug.gr +0 -2
  150. package/runtime/debug.md +0 -6
  151. package/runtime/unsafe/errors.gr +0 -36
  152. package/runtime/unsafe/errors.md +0 -204
  153. package/sys/process.gr +0 -254
  154. package/sys/random.gr +0 -79
  155. package/sys/random.md +0 -66
package/json.md ADDED
@@ -0,0 +1,608 @@
1
+ ---
2
+ title: Json
3
+ ---
4
+
5
+ JSON (JavaScript Object Notation) parsing, printing, and access utilities.
6
+
7
+ ```grain
8
+ from "json" include Json
9
+ ```
10
+
11
+ ```grain
12
+ Json.parse("{\"currency\":\"€\",\"price\":99.99}")
13
+ ```
14
+
15
+ ```grain
16
+ print(
17
+ toString(
18
+ format=Pretty,
19
+ JsonObject([("currency", JsonString("€")), ("price", JsonNumber(99.9))])
20
+ )
21
+ )
22
+ ```
23
+
24
+ ## Types
25
+
26
+ Type declarations included in the Json module.
27
+
28
+ ### Json.**Json**
29
+
30
+ ```grain
31
+ enum Json {
32
+ JsonNull,
33
+ JsonBoolean(Bool),
34
+ JsonNumber(Number),
35
+ JsonString(String),
36
+ JsonArray(List<Json>),
37
+ JsonObject(List<(String, Json)>),
38
+ }
39
+ ```
40
+
41
+ Data structure representing JSON in Grain.
42
+
43
+ Examples:
44
+
45
+ ```grain
46
+ assert Json.parse("{\"currency\":\"€\",\"price\":99.99}") == JsonObject([
47
+ ("currency", JsonString("€")),
48
+ ("price", JsonNumber(99.99)),
49
+ ])
50
+ ```
51
+
52
+ ```grain
53
+ assert Json.parse("{\n\"currency\":\"€\",\n\"price\":99.99\n}") == JsonObject([
54
+ ("currency", JsonString("€")),
55
+ ("price", JsonNumber(99.99)),
56
+ ])
57
+ ```
58
+
59
+ ### Json.**JsonToStringError**
60
+
61
+ ```grain
62
+ enum JsonToStringError {
63
+ InvalidNumber(String),
64
+ }
65
+ ```
66
+
67
+ Represents errors for cases where a `Json` data structure cannot be represented as a
68
+ JSON string.
69
+
70
+ Variants:
71
+
72
+ ```grain
73
+ InvalidNumber(String)
74
+ ```
75
+
76
+ The `Json` data structure contains a number value of `NaN`, `Infinity`, or `-Infinity`.
77
+
78
+ ### Json.**IndentationFormat**
79
+
80
+ ```grain
81
+ enum IndentationFormat {
82
+ NoIndentation,
83
+ IndentWithTab,
84
+ IndentWithSpaces(Number),
85
+ }
86
+ ```
87
+
88
+ Controls how indentation is output in custom formatting.
89
+
90
+ Variants:
91
+
92
+ ```grain
93
+ NoIndentation
94
+ ```
95
+
96
+ No indentation is emitted.
97
+
98
+ ```json
99
+ {
100
+ "currency": "€",
101
+ "price": 99.9
102
+ }
103
+ ```
104
+
105
+ ```grain
106
+ IndentWithTab
107
+ ```
108
+
109
+ Tabs are emitted.
110
+
111
+ ```json
112
+ {
113
+ "currency": "€",
114
+ "price": 99.9
115
+ }
116
+ ```
117
+
118
+ ```grain
119
+ IndentWithSpaces(Number)
120
+ ```
121
+
122
+ The desired number of spaces are emitted.
123
+
124
+ `IndentWithSpaces(2)`
125
+ ```json
126
+ {
127
+ "currency": "€",
128
+ "price": 99.9
129
+ }
130
+ ```
131
+
132
+ `IndentWithSpaces(4)`
133
+ ```json
134
+ {
135
+ "currency": "€",
136
+ "price": 99.9
137
+ }
138
+ ```
139
+
140
+ ### Json.**ArrayFormat**
141
+
142
+ ```grain
143
+ enum ArrayFormat {
144
+ CompactArrayEntries,
145
+ SpacedArrayEntries,
146
+ OneArrayEntryPerLine,
147
+ }
148
+ ```
149
+
150
+ Controls how arrays are output in custom formatting.
151
+
152
+ Variants:
153
+
154
+ ```grain
155
+ CompactArrayEntries
156
+ ```
157
+
158
+ Arrays are emitted in a compact manner.
159
+
160
+ ```json
161
+ []
162
+ ```
163
+
164
+ ```json
165
+ [1]
166
+ ```
167
+
168
+ ```json
169
+ [1,2,3]
170
+ ```
171
+
172
+ ```grain
173
+ SpacedArrayEntries
174
+ ```
175
+
176
+ Arrays are emitted with spaces between elements.
177
+
178
+ ```json
179
+ [ ]
180
+ ```
181
+
182
+ ```json
183
+ [1]
184
+ ```
185
+
186
+ ```json
187
+ [1, 2, 3]
188
+ ```
189
+
190
+ ```grain
191
+ OneArrayEntryPerLine
192
+ ```
193
+
194
+ Arrays are emitted with newlines and indentation between each element.
195
+
196
+ ```json
197
+ []
198
+ ```
199
+
200
+ ```json
201
+ [
202
+ 1
203
+ ]
204
+ ```
205
+
206
+ ```json
207
+ [
208
+ 1,
209
+ 2,
210
+ 3
211
+ ]
212
+ ```
213
+
214
+ ### Json.**ObjectFormat**
215
+
216
+ ```grain
217
+ enum ObjectFormat {
218
+ CompactObjectEntries,
219
+ SpacedObjectEntries,
220
+ OneObjectEntryPerLine,
221
+ }
222
+ ```
223
+
224
+ Controls how objects are output in custom formatting.
225
+
226
+ Variants:
227
+
228
+ ```grain
229
+ CompactObjectEntries
230
+ ```
231
+
232
+ Objects are emitted in a compact manner.
233
+
234
+ ```json
235
+ {}
236
+ ```
237
+
238
+ ```json
239
+ {"a":1}
240
+ ```
241
+
242
+ ```json
243
+ {"a":1,"b":2,"c":3}
244
+ ```
245
+
246
+ ```grain
247
+ SpacedObjectEntries
248
+ ```
249
+
250
+ Objects are emitted with spaces between entries.
251
+
252
+ ```json
253
+ { }
254
+ ```
255
+
256
+ ```json
257
+ {"a": 1}
258
+ ```
259
+
260
+ ```json
261
+ {"a": 1, "b": 2, "c": 3}
262
+ ```
263
+
264
+ ```grain
265
+ OneObjectEntryPerLine
266
+ ```
267
+
268
+ Objects are emitted with each entry on a new line.
269
+
270
+ ```
271
+ {}
272
+ ```
273
+
274
+ ```
275
+ {
276
+ "a": 1
277
+ }
278
+ ```
279
+
280
+ ```
281
+ {
282
+ "a": 1,
283
+ "b": 2,
284
+ "c": 3
285
+ }
286
+ ```
287
+
288
+ ### Json.**LineEnding**
289
+
290
+ ```grain
291
+ enum LineEnding {
292
+ NoLineEnding,
293
+ LineFeed,
294
+ CarriageReturnLineFeed,
295
+ CarriageReturn,
296
+ }
297
+ ```
298
+
299
+ Controls how line endings are output in custom formatting.
300
+
301
+ Variants:
302
+
303
+ ```grain
304
+ NoLineEnding
305
+ ```
306
+
307
+ No line endings will be emitted.
308
+
309
+ ```grain
310
+ LineFeed
311
+ ```
312
+
313
+ A `\n` will be emitted at the end of each line.
314
+
315
+ ```grain
316
+ CarriageReturnLineFeed
317
+ ```
318
+
319
+ A `\r\n` will be emitted at the end of each line.
320
+
321
+ ```grain
322
+ CarriageReturn
323
+ ```
324
+
325
+ A `\r` will be emitted at the end of each line.
326
+
327
+ ### Json.**FormattingChoices**
328
+
329
+ ```grain
330
+ enum FormattingChoices {
331
+ Pretty,
332
+ Compact,
333
+ PrettyAndSafe,
334
+ CompactAndSafe,
335
+ Custom{
336
+ indentation: IndentationFormat,
337
+ arrayFormat: ArrayFormat,
338
+ objectFormat: ObjectFormat,
339
+ lineEnding: LineEnding,
340
+ finishWithNewLine: Bool,
341
+ escapeAllControlPoints: Bool,
342
+ escapeHTMLUnsafeSequences: Bool,
343
+ escapeNonASCII: Bool,
344
+ },
345
+ }
346
+ ```
347
+
348
+ Allows control of formatting in JSON output.
349
+
350
+ Variants:
351
+
352
+ ```grain
353
+ Pretty
354
+ ```
355
+
356
+ Recommended human readable formatting.
357
+
358
+ Escapes all control points for the sake of clarity, but outputs unicode
359
+ codepoints directly so the result needs to be treated as proper unicode and
360
+ is not safe to be transported in ASCII encoding.
361
+
362
+ Roughly Equivalent to:
363
+ ```grain
364
+ Custom{
365
+ indentation: IndentWithSpaces(2),
366
+ arrayFormat: OneArrayEntryPerLine,
367
+ objectFormat: OneObjectEntryPerLine,
368
+ lineEnding: LineFeed,
369
+ finishWithNewLine: true,
370
+ escapeAllControlPoints: true,
371
+ escapeHTMLUnsafeSequences: false,
372
+ escapeNonASCII: false,
373
+ }
374
+ ```
375
+
376
+ ```json
377
+ {
378
+ "currency": "€",
379
+ "price": 99.9,
380
+ "currencyDescription": "EURO\u007f",
381
+ }
382
+ ```
383
+
384
+ ```grain
385
+ Compact
386
+ ```
387
+
388
+ Compact formatting that minimizes the size of resulting JSON at cost of not
389
+ being easily human readable.
390
+
391
+ Only performs minimal string escaping as required by the ECMA-404 standard,
392
+ so the result needs to be treated as proper unicode and is not safe to be
393
+ transported in ASCII encoding.
394
+
395
+ Roughly Equivalent to:
396
+ ```grain
397
+ Custom{
398
+ indentation: NoIndentation,
399
+ arrayFormat: CompactArrayEntries,
400
+ objectFormat: CompactObjectEntries,
401
+ lineEnding: NoLineEnding,
402
+ finishWithNewLine: false,
403
+ escapeAllControlPoints: false,
404
+ escapeHTMLUnsafeSequences: false,
405
+ escapeNonASCII: false,
406
+ }
407
+ ```
408
+
409
+ ```json
410
+ {"currency":"€","price":99.9,"currencyDescription":"EURO␡"}
411
+ ```
412
+
413
+ ```grain
414
+ PrettyAndSafe
415
+ ```
416
+
417
+ Pretty and conservative formatting to maximize compatibility and
418
+ embeddability of the resulting JSON.
419
+
420
+ Should be safe to copy and paste directly into HTML and to be transported in
421
+ plain ASCII.
422
+
423
+ Roughly Equivalent to:
424
+ ```grain
425
+ Custom{
426
+ indentation: IndentWithSpaces(2),
427
+ arrayFormat: OneArrayEntryPerLine,
428
+ objectFormat: OneObjectEntryPerLine,
429
+ lineEnding: LineFeed,
430
+ finishWithNewLine: true,
431
+ escapeAllControlPoints: true,
432
+ escapeHTMLUnsafeSequences: true,
433
+ escapeNonASCII: true,
434
+ }
435
+ ```
436
+
437
+ ```json
438
+ {
439
+ "currency": "\u20ac",
440
+ "price": 99.9,
441
+ "currencyDescription": "EURO\u007f",
442
+ }
443
+ ```
444
+
445
+ ```grain
446
+ CompactAndSafe
447
+ ```
448
+
449
+ Compact and conservative formatting to maximize compatibility and
450
+ embeddability of the resulting JSON.
451
+
452
+ Should be safe to copy and paste directly into HTML and to transported in
453
+ plain ASCII.
454
+
455
+ Roughly Equivalent to:
456
+ ```grain
457
+ Custom{
458
+ indentation: NoIndentation,
459
+ arrayFormat: CompactArrayEntries,
460
+ objectFormat: CompactObjectEntries,
461
+ lineEnding: NoLineEnding,
462
+ finishWithNewLine: false,
463
+ escapeAllControlPoints: true,
464
+ escapeHTMLUnsafeSequences: true,
465
+ escapeNonASCII: true,
466
+ }
467
+ ```
468
+
469
+ ```json
470
+ {"currency":"\u20ac","price":99.9,"currencyDescription":"EURO\u007f"}
471
+ ```
472
+
473
+ ```grain
474
+ Custom{
475
+ indentation: IndentationFormat,
476
+ arrayFormat: ArrayFormat,
477
+ objectFormat: ObjectFormat,
478
+ lineEnding: LineEnding,
479
+ finishWithNewLine: Bool,
480
+ escapeAllControlPoints: Bool,
481
+ escapeHTMLUnsafeSequences: Bool,
482
+ escapeNonASCII: Bool,
483
+ }
484
+ ```
485
+
486
+ Allows for fined grained control of the formatting output.
487
+
488
+ ### Json.**JsonParseError**
489
+
490
+ ```grain
491
+ enum JsonParseError {
492
+ UnexpectedEndOfInput(String),
493
+ UnexpectedToken(String),
494
+ InvalidUTF16SurrogatePair(String),
495
+ }
496
+ ```
497
+
498
+ Represents errors for JSON parsing along with a human readable message.
499
+
500
+ ## Values
501
+
502
+ Functions and constants included in the Json module.
503
+
504
+ ### Json.**toString**
505
+
506
+ <details disabled>
507
+ <summary tabindex="-1">Added in <code>0.6.0</code></summary>
508
+ No other changes yet.
509
+ </details>
510
+
511
+ ```grain
512
+ toString :
513
+ (?format: FormattingChoices, json: Json) =>
514
+ Result<String, JsonToStringError>
515
+ ```
516
+
517
+ Converts the `Json` data structure into a JSON string with specific formatting settings.
518
+
519
+ Parameters:
520
+
521
+ |param|type|description|
522
+ |-----|----|-----------|
523
+ |`?format`|`FormattingChoices`|Formatting options|
524
+ |`json`|`Json`|The `Json` data structure to convert|
525
+
526
+ Returns:
527
+
528
+ |type|description|
529
+ |----|-----------|
530
+ |`Result<String, JsonToStringError>`|`Ok(str)` containing the JSON string or `Err(err)` if the provided `Json` data structure cannot be converted to a string|
531
+
532
+ Examples:
533
+
534
+ ```grain
535
+ assert toString(
536
+ JsonObject([("currency", JsonString("€")), ("price", JsonNumber(99.9))]
537
+ ) == Ok("{\"currency\":\"€\",\"price\":99.9}")
538
+ ```
539
+
540
+ ```grain
541
+ assert toString(
542
+ format=Compact
543
+ JsonObject([("currency", JsonString("€")), ("price", JsonNumber(99.9))])
544
+ ) == Ok("{\"currency\":\"€\",\"price\":99.9}")
545
+ ```
546
+
547
+ ```grain
548
+ assert toString(
549
+ format=Pretty,
550
+ JsonObject([("currency", JsonString("€")), ("price", JsonNumber(99.9))])
551
+ ) == Ok("{
552
+ \"currency\": \"€\",
553
+ \"price\": 99.9
554
+ }")
555
+ ```
556
+
557
+ ```grain
558
+ assert toString(
559
+ format=Custom{
560
+ indentation: NoIndentation,
561
+ arrayFormat: CompactArrayEntries,
562
+ objectFormat: CompactObjectEntries,
563
+ lineEnding: NoLineEnding,
564
+ finishWithNewLine: false,
565
+ escapeAllControlPoints: true,
566
+ escapeHTMLUnsafeSequences: true,
567
+ escapeNonASCII: true,
568
+ },
569
+ JsonObject([("currency", JsonString("€")), ("price", JsonNumber(99.9))])
570
+ ) == Ok("{\"currency\":\"\\u20ac\",\"price\":99.9}")
571
+ ```
572
+
573
+ ### Json.**parse**
574
+
575
+ <details disabled>
576
+ <summary tabindex="-1">Added in <code>0.6.0</code></summary>
577
+ No other changes yet.
578
+ </details>
579
+
580
+ ```grain
581
+ parse : (str: String) => Result<Json, JsonParseError>
582
+ ```
583
+
584
+ Parses JSON string into a `Json` data structure.
585
+
586
+ Parameters:
587
+
588
+ |param|type|description|
589
+ |-----|----|-----------|
590
+ |`str`|`String`|The JSON string to parse|
591
+
592
+ Returns:
593
+
594
+ |type|description|
595
+ |----|-----------|
596
+ |`Result<Json, JsonParseError>`|`Ok(json)` containing the parsed data structure on a successful parse or `Err(err)` containing a parse error otherwise|
597
+
598
+ Examples:
599
+
600
+ ```grain
601
+ assert parse("{\"currency\":\"$\",\"price\":119}") == Ok(
602
+ JsonObject([
603
+ ("currency", JsonString("$")),
604
+ ("price", JsonNumber(119))
605
+ ])
606
+ )
607
+ ```
608
+