Blame view

node_modules/stylus/lib/functions/base-convert.js 725 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
  var utils = require('../utils')
    , nodes = require('../nodes');
  
  /**
   * Return a `Literal` `num` converted to the provided `base`, padded to `width`
   * with zeroes (default width is 2)
   *
   * @param {Number} num
   * @param {Number} base
   * @param {Number} width
   * @return {Literal}
   * @api public
   */
  
  (module.exports = function(num, base, width) {
    utils.assertPresent(num, 'number');
    utils.assertPresent(base, 'base');
    num = utils.unwrap(num).nodes[0].val;
    base = utils.unwrap(base).nodes[0].val;
    width = (width && utils.unwrap(width).nodes[0].val) || 2;
    var result = Number(num).toString(base);
    while (result.length < width) {
      result = '0' + result;
    }
    return new nodes.Literal(result);
  }).raw = true;