@grain/stdlib 0.4.0 → 0.4.4

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.
package/string.md ADDED
@@ -0,0 +1,815 @@
1
+ ---
2
+ title: String
3
+ ---
4
+
5
+ Utilities for working with strings.
6
+
7
+ <details>
8
+ <summary>Added in <code>0.2.0</code></summary>
9
+ <table>
10
+ <thead>
11
+ <tr><th>version</th><th>changes</th></tr>
12
+ </thead>
13
+ <tbody>
14
+ <tr><td><code>0.1.0</code></td><td>Originally named `strings`</td></tr>
15
+ <tr><td><code>0.2.0</code></td><td>Renamed to `string`</td></tr>
16
+ </tbody>
17
+ </table>
18
+ </details>
19
+
20
+ ```grain
21
+ import String from "string"
22
+ ```
23
+
24
+ ## Types
25
+
26
+ Type declarations included in the String module.
27
+
28
+ ### String.**Encoding**
29
+
30
+ ```grain
31
+ enum Encoding {
32
+ UTF8,
33
+ UTF16_BE,
34
+ UTF16_LE,
35
+ UTF32_BE,
36
+ UTF32_LE,
37
+ }
38
+ ```
39
+
40
+ Byte encodings
41
+
42
+ ## Values
43
+
44
+ Functions for working with the String data type.
45
+
46
+ ### String.**concat**
47
+
48
+ <details disabled>
49
+ <summary tabindex="-1">Added in <code>0.1.0</code></summary>
50
+ No other changes yet.
51
+ </details>
52
+
53
+ ```grain
54
+ concat : (String, String) -> String
55
+ ```
56
+
57
+ Concatenate two strings.
58
+
59
+ Parameters:
60
+
61
+ |param|type|description|
62
+ |-----|----|-----------|
63
+ |`str1`|`String`|The beginning string|
64
+ |`str2`|`String`|The ending string|
65
+
66
+ Returns:
67
+
68
+ |type|description|
69
+ |----|-----------|
70
+ |`String`|The combined string|
71
+
72
+ Examples:
73
+
74
+ ```grain
75
+ String.concat("Foo", " Bar") == "FooBar"
76
+ ```
77
+
78
+ ### String.**length**
79
+
80
+ <details disabled>
81
+ <summary tabindex="-1">Added in <code>0.1.0</code></summary>
82
+ No other changes yet.
83
+ </details>
84
+
85
+ ```grain
86
+ length : String -> Number
87
+ ```
88
+
89
+ Returns the character length of the input string.
90
+
91
+ Parameters:
92
+
93
+ |param|type|description|
94
+ |-----|----|-----------|
95
+ |`string`|`String`|The string to inspect|
96
+
97
+ Returns:
98
+
99
+ |type|description|
100
+ |----|-----------|
101
+ |`Number`|The number of characters in the string|
102
+
103
+ Examples:
104
+
105
+ ```grain
106
+ String.length("Hello world") == 11
107
+ ```
108
+
109
+ ### String.**byteLength**
110
+
111
+ <details disabled>
112
+ <summary tabindex="-1">Added in <code>0.1.0</code></summary>
113
+ No other changes yet.
114
+ </details>
115
+
116
+ ```grain
117
+ byteLength : String -> Number
118
+ ```
119
+
120
+ Returns the byte length of the input string.
121
+
122
+ Parameters:
123
+
124
+ |param|type|description|
125
+ |-----|----|-----------|
126
+ |`string`|`String`|The string to inspect|
127
+
128
+ Returns:
129
+
130
+ |type|description|
131
+ |----|-----------|
132
+ |`Number`|The number of bytes in the string|
133
+
134
+ Examples:
135
+
136
+ ```grain
137
+ String.byteLength("🌾") == 4
138
+ ```
139
+
140
+ ### String.**indexOf**
141
+
142
+ <details disabled>
143
+ <summary tabindex="-1">Added in <code>0.3.0</code></summary>
144
+ No other changes yet.
145
+ </details>
146
+
147
+ ```grain
148
+ indexOf : (String, String) -> Option<Number>
149
+ ```
150
+
151
+ Finds the position of a substring in the input string.
152
+
153
+ Parameters:
154
+
155
+ |param|type|description|
156
+ |-----|----|-----------|
157
+ |`search`|`String`|The substring to find|
158
+ |`string`|`String`|The string to inspect|
159
+
160
+ Returns:
161
+
162
+ |type|description|
163
+ |----|-----------|
164
+ |`Option<Number>`|`Some(position)` containing the starting position of the substring if found or `None` otherwise|
165
+
166
+ Examples:
167
+
168
+ ```grain
169
+ String.indexOf("world", "Hello world") == Some(6)
170
+ ```
171
+
172
+ ### String.**charAt**
173
+
174
+ <details disabled>
175
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
176
+ No other changes yet.
177
+ </details>
178
+
179
+ ```grain
180
+ charAt : (Number, String) -> Char
181
+ ```
182
+
183
+ Get the character at the position in the input string.
184
+
185
+ Parameters:
186
+
187
+ |param|type|description|
188
+ |-----|----|-----------|
189
+ |`position`|`Number`|The position to check|
190
+ |`string`|`String`|The string to search|
191
+
192
+ Returns:
193
+
194
+ |type|description|
195
+ |----|-----------|
196
+ |`Char`|The character at the provided position|
197
+
198
+ Examples:
199
+
200
+ ```grain
201
+ String.charAt(5, "Hello world") == ' '
202
+ ```
203
+
204
+ ### String.**explode**
205
+
206
+ <details disabled>
207
+ <summary tabindex="-1">Added in <code>0.3.0</code></summary>
208
+ No other changes yet.
209
+ </details>
210
+
211
+ ```grain
212
+ explode : String -> Array<Char>
213
+ ```
214
+
215
+ Split a string into its Unicode characters.
216
+
217
+ Parameters:
218
+
219
+ |param|type|description|
220
+ |-----|----|-----------|
221
+ |`string`|`String`|The string to split|
222
+
223
+ Returns:
224
+
225
+ |type|description|
226
+ |----|-----------|
227
+ |`Array<Char>`|An array containing all characters in the string|
228
+
229
+ Examples:
230
+
231
+ ```grain
232
+ String.explode("Hello") == [> 'H', 'e', 'l', 'l', 'o']
233
+ ```
234
+
235
+ ### String.**implode**
236
+
237
+ <details disabled>
238
+ <summary tabindex="-1">Added in <code>0.3.0</code></summary>
239
+ No other changes yet.
240
+ </details>
241
+
242
+ ```grain
243
+ implode : Array<Char> -> String
244
+ ```
245
+
246
+ Create a string from an array of characters.
247
+
248
+ Parameters:
249
+
250
+ |param|type|description|
251
+ |-----|----|-----------|
252
+ |`arr`|`Array<Char>`|The array to combine|
253
+
254
+ Returns:
255
+
256
+ |type|description|
257
+ |----|-----------|
258
+ |`String`|A string representation of the array of characters|
259
+
260
+ Examples:
261
+
262
+ ```grain
263
+ String.implode([> 'H', 'e', 'l', 'l', 'o']) == "Hello"
264
+ ```
265
+
266
+ ### String.**reverse**
267
+
268
+ <details disabled>
269
+ <summary tabindex="-1">Added in <code>next</code></summary>
270
+ No other changes yet.
271
+ </details>
272
+
273
+ ```grain
274
+ reverse : String -> String
275
+ ```
276
+
277
+ Create a string that is the given string reversed.
278
+
279
+ Parameters:
280
+
281
+ |param|type|description|
282
+ |-----|----|-----------|
283
+ |`string`|`String`|The string to reverse|
284
+
285
+ Returns:
286
+
287
+ |type|description|
288
+ |----|-----------|
289
+ |`String`|A string whose characters are in the reverse order of the given string|
290
+
291
+ Examples:
292
+
293
+ ```grain
294
+ String.reverse("olleH") == "Hello"
295
+ ```
296
+
297
+ ### String.**split**
298
+
299
+ ```grain
300
+ split : (String, String) -> Array<String>
301
+ ```
302
+
303
+ Split a string by the given separator.
304
+
305
+ Parameters:
306
+
307
+ |param|type|description|
308
+ |-----|----|-----------|
309
+ |`separator`|`String`|The separator to split on|
310
+ |`string`|`String`|The string to split|
311
+
312
+ Returns:
313
+
314
+ |type|description|
315
+ |----|-----------|
316
+ |`Array<String>`|An array of substrings from the initial string|
317
+
318
+ Examples:
319
+
320
+ ```grain
321
+ String.split(" ", "Hello world") == [> "Hello", "world"]
322
+ ```
323
+
324
+ ### String.**slice**
325
+
326
+ <details disabled>
327
+ <summary tabindex="-1">Added in <code>0.1.0</code></summary>
328
+ No other changes yet.
329
+ </details>
330
+
331
+ ```grain
332
+ slice : (Number, Number, String) -> String
333
+ ```
334
+
335
+ Get a portion of a string.
336
+
337
+ Parameters:
338
+
339
+ |param|type|description|
340
+ |-----|----|-----------|
341
+ |`start`|`Number`|The start position of the substring|
342
+ |`to`|`Number`|The end position of the substring, exclusive|
343
+ |`string`|`String`|The input string|
344
+
345
+ Returns:
346
+
347
+ |type|description|
348
+ |----|-----------|
349
+ |`String`|The substring from the initial string|
350
+
351
+ Examples:
352
+
353
+ ```grain
354
+ String.slice(0, 5, "Hello world") == "Hello"
355
+ ```
356
+
357
+ ### String.**contains**
358
+
359
+ <details disabled>
360
+ <summary tabindex="-1">Added in <code>0.1.0</code></summary>
361
+ No other changes yet.
362
+ </details>
363
+
364
+ ```grain
365
+ contains : (String, String) -> Bool
366
+ ```
367
+
368
+ Check if a string contains a substring.
369
+
370
+ Parameters:
371
+
372
+ |param|type|description|
373
+ |-----|----|-----------|
374
+ |`search`|`String`|The substring to check|
375
+ |`string`|`String`|The string to search|
376
+
377
+ Returns:
378
+
379
+ |type|description|
380
+ |----|-----------|
381
+ |`Bool`|`true` if the input string contains the search value or `false` otherwise|
382
+
383
+ Examples:
384
+
385
+ ```grain
386
+ String.contains("world", "Hello world") == true
387
+ ```
388
+
389
+ ### String.**startsWith**
390
+
391
+ <details disabled>
392
+ <summary tabindex="-1">Added in <code>0.1.0</code></summary>
393
+ No other changes yet.
394
+ </details>
395
+
396
+ ```grain
397
+ startsWith : (String, String) -> Bool
398
+ ```
399
+
400
+ Check if a string begins with another string.
401
+
402
+ Parameters:
403
+
404
+ |param|type|description|
405
+ |-----|----|-----------|
406
+ |`search`|`String`|The string to compare to the start|
407
+ |`string`|`String`|The string to search|
408
+
409
+ Returns:
410
+
411
+ |type|description|
412
+ |----|-----------|
413
+ |`Bool`|`true` if the input string starts with the search value or `false` otherwise|
414
+
415
+ Examples:
416
+
417
+ ```grain
418
+ String.startsWith("Hello", "Hello world") == true
419
+ ```
420
+
421
+ ### String.**endsWith**
422
+
423
+ <details disabled>
424
+ <summary tabindex="-1">Added in <code>0.1.0</code></summary>
425
+ No other changes yet.
426
+ </details>
427
+
428
+ ```grain
429
+ endsWith : (String, String) -> Bool
430
+ ```
431
+
432
+ Check if a string ends with another string.
433
+
434
+ Parameters:
435
+
436
+ |param|type|description|
437
+ |-----|----|-----------|
438
+ |`search`|`String`|The string to compare to the end|
439
+ |`string`|`String`|The string to search|
440
+
441
+ Returns:
442
+
443
+ |type|description|
444
+ |----|-----------|
445
+ |`Bool`|`true` if the input string ends with the search value or `false` otherwise|
446
+
447
+ Examples:
448
+
449
+ ```grain
450
+ String.endsWith("world", "Hello world") == true
451
+ ```
452
+
453
+ ### String.**encodeAt**
454
+
455
+ <details disabled>
456
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
457
+ No other changes yet.
458
+ </details>
459
+
460
+ ```grain
461
+ encodeAt : (String, Encoding, Bytes, Number) -> Bytes
462
+ ```
463
+
464
+ Encodes the given string into a byte sequence at the supplied position, excluding any byte-order marker, using the encoding scheme provided.
465
+
466
+ Parameters:
467
+
468
+ |param|type|description|
469
+ |-----|----|-----------|
470
+ |`string`|`String`|The input string|
471
+ |`encoding`|`Encoding`|The encoding to use|
472
+ |`dest`|`Bytes`|The byte sequence that will be copied|
473
+ |`destPos`|`Number`|The location in the byte sequence to write the output|
474
+
475
+ Returns:
476
+
477
+ |type|description|
478
+ |----|-----------|
479
+ |`Bytes`|A copy of the input bytes with the encoded string replaced at the given position|
480
+
481
+ ### String.**encodeAtWithBom**
482
+
483
+ <details disabled>
484
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
485
+ No other changes yet.
486
+ </details>
487
+
488
+ ```grain
489
+ encodeAtWithBom : (String, Encoding, Bytes, Number) -> Bytes
490
+ ```
491
+
492
+ Encodes the given string into a byte sequence at the supplied position, including any byte-order marker, using the encoding scheme provided.
493
+
494
+ Parameters:
495
+
496
+ |param|type|description|
497
+ |-----|----|-----------|
498
+ |`string`|`String`|The input string|
499
+ |`encoding`|`Encoding`|The encoding to use|
500
+ |`dest`|`Bytes`|The byte sequence that will be copied|
501
+ |`destPos`|`Number`|The location in the byte sequence to write the output|
502
+
503
+ Returns:
504
+
505
+ |type|description|
506
+ |----|-----------|
507
+ |`Bytes`|A copy of the input bytes with the encoded string replaced at the given position|
508
+
509
+ ### String.**encode**
510
+
511
+ <details disabled>
512
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
513
+ No other changes yet.
514
+ </details>
515
+
516
+ ```grain
517
+ encode : (String, Encoding) -> Bytes
518
+ ```
519
+
520
+ Encodes the given string using the given encoding scheme, excluding any byte-order marker.
521
+
522
+ Parameters:
523
+
524
+ |param|type|description|
525
+ |-----|----|-----------|
526
+ |`string`|`String`|The input string|
527
+ |`encoding`|`Encoding`|The encoding to use|
528
+
529
+ Returns:
530
+
531
+ |type|description|
532
+ |----|-----------|
533
+ |`Bytes`|The byte representation of the string in the given encoding|
534
+
535
+ ### String.**encodeWithBom**
536
+
537
+ <details disabled>
538
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
539
+ No other changes yet.
540
+ </details>
541
+
542
+ ```grain
543
+ encodeWithBom : (String, Encoding) -> Bytes
544
+ ```
545
+
546
+ Encodes the given string using the given encoding scheme, including any byte-order marker.
547
+
548
+ Parameters:
549
+
550
+ |param|type|description|
551
+ |-----|----|-----------|
552
+ |`string`|`String`|The input string|
553
+ |`encoding`|`Encoding`|The encoding to use|
554
+
555
+ Returns:
556
+
557
+ |type|description|
558
+ |----|-----------|
559
+ |`Bytes`|The byte representation of the string in the given encoding|
560
+
561
+ ### String.**decodeRange**
562
+
563
+ <details disabled>
564
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
565
+ No other changes yet.
566
+ </details>
567
+
568
+ ```grain
569
+ decodeRange : (Bytes, Encoding, Number, Number) -> String
570
+ ```
571
+
572
+ Decodes the given byte sequence of the specified range into a string, excluding any byte-order marker, using encoding scheme provided.
573
+
574
+ Parameters:
575
+
576
+ |param|type|description|
577
+ |-----|----|-----------|
578
+ |`bytes`|`Bytes`|The input bytes|
579
+ |`encoding`|`Encoding`|The encoding to use|
580
+ |`start`|`Number`|The byte offset to begin decoding from|
581
+ |`size`|`Number`|The maximum number of bytes to decode|
582
+
583
+ Returns:
584
+
585
+ |type|description|
586
+ |----|-----------|
587
+ |`String`|The decoded string|
588
+
589
+ ### String.**decodeRangeKeepBom**
590
+
591
+ <details disabled>
592
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
593
+ No other changes yet.
594
+ </details>
595
+
596
+ ```grain
597
+ decodeRangeKeepBom : (Bytes, Encoding, Number, Number) -> String
598
+ ```
599
+
600
+ Decodes the given byte sequence of the specified range into a string, including any byte-order marker, using encoding scheme provided.
601
+
602
+ Parameters:
603
+
604
+ |param|type|description|
605
+ |-----|----|-----------|
606
+ |`bytes`|`Bytes`|The input bytes|
607
+ |`encoding`|`Encoding`|The encoding to use|
608
+ |`start`|`Number`|The byte offset to begin decoding from|
609
+ |`size`|`Number`|The maximum number of bytes to decode|
610
+
611
+ Returns:
612
+
613
+ |type|description|
614
+ |----|-----------|
615
+ |`String`|The decoded string|
616
+
617
+ ### String.**decode**
618
+
619
+ <details disabled>
620
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
621
+ No other changes yet.
622
+ </details>
623
+
624
+ ```grain
625
+ decode : (Bytes, Encoding) -> String
626
+ ```
627
+
628
+ Decodes the given byte sequence into a string using the given encoding scheme, excluding any byte-order marker.
629
+
630
+ Parameters:
631
+
632
+ |param|type|description|
633
+ |-----|----|-----------|
634
+ |`bytes`|`Bytes`|The input bytes|
635
+ |`encoding`|`Encoding`|The encoding to use|
636
+
637
+ Returns:
638
+
639
+ |type|description|
640
+ |----|-----------|
641
+ |`String`|The decoded string|
642
+
643
+ ### String.**decodeKeepBom**
644
+
645
+ <details disabled>
646
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
647
+ No other changes yet.
648
+ </details>
649
+
650
+ ```grain
651
+ decodeKeepBom : (Bytes, Encoding) -> String
652
+ ```
653
+
654
+ Decodes the given byte sequence into a string using the given encoding scheme, including any byte-order marker.
655
+
656
+ Parameters:
657
+
658
+ |param|type|description|
659
+ |-----|----|-----------|
660
+ |`bytes`|`Bytes`|The input bytes|
661
+ |`encoding`|`Encoding`|The encoding to use|
662
+
663
+ Returns:
664
+
665
+ |type|description|
666
+ |----|-----------|
667
+ |`String`|The decoded string|
668
+
669
+ ### String.**forEachCodePoint**
670
+
671
+ <details disabled>
672
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
673
+ No other changes yet.
674
+ </details>
675
+
676
+ ```grain
677
+ forEachCodePoint : ((Number -> Void), String) -> Void
678
+ ```
679
+
680
+ Iterates over Unicode code points in a string.
681
+
682
+ Parameters:
683
+
684
+ |param|type|description|
685
+ |-----|----|-----------|
686
+ |`fn`|`Number -> Void`|The iterator function|
687
+ |`str`|`String`|The string to iterate|
688
+
689
+ Examples:
690
+
691
+ ```grain
692
+ String.forEachCodePoint(print, "Hello world")
693
+ ```
694
+
695
+ ### String.**forEachCodePointi**
696
+
697
+ <details disabled>
698
+ <summary tabindex="-1">Added in <code>0.4.0</code></summary>
699
+ No other changes yet.
700
+ </details>
701
+
702
+ ```grain
703
+ forEachCodePointi : (((Number, Number) -> Void), String) -> Void
704
+ ```
705
+
706
+ Iterates over Unicode code points in a string. This is the same as
707
+ `forEachCodePoint`, but provides the code point's index in the string
708
+ as the second argument to the iterator function.
709
+
710
+ Parameters:
711
+
712
+ |param|type|description|
713
+ |-----|----|-----------|
714
+ |`fn`|`(Number, Number) -> Void`|The iterator function|
715
+ |`str`|`String`|The string to iterate|
716
+
717
+ Examples:
718
+
719
+ ```grain
720
+ String.forEachCodePointi((codepoint, index) => print((codepoint, index)), "Hello world")
721
+ ```
722
+
723
+ ### String.**trimStart**
724
+
725
+ <details disabled>
726
+ <summary tabindex="-1">Added in <code>0.4.2</code></summary>
727
+ No other changes yet.
728
+ </details>
729
+
730
+ ```grain
731
+ trimStart : String -> String
732
+ ```
733
+
734
+ Trims the beginning of a string—removing any leading whitespace characters.
735
+
736
+ Parameters:
737
+
738
+ |param|type|description|
739
+ |-----|----|-----------|
740
+ |`string`|`String`|The string to be trimmed|
741
+
742
+ Returns:
743
+
744
+ |type|description|
745
+ |----|-----------|
746
+ |`String`|The trimmed string|
747
+
748
+ Examples:
749
+
750
+ ```grain
751
+ String.trimStart(" Hello World") == "Hello World"
752
+ ```
753
+
754
+ ### String.**trimEnd**
755
+
756
+ <details disabled>
757
+ <summary tabindex="-1">Added in <code>0.4.2</code></summary>
758
+ No other changes yet.
759
+ </details>
760
+
761
+ ```grain
762
+ trimEnd : String -> String
763
+ ```
764
+
765
+ Trims the end of a string—removing any trailing whitespace characters.
766
+
767
+ Parameters:
768
+
769
+ |param|type|description|
770
+ |-----|----|-----------|
771
+ |`string`|`String`|The string to be trimmed|
772
+
773
+ Returns:
774
+
775
+ |type|description|
776
+ |----|-----------|
777
+ |`String`|The trimmed string|
778
+
779
+ Examples:
780
+
781
+ ```grain
782
+ String.trimEnd("Hello World ") == "Hello World"
783
+ ```
784
+
785
+ ### String.**trim**
786
+
787
+ <details disabled>
788
+ <summary tabindex="-1">Added in <code>0.4.2</code></summary>
789
+ No other changes yet.
790
+ </details>
791
+
792
+ ```grain
793
+ trim : String -> String
794
+ ```
795
+
796
+ Trims a string—removing all leading and trailing whitespace characters.
797
+
798
+ Parameters:
799
+
800
+ |param|type|description|
801
+ |-----|----|-----------|
802
+ |`string`|`String`|The string to be trimmed|
803
+
804
+ Returns:
805
+
806
+ |type|description|
807
+ |----|-----------|
808
+ |`String`|The trimmed string|
809
+
810
+ Examples:
811
+
812
+ ```grain
813
+ String.trim(" Hello World ") == "Hello World"
814
+ ```
815
+