base-convert.js 725 B

1234567891011121314151617181920212223242526
  1. var utils = require('../utils')
  2. , nodes = require('../nodes');
  3. /**
  4. * Return a `Literal` `num` converted to the provided `base`, padded to `width`
  5. * with zeroes (default width is 2)
  6. *
  7. * @param {Number} num
  8. * @param {Number} base
  9. * @param {Number} width
  10. * @return {Literal}
  11. * @api public
  12. */
  13. (module.exports = function(num, base, width) {
  14. utils.assertPresent(num, 'number');
  15. utils.assertPresent(base, 'base');
  16. num = utils.unwrap(num).nodes[0].val;
  17. base = utils.unwrap(base).nodes[0].val;
  18. width = (width && utils.unwrap(width).nodes[0].val) || 2;
  19. var result = Number(num).toString(base);
  20. while (result.length < width) {
  21. result = '0' + result;
  22. }
  23. return new nodes.Literal(result);
  24. }).raw = true;