s.js 881 B

12345678910111213141516171819202122232425262728293031323334353637
  1. var utils = require('../utils')
  2. , nodes = require('../nodes')
  3. , Compiler = require('../visitor/compiler');
  4. /**
  5. * Return a `Literal` with the given `fmt`, and
  6. * variable number of arguments.
  7. *
  8. * @param {String} fmt
  9. * @param {Node} ...
  10. * @return {Literal}
  11. * @api public
  12. */
  13. (module.exports = function s(fmt){
  14. fmt = utils.unwrap(fmt).nodes[0];
  15. utils.assertString(fmt);
  16. var self = this
  17. , str = fmt.string
  18. , args = arguments
  19. , i = 1;
  20. // format
  21. str = str.replace(/%(s|d)/g, function(_, specifier){
  22. var arg = args[i++] || nodes.null;
  23. switch (specifier) {
  24. case 's':
  25. return new Compiler(arg, self.options).compile();
  26. case 'd':
  27. arg = utils.unwrap(arg).first;
  28. if ('unit' != arg.nodeName) throw new Error('%d requires a unit');
  29. return arg.val;
  30. }
  31. });
  32. return new nodes.Literal(str);
  33. }).raw = true;