Number.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import padEnd from 'lodash/padEnd';
  2. export default {
  3. name: 'AStatisticNumber',
  4. functional: true,
  5. render: function render(h, context) {
  6. var _context$props = context.props,
  7. value = _context$props.value,
  8. formatter = _context$props.formatter,
  9. precision = _context$props.precision,
  10. decimalSeparator = _context$props.decimalSeparator,
  11. _context$props$groupS = _context$props.groupSeparator,
  12. groupSeparator = _context$props$groupS === undefined ? '' : _context$props$groupS,
  13. prefixCls = _context$props.prefixCls;
  14. var valueNode = void 0;
  15. if (typeof formatter === 'function') {
  16. // Customize formatter
  17. valueNode = formatter({ value: value, h: h });
  18. } else {
  19. // Internal formatter
  20. var val = String(value);
  21. var cells = val.match(/^(-?)(\d*)(\.(\d+))?$/);
  22. // Process if illegal number
  23. if (!cells) {
  24. valueNode = val;
  25. } else {
  26. var negative = cells[1];
  27. var int = cells[2] || '0';
  28. var decimal = cells[4] || '';
  29. int = int.replace(/\B(?=(\d{3})+(?!\d))/g, groupSeparator);
  30. if (typeof precision === 'number') {
  31. decimal = padEnd(decimal, precision, '0').slice(0, precision);
  32. }
  33. if (decimal) {
  34. decimal = '' + decimalSeparator + decimal;
  35. }
  36. valueNode = [h(
  37. 'span',
  38. { key: 'int', 'class': prefixCls + '-content-value-int' },
  39. [negative, int]
  40. ), decimal && h(
  41. 'span',
  42. { key: 'decimal', 'class': prefixCls + '-content-value-decimal' },
  43. [decimal]
  44. )];
  45. }
  46. }
  47. return h(
  48. 'span',
  49. { 'class': prefixCls + '-content-value' },
  50. [valueNode]
  51. );
  52. }
  53. };