Blame view

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