lfapi

diff lib/stringthesizer.js @ 0:ce6f95d23e1c

Initial checkin
author bsw
date Sat Sep 10 23:31:20 2011 +0200 (2011-09-10)
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/lib/stringthesizer.js	Sat Sep 10 23:31:20 2011 +0200
     1.3 @@ -0,0 +1,53 @@
     1.4 +function stringthesizer(options, struct) {
     1.5 +  // options:
     1.6 +  // nextPlaceholder (function)
     1.7 +  // valueSeparator (string)
     1.8 +  // coerce (function)
     1.9 +  var cmdParts = [];
    1.10 +  var args = [];
    1.11 +  var process = function(struct, skipCoercion) {
    1.12 +    if (struct instanceof Array) {
    1.13 +      var structIdx = 0;
    1.14 +      var next = function() { return struct[structIdx++]; };
    1.15 +      next().match(/[^?$]+|\?\??\??|\$\$?\$?/g).forEach(function(stringPart) {
    1.16 +        if (stringPart == "?") {
    1.17 +          cmdParts.push(options.nextPlaceholder(args.length));
    1.18 +          args.push(next())
    1.19 +        } else if (stringPart == "??") {
    1.20 +          var first = true;
    1.21 +          next().forEach(function(entry) {
    1.22 +            if (first) first = false;
    1.23 +            else cmdParts.push(options.valueSeparator);
    1.24 +            cmdParts.push(options.nextPlaceholder(args.length));
    1.25 +            args.push(entry)
    1.26 +          });
    1.27 +        } else if (stringPart == "???") {
    1.28 +          cmdParts.push("?");
    1.29 +        } else if (stringPart == "$") {
    1.30 +          process(next());
    1.31 +        } else if (stringPart == "$$") {
    1.32 +          var sep = next();
    1.33 +          var first = true;
    1.34 +          next().forEach(function(entry) {
    1.35 +            if (first) first = false;
    1.36 +            else cmdParts.push(sep);
    1.37 +            process(entry);
    1.38 +          });
    1.39 +        } else if (stringPart == "$$$") {
    1.40 +          cmdParts.push("$");
    1.41 +        } else {
    1.42 +          cmdParts.push(stringPart);
    1.43 +        }
    1.44 +      });
    1.45 +      if (structIdx != struct.length) { throw "Wrong argument count for stringthesizer"; }
    1.46 +    } else if (skipCoercion || typeof (struct) == 'string') {
    1.47 +      cmdParts.push(struct);
    1.48 +    } else {
    1.49 +      process(options.coerce(struct), true);
    1.50 +    }
    1.51 +  }
    1.52 +  process(struct);
    1.53 +  return { cmd: cmdParts.join(""), args: args };
    1.54 +}
    1.55 +
    1.56 +exports.stringthesizer = stringthesizer;
    1.57 \ No newline at end of file

Impressum / About Us