summaryrefslogtreecommitdiff
path: root/source4/scripting/libjs/base.js
diff options
context:
space:
mode:
Diffstat (limited to 'source4/scripting/libjs/base.js')
-rw-r--r--source4/scripting/libjs/base.js29
1 files changed, 29 insertions, 0 deletions
diff --git a/source4/scripting/libjs/base.js b/source4/scripting/libjs/base.js
index f5498789c5..c6b05755c4 100644
--- a/source4/scripting/libjs/base.js
+++ b/source4/scripting/libjs/base.js
@@ -50,3 +50,32 @@ function check_array_zero(a)
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]);
+ if (list2.length < 2) {
+ 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);
+}