You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: ascii.libsonnet
+33Lines changed: 33 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -96,4 +96,37 @@ local d = import 'github.com/jsonnet-libs/docsonnet/doc-util/main.libsonnet';
96
96
fraction(expectFraction),
97
97
exponent(expectExponent),
98
98
]),
99
+
100
+
'#stringToRFC1123': d.fn(
101
+
|||
102
+
`stringToRFC113` converts a strings to match RFC1123, replacing non-alphanumeric characters with dashes. It'll throw an assertion if the string is too long.
103
+
104
+
* RFC 1123. This means the string must:
105
+
* - contain at most 63 characters
106
+
* - contain only lowercase alphanumeric characters or '-'
107
+
* - start with an alphanumeric character
108
+
* - end with an alphanumeric character
109
+
|||,
110
+
[d.arg('str', d.T.string)]
111
+
),
112
+
stringToRFC1123(str):
113
+
// lowercase alphabetic characters
114
+
local lowercase = std.asciiLower(str);
115
+
// replace non-alphanumeric characters with dashes
116
+
local alphanumeric =
117
+
std.join(
118
+
'',
119
+
std.map(
120
+
function(c)
121
+
ifself.isLower(c)
122
+
|| self.isNumber(c)
123
+
then c
124
+
else'-',
125
+
std.stringChars(lowercase)
126
+
)
127
+
);
128
+
// remove leading/trailing dashes
129
+
local return = std.stripChars(alphanumeric, '-');
130
+
assertstd.length(return) <= 63 : 'String too long';
`isUpper` reports whether ASCII character `c` is a upper case letter
62
+
`isUpper` reports whether ASCII character `c` is a upper case letter
63
+
64
+
### fn stringToRFC1123
65
+
66
+
```ts
67
+
stringToRFC1123(str)
68
+
```
69
+
70
+
`stringToRFC113` converts a strings to match RFC1123, replacing non-alphanumeric characters with dashes. It'll throw an assertion if the string is too long.
71
+
72
+
* RFC 1123. This means the string must:
73
+
*- contain at most 63 characters
74
+
*- contain only lowercase alphanumeric characters or '-'
0 commit comments