]> granicus.if.org Git - icinga2/blob - doc/11-library-reference.md
0fca7a517b9779aab868c7513a772d6c2bfbc9c1
[icinga2] / doc / 11-library-reference.md
1 # <a id="library-reference"></a> Library Reference
2
3 ## <a id="global-functions"></a> Global functions
4
5 Function                        | Description
6 --------------------------------|-----------------------
7 regex(pattern, text)            | Returns true if the regex pattern matches the text, false otherwise.
8 match(pattern, text)            | Returns true if the wildcard pattern matches the text, false otherwise.
9 len(value)                      | Returns the length of the value, i.e. the number of elements for an array or dictionary, or the length of the string in bytes.
10 union(array, array, ...)        | Returns an array containing all unique elements from the specified arrays.
11 intersection(array, array, ...) | Returns an array containing all unique elements which are common to all specified arrays.
12 keys(dict)                      | Returns an array containing the dictionary's keys.
13 string(value)                   | Converts the value to a string.
14 number(value)                   | Converts the value to a number.
15 bool(value)                     | Converts the value to a bool.
16 random()                        | Returns a random value between 0 and RAND_MAX (as defined in stdlib.h).
17 log(value)                      | Writes a message to the log. Non-string values are converted to a JSON string.
18 log(severity, facility, value)  | Writes a message to the log. `severity` can be one of `LogDebug`, `LogNotice`, `LogInformation`, `LogWarning`, and `LogCritical`. Non-string values are converted to a JSON string.
19 exit(integer)                   | Terminates the application.
20
21 ## <a id="math-object"></a> Math object
22
23 The global `Math` object can be used to access a number of mathematical constants
24 and functions.
25
26 ### <a id="math-e"></a> Math.E
27
28 Euler's constant.
29
30 ### <a id="math-ln2"></a> Math.LN2
31
32 Natural logarithm of 2.
33
34 ### <a id="math-ln10"></a> Math.LN10
35
36 Natural logarithm of 10.
37
38 ### <a id="math-log2e"></a> Math.LOG2E
39
40 Base 2 logarithm of E.
41
42 ### <a id="math-pi"></a> Math.PI
43
44 The mathematical constant Pi.
45
46 ### <a id="math-sqrt1_2"></a> Math.SQRT1_2
47
48 Square root of 1/2.
49
50 ### <a id="math-sqrt2"></a> Math.SQRT2
51
52 Square root of 2.
53
54 ### <a id="math-abs"></a> Math.abs
55
56 Signature:
57
58     function abs(x);
59
60 Returns the absolute value of `x`.
61
62 ### <a id="math-acos"></a> Math.acos
63
64 Signature:
65
66     function acos(x);
67
68 Returns the arccosine of `x`.
69
70 ### <a id="math-asin"></a> Math.asin
71
72 Signature:
73
74     function asin(x);
75
76 Returns the arcsine of `x`.
77
78 ### <a id="math-atan"></a> Math.atan
79
80 Signature:
81
82     function atan(x);
83
84 Returns the arctangent of `x`.
85
86 ### <a id="math-atan2"></a> Math.atan2
87
88 Signature:
89
90     function atan2(y, x);
91
92 Returns the arctangent of the quotient of `y` and `x`.
93
94 ### <a id="math-ceil"></a> Math.ceil
95
96 Signature:
97
98     function ceil(x);
99
100 Returns the smallest integer value not less than `x`.
101
102 ### <a id="math-cos"></a> Math.cos
103
104 Signature:
105
106     function cos(x);
107
108 Returns the cosine of `x`.
109
110 ### <a id="math-exp"></a> Math.exp
111
112 Signature:
113
114     function exp(x);
115
116 Returns E raised to the `x`th power.
117
118 ### <a id="math-floor"></a> Math.floor
119
120 Signature:
121
122     function floor(x);
123
124 Returns the largest integer value not greater than `x`.
125
126 ### <a id="math-isinf"></a> Math.isinf
127
128 Signature:
129
130     function isinf(x);
131
132 Returns whether `x` is infinite.
133
134 ### <a id="math-isnan"></a> Math.isnan
135
136 Signature:
137
138     function isnan(x);
139
140 Returns whether `x` is NaN (not-a-number).
141
142 ### <a id="math-log"></a> Math.log
143
144 Signature:
145
146     function log(x);
147
148 Returns the natural logarithm of `x`.
149
150 ### <a id="math-max"></a> Math.max
151
152 Signature:
153
154     function max(...);
155
156 Returns the largest argument. A variable number of arguments can be specified.
157 If no arguments are given -Infinity is returned.
158
159 ### <a id="math-min"></a> Math.min
160
161 Signature:
162
163     function min(...);
164
165 Returns the smallest argument. A variable number of arguments can be specified.
166 If no arguments are given +Infinity is returned.
167
168 ### <a id="math-pow"></a> Math.pow
169
170 Signature:
171
172     function pow(x, y);
173
174 Returns `x` raised to the `y`th power.
175
176 ### <a id="math-random"></a> Math.random
177
178 Signature:
179
180     function random();
181
182 Returns a pseudo-random number between 0 and 1.
183
184 ### <a id="math-round"></a> Math.round
185
186 Signature:
187
188     function round(x);
189
190 Returns `x` rounded to the nearest integer value.
191
192 ### <a id="math-sign"></a> Math.sign
193
194 Signature:
195
196     function sign(x);
197
198 Returns -1 if `x` is negative, 1 if `x` is positive
199 and 0 if `x` is 0.
200
201 ### <a id="math-sin"></a> Math.sin
202
203 Signature:
204
205     function sin(x);
206
207 Returns the sine of `x`.
208
209 ### <a id="math-sqrt"></a> Math.sqrt
210
211 Signature:
212
213     function sqrt(x);
214
215 Returns the square root of `x`.
216
217 ### <a id="math-tan"></a> Math.tan
218
219 Signature:
220
221     function tan(x);
222
223 Returns the tangent of `x`.
224
225 ## <a id="number-type"></a> Number type
226
227 ### <a id="number-to_string"></a> Number#to_string
228
229 Signature:
230
231     function to_string();
232
233 The `to_string` method returns a string representation of the number.
234
235 Example:
236
237     var example = 7
238         example.to_string() /* Returns "7" */
239
240 ## <a id="boolean-type"></a> Boolean type
241
242 ### <a id="boolean-to_string"></a> Boolean#to_string
243
244 Signature:
245
246     function to_string();
247
248 The `to_string` method returns a string representation of the boolean value.
249
250 Example:
251
252     var example = true
253         example.to_string() /* Returns "true" */
254
255 ## <a id="string-type"></a> String type
256
257 ### <a id="string-find"></a> String#find
258
259 Signature:
260
261     function find(str, start);
262
263 Returns the zero-based index at which the string `str` was found in the string. If the string
264 was not found -1 is returned. `start` specifies the zero-based index at which `find` should
265 start looking for the string (defaults to 0 when not specified).
266
267 Example:
268
269     "Hello World".find("World") /* Returns 6 */
270
271 ### <a id="string-len"></a> String#len
272
273 Signature
274
275     function len();
276
277 Returns the length of the string in bytes. Note that depending on the encoding type of the string
278 this is not necessarily the number of characters.
279
280 Example:
281
282     "Hello World".len() /* Returns 11 */
283
284 ### <a id="string-lower"></a> String#lower
285
286 Signature:
287
288     function lower();
289
290 Returns a copy of the string with all of its characters converted to lower-case.
291
292 Example:
293
294     "Hello World".lower() /* Returns "hello world" */
295
296 ### <a id="string-upper"></a> String#upper
297
298 Signature:
299
300     function upper();
301
302 Returns a copy of the string with all of its characters converted to upper-case.
303
304 Example:
305
306     "Hello World".upper() /* Returns "HELLO WORLD" */
307
308 ### <a id="string-replace"></a> String#replace
309
310 Signature:
311
312     function replace(search, replacement);
313
314 Returns a copy of the string with all occurences of the string specified in `search` replaced
315 with the string specified in `replacement`.
316
317 ### <a id="string-split"></a> String#split
318
319 Signature:
320
321     function split(delimiters);
322
323 Splits a string into individual parts and returns them as an array. The `delimiters` argument
324 specifies the characters which should be used as delimiters between parts.
325
326 Example:
327
328     "x-7,y".split("-,") /* Returns [ "x", "7", "y" ] */
329
330 ### <a id="string-substr"></a> String#substr
331
332 Signature:
333
334     function substr(start, len);
335
336 Returns a part of a string. The `start` argument specifies the zero-based index at which the part begins.
337 The optional `len` argument specifies the length of the part ("until the end of the string" if omitted).
338
339 Example:
340
341     "Hello World".substr(6) /* Returns "World" */
342
343 ### <a id="string-to_string"></a> String#to_string
344
345 Signature:
346
347     function to_string();
348
349 Returns a copy of the string.
350
351 ## <a id="array-type"></a> Array type
352
353 ### <a id="array-add"></a> Array#add
354
355 Signature:
356
357     function add(value);
358
359 TODO
360
361 ### <a id="array-clear"></a> Array#clear
362
363 Signature:
364
365     function clear();
366
367 TODO
368
369 ### <a id="array-clone"></a> Array#clone
370
371     function clone();
372
373 TODO
374
375 ### <a id="array-contains"></a> Array#contains
376
377 Signature:
378
379     function contains(value);
380
381 TODO
382
383 ### <a id="array-len"></a> Array#len
384
385 Signature:
386
387     function len();
388
389 TODO
390
391 ### <a id="array-remove"></a> Array#remove
392
393 Signature:
394
395     function remove(index);
396
397 TODO
398
399 ### <a id="array-set"></a> Array#set
400
401 Signature:
402
403     function set(index, value);
404
405 TODO
406
407 ### <a id="array-sort"></a> Array#sort
408
409 Signature:
410
411     function sort(less_cmp);
412
413 Returns a copy of the array where all items are sorted. The items are
414 compared using the `<` (less-than) operator. A custom comparator function
415 can be specified with the `less_cmp` argument.
416
417 ## <a id="dictionary-type"></a> Dictionary type
418
419 ### <a id="dictionary-clone"></a> Dictionary#clone
420
421 Signature:
422
423     function clone();
424
425 TODO
426
427 ### <a id="dictionary-contains"></a> Dictionary#contains
428
429 Signature:
430
431     function contains(key);
432
433 TODO
434
435 ### <a id="dictionary-len"></a> Dictionary#len
436
437 Signature:
438
439     function len();
440
441 TODO
442
443 ### <a id="dictionary-remove"></a> Dictionary#remove
444
445 Signature:
446
447     function remove(key);
448
449 TODO
450
451 ### <a id="dictionary-set"></a> Dictionary#set
452
453 Signature:
454
455     function set(key, value);
456
457 TODO
458
459 ## <a id="scriptfunction-type"></a> Function type
460
461 ### <a id="scriptfunction-call"></a> Function#call
462
463 Signature:
464
465     function call(thisArg, ...);
466
467 Invokes the function using an alternative `this` scope. The `thisArg` argument specifies the `this`
468 scope for the function. All other arguments are passed directly to the function.
469
470 Example:
471
472     function set_x(val) {
473           this.x = val
474         }
475         
476         dict = {}
477         
478         set_x.call(dict, 7) /* Invokes set_x using `dict` as `this` */
479
480 ### <a id="scriptfunction-callv"></a> Function#callv
481
482 Signature:
483
484     function callv(thisArg, args);
485
486 Invokes the function using an alternative `this` scope. The `thisArg` argument specifies the `this`
487 scope for the function. The items in the `args` array are passed to the function as individual arguments.
488
489 Example:
490
491     function set_x(val) {
492           this.x = val
493         }
494         
495         var dict = {}
496
497         var args = [ 7 ]
498
499         set_x.callv(dict, args) /* Invokes set_x using `dict` as `this` */
500