]> granicus.if.org Git - icinga2/blob - doc/18-library-reference.md
Docs: Update API and Library Reference chapters
[icinga2] / doc / 18-library-reference.md
1 # <a id="library-reference"></a> Library Reference
2
3 ## <a id="global-functions"></a> Global functions
4
5 These functions are globally available in [assign/ignore where expressions](3-monitoring-basics.md#using-apply-expressions),
6 [functions](17-language-reference.md#functions), [API filters](12-icinga2-api.md#icinga2-api-filters)
7 and the [Icinga 2 console](11-cli-commands.md#cli-command-console).
8
9 You can use the [Icinga 2 console](11-cli-commands.md#cli-command-console)
10 as a sandbox to test these functions before implementing
11 them in your scenarios.
12
13 ### <a id="global-functions-regex"></a> regex
14
15 Signature:
16
17     function regex(pattern, text)
18
19 Returns true if the regular expression matches the text, false otherwise.
20 **Tip**: In case you are looking for regular expression tests try [regex101](https://regex101.com).
21
22 Example:
23
24     $ icinga2 console
25     Icinga 2 (version: v2.6.0)
26     <1> => host.vars.os_type = "Linux/Unix"
27     null
28     <2> => regex("^Linux", host.vars.os_type)
29     true
30     <3> => regex("^Linux$", host.vars.os_type)
31     false
32
33 ### <a id="global-functions-match"></a> match
34
35 Signature:
36
37     function match(pattern, text)
38
39 Returns true if the wildcard (`\*`) pattern matches the text, false otherwise.
40
41 Example:
42
43     $ icinga2 console
44     Icinga 2 (version: v2.6.0)
45     <1> => host.display_name = "NUE-DB-PROD-586"
46     null
47     <2> => match("NUE-*", host.display_name)
48     true
49     <3> => match("*NUE-*", host.display_name)
50     true
51     <4> => match("NUE-*-DEV-*", host.display_name)
52     false
53
54 ### <a id="global-functions-cidr_match"></a> cidr_match
55
56 Signature:
57
58     function cidr_match(pattern, ip)
59
60 Returns true if the CIDR pattern matches the IP address, false otherwise.
61 IPv4 addresses are converted to IPv4-mapped IPv6 addresses before being
62 matched against the pattern.
63
64 Example:
65
66     $ icinga2 console
67     Icinga 2 (version: v2.6.0)
68     <1> => host.address = "192.168.56.101"
69     null
70     <2> => cidr_match("192.168.56.0/24", host.address)
71     true
72     <3> => cidr_match("192.168.56.0/26", host.address)
73     false
74
75 ### <a id="global-functions-range"></a> range
76
77 Signature:
78
79     function range(end)
80     function range(start, end)
81     function range(start, end, increment)
82
83 Returns an array of numbers in the specified range.
84 If you specify one parameter, the first element starts at `0`.
85 The following array numbers are incremented by `1` and stop before
86 the specified end.
87 If you specify the start and end numbers, the returned array
88 number are incremented by `1`. They start at the specified start
89 number and stop before the end number.
90 Optionally you can specify the incremented step between numbers
91 as third parameter.
92
93 Example:
94
95     $ icinga2 console
96     Icinga 2 (version: v2.6.0)
97     <1> => range(5)
98     [ 0.000000, 1.000000, 2.000000, 3.000000, 4.000000 ]
99     <2> => range(2,4)
100     [ 2.000000, 3.000000 ]
101     <3> => range(2,10,2)
102     [ 2.000000, 4.000000, 6.000000, 8.000000 ]
103
104 ### <a id="global-functions-len"></a> len
105
106 Signature:
107
108     function len(value)
109
110 Returns the length of the value, i.e. the number of elements for an array
111 or dictionary, or the length of the string in bytes.
112
113 **Note**: Instead of using this global function you are advised to use the type's
114 prototype method: [Array#len](18-library-reference.md#array-len), [Dictionary#len](18-library-reference.md#dictionary-len) and
115 [String#len](18-library-reference.md#string-len).
116
117 Example:
118
119     $ icinga2 console
120     Icinga 2 (version: v2.6.0)
121     <1> => host.groups = [ "linux-servers", "db-servers" ]
122     null
123     <2> => host.groups.len()
124     2.000000
125     <3> => host.vars.disks["/"] = {}
126     null
127     <4> => host.vars.disks["/var"] = {}
128     null
129     <5> => host.vars.disks.len()
130     2.000000
131     <6> => host.vars.os_type = "Linux/Unix"
132     null
133     <7> => host.vars.os_type.len()
134     10.000000
135
136
137 ### <a id="global-functions-union"></a> union
138
139 Signature:
140
141     function union(array, array, ...)
142
143 Returns an array containing all unique elements from the specified arrays.
144
145 Example:
146
147     $ icinga2 console
148     Icinga 2 (version: v2.6.0)
149     <1> => var dev_notification_groups = [ "devs", "slack" ]
150     null
151     <2> => var host_notification_groups = [ "slack", "noc" ]
152     null
153     <3> => union(dev_notification_groups, host_notification_groups)
154     [ "devs", "noc", "slack" ]
155
156 ### <a id="global-functions-intersection"></a> intersection
157
158 Signature:
159
160     function intersection(array, array, ...)
161
162 Returns an array containing all unique elements which are common to all
163 specified arrays.
164
165 Example:
166
167     $ icinga2 console
168     Icinga 2 (version: v2.6.0)
169     <1> => var dev_notification_groups = [ "devs", "slack" ]
170     null
171     <2> => var host_notification_groups = [ "slack", "noc" ]
172     null
173     <3> => intersection(dev_notification_groups, host_notification_groups)
174     [ "slack" ]
175
176 ### <a id="global-functions-keys"></a> keys
177
178 Signature:
179
180     function keys(dict)
181
182 Returns an array containing the dictionary's keys.
183
184 **Note**: Instead of using this global function you are advised to use the type's
185 prototype method: [Dictionary#keys](18-library-reference.md#dictionary-keys).
186
187 Example:
188
189     $ icinga2 console
190     Icinga 2 (version: v2.6.0)
191     <1> => host.vars.disks["/"] = {}
192     null
193     <2> => host.vars.disks["/var"] = {}
194     null
195     <3> => host.vars.disks.keys()
196     [ "/", "/var" ]
197
198 ### <a id="global-functions-string"></a> string
199
200 Signature:
201
202     function string(value)
203
204 Converts the value to a string.
205
206 **Note**: Instead of using this global function you are advised to use the type's
207 prototype method:
208
209 * [Number#to_string](18-library-reference.md#number-to_string)
210 * [Boolean#to_string](18-library-reference.md#boolean-to_string)
211 * [String#to_string](18-library-reference.md#string-to_string)
212 * [Object#to_string](18-library-reference.md#object-to-string) for Array and Dictionary types
213 * [DateTime#to_string](18-library-reference.md#datetime-tostring)
214
215 Example:
216
217     $ icinga2 console
218     Icinga 2 (version: v2.6.0)
219     <1> => 5.to_string()
220     "5"
221     <2> => false.to_string()
222     "false"
223     <3> => "abc".to_string()
224     "abc"
225     <4> => [ "dev", "slack" ].to_string()
226     "[ \"dev\", \"slack\" ]"
227     <5> => { "/" = {}, "/var" = {} }.to_string()
228     "{\n\t\"/\" = {\n\t}\n\t\"/var\" = {\n\t}\n}"
229     <6> => DateTime(2016, 11, 25).to_string()
230     "2016-11-25 00:00:00 +0100"
231
232 ### <a id="global-functions-number"></a> number
233
234 Signature:
235
236     function number(value)
237
238 Converts the value to a number.
239
240 Example:
241
242     $ icinga2 console
243     Icinga 2 (version: v2.6.0)
244     <1> => number(false)
245     0.000000
246     <2> => number("78")
247     78.000000
248
249 ### <a id="global-functions-bool"></a> bool
250
251 Signature:
252
253     function bool(value)
254
255 Converts the value to a bool.
256
257 Example:
258
259     $ icinga2 console
260     Icinga 2 (version: v2.6.0)
261     <1> => bool(1)
262     true
263     <2> => bool(0)
264     false
265
266 ### <a id="global-functions-random"></a> random
267
268 Signature:
269
270     function random()
271
272 Returns a random value between 0 and RAND\_MAX (as defined in stdlib.h).
273
274     $ icinga2 console
275     Icinga 2 (version: v2.6.0)
276     <1> => random()
277     1263171996.000000
278     <2> => random()
279     108402530.000000
280
281 ### <a id="global-functions-log"></a> log
282
283 Signature:
284
285     function log(value)
286
287 Writes a message to the log. Non-string values are converted to a JSON string.
288
289 Signature:
290
291     function log(severity, facility, value)
292
293 Writes a message to the log. `severity` can be one of `LogDebug`, `LogNotice`,
294 `LogInformation`, `LogWarning`, and `LogCritical`.
295
296 Non-string values are converted to a JSON string.
297
298 Example:
299
300     $ icinga2 console
301     Icinga 2 (version: v2.6.0)
302     <1> => log(LogCritical, "Console", "First line")
303     critical/Console: First line
304     null
305     <2> => var groups = [ "devs", "slack" ]
306     null
307     <3> => log(LogCritical, "Console", groups)
308     critical/Console: ["devs","slack"]
309     null
310
311 ### <a id="global-functions-typeof"></a> typeof
312
313 Signature:
314
315     function typeof(value)
316
317 Returns the [Type](18-library-reference.md#type-type) object for a value.
318
319 Example:
320
321     $ icinga2 console
322     Icinga 2 (version: v2.6.0)
323     <1> => typeof(3) == Number
324     true
325     <2> => typeof("str") == String
326     true
327     <3> => typeof(true) == Boolean
328     true
329     <4> => typeof([ 1, 2, 3]) == Array
330     true
331     <5> => typeof({ a = 2, b = 3}) == Dictionary
332
333 ### <a id="global-functions-get_time"></a> get_time
334
335 Signature:
336
337     function get_time()
338
339 Returns the current UNIX timestamp as floating point number.
340
341 Example:
342
343     $ icinga2 console
344     Icinga 2 (version: v2.6.0)
345     <1> => get_time()
346     1480072135.633008
347     <2> => get_time()
348     1480072140.401207
349
350 ### <a id="global-functions-parse_performance_data"></a> parse_performance_data
351
352 Signature:
353
354     function parse_performance_data(pd)
355
356 Parses a performance data string and returns an array describing the values.
357
358 Example:
359
360     $ icinga2 console
361     Icinga 2 (version: v2.6.0)
362     <1> => var pd = "'time'=1480074205.197363;;;"
363     null
364     <2> => parse_performance_data(pd)
365     {
366         counter = false
367         crit = null
368         label = "time"
369         max = null
370         min = null
371         type = "PerfdataValue"
372         unit = ""
373         value = 1480074205.197363
374         warn = null
375     }
376
377 ### <a id="global-functions-dirname"></a> dirname
378
379 Signature:
380
381     function dirname(path)
382
383 Returns the directory portion of the specified path.
384
385 Example:
386
387     $ icinga2 console
388     Icinga 2 (version: v2.6.0)
389     <1> => var path = "/etc/icinga2/scripts/xmpp-notification.pl"
390     null
391     <2> => dirname(path)
392     "/etc/icinga2/scripts"
393
394 ### <a id="global-functions-basename"></a> basename
395
396 Signature:
397
398     function basename(path)
399
400 Returns the filename portion of the specified path.
401
402 Example:
403
404     $ icinga2 console
405     Icinga 2 (version: v2.6.0)
406     <1> => var path = "/etc/icinga2/scripts/xmpp-notification.pl"
407     null
408     <2> => basename(path)
409     "xmpp-notification.pl"
410
411 ### <a id="global-functions-escape_shell_arg"></a> escape_shell_arg
412
413 Signature:
414
415     function escape_shell_arg(text)
416
417 Escapes a string for use as a single shell argument.
418
419 Example:
420
421     $ icinga2 console
422     Icinga 2 (version: v2.6.0)
423     <1> => escape_shell_arg("'$host.name$' '$service.name$'")
424     "''\\''$host.name$'\\'' '\\''$service.name$'\\'''"
425
426 ### <a id="global-functions-escape_shell_cmd"></a> escape_shell_cmd
427
428 Signature:
429
430     function escape_shell_cmd(text)
431
432 Escapes shell meta characters in a string.
433
434 Example:
435
436     $ icinga2 console
437     Icinga 2 (version: v2.6.0)
438     <1> => escape_shell_cmd("/bin/echo 'shell test' $ENV")
439     "/bin/echo 'shell test' \\$ENV"
440
441 ### <a id="global-functions-escape_create_process_arg"></a> escape_create_process_arg
442
443 Signature:
444
445     function escape_create_process_arg(text)
446
447 Escapes a string for use as an argument for CreateProcess(). Windows only.
448
449 ### <a id="global-functions-sleep"></a> sleep
450
451 Signature:
452
453     function sleep(interval)
454
455 Sleeps for the specified amount of time (in seconds).
456
457 ## <a id="object-accessor-functions"></a> Object Accessor Functions
458
459 These functions can be used to retrieve a reference to another object by name.
460
461 ### <a id="objref-get_check_command"></a> get_check_command
462
463 Signature:
464
465     function get_check_command(name);
466
467 Returns the CheckCommand object with the specified name, or `null` if no such CheckCommand object exists.
468
469 ### <a id="objref-get_event_command"></a> get_event_command
470
471 Signature:
472
473     function get_event_command(name);
474
475 Returns the EventCommand object with the specified name, or `null` if no such EventCommand object exists.
476
477 ### <a id="objref-get_notification_command"></a> get_notification_command
478
479 Signature:
480
481     function get_notification_command(name);
482
483 Returns the NotificationCommand object with the specified name, or `null` if no such NotificationCommand object exists.
484
485 ### <a id="objref-get_host"></a> get_host
486
487 Signature:
488
489     function get_host(host_name);
490
491 Returns the Host object with the specified name, or `null` if no such Host object exists.
492
493
494 ### <a id="objref-get_service"></a> get_service
495
496 Signature:
497
498     function get_service(host_name, service_name);
499
500 Returns the Service object with the specified name, or `null` if no such Service object exists.
501
502
503 ### <a id="objref-get_user"></a> get_user
504
505 Signature:
506
507     function get_user(name);
508
509 Returns the User object with the specified name, or `null` if no such User object exists.
510
511 ### <a id="objref-get_host_group"></a> get_host_group
512
513 Signature:
514
515     function get_host_group(name);
516
517 Returns the HostGroup object with the specified name, or `null` if no such HostGroup object exists.
518
519
520 ### <a id="objref-get_service_group"></a> get_service_group
521
522 Signature:
523
524     function get_service_group(name);
525
526 Returns the ServiceGroup object with the specified name, or `null` if no such ServiceGroup object exists.
527
528 ### <a id="objref-get_user_group"></a> get_user_group
529
530 Signature:
531
532     function get_user_group(name);
533
534 Returns the UserGroup object with the specified name, or `null` if no such UserGroup object exists.
535
536
537 ### <a id="objref-get_time_period"></a> get_time_period
538
539 Signature:
540
541     function get_time_period(name);
542
543 Returns the TimePeriod object with the specified name, or `null` if no such TimePeriod object exists.
544
545
546 ### <a id="objref-get_object"></a> get_object
547
548 Signature:
549
550     function get_object(type, name);
551
552 Returns the object with the specified type and name, or `null` if no such object exists. `type` must refer
553 to a type object.
554
555
556 ### <a id="objref-get_objects"></a> get_objects
557
558 Signature:
559
560     function get_objects(type);
561
562 Returns an array of objects whose type matches the specified type. `type` must refer
563 to a type object.
564
565
566 ## <a id="math-object"></a> Math object
567
568 The global `Math` object can be used to access a number of mathematical constants
569 and functions.
570
571 ### <a id="math-e"></a> Math.E
572
573 Euler's constant.
574
575 ### <a id="math-ln2"></a> Math.LN2
576
577 Natural logarithm of 2.
578
579 ### <a id="math-ln10"></a> Math.LN10
580
581 Natural logarithm of 10.
582
583 ### <a id="math-log2e"></a> Math.LOG2E
584
585 Base 2 logarithm of E.
586
587 ### <a id="math-pi"></a> Math.PI
588
589 The mathematical constant Pi.
590
591 ### <a id="math-sqrt1_2"></a> Math.SQRT1_2
592
593 Square root of 1/2.
594
595 ### <a id="math-sqrt2"></a> Math.SQRT2
596
597 Square root of 2.
598
599 ### <a id="math-abs"></a> Math.abs
600
601 Signature:
602
603     function abs(x);
604
605 Returns the absolute value of `x`.
606
607 ### <a id="math-acos"></a> Math.acos
608
609 Signature:
610
611     function acos(x);
612
613 Returns the arccosine of `x`.
614
615 ### <a id="math-asin"></a> Math.asin
616
617 Signature:
618
619     function asin(x);
620
621 Returns the arcsine of `x`.
622
623 ### <a id="math-atan"></a> Math.atan
624
625 Signature:
626
627     function atan(x);
628
629 Returns the arctangent of `x`.
630
631 ### <a id="math-atan2"></a> Math.atan2
632
633 Signature:
634
635     function atan2(y, x);
636
637 Returns the arctangent of the quotient of `y` and `x`.
638
639 ### <a id="math-ceil"></a> Math.ceil
640
641 Signature:
642
643     function ceil(x);
644
645 Returns the smallest integer value not less than `x`.
646
647 ### <a id="math-cos"></a> Math.cos
648
649 Signature:
650
651     function cos(x);
652
653 Returns the cosine of `x`.
654
655 ### <a id="math-exp"></a> Math.exp
656
657 Signature:
658
659     function exp(x);
660
661 Returns E raised to the `x`th power.
662
663 ### <a id="math-floor"></a> Math.floor
664
665 Signature:
666
667     function floor(x);
668
669 Returns the largest integer value not greater than `x`.
670
671 ### <a id="math-isinf"></a> Math.isinf
672
673 Signature:
674
675     function isinf(x);
676
677 Returns whether `x` is infinite.
678
679 ### <a id="math-isnan"></a> Math.isnan
680
681 Signature:
682
683     function isnan(x);
684
685 Returns whether `x` is NaN (not-a-number).
686
687 ### <a id="math-log"></a> Math.log
688
689 Signature:
690
691     function log(x);
692
693 Returns the natural logarithm of `x`.
694
695 ### <a id="math-max"></a> Math.max
696
697 Signature:
698
699     function max(...);
700
701 Returns the largest argument. A variable number of arguments can be specified.
702 If no arguments are given, -Infinity is returned.
703
704 ### <a id="math-min"></a> Math.min
705
706 Signature:
707
708     function min(...);
709
710 Returns the smallest argument. A variable number of arguments can be specified.
711 If no arguments are given, +Infinity is returned.
712
713 ### <a id="math-pow"></a> Math.pow
714
715 Signature:
716
717     function pow(x, y);
718
719 Returns `x` raised to the `y`th power.
720
721 ### <a id="math-random"></a> Math.random
722
723 Signature:
724
725     function random();
726
727 Returns a pseudo-random number between 0 and 1.
728
729 ### <a id="math-round"></a> Math.round
730
731 Signature:
732
733     function round(x);
734
735 Returns `x` rounded to the nearest integer value.
736
737 ### <a id="math-sign"></a> Math.sign
738
739 Signature:
740
741     function sign(x);
742
743 Returns -1 if `x` is negative, 1 if `x` is positive
744 and 0 if `x` is 0.
745
746 ### <a id="math-sin"></a> Math.sin
747
748 Signature:
749
750     function sin(x);
751
752 Returns the sine of `x`.
753
754 ### <a id="math-sqrt"></a> Math.sqrt
755
756 Signature:
757
758     function sqrt(x);
759
760 Returns the square root of `x`.
761
762 ### <a id="math-tan"></a> Math.tan
763
764 Signature:
765
766     function tan(x);
767
768 Returns the tangent of `x`.
769
770 ## <a id="json-object"></a> Json object
771
772 The global `Json` object can be used to encode and decode JSON.
773
774 ### <a id="json-encode"></a> Json.encode
775
776 Signature:
777
778     function encode(x);
779
780 Encodes an arbitrary value into JSON.
781
782 ### <a id="json-decode"></a> Json.decode
783
784 Signature:
785
786     function decode(x);
787
788 Decodes a JSON string.
789
790 ## <a id="number-type"></a> Number type
791
792 ### <a id="number-to_string"></a> Number#to_string
793
794 Signature:
795
796     function to_string();
797
798 The `to_string` method returns a string representation of the number.
799
800 Example:
801
802     var example = 7
803         example.to_string() /* Returns "7" */
804
805 ## <a id="boolean-type"></a> Boolean type
806
807 ### <a id="boolean-to_string"></a> Boolean#to_string
808
809 Signature:
810
811     function to_string();
812
813 The `to_string` method returns a string representation of the boolean value.
814
815 Example:
816
817     var example = true
818         example.to_string() /* Returns "true" */
819
820 ## <a id="string-type"></a> String type
821
822 ### <a id="string-find"></a> String#find
823
824 Signature:
825
826     function find(str, start);
827
828 Returns the zero-based index at which the string `str` was found in the string. If the string
829 was not found, -1 is returned. `start` specifies the zero-based index at which `find` should
830 start looking for the string (defaults to 0 when not specified).
831
832 Example:
833
834     "Hello World".find("World") /* Returns 6 */
835
836 ### <a id="string-contains"></a> String#contains
837
838 Signature:
839
840     function contains(str);
841
842 Returns `true` if the string `str` was found in the string. If the string
843 was not found, `false` is returned. Use [find](18-library-reference.md#string-find)
844 for getting the index instead.
845
846 Example:
847
848     "Hello World".contains("World") /* Returns true */
849
850 ### <a id="string-len"></a> String#len
851
852 Signature
853
854     function len();
855
856 Returns the length of the string in bytes. Note that depending on the encoding type of the string
857 this is not necessarily the number of characters.
858
859 Example:
860
861     "Hello World".len() /* Returns 11 */
862
863 ### <a id="string-lower"></a> String#lower
864
865 Signature:
866
867     function lower();
868
869 Returns a copy of the string with all of its characters converted to lower-case.
870
871 Example:
872
873     "Hello World".lower() /* Returns "hello world" */
874
875 ### <a id="string-upper"></a> String#upper
876
877 Signature:
878
879     function upper();
880
881 Returns a copy of the string with all of its characters converted to upper-case.
882
883 Example:
884
885     "Hello World".upper() /* Returns "HELLO WORLD" */
886
887 ### <a id="string-replace"></a> String#replace
888
889 Signature:
890
891     function replace(search, replacement);
892
893 Returns a copy of the string with all occurences of the string specified in `search` replaced
894 with the string specified in `replacement`.
895
896 ### <a id="string-split"></a> String#split
897
898 Signature:
899
900     function split(delimiters);
901
902 Splits a string into individual parts and returns them as an array. The `delimiters` argument
903 specifies the characters which should be used as delimiters between parts.
904
905 Example:
906
907     "x-7,y".split("-,") /* Returns [ "x", "7", "y" ] */
908
909 ### <a id="string-substr"></a> String#substr
910
911 Signature:
912
913     function substr(start, len);
914
915 Returns a part of a string. The `start` argument specifies the zero-based index at which the part begins.
916 The optional `len` argument specifies the length of the part ("until the end of the string" if omitted).
917
918 Example:
919
920     "Hello World".substr(6) /* Returns "World" */
921
922 ### <a id="string-to_string"></a> String#to_string
923
924 Signature:
925
926     function to_string();
927
928 Returns a copy of the string.
929
930 ### <a id="string-reverse"></a> String#reverse
931
932 Signature:
933
934     function reverse();
935
936 Returns a copy of the string in reverse order.
937
938 ### <a id="string-trim"></a> String#trim
939
940 Signature:
941
942     function trim();
943
944 Removes trailing whitespaces and returns the string.
945
946 ## <a id="object-type"></a> Object type
947
948 This is the base type for all types in the Icinga application.
949
950 ### <a id="object-clone"></a> Object#clone
951
952 Signature:
953
954      function clone();
955
956 Returns a copy of the object. Note that for object elements which are
957 reference values (e.g. objects such as arrays or dictionaries) the entire
958 object is recursively copied.
959
960 ### <a id="object-to-string"></a> Object#to_string
961
962 Signature:
963
964     function to_string();
965
966 Returns a string representation for the object. Unless overridden this returns a string
967 of the format "Object of type '<typename>'" where <typename> is the name of the
968 object's type.
969
970 Example:
971
972     [ 3, true ].to_string() /* Returns "[ 3.000000, true ]" */
973
974 ### <a id="object-type-field"></a> Object#type
975
976 Signature:
977
978     String type;
979
980 Returns the object's type name. This attribute is read-only.
981
982 Example:
983
984     get_host("localhost").type /* Returns "Host" */
985
986 ## <a id="type-type"></a> Type type
987
988 Inherits methods from the [Object type](18-library-reference.md#object-type).
989
990 The `Type` type provides information about the underlying type of an object or scalar value.
991
992 All types are registered as global variables. For example, in order to obtain a reference to the `String` type the global variable `String` can be used.
993
994 ### <a id="type-base"></a> Type#base
995
996 Signature:
997
998     Type base;
999
1000 Returns a reference to the type's base type. This attribute is read-only.
1001
1002 Example:
1003
1004     Dictionary.base == Object /* Returns true, because the Dictionary type inherits directly from the Object type. */
1005
1006 ### <a id="type-name"></a> Type#name
1007
1008 Signature:
1009
1010     String name;
1011
1012 Returns the name of the type.
1013
1014 ### <a id="type-prototype"></a> Type#prototype
1015
1016 Signature:
1017
1018     Object prototype;
1019
1020 Returns the prototype object for the type. When an attribute is accessed on an object that doesn't exist the prototype object is checked to see if an attribute with the requested name exists. If it does, the attribute's value is returned.
1021
1022 The prototype functionality is used to implement methods.
1023
1024 Example:
1025
1026     3.to_string() /* Even though '3' does not have a to_string property the Number type's prototype object does. */
1027
1028 ## <a id="array-type"></a> Array type
1029
1030 Inherits methods from the [Object type](18-library-reference.md#object-type).
1031
1032 ### <a id="array-add"></a> Array#add
1033
1034 Signature:
1035
1036     function add(value);
1037
1038 Adds a new value after the last element in the array.
1039
1040 ### <a id="array-clear"></a> Array#clear
1041
1042 Signature:
1043
1044     function clear();
1045
1046 Removes all elements from the array.
1047
1048 ### <a id="array-shallow-clone"></a> Array#shallow_clone
1049
1050     function shallow_clone();
1051
1052 Returns a copy of the array. Note that for elements which are reference values (e.g. objects such
1053 as arrays and dictionaries) only the references are copied.
1054
1055 ### <a id="array-contains"></a> Array#contains
1056
1057 Signature:
1058
1059     function contains(value);
1060
1061 Returns true if the array contains the specified value, false otherwise.
1062
1063 ### <a id="array-len"></a> Array#len
1064
1065 Signature:
1066
1067     function len();
1068
1069 Returns the number of elements contained in the array.
1070
1071 ### <a id="array-remove"></a> Array#remove
1072
1073 Signature:
1074
1075     function remove(index);
1076
1077 Removes the element at the specified zero-based index.
1078
1079 ### <a id="array-set"></a> Array#set
1080
1081 Signature:
1082
1083     function set(index, value);
1084
1085 Sets the element at the zero-based index to the specified value. The `index` must refer to an element
1086 which already exists in the array.
1087
1088 ### <a id="array-get"></a> Array#get
1089
1090 Signature:
1091
1092     function get(index);
1093
1094 Retrieves the element at the specified zero-based index.
1095
1096 ### <a id="array-sort"></a> Array#sort
1097
1098 Signature:
1099
1100     function sort(less_cmp);
1101
1102 Returns a copy of the array where all items are sorted. The items are
1103 compared using the `<` (less-than) operator. A custom comparator function
1104 can be specified with the `less_cmp` argument.
1105
1106 ### <a id="array-join"></a> Array#join
1107
1108 Signature:
1109
1110     function join(separator);
1111
1112 Joins all elements of the array using the specified separator.
1113
1114 ### <a id="array-reverse"></a> Array#reverse
1115
1116 Signature:
1117
1118     function reverse();
1119
1120 Returns a new array with all elements of the current array in reverse order.
1121
1122 ### <a id="array-map"></a> Array#map
1123
1124 Signature:
1125
1126     function map(func);
1127
1128 Calls `func(element)` for each of the elements in the array and returns
1129 a new array containing the return values of these function calls.
1130
1131 ### <a id="array-reduce"></a> Array#reduce
1132
1133 Signature:
1134
1135     function reduce(func);
1136
1137 Reduces the elements of the array into a single value by calling the provided
1138 function `func` as `func(a, b)` repeatedly where `a` and `b` are elements of the array
1139 or results from previous function calls.
1140
1141 ### <a id="array-filter"></a> Array#filter
1142
1143 Signature:
1144
1145     function filter(func);
1146
1147 Returns a copy of the array containing only the elements for which `func(element)`
1148 is true.
1149
1150 ### <a id="array-unique"></a> Array#unique
1151
1152 Signature:
1153
1154     function unique();
1155
1156 Returns a copy of the array with all duplicate elements removed. The original order
1157 of the array is not preserved.
1158
1159 ## <a id="dictionary-type"></a> Dictionary type
1160
1161 Inherits methods from the [Object type](18-library-reference.md#object-type).
1162
1163 ### <a id="dictionary-shallow-clone"></a> Dictionary#shallow_clone
1164
1165 Signature:
1166
1167     function shallow_clone();
1168
1169 Returns a copy of the dictionary. Note that for elements which are reference values (e.g. objects such
1170 as arrays and dictionaries) only the references are copied.
1171
1172 ### <a id="dictionary-contains"></a> Dictionary#contains
1173
1174 Signature:
1175
1176     function contains(key);
1177
1178 Returns true if a dictionary item with the specified `key` exists, false otherwise.
1179
1180 ### <a id="dictionary-len"></a> Dictionary#len
1181
1182 Signature:
1183
1184     function len();
1185
1186 Returns the number of items contained in the dictionary.
1187
1188 ### <a id="dictionary-remove"></a> Dictionary#remove
1189
1190 Signature:
1191
1192     function remove(key);
1193
1194 Removes the item with the specified `key`. Trying to remove an item which does not exist
1195 is a no-op.
1196
1197 ### <a id="dictionary-set"></a> Dictionary#set
1198
1199 Signature:
1200
1201     function set(key, value);
1202
1203 Creates or updates an item with the specified `key` and `value`.
1204
1205 ### <a id="dictionary-get"></a> Dictionary#get
1206
1207 Signature:
1208
1209     function get(key);
1210
1211 Retrieves the value for the specified `key`. Returns `null` if they `key` does not exist
1212 in the dictionary.
1213
1214 ### <a id="dictionary-keys"></a> Dictionary#keys
1215
1216 Signature:
1217
1218     function keys();
1219
1220 Returns a list of keys for all items that are currently in the dictionary.
1221
1222 ## <a id="scriptfunction-type"></a> Function type
1223
1224 Inherits methods from the [Object type](18-library-reference.md#object-type).
1225
1226 ### <a id="scriptfunction-call"></a> Function#call
1227
1228 Signature:
1229
1230     function call(thisArg, ...);
1231
1232 Invokes the function using an alternative `this` scope. The `thisArg` argument specifies the `this`
1233 scope for the function. All other arguments are passed directly to the function.
1234
1235 Example:
1236
1237     function set_x(val) {
1238           this.x = val
1239         }
1240         
1241         dict = {}
1242         
1243         set_x.call(dict, 7) /* Invokes set_x using `dict` as `this` */
1244
1245 ### <a id="scriptfunction-callv"></a> Function#callv
1246
1247 Signature:
1248
1249     function callv(thisArg, args);
1250
1251 Invokes the function using an alternative `this` scope. The `thisArg` argument specifies the `this`
1252 scope for the function. The items in the `args` array are passed to the function as individual arguments.
1253
1254 Example:
1255
1256     function set_x(val) {
1257           this.x = val
1258         }
1259         
1260         var dict = {}
1261
1262         var args = [ 7 ]
1263
1264         set_x.callv(dict, args) /* Invokes set_x using `dict` as `this` */
1265
1266 ## <a id="datetime-type"></a> DateTime type
1267
1268 Inherits methods from the [Object type](18-library-reference.md#object-type).
1269
1270 ### <a id="datetime-ctor"></a> DateTime constructor
1271
1272 Signature:
1273
1274     function DateTime()
1275     function DateTime(unixTimestamp)
1276     function DateTime(year, month, day)
1277     function DateTime(year, month, day, hours, minutes, seconds)
1278
1279 Constructs a new DateTime object. When no arguments are specified for the constructor a new
1280 DateTime object representing the current time is created.
1281
1282 Example:
1283
1284     var d1 = DateTime() /* current time */
1285     var d2 = DateTime(2016, 5, 21) /* midnight April 21st, 2016 (local time) */
1286
1287 ### <a id="datetime-arithmetic"></a> DateTime arithmetic
1288
1289 Subtracting two DateTime objects yields the interval between them, in seconds.
1290
1291 Example:
1292
1293     var delta = DateTime() - DateTime(2016, 5, 21) /* seconds since midnight April 21st, 2016 */
1294
1295 Subtracting a number from a DateTime object yields a new DateTime object that is further in the past:
1296
1297 Example:
1298
1299     var dt = DateTime() - 2 * 60 * 60 /* Current time minus 2 hours */
1300
1301 Adding a number to a DateTime object yields a new DateTime object that is in the future:
1302
1303 Example:
1304
1305     var dt = DateTime() + 24 * 60 60 /* Current time plus 24 hours */
1306
1307 ### <a id="datetime-format"></a> DateTime#format
1308
1309 Signature:
1310
1311     function format(fmt)
1312
1313 Returns a string representation for the DateTime object using the specified format string.
1314 The format string may contain format conversion placeholders as specified in strftime(3).
1315
1316 Example:
1317
1318     var s = DateTime(2016, 4, 21).format("%A") /* Sets s to "Thursday". */
1319
1320 ### <a id="datetime-tostring"></a> DateTime#to_string
1321
1322 Signature:
1323
1324     function to_string()
1325
1326 Returns a string representation for the DateTime object. Uses a suitable default format.
1327
1328 Example:
1329
1330     var s = DateTime(2016, 4, 21).to_string() /* Sets s to "2016-04-21 00:00:00 +0200". */