-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfunctions_defs_codecs.go
More file actions
870 lines (853 loc) · 25.2 KB
/
functions_defs_codecs.go
File metadata and controls
870 lines (853 loc) · 25.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
package diecast
import (
"bytes"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"html/template"
"net/http"
"net/url"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/ghetzel/go-stockutil/httputil"
"github.com/ghetzel/go-stockutil/maputil"
"github.com/ghetzel/go-stockutil/sliceutil"
"github.com/ghetzel/go-stockutil/typeutil"
"github.com/go-shiori/go-readability"
base58 "github.com/jbenet/go-base58"
"github.com/microcosm-cc/bluemonday"
"github.com/russross/blackfriday/v2"
"golang.org/x/net/html"
)
func loadStandardFunctionsCodecs(_ FuncMap, _ *Server) funcGroup {
return funcGroup{
Name: `Encoding and Decoding`,
Description: `For encoding typed data and data structures into well-known formats like JSON, CSV, and TSV.`,
Functions: []funcDef{
{
Name: `jsonify`,
Summary: `Encode the given argument as a JSON document.`,
Arguments: []funcArg{
{
Name: `data`,
Type: `any`,
Description: `The data to encode as JSON.`,
}, {
Name: `indent`,
Type: `string`,
Optional: true,
Default: ` `,
Description: `The string to indent successive tiers in the document hierarchy with.`,
},
},
Function: func(value any, indent ...string) (string, error) {
var indentString = ` `
if len(indent) > 0 {
indentString = indent[0]
}
data, err := json.MarshalIndent(value, ``, indentString)
return string(data[:]), err
},
}, {
// fn markdown: Render the given Markdown string *value* as sanitized HTML.
Name: `markdown`,
Summary: `Parse the given string as a Markdown document and return it represented as HTML.`,
Arguments: []funcArg{
{
Name: `document`,
Type: `string`,
Description: `The full text of the Markdown to parse`,
}, {
Name: `extensions`,
Type: `string(s)`,
Description: `A list of zero of more Markdown extensions to enable when rendering the HTML.`,
Valid: []funcArg{
{
Name: `no-intra-emphasis`,
Description: ``,
}, {
Name: `tables`,
Description: ``,
}, {
Name: `fenced-code`,
Description: ``,
}, {
Name: `autolink`,
Description: ``,
}, {
Name: `strikethrough`,
Description: ``,
}, {
Name: `lax-html-blocks`,
Description: ``,
}, {
Name: `space-headings`,
Description: ``,
}, {
Name: `hard-line-break`,
Description: ``,
}, {
Name: `tab-size-eight`,
Description: ``,
}, {
Name: `footnotes`,
Description: ``,
}, {
Name: `no-empty-line-before-block`,
Description: ``,
}, {
Name: `heading-ids`,
Description: ``,
}, {
Name: `titleblock`,
Description: ``,
}, {
Name: `auto-heading-ids`,
Description: ``,
}, {
Name: `backslash-line-break`,
Description: ``,
}, {
Name: `definition-lists`,
Description: ``,
}, {
Name: `common`,
Description: ``,
},
},
Variadic: true,
},
},
Function: func(value any, extensions ...string) (template.HTML, error) {
var input = typeutil.String(value)
var output = blackfriday.Run(
[]byte(input),
blackfriday.WithExtensions(toMarkdownExt(extensions...)),
)
output = bluemonday.UGCPolicy().SanitizeBytes(output)
return template.HTML(output), nil
// if doc, err := htmldoc(string(output)); err == nil {
// if contents, err := doc.Find(`body`).Html(); err == nil {
// return template.HTML(contents), nil
// } else {
// return ``, err
// }
// } else {
// return ``, err
// }
},
}, {
Name: `csv`,
Summary: `Encode the given data as a comma-separated values document.`,
Arguments: []funcArg{
{
Name: `columns`,
Type: `array[string]`,
Description: `An array of values that represent the column names of the table being created.`,
}, {
Name: `rows`,
Type: `array[array[string]], array[object]`,
Description: `An array of values that represent the column names of the table being created.`,
},
},
Function: func(columns []any, rows []any) (string, error) {
return delimited(',', columns, rows)
},
}, {
Name: `tsv`,
Summary: `Encode the given data as a tab-separated values document.`,
Arguments: []funcArg{
{
Name: `columns`,
Type: `array[string]`,
Description: `An array of values that represent the column names of the table being created.`,
}, {
Name: `rows`,
Type: `array[array[string]], array[object]`,
Description: `An array of values that represent the column names of the table being created.`,
},
},
Function: func(columns []any, rows []any) (string, error) {
return delimited('\t', columns, rows)
},
}, {
Name: `unsafe`,
Summary: `Return an unescaped raw HTML segment for direct inclusion in the rendered template output.` +
`This function bypasses the built-in HTML escaping and sanitization security features, and you ` +
`almost certainly want to use [sanitize](#fn-sanitize) instead. This is a common antipattern that ` +
`leads to all kinds of security issues from poorly-constrained implementations, so you are forced ` +
`to acknowledge this by typing "unsafe".`,
Arguments: []funcArg{
{
Name: `document`,
Type: `string`,
Description: `The raw HTML snippet you sneakily want to sneak past the HTML sanitizer for reasons.`,
},
},
Function: func(value any) (template.HTML, error) {
switch value := value.(type) {
case *goquery.Document:
if doc, err := value.Html(); err == nil {
return template.HTML(doc), nil
} else {
return ``, err
}
default:
return template.HTML(typeutil.String(value)), nil
}
},
}, {
Name: `unsafeCSS`,
Summary: `Return an unescaped raw CSS segment for direct inclusion in the rendered template output.` +
`This function bypasses the built-in CSS escaping and sanitization security features.`,
Arguments: []funcArg{
{
Name: `document`,
Type: `string`,
Description: `The raw CSS snippet you sneakily want to sneak past the CSS sanitizer for reasons.`,
},
},
Function: func(value any) (template.CSS, error) {
switch value := value.(type) {
case *goquery.Document:
if doc, err := value.Html(); err == nil {
return template.CSS(doc), nil
} else {
return ``, err
}
default:
return template.CSS(typeutil.String(value)), nil
}
},
}, {
Name: `unsafeJS`,
Summary: `Return an unescaped raw Javascript segment for direct inclusion in the rendered template output.` +
`This function bypasses the built-in Javascript escaping and sanitization security features.`,
Arguments: []funcArg{
{
Name: `document`,
Type: `string`,
Description: `The raw Javascript snippet you sneakily want to sneak past the Javascript sanitizer for reasons.`,
},
},
Function: func(value any) (template.JS, error) {
switch value := value.(type) {
case *goquery.Document:
if doc, err := value.Html(); err == nil {
return template.JS(doc), nil
} else {
return ``, err
}
default:
return template.JS(typeutil.String(value)), nil
}
},
}, {
Name: `sanitize`,
Summary: `Takes a raw HTML string and santizes it, removing attributes and elements that can be used ` +
`to evaluate scripts, but leaving the rest. Useful for preparing user-generated HTML for display.`,
Arguments: []funcArg{
{
Name: `value`,
Type: `string, HTML document object`,
Description: `The document to sanitize.`,
},
},
Function: func(value any) (template.HTML, error) {
var document string
switch value := value.(type) {
case *goquery.Document:
if doc, err := value.Html(); err == nil {
document = doc
} else {
return ``, err
}
default:
document = typeutil.String(value)
}
return template.HTML(bluemonday.UGCPolicy().Sanitize(document)), nil
},
}, {
Name: `readabilize`,
Summary: `Takes a raw HTML string and a readable version out of it.`,
Arguments: []funcArg{
{
Name: `value`,
Type: `string, HTML document object`,
Description: `The document to sanitize.`,
},
},
Function: func(url string) (template.HTML, error) {
if article, err := readability.FromURL(url, 10*time.Second); err == nil {
var buf = bytes.NewBuffer(nil)
if err := html.Render(buf, article.Node); err == nil {
return template.HTML(buf.String()), nil
} else {
return ``, err
}
} else {
return ``, err
}
},
}, {
Name: `urlencode`,
Summary: `Encode a given string so it can be safely placed inside a URL query.`,
Arguments: []funcArg{
{
Name: `string`,
Type: `string`,
Description: `The string to encode.`,
},
},
Function: func(value string) string {
return url.QueryEscape(value)
},
}, {
Name: `urldecode`,
Summary: `Decode a URL-encoded string.`,
Arguments: []funcArg{
{
Name: `encoded`,
Type: `string`,
Description: `The string to decode.`,
},
},
Function: func(value string) (string, error) {
return url.QueryUnescape(value)
},
}, {
Name: `urlPathEncode`,
Summary: `Encode a given string so it can be safely placed inside a URL path segment.`,
Arguments: []funcArg{
{
Name: `string`,
Type: `string`,
Description: `The string to encode.`,
},
},
Function: func(value string) string {
return url.PathEscape(value)
},
}, {
Name: `urlPathDecode`,
Summary: `Decode a URL-encoded path.`,
Arguments: []funcArg{
{
Name: `encoded`,
Type: `string`,
Description: `The string to decode.`,
},
},
Function: func(value string) (string, error) {
return url.PathUnescape(value)
},
}, {
Name: `url`,
Summary: `Builds a URL with querystrings from the given base URL string and object.`,
Arguments: []funcArg{
{
Name: `baseurl`,
Type: `string`,
Description: `The URL to modify`,
}, {
Name: `querymap`,
Type: `object`,
Optional: true,
Description: `A key-value object of query string values to add to the URL.`,
},
},
Function: func(base string, queries ...map[string]any) (string, error) {
if u, err := url.Parse(base); err == nil {
for _, qs := range queries {
for k, v := range qs {
httputil.SetQ(u, k, v)
}
}
return u.String(), nil
} else {
return ``, err
}
},
}, {
Name: `urlScheme`,
Summary: `Return the scheme portion of the given URL.`,
Examples: []funcExample{
{
Code: `urlScheme "https://example.com:8443/path/to/file.xml?lang=en&active=true#s6.9"`,
Return: `https`,
},
},
Arguments: []funcArg{
{
Name: `url`,
Type: `string`,
Description: `The URL to parse`,
},
},
Function: func(in string) (string, error) {
if u, err := url.Parse(in); err == nil {
return u.Scheme, nil
} else {
return ``, err
}
},
}, {
Name: `urlHost`,
Summary: `Return the host[:port] portion of the given URL.`,
Examples: []funcExample{
{
Code: `urlHost "https://example.com:8443/path/to/file.xml?lang=en&active=true#s6.9"`,
Return: `example.com:8443`,
}, {
Code: `urlHost "https://example.com/somewhere/else/`,
Return: `example.com`,
},
},
Arguments: []funcArg{
{
Name: `url`,
Type: `string`,
Description: `The URL to parse`,
},
},
Function: func(in string) (string, error) {
if u, err := url.Parse(in); err == nil {
return u.Host, nil
} else {
return ``, err
}
},
}, {
Name: `urlHostname`,
Summary: `Return the hostname (without port number) portion of the given URL.`,
Arguments: []funcArg{
{
Name: `url`,
Type: `string`,
Description: `The URL to parse`,
},
},
Examples: []funcExample{
{
Code: `urlHostname "https://example.com:8443/path/to/file.xml?lang=en&active=true#s6.9"`,
Return: `example.com`,
}, {
Code: `urlHostname "https://other.example.com/somewhere/else/`,
Return: `other.example.com`,
},
},
Function: func(in string) (string, error) {
if u, err := url.Parse(in); err == nil {
return u.Hostname(), nil
} else {
return ``, err
}
},
}, {
Name: `urlPort`,
Summary: `Return the numeric port number of the given URL.`,
Examples: []funcExample{
{
Code: `urlPort "https://example.com:8443/path/to/file.xml?lang=en&active=true#s6.9"`,
Return: 8443,
}, {
Code: `urlPort "https://example.com/somewhere/else/`,
Return: 443,
},
},
Arguments: []funcArg{
{
Name: `url`,
Type: `string`,
Description: `The URL to parse`,
},
},
Function: func(in string) (int, error) {
if u, err := url.Parse(in); err == nil {
if p := u.Port(); p != `` {
return int(typeutil.Int(p)), nil
} else {
return 0, fmt.Errorf("invalid port number")
}
} else {
return 0, err
}
},
}, {
Name: `urlPath`,
Summary: `Return the path component of the given URL.`,
Arguments: []funcArg{
{
Name: `url`,
Type: `string`,
Description: `The URL to parse`,
},
},
Examples: []funcExample{
{
Code: `urlPath "https://example.com:8443/path/to/file.xml?lang=en&active=true#s6.9"`,
Return: `/path/to/file.xml`,
},
},
Function: func(in string) (string, error) {
if u, err := url.Parse(in); err == nil {
if p := u.Path; strings.HasPrefix(p, `/`) {
return p, nil
} else {
return `/`, nil
}
} else {
return ``, err
}
},
}, {
Name: `urlQueryString`,
Summary: `Return a querystring value from the given URL.`,
Arguments: []funcArg{
{
Name: `url`,
Type: `string`,
Description: `The URL to parse`,
}, {
Name: `key`,
Type: `string`,
Description: `The querystring value to retrieve.`,
},
},
Examples: []funcExample{
{
Code: `urlQueryString "https://example.com:8443/path/to/file.xml?lang=en&active=true#s6.9" "lang"`,
Return: `en`,
},
},
Function: func(in string, key string) (string, error) {
if u, err := url.Parse(in); err == nil {
return u.Query().Get(key), nil
} else {
return ``, err
}
},
}, {
Name: `urlQuery`,
Summary: `Return all querystring values from the given URL.`,
Arguments: []funcArg{
{
Name: `url`,
Type: `string`,
Description: `The URL to parse`,
},
},
Examples: []funcExample{
{
Code: `urlQuery "https://example.com:8443/path/to/file.xml?lang=en&active=true#s6.9"`,
Return: map[string]any{
`lang`: `en`,
`active`: true,
},
},
},
Function: func(in string) (map[string]any, error) {
if u, err := url.Parse(in); err == nil {
return maputil.M(u.Query()).MapNative(), nil
} else {
return nil, err
}
},
}, {
Name: `urlFragment`,
Summary: `Return the fragment component from the given URL.`,
Arguments: []funcArg{
{
Name: `url`,
Type: `string`,
Description: `The URL to parse`,
},
},
Examples: []funcExample{
{
Code: `urlFragment "https://example.com:8443/path/to/file.xml?lang=en&active=true#s6.9"`,
Return: `s6.9`,
},
},
Function: func(in string) (string, error) {
if u, err := url.Parse(in); err == nil {
return u.Fragment, nil
} else {
return ``, err
}
},
}, {
Name: `hex`,
Summary: `Encode the given value as a hexadecimal string.`,
Arguments: []funcArg{
{
Name: `input`,
Type: `string, bytes`,
Description: `The value to encode. If a byte array is provided, it will be encoded in ` +
`hexadecimal. If a string is provided, it will converted to a byte array first, then encoded.`,
},
},
Examples: []funcExample{
{
Code: `hex "hello"`,
Return: `68656c6c6f`,
},
},
Function: func(input any) (string, error) {
return hex.EncodeToString(toBytes(input)), nil
},
}, {
Name: `base32`,
Summary: `Encode the given bytes with the Base32 encoding scheme.`,
Arguments: []funcArg{
{
Name: `input`,
Type: `string, bytes`,
Description: `The value to encode. If a byte array is provided, it will be encoded directly. ` +
`If a string is provided, it will converted to a byte array first, then encoded.`,
},
},
Examples: []funcExample{
{
Code: `base32 "hello"`,
Return: `nbswy3dp`,
},
},
Function: func(input any) string {
return Base32Alphabet.EncodeToString(toBytes(input))
},
}, {
Name: `base58`,
Summary: `Encode the given bytes with the Base58 (Bitcoin alphabet) encoding scheme.`,
Function: func(input any) string {
return base58.Encode(toBytes(input))
},
}, {
Name: `base64`,
Summary: `Encode the given bytes with the Base64 encoding scheme. Optionally specify the encoding mode: one of "padded", "url", "url-padded", or empty (unpadded, default).`,
Arguments: []funcArg{
{
Name: `input`,
Type: `string, bytes`,
Description: `The value to encode. If a byte array is provided, it will be encoded directly. ` +
`If a string is provided, it will converted to a byte array first, then encoded.`,
}, {
Name: `encoding`,
Type: `string`,
Optional: true,
Description: `Specify an encoding option for generating the Base64 representation.`,
Valid: []funcArg{
{
Name: `standard`,
Description: `Standard Base-64 encoding scheme, no padding.`,
}, {
Name: `padded`,
Description: `Standard Base-64 encoding scheme, preserves padding.`,
}, {
Name: `url`,
Description: `Encoding that can be used in URLs and filenames, no padding.`,
}, {
Name: `url-padded`,
Description: `Encoding that can be used in URLs and filenames, preserves padding.`,
},
},
},
},
Examples: []funcExample{
{
Code: `base64 "hello?yes=this&is=dog#"`,
Return: `aGVsbG8/eWVzPXRoaXMmaXM9ZG9nIw`,
}, {
Description: `This is identical to the above example, but with the encoding explicitly specified.`,
Code: `base64 "hello?yes=this&is=dog#" "standard"`,
Return: `aGVsbG8/eWVzPXRoaXMmaXM9ZG9nIw`,
}, {
Code: `base64 "hello?yes=this&is=dog#" "padded"`,
Return: `aGVsbG8/eWVzPXRoaXMmaXM9ZG9nIw==`,
}, {
Code: `base64 "hello?yes=this&is=dog#" "url"`,
Return: `aGVsbG8_eWVzPXRoaXMmaXM9ZG9nIw`,
}, {
Code: `base64 "hello?yes=this&is=dog#" "url-padded"`,
Return: `aGVsbG8_eWVzPXRoaXMmaXM9ZG9nIw==`,
},
},
Function: func(input any, encoding ...string) string {
if len(encoding) == 0 {
encoding = []string{`standard`}
}
switch encoding[0] {
case `padded`:
return base64.StdEncoding.EncodeToString(toBytes(input))
case `url`:
return base64.RawURLEncoding.EncodeToString(toBytes(input))
case `url-padded`:
return base64.URLEncoding.EncodeToString(toBytes(input))
default:
return base64.RawStdEncoding.EncodeToString(toBytes(input))
}
},
},
{
Name: `unhex`,
Summary: `Decode the given hexadecimal string into bytes.`,
Arguments: []funcArg{
{
Name: `input`,
Type: `string`,
Description: `The value to decode into a byte array.`,
},
},
Examples: []funcExample{
{
Code: `unhex "68656c6c6f"`,
Return: []byte{'h', 'e', 'l', 'l', 'o'},
},
},
Function: func(input any) ([]byte, error) {
return hex.DecodeString(typeutil.String(input))
},
}, {
Name: `unbase32`,
Summary: `Decode the given Base32-encoded string into bytes.`,
Arguments: []funcArg{
{
Name: `input`,
Type: `string`,
Description: `The string to decode.`,
},
},
Examples: []funcExample{
{
Code: `unbase32 "nbswy3dp"`,
Return: []byte{'h', 'e', 'l', 'l', 'o'},
},
},
Function: func(input any) ([]byte, error) {
return Base32Alphabet.DecodeString(typeutil.String(input))
},
}, {
Name: `unbase58`,
Summary: `Decode the given Base58-encoded string (Bitcoin alphabet) into bytes.`,
Function: func(input any) []byte {
return base58.Decode(typeutil.String(input))
},
}, {
Name: `unbase64`,
Summary: `Decode the given Base64-encoded string into bytes.`,
Arguments: []funcArg{
{
Name: `input`,
Type: `string`,
Description: `The string to decode.`,
}, {
Name: `encoding`,
Type: `string`,
Optional: true,
Description: `Specify the encoding of the input string.`,
Valid: []funcArg{
{
Name: `standard`,
Description: `Standard Base-64 encoding scheme, no padding.`,
}, {
Name: `padded`,
Description: `Standard Base-64 encoding scheme, preserves padding.`,
}, {
Name: `url`,
Description: `Encoding that can be used in URLs and filenames, no padding.`,
}, {
Name: `url-padded`,
Description: `Encoding that can be used in URLs and filenames, preserves padding.`,
},
},
},
},
Examples: []funcExample{
{
Code: `unbase64 "aGVsbG8/eWVzPXRoaXMmaXM9ZG9nIw"`,
Return: []byte("hello?yes=this&is=dog#"),
}, {
Description: `This is identical to the above example, but with the encoding explicitly specified.`,
Code: `unbase64 "aGVsbG8/eWVzPXRoaXMmaXM9ZG9nIw" "standard"`,
Return: []byte("hello?yes=this&is=dog#"),
}, {
Description: `This shows how to convert the binary data to a Unicode string (assuming it is a Unicode bytestream)`,
Code: `chr2str (unbase64 "aGVsbG8/eWVzPXRoaXMmaXM9ZG9nIw")`,
Return: "hello?yes=this&is=dog#",
}, {
Code: `unbase64 "aGVsbG8/eWVzPXRoaXMmaXM9ZG9nIw==" "padded"`,
Return: []byte("hello?yes=this&is=dog#"),
}, {
Code: `unbase64 "aGVsbG8_eWVzPXRoaXMmaXM9ZG9nIw" "url"`,
Return: []byte("hello?yes=this&is=dog#"),
}, {
Code: `unbase64 "aGVsbG8_eWVzPXRoaXMmaXM9ZG9nIw==" "url-padded"`,
Return: []byte("hello?yes=this&is=dog#"),
},
},
Function: func(input any, encoding ...string) ([]byte, error) {
var s = typeutil.String(input)
if len(encoding) == 0 {
if strings.Contains(s, `=`) {
encoding = []string{`padded`}
} else {
encoding = []string{`standard`}
}
}
switch encoding[0] {
case `padded`:
return base64.StdEncoding.DecodeString(s)
case `url`:
return base64.RawURLEncoding.DecodeString(s)
case `url-padded`:
return base64.URLEncoding.DecodeString(s)
default:
return base64.RawStdEncoding.DecodeString(s)
}
},
}, {
Name: `httpStatusText`,
Summary: `Return a human-readable description of the given HTTP error code.`,
Function: func(code any) string {
return http.StatusText(int(typeutil.Int(code)))
},
Examples: []funcExample{
{
Code: `httpStatusText 404`,
Return: `Not Found`,
}, {
Code: `httpStatusText "404"`,
Return: `Not Found`,
}, {
Code: `httpStatusText 979`,
Return: ``,
},
},
}, {
Name: `chr2str`,
Summary: `Takes an array of integers representing Unicode codepoints and returns the resulting string.`,
Function: func(codepoints any) string {
var points = sliceutil.Sliceify(codepoints)
var chars = make([]rune, len(points))
for i, n := range points {
if codepoint := int(typeutil.Int(n)); codepoint > 0 {
chars[i] = rune(codepoint)
}
}
return string(chars)
},
Examples: []funcExample{
{
Code: `chr2str [72, 69, 76, 76, 79]`,
Return: `HELLO`,
}, {
Code: `chr2str [84, 72, 69, 82, 69]`,
Return: `THERE`,
},
},
},
},
}
}