jpc_helper_common_output.js 4.0 KB

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