annotate lib/stringthesizer.js @ 33:25aba6a34c44
Added missing related objects, fixed smaller bugs,
author |
bsw |
date |
Sat May 19 13:58:50 2012 +0200 (2012-05-19) |
parents |
ce6f95d23e1c |
children |
|
rev |
line source |
bsw@0
|
1 function stringthesizer(options, struct) {
|
bsw@0
|
2 // options:
|
bsw@0
|
3 // nextPlaceholder (function)
|
bsw@0
|
4 // valueSeparator (string)
|
bsw@0
|
5 // coerce (function)
|
bsw@0
|
6 var cmdParts = [];
|
bsw@0
|
7 var args = [];
|
bsw@0
|
8 var process = function(struct, skipCoercion) {
|
bsw@0
|
9 if (struct instanceof Array) {
|
bsw@0
|
10 var structIdx = 0;
|
bsw@0
|
11 var next = function() { return struct[structIdx++]; };
|
bsw@0
|
12 next().match(/[^?$]+|\?\??\??|\$\$?\$?/g).forEach(function(stringPart) {
|
bsw@0
|
13 if (stringPart == "?") {
|
bsw@0
|
14 cmdParts.push(options.nextPlaceholder(args.length));
|
bsw@0
|
15 args.push(next())
|
bsw@0
|
16 } else if (stringPart == "??") {
|
bsw@0
|
17 var first = true;
|
bsw@0
|
18 next().forEach(function(entry) {
|
bsw@0
|
19 if (first) first = false;
|
bsw@0
|
20 else cmdParts.push(options.valueSeparator);
|
bsw@0
|
21 cmdParts.push(options.nextPlaceholder(args.length));
|
bsw@0
|
22 args.push(entry)
|
bsw@0
|
23 });
|
bsw@0
|
24 } else if (stringPart == "???") {
|
bsw@0
|
25 cmdParts.push("?");
|
bsw@0
|
26 } else if (stringPart == "$") {
|
bsw@0
|
27 process(next());
|
bsw@0
|
28 } else if (stringPart == "$$") {
|
bsw@0
|
29 var sep = next();
|
bsw@0
|
30 var first = true;
|
bsw@0
|
31 next().forEach(function(entry) {
|
bsw@0
|
32 if (first) first = false;
|
bsw@0
|
33 else cmdParts.push(sep);
|
bsw@0
|
34 process(entry);
|
bsw@0
|
35 });
|
bsw@0
|
36 } else if (stringPart == "$$$") {
|
bsw@0
|
37 cmdParts.push("$");
|
bsw@0
|
38 } else {
|
bsw@0
|
39 cmdParts.push(stringPart);
|
bsw@0
|
40 }
|
bsw@0
|
41 });
|
bsw@0
|
42 if (structIdx != struct.length) { throw "Wrong argument count for stringthesizer"; }
|
bsw@0
|
43 } else if (skipCoercion || typeof (struct) == 'string') {
|
bsw@0
|
44 cmdParts.push(struct);
|
bsw@0
|
45 } else {
|
bsw@0
|
46 process(options.coerce(struct), true);
|
bsw@0
|
47 }
|
bsw@0
|
48 }
|
bsw@0
|
49 process(struct);
|
bsw@0
|
50 return { cmd: cmdParts.join(""), args: args };
|
bsw@0
|
51 }
|
bsw@0
|
52
|
bsw@0
|
53 exports.stringthesizer = stringthesizer; |