1
0
mirror of https://github.com/samba-team/samba.git synced 2025-01-12 09:18:10 +03:00
samba-mirror/source4/scripting/libjs/base.js
Andrew Bartlett c642680e5b r17548: It is a good idea to commit the fix (from mkhl) before the test that
shows the need for...

Martin Kuhl writes:

The ejs function `substitute_var' returns `undefined' when the first
argument ends in a pattern that should be substituted.

For that reason, the second assertion fails in the following test-case:

,----
| libinclude("base.js");
|
| var obj = new Object();
| obj.FOO = "foo";
| obj.BAR = "bar";
| var str1 = "${FOO}:${BAR}";
| var str2 = "${FOO}:${BAR} "; // note the space after the brace
| var sub1 = substitute_var(str1, obj);
| var sub2 = substitute_var(str2, obj);
|
| assert(str1 + " " == str2);
| assert(sub1 + " " == sub2);
`----

The problem is that the function `split' returns a single-element
array in both cases:
a) the string to split doesn't contain the split pattern
b) the string ends with the split pattern

To work around this, the following patch tests this condition and
returns `undefined' only if the string to split (`list[i]') really
didn't contain a closing brace.
(This used to be commit 8a6908200b)
2007-10-10 14:15:37 -05:00

104 lines
1.7 KiB
JavaScript

/*
base js library functions
Copyright Andrew Tridgell 2005
released under the GNU GPL v2 or later
*/
if (global["HAVE_BASE_JS"] != undefined) {
return;
}
HAVE_BASE_JS=1
/* bring the string functions into the global frame */
string_init(global);
/*
an essential function!
*/
function printf()
{
print(vsprintf(arguments));
}
/*
helper function to setup a rpc io object, ready for input
*/
function irpcObj()
{
var o = new Object();
o.input = new Object();
return o;
}
/*
check that a status result is OK
*/
function check_status_ok(status)
{
if (status.is_ok != true) {
printVars(status);
}
assert(status.is_ok == true);
}
/*
check that two arrays are equal
*/
function check_array_equal(a1, a2)
{
assert(a1.length == a2.length);
for (i=0; i<a1.length; i++) {
assert(a1[i] == a2[i]);
}
}
/*
check that an array is all zeros
*/
function check_array_zero(a)
{
for (i=0; i<a.length; i++) {
assert(a[i] == 0);
}
}
/*
substitute strings of the form ${NAME} in str, replacing
with substitutions from subobj
*/
function substitute_var(str, subobj)
{
var list = split("${", str);
var i;
for (i=1;i<list.length;i++) {
var list2 = split("}", list[i], 1);
if ((list2.length < 2) && (list2[0] + "}" != list[i])) {
return undefined;
}
var key = list2[0];
var val;
if (typeof(subobj[key]) == "undefined") {
val = "${" + key + "}";
} else if (typeof(subobj[key]) == "string") {
val = subobj[key];
} else {
var fn = subobj[key];
val = fn(key);
}
list2[0] = "" + val;
list[i] = join("", list2);
}
return join("", list);
}
/*
return "s" if a number should be shown as plural
*/
function plural(n)
{
if (n == 1) {
return "";
}
return "s";
}