r8590: added server status utility functions for checking on the status of a task...
[jra/samba/.git] / source4 / scripting / libjs / base.js
1 /*
2         base js library functions
3         Copyright Andrew Tridgell 2005
4         released under the GNU GPL v2 or later
5 */
6
7 if (global["HAVE_BASE_JS"] != undefined) {
8    return;
9 }
10 HAVE_BASE_JS=1
11
12 /*
13   an essential function!
14 */
15 function printf()
16 {
17         print(vsprintf(arguments));
18 }
19
20 /*
21   helper function to setup a rpc io object, ready for input
22 */
23 function irpcObj()
24 {
25         var o = new Object();
26         o.input = new Object();
27         return o;
28 }
29
30 /*
31   check that a status result is OK
32 */
33 function check_status_ok(status)
34 {
35         if (status.is_ok != true) {
36                 printVars(status);
37         }
38         assert(status.is_ok == true);
39 }
40
41 /*
42   check that two arrays are equal
43 */
44 function check_array_equal(a1, a2)
45 {
46         assert(a1.length == a2.length);
47         for (i=0; i<a1.length; i++) {
48                 assert(a1[i] == a2[i]);
49         }
50 }
51
52 /*
53   check that an array is all zeros
54 */
55 function check_array_zero(a)
56 {
57         for (i=0; i<a.length; i++) {
58                 assert(a[i] == 0);
59         }
60 }
61
62 /*
63   substitute strings of the form ${NAME} in str, replacing
64   with substitutions from subobj
65 */
66 function substitute_var(str, subobj)
67 {
68         var list = split("${", str);
69         var i;
70         for (i=1;i<list.length;i++) {
71                 var list2 = split("}", list[i]);
72                 if (list2.length < 2) {
73                         return undefined;
74                 }
75                 var key = list2[0];
76                 var val;
77                 if (typeof(subobj[key]) == "undefined") {
78                         val = "${" + key + "}";
79                 } else if (typeof(subobj[key]) == "string") {
80                         val = subobj[key];
81                 } else {
82                         var fn = subobj[key];
83                         val = fn(key);
84                 }
85                 list2[0] = "" + val;
86                 list[i] = join("", list2);
87         }
88         return join("", list);
89 }
90
91 /*
92   return "s" if a number should be shown as plural
93 */
94 function plural(n)
95 {
96         if (n == 1) {
97                 return "";
98         }
99         return "s";
100 }