format method

  1. @DefineMethod(name: '%', listArguments: true)
Quoted format (
  1. List<Node> args
)
@DefineMethod(name: '%', listArguments: true)

% format The function %(string, arguments ...) formats a string.

The first argument is string with placeholders. All placeholders start with percentage symbol % followed by letter s,S,d,D,a, or A. Remaining arguments contain expressions to replace placeholders.

Placeholders: d, D, a, A - can be replaced by any kind of argument (color, number, escaped value, expression, ...). If you use them in combination with string, the whole string will be used - including its quotes. However, the quotes are placed into the string as they are, they are not escaped by "/" nor anything similar. s, S - can be replaced by any kind of argument except color. If you use them in combination with string, only the string value will be used - string quotes are omitted. Parameters: string: format string with placeholders, anything* : values to replace placeholders. Returns: formatted string. Example: format-a-d: %("repetitions: %a file: %d", 1 + 2, "directory/file.less"); Output: format-a-d: "repetitions: 3 file: "directory/file.less"";

Implementation

@DefineMethod(name: '%', listArguments: true)
Quoted format(List<Node> args) {
  final qstr = args[0];
  String result = qstr.value;
  final sda = MoreRegExp(r'%[sda]', 'i');
  final az = RegExp(r'[A-Z]$', caseSensitive: true);

  String value;
  for (var i = 1; i < args.length; i++) {
    result = sda.replaceMap(result, (Match m) {
      value = (args[i] is Quoted && m[0].toLowerCase() == '%s')
          ? args[i].value
          : args[i].toCSS(context);
      return az.hasMatch(m[0]) ? Uri.encodeComponent(value) : value;
    });
  }

  result.replaceAll(RegExp(r'%%'), '%');
  return qstr is Quoted
      ? Quoted(qstr.quote ?? '', result,
          escaped: qstr.escaped,
          index: qstr.index,
          currentFileInfo: currentFileInfo)
      : Quoted('', result, escaped: null);

//2.4.0 20150331
//  '%': function (string /* arg, arg, ...*/) {
//      var args = Array.prototype.slice.call(arguments, 1),
//          result = string.value;
//
//      for (var i = 0; i < args.length; i++) {
//          /*jshint loopfunc:true */
//          result = result.replace(/%[sda]/i, function(token) {
//              var value = ((args[i].type === "Quoted") &&
//                  token.match(/s/i)) ? args[i].value : args[i].toCSS();
//              return token.match(/[A-Z]$/) ? encodeURIComponent(value) : value;
//          });
//      }
//      result = result.replace(/%%/g, '%');
//      return new Quoted(string.quote || '', result, string.escaped);
//  }
}