jpc_helper_common_output.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. 'use strict';
  2. const JV = require('../jpc_value_define');
  3. const JpcFieldHelper = require('./jpc_helper_field');
  4. const JpcCommonOutputHelper = {
  5. createCommonOutputWithoutDecorate: function(node, value, forceCombine) {
  6. const me = this;
  7. const rst = {};
  8. // 1. font/style/control
  9. rst[JV.PROP_FONT] = node[[JV.PROP_FONT]];
  10. rst[JV.PROP_CONTROL] = node[[JV.PROP_CONTROL]];
  11. rst[JV.PROP_STYLE] = node[[JV.PROP_STYLE]];
  12. // 2. value
  13. rst[JV.PROP_VALUE] = value;
  14. me.formatCell(node[JV.PROP_FORMAT], rst);
  15. // innerFormat(node[JV.PROP_FORMAT], rst);
  16. if (node[JV.PROP_PREFIX] && rst[JV.PROP_VALUE] !== null) {
  17. rst[JV.PROP_VALUE] = node[JV.PROP_PREFIX] + rst[JV.PROP_VALUE];
  18. } else if (node[JV.PROP_PREFIX] && forceCombine) {
  19. rst[JV.PROP_VALUE] = node[JV.PROP_PREFIX];
  20. }
  21. if (node[JV.PROP_SUFFIX] && rst[JV.PROP_VALUE] !== null) {
  22. rst[JV.PROP_VALUE] = rst[JV.PROP_VALUE] + node[JV.PROP_SUFFIX];
  23. } else if (node[JV.PROP_SUFFIX] && forceCombine) {
  24. rst[JV.PROP_VALUE] = node[JV.PROP_SUFFIX];
  25. }
  26. return rst;
  27. },
  28. createCommonOutput: function(node, value, controls) {
  29. const me = this;
  30. const rst = {};
  31. // 1. font/style/control
  32. rst[JV.PROP_FONT] = node[[JV.PROP_FONT]];
  33. rst[JV.PROP_CONTROL] = node[[JV.PROP_CONTROL]];
  34. rst[JV.PROP_STYLE] = node[[JV.PROP_STYLE]];
  35. // 2. value
  36. rst[JV.PROP_VALUE] = value;
  37. JpcFieldHelper.decorateValue(rst, controls);
  38. me.formatCell(node[JV.PROP_FORMAT], rst);
  39. // innerFormat(node[JV.PROP_FORMAT], rst);
  40. if (node[JV.PROP_PREFIX] && rst[JV.PROP_VALUE] !== null && rst[JV.PROP_VALUE] !== '') {
  41. rst[JV.PROP_VALUE] = node[JV.PROP_PREFIX] + rst[JV.PROP_VALUE];
  42. }
  43. if (node[JV.PROP_SUFFIX] && rst[JV.PROP_VALUE] !== null && rst[JV.PROP_VALUE] !== '') {
  44. rst[JV.PROP_VALUE] = rst[JV.PROP_VALUE] + node[JV.PROP_SUFFIX];
  45. }
  46. return rst;
  47. },
  48. formatCell: function(formatStr, rstCell) {
  49. if (formatStr) {
  50. if (!(isNaN(parseFloat(rstCell[JV.PROP_VALUE])))) {
  51. const dotIdx = formatStr.indexOf('.');
  52. if (dotIdx >= 0) {
  53. let tmpStr = parseFloat(rstCell[JV.PROP_VALUE]).toFixed(formatStr.length - dotIdx - 1);
  54. const digStr = formatStr.substr(dotIdx + 1, formatStr.length - dotIdx);
  55. for (let sIdx = digStr.length - 1; sIdx >= 0; sIdx--) {
  56. if (digStr[sIdx] === '#') {
  57. if (tmpStr.length > 0 && tmpStr[tmpStr.length - 1] === '0') {
  58. tmpStr = tmpStr.substr(0, tmpStr.length - 1);
  59. } else {
  60. break;
  61. }
  62. } else {
  63. break;
  64. }
  65. }
  66. if (tmpStr[tmpStr.length - 1] === '.') tmpStr = tmpStr.substr(0, tmpStr.length - 1);
  67. rstCell[JV.PROP_VALUE] = tmpStr;
  68. } else {
  69. rstCell[JV.PROP_VALUE] = parseFloat(rstCell[JV.PROP_VALUE]).toFixed(0);
  70. }
  71. const commaIdx = formatStr.indexOf(',');
  72. if (commaIdx >= 0) {
  73. rstCell[JV.PROP_VALUE] = comdify(rstCell[JV.PROP_VALUE].toString());
  74. }
  75. }
  76. }
  77. },
  78. };
  79. function comdify(numStr) {
  80. const re = /\d{1,3}(?=(\d{3})+$)/g;
  81. return numStr.replace(/^(\d+)((\.\d+)?)$/, function(s, s1, s2) { return s1.replace(re, '$&,') + s2; });
  82. }
  83. module.exports = JpcCommonOutputHelper;