Debian Codegraph @

doc/_stdlib_gen/stdlib-content.jsonnet

raw
1 local html = import 'html.libsonnet';
2
3 local exampleDocMultiline(mid, ex) =
4 html.spaceless([
5 html.p({}, 'Example:'),
6 html.pre({}, ex.input),
7 html.p({}, mid),
8 html.pre({}, ex.output),
9 ])
10 ;
11
12 {
13 intro: html.paragraphs([
14 |||
15 This page describes the functions available in Jsonnet's standard library, i.e. the object
16 implicitly bound to the <code>std</code> variable. Some of the standard library functions
17 can be implemented in Jsonnet. Their code can be found in the <tt>std.jsonnet</tt> file.
18 The behavior of some of the other functions, i.e. the ones that expose extra functionality
19 not otherwise available to programmers, is described formally in the <a href="/language/spec.html">specification</a>.
20 |||,
21 |||
22 The standard library is implicitly added to all Jsonnet programs by enclosing them in a
23 local construct. For example, if the program given by the user is <code>{x: "foo"}</code>,
24 then the actual code executed would be <code>local std = { ... }; {x: "foo"}</code>. The
25 functions in the standard library are all hidden fields of the <code>std</code> object.
26 |||,
27 |||
28 Note: Some of these functions marked available since v0.10.0 were actually available earlier.
29 |||,
30 ]),
31 prefix: 'std',
32 groups: [
33 {
34 name: 'External Variables',
35 id: 'ext_vars',
36 fields: [
37 {
38 name: 'extVar',
39 params: ['x'],
40 availableSince: '0.10.0',
41 description: 'If an external variable with the given name was defined, return its value. Otherwise, raise an error.',
42 },
43 ],
44 },
45 {
46 name: 'Types and Reflection',
47 id: 'types_reflection',
48 fields: [
49 {
50 name: 'thisFile',
51 availableSince: '0.10.0',
52 description: 'Note that this is a field. It contains the current Jsonnet filename as a string.',
53 },
54 {
55 name: 'type',
56 params: ['x'],
57 availableSince: '0.10.0',
58 description: html.paragraphs([
59 |||
60 Return a string that indicates the type of the value. The possible return values are:
61 "array", "boolean", "function", "null", "number", "object", and "string".
62 |||,
63 |||
64 The following functions are also available and return a boolean:
65 <code>std.isArray(v)</code>, <code>std.isBoolean(v)</code>, <code>std.isFunction(v)</code>,
66 <code>std.isNumber(v)</code>, <code>std.isObject(v)</code>, and
67 <code>std.isString(v)</code>.
68 |||,
69 ]),
70 },
71 {
72 name: 'length',
73 params: ['x'],
74 availableSince: '0.10.0',
75 description: |||
76 Depending on the type of the value given, either returns the number of elements in the
77 array, the number of codepoints in the string, the number of parameters in the function, or
78 the number of fields in the object. Raises an error if given a primitive value, i.e.
79 <code>null</code>, <code>true</code> or <code>false</code>.
80 |||,
81 },
82 {
83 name: 'prune',
84 params: ['a'],
85 availableSince: '0.10.0',
86 description: |||
87 Recursively remove all "empty" members of <code>a</code>. "Empty" is defined as zero
88 length `arrays`, zero length `objects`, or `null` values.
89 The argument <code>a</code> may have any type.
90 |||,
91 },
92 {
93 name: 'isNull',
94 params: ['x'],
95 availableSince: '0.22.0',
96 description: |||
97 Returns true if the given value is null, false otherwise.
98 |||,
99 examples: [
100 {
101 input: 'std.isNull(null)',
102 output: true,
103 },
104 {
105 input: 'std.isNull(42)',
106 output: false,
107 },
108 {
109 input: 'std.isNull("string")',
110 output: false,
111 },
112 ],
113 },
114 ],
115 },
116 {
117 name: 'Mathematical Utilities',
118 id: 'math',
119 intro: [
120 |||
121 <p>
122 The following mathematical functions are available:
123 </p>
124 <ul>
125 <ul><code>std.abs(n)</code></ul>
126 <ul><code>std.sign(n)</code></ul>
127 <ul><code>std.max(a, b)</code></ul>
128 <ul><code>std.min(a, b)</code></ul>
129 <ul><code>std.pow(x, n)</code></ul>
130 <ul><code>std.exp(x)</code></ul>
131 <ul><code>std.log(x)</code></ul>
132 <ul><code>std.log2(x)</code> (available since 0.21.0)</ul>
133 <ul><code>std.log10(x)</code> (available since 0.21.0)</ul>
134 <ul><code>std.exponent(x)</code></ul>
135 <ul><code>std.mantissa(x)</code></ul>
136 <ul><code>std.floor(x)</code></ul>
137 <ul><code>std.ceil(x)</code></ul>
138 <ul><code>std.sqrt(x)</code></ul>
139 <ul><code>std.sin(x)</code></ul>
140 <ul><code>std.cos(x)</code></ul>
141 <ul><code>std.tan(x)</code></ul>
142 <ul><code>std.asin(x)</code></ul>
143 <ul><code>std.acos(x)</code></ul>
144 <ul><code>std.atan(x)</code></ul>
145 <ul><code>std.atan2(y, x)</code> (available since 0.21.0)</ul>
146 <ul><code>std.deg2rad(x)</code> (available since 0.21.0)</ul>
147 <ul><code>std.rad2deg(x)</code> (available since 0.21.0)</ul>
148 <ul><code>std.hypot(a, b)</code> (available since 0.21.0)</ul>
149 <ul><code>std.round(x)</code> (available since 0.20.0)</ul>
150 <ul><code>std.isEven(x)</code> (available since 0.21.0)</ul>
151 <ul><code>std.isOdd(x)</code> (available since 0.21.0)</ul>
152 <ul><code>std.isInteger(x)</code> (available since 0.21.0)</ul>
153 <ul><code>std.isDecimal(x)</code> (available since 0.21.0)</ul>
154 </ul>
155 <p>
156 The constant <code>std.pi</code> is also available (since 0.21.0).
157 </p>
158 <p>
159 The function <code>std.mod(a, b)</code> is what the % operator is desugared to. It performs
160 modulo arithmetic if the left hand side is a number, or if the left hand side is a string,
161 it does Python-style string formatting with <code>std.format()</code>.
162 </p>
163 <p>
164 The functions <code>std.isEven(x)</code> and <code>std.isOdd(x)</code> use integral part of a
165 floating number to test for even or odd.
166 </p>
167 |||,
168 ],
169 fields: [
170 {
171 name: 'clamp',
172 params: ['x', 'minVal', 'maxVal'],
173 availableSince: '0.15.0',
174 description: |||
175 Clamp a value to fit within the range [<code>minVal</code>, <code>maxVal</code>].
176 Equivalent to <code>std.max(minVal, std.min(x, maxVal))</code>.
177 |||,
178 examples: [
179 {
180 input: 'std.clamp(-3, 0, 5)',
181 output: std.clamp(-3, 0, 5),
182 },
183 {
184 input: 'std.clamp(4, 0, 5)',
185 output: std.clamp(4, 0, 5),
186 },
187 {
188 input: 'std.clamp(7, 0, 5)',
189 output: std.clamp(7, 0, 5),
190 },
191 ],
192 },
193
194 ],
195 },
196 {
197 name: 'Assertions and Debugging',
198 id: 'assertions_debugging',
199 fields: [
200 {
201 name: 'assertEqual',
202 params: ['a', 'b'],
203 availableSince: '0.10.0',
204 description: 'Ensure that <code>a == b</code>. Returns <code>true</code> or throws an error message.',
205 },
206 ],
207 },
208 {
209 name: 'String Manipulation',
210 id: 'string',
211 fields: [
212 {
213 name: 'toString',
214 params: ['a'],
215 availableSince: '0.10.0',
216 description: |||
217 Convert the given argument to a string.
218 |||,
219 },
220 {
221 name: 'codepoint',
222 params: ['str'],
223 availableSince: '0.10.0',
224 description: |||
225 Returns the positive integer representing the unicode codepoint of the character in the
226 given single-character string. This function is the inverse of <code>std.char(n)</code>.
227 |||,
228 },
229 {
230 name: 'char',
231 params: ['n'],
232 availableSince: '0.10.0',
233 description: |||
234 Returns a string of length one whose only unicode codepoint has integer id <code>n</code>.
235 This function is the inverse of <code>std.codepoint(str)</code>.
236 |||,
237 },
238 {
239 name: 'substr',
240 params: ['str', 'from', 'len'],
241 availableSince: '0.10.0',
242 description: html.paragraphs([
243 |||
244 Returns a string that is the part of <code>str</code> that starts at offset <code>from</code>
245 and is <code>len</code> codepoints long. If the string <code>str</code> is shorter than
246 <code>from+len</code>, the suffix starting at position <code>from</code> will be returned.
247 |||,
248 |||
249 The slice operator (e.g., <code>str[from:to]</code>) can also be used on strings, as an
250 alternative to this function. However, note that the slice operator takes a start and an
251 end index, but <code>std.substr</code> takes a start index and a length.
252 |||,
253 ]),
254 },
255 {
256 name: 'findSubstr',
257 params: ['pat', 'str'],
258 availableSince: '0.10.0',
259 description: |||
260 Returns an array that contains the indexes of all occurrences of <code>pat</code> in
261 <code>str</code>.
262 |||,
263 },
264 {
265 name: 'startsWith',
266 params: ['a', 'b'],
267 availableSince: '0.10.0',
268 description: |||
269 Returns whether the string a is prefixed by the string b.
270 |||,
271 },
272 {
273 name: 'endsWith',
274 params: ['a', 'b'],
275 availableSince: '0.10.0',
276 description: |||
277 Returns whether the string a is suffixed by the string b.
278 |||,
279 },
280 {
281 name: 'stripChars',
282 params: ['str', 'chars'],
283 availableSince: '0.15.0',
284 description: |||
285 Removes characters <code>chars</code> from the beginning and from the end of <code>str</code>.
286 |||,
287 examples: [
288 {
289 input: 'std.stripChars(" test test test ", " ")',
290 output: std.stripChars(' test test test ', ' '),
291 },
292 {
293 input: 'std.stripChars("aaabbbbcccc", "ac")',
294 output: std.stripChars('aaabbbbcccc', 'ac'),
295 },
296 {
297 input: 'std.stripChars("cacabbbbaacc", "ac")',
298 output: std.stripChars('cacabbbbaacc', 'ac'),
299 },
300 ],
301 },
302 {
303 name: 'lstripChars',
304 params: ['str', 'chars'],
305 availableSince: '0.15.0',
306 description: |||
307 Removes characters <code>chars</code> from the beginning of <code>str</code>.
308 |||,
309 examples: [
310 {
311 input: 'std.lstripChars(" test test test ", " ")',
312 output: std.lstripChars(' test test test ', ' '),
313 },
314 {
315 input: 'std.lstripChars("aaabbbbcccc", "ac")',
316 output: std.lstripChars('aaabbbbcccc', 'ac'),
317 },
318 {
319 input: 'std.lstripChars("cacabbbbaacc", "ac")',
320 output: std.lstripChars('cacabbbbaacc', 'ac'),
321 },
322 ],
323 },
324 {
325 name: 'rstripChars',
326 params: ['str', 'chars'],
327 availableSince: '0.15.0',
328 description: |||
329 Removes characters <code>chars</code> from the end of <code>str</code>.
330 |||,
331 examples: [
332 {
333 input: 'std.rstripChars(" test test test ", " ")',
334 output: std.rstripChars(' test test test ', ' '),
335 },
336 {
337 input: 'std.rstripChars("aaabbbbcccc", "ac")',
338 output: std.rstripChars('aaabbbbcccc', 'ac'),
339 },
340 {
341 input: 'std.rstripChars("cacabbbbaacc", "ac")',
342 output: std.rstripChars('cacabbbbaacc', 'ac'),
343 },
344 ],
345 },
346 {
347 name: 'split',
348 params: ['str', 'c'],
349 availableSince: '0.10.0',
350 description: [
351 html.p({}, |||
352 Split the string <code>str</code> into an array of strings, divided by the string
353 <code>c</code>.
354 |||),
355 html.p({}, |||
356 Note: Versions up to and including 0.18.0 require <code>c</code> to be a single character.
357 |||),
358 ],
359 examples: [
360 {
361 input: @'std.split("foo/_bar", "/_")',
362 output: std.split('foo/_bar', '/_'),
363 },
364 {
365 input: @'std.split("/_foo/_bar", "/_")',
366 output: std.split('/_foo/_bar', '/_'),
367 },
368 ],
369 },
370 {
371 name: 'splitLimit',
372 params: ['str', 'c', 'maxsplits'],
373 availableSince: '0.10.0',
374 description: [
375 html.p({}, |||
376 As <code>std.split(str, c)</code> but will stop after <code>maxsplits</code> splits, thereby the largest
377 array it will return has length <code>maxsplits + 1</code>. A limit of <code>-1</code> means unlimited.
378 |||),
379 html.p({}, |||
380 Note: Versions up to and including 0.18.0 require <code>c</code> to be a single character.
381 |||),
382 ],
383 examples: [
384 {
385 input: @'std.splitLimit("foo/_bar", "/_", 1)',
386 output: std.splitLimit('foo/_bar', '/_', 1),
387 },
388 {
389 input: @'std.splitLimit("/_foo/_bar", "/_", 1)',
390 output: std.splitLimit('/_foo/_bar', '/_', 1),
391 },
392 ],
393 },
394 {
395 name: 'splitLimitR',
396 params: ['str', 'c', 'maxsplits'],
397 availableSince: '0.19.0',
398 description: 'As <code>std.splitLimit(str, c, maxsplits)</code> but will split from right to left.',
399 examples: [
400 {
401 input: @'std.splitLimitR("/_foo/_bar", "/_", 1)',
402 output: std.splitLimitR('/_foo/_bar', '/_', 1),
403 },
404 ],
405 },
406 {
407 name: 'strReplace',
408 params: ['str', 'from', 'to'],
409 availableSince: '0.10.0',
410 description: |||
411 Returns a copy of the string in which all occurrences of string <code>from</code> have been
412 replaced with string <code>to</code>.
413 |||,
414 examples: [
415 {
416 input: @"std.strReplace('I like to skate with my skateboard', 'skate', 'surf')",
417 output: std.strReplace('I like to skate with my skateboard', 'skate', 'surf'),
418 },
419 ],
420 },
421 {
422 name: 'isEmpty',
423 params: ['str'],
424 availableSince: '0.20.0',
425 description: |||
426 Returns true if the given string is of zero length.
427 |||,
428 },
429 {
430 name: 'trim',
431 params: ['str'],
432 availableSince: '0.21.0',
433 description: |||
434 Returns a copy of string after eliminating leading and trailing whitespaces.
435 |||,
436 },
437 {
438 name: 'equalsIgnoreCase',
439 params: ['str1', 'str2'],
440 availableSince: '0.21.0',
441 description: |||
442 Returns true if the the given <code>str1</code> is equal to <code>str2</code> by doing case insensitive comparison, false otherwise.
443 |||,
444 },
445 {
446 name: 'asciiUpper',
447 params: ['str'],
448 availableSince: '0.10.0',
449 description: |||
450 Returns a copy of the string in which all ASCII letters are capitalized.
451 |||,
452 examples: [
453 {
454 input: "std.asciiUpper('100 Cats!')",
455 output: std.asciiUpper('100 Cats!'),
456 },
457 ],
458 },
459 {
460 name: 'asciiLower',
461 params: ['str'],
462 availableSince: '0.10.0',
463 description: |||
464 Returns a copy of the string in which all ASCII letters are lower cased.
465 |||,
466 examples: [
467 {
468 input: "std.asciiLower('100 Cats!')",
469 output: std.asciiLower('100 Cats!'),
470 },
471 ],
472 },
473 {
474 name: 'stringChars',
475 params: ['str'],
476 availableSince: '0.10.0',
477 description: |||
478 Split the string <code>str</code> into an array of strings, each containing a single
479 codepoint.
480 |||,
481 examples: [
482 {
483 input: 'std.stringChars("foo")',
484 output: std.stringChars('foo'),
485 },
486 ],
487 },
488 {
489 name: 'format',
490 params: ['str', 'vals'],
491 availableSince: '0.10.0',
492 description: |||
493 Format the string <code>str</code> using the values in <code>vals</code>. The values can be
494 an array, an object, or in other cases are treated as if they were provided in a singleton
495 array. The string formatting follows the <a
496 href="https://docs.python.org/2/library/stdtypes.html#string-formatting">same rules</a> as
497 Python. The <code>%</code> operator can be used as a shorthand for this function.
498 |||,
499 examples: [
500 {
501 input: 'std.format("Hello %03d", 12)',
502 output: std.format('Hello %03d', 12),
503 },
504 {
505 input: '"Hello %03d" % 12',
506 output: 'Hello %03d' % 12,
507 },
508 {
509 input: '"Hello %s, age %d" % ["Foo", 25]',
510 output: 'Hello %s, age %d' % ['Foo', 25],
511 },
512 {
513 input: '"Hello %(name)s, age %(age)d" % {age: 25, name: "Foo"}',
514 output: 'Hello %(name)s, age %(age)d' % { age: 25, name: 'Foo' },
515 },
516 ],
517 },
518 {
519 name: 'escapeStringBash',
520 params: ['str'],
521 availableSince: '0.10.0',
522 description: |||
523 Wrap <code>str</code> in single quotes, and escape any single quotes within <code>str</code>
524 by changing them to a sequence <tt>'"'"'</tt>. This allows injection of arbitrary strings
525 as arguments of commands in bash scripts.
526 |||,
527 },
528 {
529 name: 'escapeStringDollars',
530 params: ['str'],
531 availableSince: '0.10.0',
532 description: |||
533 Convert $ to $$ in <code>str</code>. This allows injection of arbitrary strings into
534 systems that use $ for string interpolation (like Terraform).
535 |||,
536 },
537 {
538 name: 'escapeStringJson',
539 params: ['str'],
540 availableSince: '0.10.0',
541 description: |||
542 Convert <code>str</code> to allow it to be embedded in a JSON representation, within a
543 string. This adds quotes, escapes backslashes, and escapes unprintable characters.
544 |||,
545 examples: [
546 {
547 input: |||
548 local description = "Multiline\nc:\\path";
549 "{name: %s}" % std.escapeStringJson(description)
550 |||,
551 output: (
552 local description = 'Multiline\nc:\\path';
553 '{name: %s}' % std.escapeStringJson(description)
554 ),
555 },
556 ],
557 },
558 {
559 name: 'escapeStringPython',
560 params: ['str'],
561 availableSince: '0.10.0',
562 description: |||
563 Convert <code>str</code> to allow it to be embedded in Python. This is an alias for
564 <code>std.escapeStringJson</code>.
565 |||,
566 },
567 {
568 name: 'escapeStringXml',
569 params: ['str'],
570 availableSince: '0.10.0',
571 description: |||
572 Convert <code>str</code> to allow it to be embedded in XML (or HTML). The following replacements are made:
573 <pre>
574 {
575 "&lt;": "&amp;lt;",
576 "&gt;": "&amp;gt;",
577 "&amp;": "&amp;amp;",
578 "\&quot;": "&amp;quot;",
579 "&apos;": "&amp;apos;",
580 }
581 </pre>
582 |||,
583 },
584 ],
585 },
586 {
587 name: 'Parsing',
588 id: 'parsing',
589 fields: [
590 {
591 name: 'parseInt',
592 params: ['str'],
593 availableSince: '0.10.0',
594 description: |||
595 Parses a signed decimal integer from the input string.
596 |||,
597 examples: [
598 {
599 input: 'std.parseInt("123")',
600 output: std.parseInt('123'),
601 },
602 {
603 input: 'std.parseInt("-123")',
604 output: std.parseInt('-123'),
605 },
606 ],
607 },
608 {
609 name: 'parseOctal',
610 params: ['str'],
611 availableSince: '0.10.0',
612 description: |||
613 Parses an unsigned octal integer from the input string. Initial zeroes are tolerated.
614 |||,
615 examples: [
616 {
617 input: 'std.parseOctal("755")',
618 output: std.parseOctal('755'),
619 },
620 ],
621 },
622 {
623 name: 'parseHex',
624 params: ['str'],
625 availableSince: '0.10.0',
626 description: |||
627 Parses an unsigned hexadecimal integer, from the input string. Case insensitive.
628 |||,
629 examples: [
630 {
631 input: 'std.parseHex("ff")',
632 output: std.parseHex('ff'),
633 },
634 ],
635 },
636 {
637 name: 'parseJson',
638 params: ['str'],
639 availableSince: '0.13.0',
640 description: |||
641 Parses a JSON string.
642 |||,
643 examples: [
644 {
645 input: 'std.parseJson(\'{"foo": "bar"}\')',
646 output: std.parseJson('{"foo": "bar"}'),
647 },
648 ],
649 },
650 {
651 name: 'parseYaml',
652 params: ['str'],
653 availableSince: '0.18.0',
654 description: |||
655 Parses a YAML string. This is provided as a "best-effort" mechanism and should not be relied on to provide
656 a fully standards compliant YAML parser. YAML is a superset of JSON, consequently "downcasting" or
657 manifestation of YAML into JSON or Jsonnet values will only succeed when using the subset of YAML that is
658 compatible with JSON. The parser does not support YAML documents with scalar values at the root. The
659 root node of a YAML document must start with either a YAML sequence or map to be successfully parsed.
660 |||,
661 examples: [
662 {
663 input: "std.parseYaml('foo: bar')",
664 output: std.parseYaml('foo: bar'),
665 },
666 ],
667 },
668 {
669 name: 'encodeUTF8',
670 params: ['str'],
671 availableSince: '0.13.0',
672 description: |||
673 Encode a string using <a href="https://en.wikipedia.org/wiki/UTF-8">UTF8</a>. Returns an array of numbers
674 representing bytes.
675 |||,
676 },
677 {
678 name: 'decodeUTF8',
679 params: ['arr'],
680 availableSince: '0.13.0',
681 description: |||
682 Decode an array of numbers representing bytes using <a href="https://en.wikipedia.org/wiki/UTF-8">UTF8</a>.
683 Returns a string.
684 |||,
685 },
686 ],
687 },
688 {
689 name: 'Manifestation',
690 id: 'manifestation',
691 // TODO(sbarzowski): Clean up the example's representation
692 fields: [
693 {
694 name: 'manifestIni',
695 params: ['ini'],
696 availableSince: '0.10.0',
697 description: [
698 html.p({}, |||
699 Convert the given structure to a string in <a href="https://en.wikipedia.org/wiki/INI_file">INI format</a>. This
700 allows using Jsonnet's
701 object model to build a configuration to be consumed by an application expecting an INI
702 file. The data is in the form of a set of sections, each containing a key/value mapping.
703 These examples should make it clear:
704 |||),
705 html.pre({}, |||
706 {
707 main: { a: "1", b: "2" },
708 sections: {
709 s1: {x: "11", y: "22", z: "33"},
710 s2: {p: "yes", q: ""},
711 empty: {},
712 }
713 }
714 |||),
715
716 html.p({}, |||
717 Yields a string containing this INI file:
718 |||),
719
720 html.pre({}, |||
721 a = 1
722 b = 2
723 [empty]
724 [s1]
725 x = 11
726 y = 22
727 z = 33
728 [s2]
729 p = yes
730 q =
731 |||),
732 ],
733 },
734 {
735 name: 'manifestPython',
736 params: ['v'],
737 availableSince: '0.10.0',
738 description: [
739 html.p({}, |||
740 Convert the given value to a JSON-like form that is compatible with Python. The chief
741 differences are True / False / None instead of true / false / null.
742 |||),
743
744 html.pre({}, |||
745 {
746 b: ["foo", "bar"],
747 c: true,
748 d: null,
749 e: { f1: false, f2: 42 },
750 }
751 |||),
752
753 html.p({}, |||
754 Yields a string containing Python code like:
755 |||),
756
757 html.pre({}, |||
758 {
759 "b": ["foo", "bar"],
760 "c": True,
761 "d": None,
762 "e": {"f1": False, "f2": 42}
763 }
764 |||),
765 ],
766 },
767 {
768 name: 'manifestPythonVars',
769 params: ['conf'],
770 availableSince: '0.10.0',
771 description: [
772 html.p({}, |||
773 Convert the given object to a JSON-like form that is compatible with Python. The key
774 difference to <code>std.manifestPython</code> is that the top level is represented as a list
775 of Python global variables.
776 |||),
777
778 html.pre({}, |||
779 {
780 b: ["foo", "bar"],
781 c: true,
782 d: null,
783 e: { f1: false, f2: 42 },
784 }
785 |||),
786
787 html.p({}, |||
788 Yields a string containing this Python code:
789 |||),
790
791 html.pre({}, |||
792 b = ["foo", "bar"]
793 c = True
794 d = None
795 e = {"f1": False, "f2": 42}
796 |||),
797 ],
798 },
799 {
800 name: 'manifestJsonEx',
801 params: ['value', 'indent', 'newline', 'key_val_sep'],
802 availableSince: '0.10.0',
803 description: [
804 html.p({}, |||
805 Convert the given object to a JSON form. <code>indent</code> is a string containing
806 one or more whitespaces that are used for indentation. <code>newline</code> is
807 by default <code>\n</code> and is inserted where a newline would normally be used
808 to break long lines. <code>key_val_sep</code> is used to separate the key and value
809 of an object field:
810 |||),
811 ],
812 examples: [
813 exampleDocMultiline('Yields a string containing this JSON:', {
814 input: |||
815 std.manifestJsonEx(
816 {
817 x: [1, 2, 3, true, false, null,
818 "string\nstring"],
819 y: { a: 1, b: 2, c: [1, 2] },
820 }, " ")
821 |||,
822 output:
823 std.manifestJsonEx(
824 {
825 x: [
826 1,
827 2,
828 3,
829 true,
830 false,
831 null,
832 'string\nstring',
833 ],
834 y: { a: 1, b: 2, c: [1, 2] },
835 }, ' '
836 ),
837 }),
838 exampleDocMultiline('Yields a string containing this JSON:', {
839 input: |||
840 std.manifestJsonEx(
841 {
842 x: [1, 2, "string\nstring"],
843 y: { a: 1, b: [1, 2] },
844 }, "", " ", " : ")
845 |||,
846 output:
847 std.manifestJsonEx(
848 {
849 x: [1, 2, 'string\nstring'],
850 y: { a: 1, b: [1, 2] },
851 }, '', ' ', ' : '
852 ),
853 }),
854 ],
855 },
856 {
857 name: 'manifestJson',
858 params: ['value'],
859 availableSince: '0.10.0',
860 description: |||
861 Convert the given object to a JSON form. Under the covers,
862 it calls <code>std.manifestJsonEx</code> with a 4-space indent:
863 |||,
864 examples: [
865 exampleDocMultiline('Yields a string containing this JSON:', {
866 input: |||
867 std.manifestJson(
868 {
869 x: [1, 2, 3, true, false, null,
870 "string\nstring"],
871 y: { a: 1, b: 2, c: [1, 2] },
872 })
873 |||,
874 output:
875 std.manifestJson(
876 {
877 x: [
878 1,
879 2,
880 3,
881 true,
882 false,
883 null,
884 'string\nstring',
885 ],
886 y: { a: 1, b: 2, c: [1, 2] },
887 }
888 ),
889 }),
890 ],
891 },
892 {
893 name: 'manifestJsonMinified',
894 params: ['value'],
895 availableSince: '0.18.0',
896 description: |||
897 Convert the given object to a minified JSON form. Under the covers,
898 it calls <code>std.manifestJsonEx</code>:
899 |||,
900 examples: [
901 exampleDocMultiline('Yields a string containing this JSON:', {
902 input: |||
903 std.manifestJsonMinified(
904 {
905 x: [1, 2, 3, true, false, null,
906 "string\nstring"],
907 y: { a: 1, b: 2, c: [1, 2] },
908 })
909 |||,
910 output:
911 std.manifestJsonMinified(
912 {
913 x: [
914 1,
915 2,
916 3,
917 true,
918 false,
919 null,
920 'string\nstring',
921 ],
922 y: { a: 1, b: 2, c: [1, 2] },
923 }
924 ),
925 }),
926 ],
927 },
928 {
929 name: 'manifestYamlDoc',
930 params: ['value', 'indent_array_in_object=false', 'quote_keys=true'],
931 availableSince: '0.10.0',
932 description: [
933 html.p({}, |||
934 Convert the given value to a YAML form. Note that <code>std.manifestJson</code> could also
935 be used for this purpose, because any JSON is also valid YAML. But this function will
936 produce more canonical-looking YAML.
937 |||),
938 html.pre({}, |||
939 std.manifestYamlDoc(
940 {
941 x: [1, 2, 3, true, false, null,
942 "string\nstring\n"],
943 y: { a: 1, b: 2, c: [1, 2] },
944 },
945 indent_array_in_object=false)
946 |||),
947 html.p({}, |||
948 Yields a string containing this YAML:
949 |||),
950 html.pre({}, |||
951 "x":
952 - 1
953 - 2
954 - 3
955 - true
956 - false
957 - null
958 - |
959 string
960 string
961 "y":
962 "a": 1
963 "b": 2
964 "c":
965 - 1
966 - 2
967 |||),
968 html.p({}, |||
969 The <code>indent_array_in_object</code> param adds additional indentation which some people
970 may find easier to read.
971 |||),
972 html.p({}, |||
973 The <code>quote_keys</code> parameter controls whether YAML identifiers are always quoted
974 or only when necessary.
975 |||),
976 ],
977 },
978 {
979 name: 'manifestYamlStream',
980 params: ['value', 'indent_array_in_object=false', 'c_document_end=false', 'quote_keys=true'],
981 availableSince: '0.10.0',
982 description: [
983 html.p({}, |||
984 Given an array of values, emit a YAML "stream", which is a sequence of documents separated
985 by <code>---</code> and ending with <code>...</code>.
986 |||),
987
988 html.pre({}, |||
989 std.manifestYamlStream(
990 ['a', 1, []],
991 indent_array_in_object=false,
992 c_document_end=true)
993 |||),
994
995 html.p({}, |||
996 Yields this string:
997 |||),
998
999 html.pre({}, |||
1000 ---
1001 "a"
1002 ---
1003 1
1004 ---
1005 []
1006 ...
1007 |||),
1008
1009 html.p({}, |||
1010 The <code>indent_array_in_object</code> and <code>quote_keys</code> params are the
1011 same as in <code>manifestYamlDoc</code>.
1012 |||),
1013 html.p({}, |||
1014 The <code>c_document_end</code> param adds the optional terminating <code>...</code>.
1015 |||),
1016 ],
1017 },
1018 {
1019 name: 'manifestXmlJsonml',
1020 params: ['value'],
1021 availableSince: '0.10.0',
1022 description: [
1023 html.p({}, |||
1024 Convert the given <a href="http://www.jsonml.org/">JsonML</a>-encoded value to a string
1025 containing the XML.
1026 |||),
1027
1028 html.pre({}, |||
1029 std.manifestXmlJsonml([
1030 'svg', { height: 100, width: 100 },
1031 [
1032 'circle', {
1033 cx: 50, cy: 50, r: 40,
1034 stroke: 'black', 'stroke-width': 3,
1035 fill: 'red',
1036 }
1037 ],
1038 ])
1039 |||),
1040
1041 html.p({}, |||
1042 Yields a string containing this XML (all on one line):
1043 |||),
1044
1045 html.pre({}, html.escape(|||
1046 <svg height="100" width="100">
1047 <circle cx="50" cy="50" fill="red" r="40"
1048 stroke="black" stroke-width="3"></circle>;
1049 </svg>;
1050 |||)),
1051
1052 html.p({}, |||
1053 Which represents the following image:
1054 |||),
1055
1056 |||
1057 <svg height="100" width="100">
1058 <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
1059 Sorry, your browser does not support inline SVG.
1060 </svg>
1061 |||,
1062
1063 html.p({}, |||
1064 JsonML is designed to preserve "mixed-mode content" (i.e., textual data outside of or next
1065 to elements). This includes the whitespace needed to avoid having all the XML on one line,
1066 which is meaningful in XML. In order to have whitespace in the XML output, it must be
1067 present in the JsonML input:
1068 |||),
1069
1070 html.pre({}, |||
1071 std.manifestXmlJsonml([
1072 'svg',
1073 { height: 100, width: 100 },
1074 '\n ',
1075 [
1076 'circle',
1077 {
1078 cx: 50, cy: 50, r: 40, stroke: 'black',
1079 'stroke-width': 3, fill: 'red',
1080 }
1081 ],
1082 '\n',
1083 ])
1084 |||),
1085 ],
1086 },
1087 {
1088 name: 'manifestTomlEx',
1089 params: ['value', 'indent'],
1090 availableSince: '0.18.0',
1091 description: |||
1092 Convert the given object to a TOML form. <code>indent</code> is a string containing
1093 one or more whitespaces that are used for indentation:
1094 |||,
1095 examples: [
1096 exampleDocMultiline('Yields a string containing this TOML file:', {
1097 input: |||
1098 std.manifestTomlEx({
1099 key1: "value",
1100 key2: 1,
1101 section: {
1102 a: 1,
1103 b: "str",
1104 c: false,
1105 d: [1, "s", [2, 3]],
1106 subsection: {
1107 k: "v",
1108 },
1109 },
1110 sectionArray: [
1111 { k: "v1", v: 123 },
1112 { k: "v2", c: "value2" },
1113 ],
1114 }, " ")
1115 |||,
1116 output: (
1117 std.manifestTomlEx({
1118 key1: 'value',
1119 key2: 1,
1120 section: {
1121 a: 1,
1122 b: 'str',
1123 c: false,
1124 d: [1, 's', [2, 3]],
1125 subsection: {
1126 k: 'v',
1127 },
1128 },
1129 sectionArray: [
1130 { k: 'v1', v: 123 },
1131 { k: 'v2', c: 'value2' },
1132 ],
1133 }, ' ')
1134 ),
1135 }),
1136 ],
1137 },
1138 ],
1139 },
1140 {
1141 name: 'Arrays',
1142 id: 'arrays',
1143 fields: [
1144 {
1145 name: 'makeArray',
1146 params: ['sz', 'func'],
1147 availableSince: '0.10.0',
1148 description: |||
1149 Create a new array of <code>sz</code> elements by calling <code>func(i)</code> to initialize
1150 each element. Func is expected to be a function that takes a single parameter, the index of
1151 the element it should initialize.
1152 |||,
1153 examples: [
1154 {
1155 input: 'std.makeArray(3,function(x) x * x)',
1156 output: std.makeArray(3, function(x) x * x),
1157 },
1158 ],
1159 },
1160 {
1161 name: 'member',
1162 params: ['arr', 'x'],
1163 availableSince: '0.15.0',
1164 description: |||
1165 Returns whether <code>x</code> occurs in <code>arr</code>.
1166 Argument <code>arr</code> may be an array or a string.
1167 |||,
1168 },
1169 {
1170 name: 'count',
1171 params: ['arr', 'x'],
1172 availableSince: '0.10.0',
1173 description: |||
1174 Return the number of times that <code>x</code> occurs in <code>arr</code>.
1175 |||,
1176 },
1177 {
1178 name: 'find',
1179 params: ['value', 'arr'],
1180 availableSince: '0.10.0',
1181 description: |||
1182 Returns an array that contains the indexes of all occurrences of <code>value</code> in
1183 <code>arr</code>.
1184 |||,
1185 },
1186 {
1187 name: 'map',
1188 params: ['func', 'arr'],
1189 availableSince: '0.10.0',
1190 description: |||
1191 Apply the given function to every element of the array to form a new array.
1192 |||,
1193 },
1194 {
1195 name: 'mapWithIndex',
1196 params: ['func', 'arr'],
1197 availableSince: '0.10.0',
1198 description: |||
1199 Similar to <a href="#map">map</a> above, but it also passes to the function the element's
1200 index in the array. The function <code>func</code> is expected to take the index as the
1201 first parameter and the element as the second.
1202 |||,
1203 },
1204 {
1205 name: 'filterMap',
1206 params: ['filter_func', 'map_func', 'arr'],
1207 availableSince: '0.10.0',
1208 description: |||
1209 It first filters, then maps the given array, using the two functions provided.
1210 |||,
1211 },
1212 {
1213 name: 'flatMap',
1214 params: ['func', 'arr'],
1215 availableSince: '0.10.0',
1216 description: html.paragraphs([
1217 |||
1218 Apply the given function to every element of <code>arr</code> to form a new array then flatten the result.
1219 The argument <code>arr</code> must be an array or a string. If <code>arr</code> is an array, function <code>func</code> must return an array.
1220 If <code>arr</code> is a string, function <code>func</code> must return an string.
1221 |||,
1222 |||
1223 The <code>std.flatMap</code> function can be thought of as a generalized <code>std.map</code>,
1224 with each element mapped to 0, 1 or more elements.
1225 |||,
1226 ]),
1227 examples: [
1228 {
1229 input: 'std.flatMap(function(x) [x, x], [1, 2, 3])',
1230 output: std.flatMap(function(x) [x, x], [1, 2, 3]),
1231 },
1232 {
1233 input: 'std.flatMap(function(x) if x == 2 then [] else [x], [1, 2, 3])',
1234 output: std.flatMap(function(x) if x == 2 then [] else [x], [1, 2, 3]),
1235 },
1236 {
1237 input: 'std.flatMap(function(x) if x == 2 then [] else [x * 3, x * 2], [1, 2, 3])',
1238 output: std.flatMap(function(x) if x == 2 then [] else [x * 3, x * 2], [1, 2, 3]),
1239 },
1240 {
1241 input: 'std.flatMap(function(x) x+x, "foo")',
1242 output: std.flatMap(function(x) x + x, 'foo'),
1243 },
1244 ],
1245 },
1246 {
1247 name: 'filter',
1248 params: ['func', 'arr'],
1249 availableSince: '0.10.0',
1250 description: |||
1251 Return a new array containing all the elements of <code>arr</code> for which the
1252 <code>func</code> function returns true.
1253 |||,
1254 },
1255 {
1256 name: 'foldl',
1257 params: ['func', 'arr', 'init'],
1258 availableSince: '0.10.0',
1259 description: html.paragraphs([
1260 |||
1261 Classic foldl function. Calls the function for each array element, passing the result from
1262 the previous call (or <code>init</code> for the first call), and the array element. Traverses
1263 the array from left to right.
1264 |||,
1265 |||
1266 For example: <code>foldl(f, [1,2,3], 0)</code> is equivalent to <code>f(f(f(0, 1), 2), 3)</code>.
1267 |||,
1268 ]),
1269 },
1270 {
1271 name: 'foldr',
1272 params: ['func', 'arr', 'init'],
1273 availableSince: '0.10.0',
1274 description: html.paragraphs([
1275 |||
1276 Classic foldr function. Calls the function for each array element, passing the array element
1277 and the result from the previous call (or <code>init</code> for the first call). Traverses
1278 the array from right to left.
1279 |||,
1280 |||
1281 For example: <code>foldr(f, [1,2,3], 0)</code> is equivalent to <code>f(1, f(2, f(3, 0)))</code>.
1282 |||,
1283 ]),
1284 },
1285 {
1286 name: 'range',
1287 params: ['from', 'to'],
1288 availableSince: '0.10.0',
1289 description: |||
1290 Return an array of ascending numbers between the two limits, inclusively.
1291 |||,
1292 },
1293 {
1294 name: 'repeat',
1295 params: ['what', 'count'],
1296 availableSince: '0.15.0',
1297 description: |||
1298 Repeats an array or a string <code>what</code> a number of times specified by an integer <code>count</code>.
1299 |||,
1300 examples: [
1301 {
1302 input: 'std.repeat([1, 2, 3], 3)',
1303 output: std.repeat([1, 2, 3], 3),
1304 },
1305 {
1306 input: 'std.repeat("blah", 2)',
1307 output: std.repeat('blah', 2),
1308 },
1309 ],
1310 },
1311 {
1312 name: 'slice',
1313 params: ['indexable', 'index', 'end', 'step'],
1314 availableSince: '0.10.0',
1315 description: html.paragraphs([
1316 |||
1317 Selects the elements of an array or a string from <code>index</code> to <code>end</code> with <code>step</code> and returns an array or a string respectively.
1318 |||,
1319 |||
1320 Note that it's recommended to use dedicated slicing syntax both for arrays and strings (e.g. <code>arr[0:4:1]</code> instead of <code>std.slice(arr, 0, 4, 1)</code>).
1321 |||,
1322 ]),
1323 examples: [
1324 {
1325 input: 'std.slice([1, 2, 3, 4, 5, 6], 0, 4, 1)',
1326 output: std.slice([1, 2, 3, 4, 5, 6], 0, 4, 1),
1327 },
1328 {
1329 input: 'std.slice([1, 2, 3, 4, 5, 6], 1, 6, 2)',
1330 output: std.slice([1, 2, 3, 4, 5, 6], 1, 6, 2),
1331 },
1332 {
1333 input: 'std.slice("jsonnet", 0, 4, 1)',
1334 output: std.slice('jsonnet', 0, 4, 1),
1335 },
1336 {
1337 input: 'std.slice("jsonnet", -3, null, null)',
1338 output: std.slice('jsonnet', -3, null, null),
1339 },
1340 ],
1341 },
1342 {
1343 name: 'join',
1344 params: ['sep', 'arr'],
1345 availableSince: '0.10.0',
1346 description: |||
1347 If <code>sep</code> is a string, then <code>arr</code> must be an array of strings, in which
1348 case they are concatenated with <code>sep</code> used as a delimiter. If <code>sep</code>
1349 is an array, then <code>arr</code> must be an array of arrays, in which case the arrays are
1350 concatenated in the same way, to produce a single array.
1351 |||,
1352 examples: [
1353 {
1354 input: 'std.join(".", ["www", "google", "com"])',
1355 output: std.join('.', ['www', 'google', 'com']),
1356 },
1357 {
1358 input: 'std.join([9, 9], [[1], [2, 3]])',
1359 output: std.join([9, 9], [[1], [2, 3]]),
1360 },
1361 ],
1362 },
1363 {
1364 name: 'deepJoin',
1365 params: ['arr'],
1366 availableSince: '0.10.0',
1367 description: |||
1368 Concatenate an array containing strings and arrays to form a single string. If <code>arr</code> is
1369 a string, it is returned unchanged. If it is an array, it is flattened and the string elements are
1370 concatenated together with no separator.
1371 |||,
1372 examples: [
1373 {
1374 input: 'std.deepJoin(["one ", ["two ", "three ", ["four "], []], "five ", ["six"]])',
1375 output: std.deepJoin(["one ", ["two ", "three ", ["four "], []], "five ", ["six"]]),
1376 },
1377 {
1378 input: 'std.deepJoin("hello")',
1379 output: std.deepJoin("hello")
1380 },
1381 ],
1382 },
1383 {
1384 name: 'lines',
1385 params: ['arr'],
1386 availableSince: '0.10.0',
1387 description: |||
1388 Concatenate an array of strings into a text file with newline characters after each string.
1389 This is suitable for constructing bash scripts and the like.
1390 |||,
1391 },
1392 {
1393 name: 'flattenArrays',
1394 params: ['arr'],
1395 availableSince: '0.10.0',
1396 description: |||
1397 Concatenate an array of arrays into a single array.
1398 |||,
1399 examples: [
1400 {
1401 input: 'std.flattenArrays([[1, 2], [3, 4], [[5, 6], [7, 8]]])',
1402 output: std.flattenArrays([[1, 2], [3, 4], [[5, 6], [7, 8]]]),
1403 },
1404 ],
1405 },
1406 {
1407 name: 'flattenDeepArray',
1408 params: ['value'],
1409 availableSince: '0.21.0',
1410 description: |||
1411 Concatenate an array containing values and arrays into a single flattened array.
1412 |||,
1413 examples: [
1414 {
1415 input: 'std.flattenDeepArray([[1, 2], [], [3, [4]], [[5, 6, [null]], [7, 8]]])',
1416 output: std.flattenDeepArray([[1, 2], [], [3, [4]], [[5, 6, [null]], [7, 8]]]),
1417 },
1418 ],
1419 },
1420 {
1421 name: 'reverse',
1422 params: ['arrs'],
1423 availableSince: '0.13.0',
1424 description: |||
1425 Reverses an array.
1426 |||,
1427 },
1428 {
1429 name: 'sort',
1430 params: ['arr', 'keyF=id'],
1431 availableSince: '0.10.0',
1432 description: html.paragraphs([
1433 |||
1434 Sorts the array using the <= operator.
1435 |||,
1436 |||
1437 Optional argument <code>keyF</code> is a single argument function used to extract comparison key from each array element.
1438 Default value is identity function <code>keyF=function(x) x</code>.
1439 |||,
1440 ]),
1441 },
1442 {
1443 name: 'uniq',
1444 params: ['arr', 'keyF=id'],
1445 availableSince: '0.10.0',
1446 description: html.paragraphs([
1447 |||
1448 Removes successive duplicates. When given a sorted array, removes all duplicates.
1449 |||,
1450 |||
1451 Optional argument <code>keyF</code> is a single argument function used to extract comparison key from each array element.
1452 Default value is identity function <code>keyF=function(x) x</code>.
1453 |||,
1454 ]),
1455 },
1456 {
1457 name: 'all',
1458 params: ['arr'],
1459 availableSince: '0.19.0',
1460 description: html.paragraphs([
1461 |||
1462 Return true if all elements of <code>arr</code> is true, false otherwise. <code>all([])</code> evaluates to true.
1463 |||,
1464 |||
1465 It's an error if 1) <code>arr</code> is not an array, or 2) <code>arr</code> contains non-boolean values.
1466 |||,
1467 ]),
1468 },
1469 {
1470 name: 'any',
1471 params: ['arr'],
1472 availableSince: '0.19.0',
1473 description: html.paragraphs([
1474 |||
1475 Return true if any element of <code>arr</code> is true, false otherwise. <code>any([])</code> evaluates to false.
1476 |||,
1477 |||
1478 It's an error if 1) <code>arr</code> is not an array, or 2) <code>arr</code> contains non-boolean values.
1479 |||,
1480 ]),
1481 },
1482 {
1483 name: 'sum',
1484 params: ['arr'],
1485 availableSince: '0.20.0',
1486 description: html.paragraphs([
1487 |||
1488 Return sum of all element in <code>arr</code>.
1489 |||,
1490 ]),
1491 },
1492 {
1493 name: 'minArray',
1494 params: ['arr', 'keyF', 'onEmpty'],
1495 availableSince: '0.21.0',
1496 description: html.paragraphs([
1497 |||
1498 Return the minimum of all elements in <code>arr</code>. If <code>keyF</code> is provided, it is called on each element
1499 of the array and should return a comparator value, and in this case <code>minArray</code> will return an element
1500 with the minimum comparator value. If <code>onEmpty</code> is provided, and <code>arr</code> is empty, then
1501 <code>minArray</code> will return the provided <code>onEmpty</code> value. If <code>onEmpty</code> is not provided,
1502 then an empty <code>arr</code> will raise an error.
1503 |||,
1504 ]),
1505 },
1506 {
1507 name: 'maxArray',
1508 params: ['arr', 'keyF', 'onEmpty'],
1509 availableSince: '0.21.0',
1510 description: html.paragraphs([
1511 |||
1512 Return the maximum of all elements in <code>arr</code>. If <code>keyF</code> is provided, it is called on each element
1513 of the array and should return a comparator value, and in this case <code>maxArray</code> will return an element
1514 with the maximum comparator value. If <code>onEmpty</code> is provided, and <code>arr</code> is empty, then
1515 <code>maxArray</code> will return the provided <code>onEmpty</code> value. If <code>onEmpty</code> is not provided,
1516 then an empty <code>arr</code> will raise an error.
1517 |||,
1518 ]),
1519 },
1520 {
1521 name: 'contains',
1522 params: ['arr', 'elem'],
1523 availableSince: '0.21.0',
1524 description: html.paragraphs([
1525 |||
1526 Return true if given <code>elem</code> is present in <code>arr</code>, false otherwise.
1527 |||,
1528 ]),
1529 },
1530 {
1531 name: 'avg',
1532 params: ['arr'],
1533 availableSince: '0.21.0',
1534 description: html.paragraphs([
1535 |||
1536 Return average of all element in <code>arr</code>.
1537 |||,
1538 ]),
1539 },
1540 {
1541 name: 'remove',
1542 params: ['arr', 'elem'],
1543 availableSince: '0.21.0',
1544 description: html.paragraphs([
1545 |||
1546 Remove first occurrence of <code>elem</code> from <code>arr</code>.
1547 |||,
1548 ]),
1549 },
1550 {
1551 name: 'removeAt',
1552 params: ['arr', 'idx'],
1553 availableSince: '0.21.0',
1554 description: html.paragraphs([
1555 |||
1556 Remove element at <code>idx</code> index from <code>arr</code>.
1557 |||,
1558 ]),
1559 },
1560 ],
1561 },
1562 {
1563 name: 'Sets',
1564 id: 'sets',
1565 intro: html.paragraphs([
1566 |||
1567 Sets are represented as ordered arrays without duplicates.
1568 |||,
1569 |||
1570 Note that the <code>std.set*</code> functions rely on the uniqueness and ordering
1571 on arrays passed to them to work. This can be guaranteed by using <code>std.set(arr)</code>.
1572 If that is not the case, the functions will quietly return non-meaningful results.
1573 |||,
1574 |||
1575 All <code>set.set*</code> functions accept <code>keyF</code> function of one argument, which can be
1576 used to extract key to use from each element. All Set operations then use extracted key for the purpose
1577 of identifying uniqueness. Default value is identity function <code>local id = function(x) x</code>.
1578 |||,
1579 ]),
1580 fields: [
1581 {
1582 name: 'set',
1583 params: ['arr', 'keyF=id'],
1584 availableSince: '0.10.0',
1585 description: |||
1586 Shortcut for std.uniq(std.sort(arr)).
1587 |||,
1588 },
1589 {
1590 name: 'setInter',
1591 params: ['a', 'b', 'keyF=id'],
1592 availableSince: '0.10.0',
1593 description: |||
1594 Set intersection operation (values in both a and b).
1595 |||,
1596 },
1597 {
1598 name: 'setUnion',
1599 params: ['a', 'b', 'keyF=id'],
1600 availableSince: '0.10.0',
1601 description: |||
1602 Set union operation (values in any of <code>a</code> or <code>b</code>). Note that + on sets will simply
1603 concatenate
1604 the arrays, possibly forming an array that is not a set (due to not being ordered without
1605 duplicates).
1606 |||,
1607 examples: [
1608 {
1609 input: 'std.setUnion([1, 2], [2, 3])',
1610 output: std.setUnion([1, 2], [2, 3]),
1611 },
1612 {
1613 input: 'std.setUnion([{n:"A", v:1}, {n:"B"}], [{n:"A", v: 9999}, {n:"C"}], keyF=function(x) x.n)',
1614 output: std.setUnion([{ n: 'A', v: 1 }, { n: 'B' }], [{ n: 'A', v: 9999 }, { n: 'C' }], keyF=function(x) x.n),
1615 },
1616 ],
1617 },
1618 {
1619 name: 'setDiff',
1620 params: ['a', 'b', 'keyF=id'],
1621 availableSince: '0.10.0',
1622 description: |||
1623 Set difference operation (values in a but not b).
1624 |||,
1625 },
1626 {
1627 name: 'setMember',
1628 params: ['x', 'arr', 'keyF=id'],
1629 availableSince: '0.10.0',
1630 description: |||
1631 Returns <code>true</code> if x is a member of array, otherwise <code>false</code>.
1632 |||,
1633 },
1634 ],
1635 },
1636 {
1637 name: 'Objects',
1638 id: 'objects',
1639 fields: [
1640 {
1641 name: 'get',
1642 params: ['o', 'f', 'default=null', 'inc_hidden=true'],
1643 availableSince: '0.18.0',
1644 description: |||
1645 Returns the object's field if it exists or default value otherwise.
1646 <code>inc_hidden</code> controls whether to include hidden fields.
1647 |||,
1648 },
1649 {
1650 name: 'objectHas',
1651 params: ['o', 'f'],
1652 availableSince: '0.10.0',
1653 description: |||
1654 Returns <code>true</code> if the given object has the field (given as a string), otherwise
1655 <code>false</code>. Raises an error if the arguments are not object and string
1656 respectively. Returns false if the field is hidden.
1657 |||,
1658 },
1659 {
1660 name: 'objectFields',
1661 params: ['o'],
1662 availableSince: '0.10.0',
1663 description: |||
1664 Returns an array of strings, each element being a field from the given object. Does not include
1665 hidden fields.
1666 |||,
1667 },
1668 {
1669 name: 'objectValues',
1670 params: ['o'],
1671 availableSince: '0.17.0',
1672 description: |||
1673 Returns an array of the values in the given object. Does not include hidden fields.
1674 |||,
1675 },
1676 {
1677 name: 'objectKeysValues',
1678 params: ['o'],
1679 availableSince: '0.20.0',
1680 description: |||
1681 Returns an array of objects from the given object, each object having two fields:
1682 <code>key</code> (string) and <code>value</code> (object). Does not include hidden fields.
1683 |||,
1684 },
1685 {
1686 name: 'objectHasAll',
1687 params: ['o', 'f'],
1688 availableSince: '0.10.0',
1689 description: |||
1690 As <code>std.objectHas</code> but also includes hidden fields.
1691 |||,
1692 },
1693 {
1694 name: 'objectFieldsAll',
1695 params: ['o'],
1696 availableSince: '0.10.0',
1697 description: |||
1698 As <code>std.objectFields</code> but also includes hidden fields.
1699 |||,
1700 },
1701 {
1702 name: 'objectValuesAll',
1703 params: ['o'],
1704 availableSince: '0.17.0',
1705 description: |||
1706 As <code>std.objectValues</code> but also includes hidden fields.
1707 |||,
1708 },
1709 {
1710 name: 'objectKeysValuesAll',
1711 params: ['o'],
1712 availableSince: '0.20.0',
1713 description: |||
1714 As <code>std.objectKeysValues</code> but also includes hidden fields.
1715 |||,
1716 },
1717 {
1718 name: 'objectRemoveKey',
1719 params: ['obj', 'key'],
1720 availableSince: '0.21.0',
1721 description: |||
1722 Returns a new object after removing the given key from object.
1723 |||,
1724 },
1725 {
1726 name: 'mapWithKey',
1727 params: ['func', 'obj'],
1728 availableSince: '0.10.0',
1729 description: |||
1730 Apply the given function to all fields of the given object, also passing
1731 the field name. The function <code>func</code> is expected to take the
1732 field name as the first parameter and the field value as the second.
1733 |||,
1734 },
1735 ],
1736 },
1737 {
1738 name: 'Encoding',
1739 id: 'encoding',
1740 fields: [
1741 {
1742 name: 'base64',
1743 params: ['input'],
1744 availableSince: '0.10.0',
1745 description: |||
1746 Encodes the given value into a base64 string. The encoding sequence is <code>A-Za-z0-9+/</code> with
1747 <code>=</code>
1748 to pad the output to a multiple of 4 characters. The value can be a string or an array of
1749 numbers, but the codepoints / numbers must be in the 0 to 255 range. The resulting string
1750 has no line breaks.
1751 |||,
1752 },
1753 {
1754 name: 'base64DecodeBytes',
1755 params: ['str'],
1756 availableSince: '0.10.0',
1757 description: |||
1758 Decodes the given base64 string into an array of bytes (number values). Currently assumes
1759 the input string has no linebreaks and is padded to a multiple of 4 (with the = character).
1760 In other words, it consumes the output of std.base64().
1761 |||,
1762 },
1763 {
1764 name: 'base64Decode',
1765 params: ['str'],
1766 availableSince: '0.10.0',
1767 description: html.paragraphs([
1768 |||
1769 <em>Deprecated, use <code>std.base64DecodeBytes</code> and decode the string explicitly (e.g. with <code>std.decodeUTF8</code>) instead.</code></em>
1770 |||,
1771 |||
1772 Behaves like std.base64DecodeBytes() except returns a naively encoded string instead of an array of bytes.
1773 |||,
1774 ]),
1775 },
1776 {
1777 name: 'md5',
1778 params: ['s'],
1779 availableSince: '0.10.0',
1780 description: |||
1781 Encodes the given value into an MD5 string.
1782 |||,
1783 },
1784 {
1785 name: 'sha1',
1786 params: ['s'],
1787 availableSince: '0.21.0',
1788 description: [
1789 html.p({}, |||
1790 Encodes the given value into an SHA1 string.
1791 |||),
1792 html.p({}, |||
1793 This function is only available in Go version of jsonnet.
1794 |||),
1795 ],
1796 },
1797 {
1798 name: 'sha256',
1799 params: ['s'],
1800 availableSince: '0.21.0',
1801 description: [
1802 html.p({}, |||
1803 Encodes the given value into an SHA256 string.
1804 |||),
1805 html.p({}, |||
1806 This function is only available in Go version of jsonnet.
1807 |||),
1808 ],
1809 },
1810 {
1811 name: 'sha512',
1812 params: ['s'],
1813 availableSince: '0.21.0',
1814 description: [
1815 html.p({}, |||
1816 Encodes the given value into an SHA512 string.
1817 |||),
1818 html.p({}, |||
1819 This function is only available in Go version of jsonnet.
1820 |||),
1821 ],
1822 },
1823 {
1824 name: 'sha3',
1825 params: ['s'],
1826 availableSince: '0.21.0',
1827 description: [
1828 html.p({}, |||
1829 Encodes the given value into an SHA3 string.
1830 |||),
1831 html.p({}, |||
1832 This function is only available in Go version of jsonnet.
1833 |||),
1834 ],
1835 },
1836 ],
1837 },
1838 {
1839 name: 'Booleans',
1840 id: 'booleans',
1841 fields: [
1842 {
1843 name: 'xor',
1844 params: ['x', 'y'],
1845 availableSince: '0.20.0',
1846 description: |||
1847 Returns the xor of the two given booleans.
1848 |||,
1849 },
1850 {
1851 name: 'xnor',
1852 params: ['x', 'y'],
1853 availableSince: '0.20.0',
1854 description: |||
1855 Returns the xnor of the two given booleans.
1856 |||,
1857 },
1858 ],
1859 },
1860 {
1861 name: 'JSON Merge Patch',
1862 id: 'json_merge_patch',
1863 fields: [
1864 {
1865 name: 'mergePatch',
1866 params: ['target', 'patch'],
1867 availableSince: '0.10.0',
1868 description: |||
1869 Applies <code>patch</code> to <code>target</code>
1870 according to <a href="https://tools.ietf.org/html/rfc7396">RFC7396</a>
1871 |||,
1872 },
1873 ],
1874 },
1875 {
1876 name: 'Debugging',
1877 id: 'debugging',
1878 fields: [
1879 {
1880 name: 'trace',
1881 params: ['str', 'rest'],
1882 availableSince: '0.11.0',
1883 description: [
1884 html.p({}, |||
1885 Outputs the given string <code>str</code> to stderr and
1886 returns <code>rest</code> as the result.
1887 |||),
1888 html.p({}, |||
1889 Example:
1890 |||),
1891 html.p(
1892 {},
1893 html.pre({}, |||
1894 local conditionalReturn(cond, in1, in2) =
1895 if (cond) then
1896 std.trace('cond is true returning '
1897 + std.toString(in1), in1)
1898 else
1899 std.trace('cond is false returning '
1900 + std.toString(in2), in2);
1901
1902 {
1903 a: conditionalReturn(true, { b: true }, { c: false }),
1904 }
1905 |||),
1906 ),
1907 html.p({}, |||
1908 Prints:
1909 |||),
1910 html.p(
1911 {},
1912 html.pre({}, |||
1913 TRACE: test.jsonnet:3 cond is true returning {"b": true}
1914 {
1915 "a": {
1916 "b": true
1917 }
1918 }
1919 |||),
1920 ),
1921 ],
1922 },
1923 ],
1924 },
1925 ],
1926 }