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