jpc_helper_common_output.js 4.0 KB

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