'use strict'; const JV = require('../jpc_value_define'); const JpcFieldHelper = require('./jpc_helper_field'); const REG1 = new RegExp('\n\r', 'g'); const REG2 = new RegExp('\r\n', 'g'); const REG3 = new RegExp('\n', 'g'); const REG4 = new RegExp('\r', 'g'); const REG5 = new RegExp('\t', 'g'); const JpcCommonOutputHelper = { createCommonOutputWithoutDecorate: function(node, value, forceCombine) { const me = this; const rst = {}; // 1. font/style/control rst[JV.PROP_FONT] = node[[JV.PROP_FONT]]; rst[JV.PROP_CONTROL] = node[[JV.PROP_CONTROL]]; rst[JV.PROP_STYLE] = node[[JV.PROP_STYLE]]; // 2. value if (typeof value === 'string') { value = value.replace(REG1, '|').replace(REG2, '|').replace(REG3, '|').replace(REG4, '|').replace(REG5, ''); } rst[JV.PROP_VALUE] = value; me.formatCell(node[JV.PROP_FORMAT], rst); // innerFormat(node[JV.PROP_FORMAT], rst); if (node[JV.PROP_PREFIX] && rst[JV.PROP_VALUE] !== null) { rst[JV.PROP_VALUE] = node[JV.PROP_PREFIX] + rst[JV.PROP_VALUE]; } else if (node[JV.PROP_PREFIX] && forceCombine) { rst[JV.PROP_VALUE] = node[JV.PROP_PREFIX]; } if (node[JV.PROP_SUFFIX] && rst[JV.PROP_VALUE] !== null) { rst[JV.PROP_VALUE] = rst[JV.PROP_VALUE] + node[JV.PROP_SUFFIX]; } else if (node[JV.PROP_SUFFIX] && forceCombine) { rst[JV.PROP_VALUE] = node[JV.PROP_SUFFIX]; } return rst; }, createCommonOutput: function(node, value, controls) { const me = this; const rst = {}; // 1. font/style/control rst[JV.PROP_FONT] = node[[JV.PROP_FONT]]; rst[JV.PROP_CONTROL] = node[[JV.PROP_CONTROL]]; rst[JV.PROP_STYLE] = node[[JV.PROP_STYLE]]; // 2. value rst[JV.PROP_VALUE] = value; JpcFieldHelper.decorateValue(rst, controls); me.formatCell(node[JV.PROP_FORMAT], rst); // innerFormat(node[JV.PROP_FORMAT], rst); if (node[JV.PROP_PREFIX] && rst[JV.PROP_VALUE] !== null && rst[JV.PROP_VALUE] !== '') { rst[JV.PROP_VALUE] = node[JV.PROP_PREFIX] + rst[JV.PROP_VALUE]; } if (node[JV.PROP_SUFFIX] && rst[JV.PROP_VALUE] !== null && rst[JV.PROP_VALUE] !== '') { rst[JV.PROP_VALUE] = rst[JV.PROP_VALUE] + node[JV.PROP_SUFFIX]; } return rst; }, formatCell: function(formatStr, rstCell) { if (formatStr) { if (!(isNaN(parseFloat(rstCell[JV.PROP_VALUE])))) { const dotIdx = formatStr.indexOf('.'); if (dotIdx >= 0) { let tmpStr = parseFloat(rstCell[JV.PROP_VALUE]).toFixed(formatStr.length - dotIdx - 1); const digStr = formatStr.substr(dotIdx + 1, formatStr.length - dotIdx); for (let sIdx = digStr.length - 1; sIdx >= 0; sIdx--) { if (digStr[sIdx] === '#') { if (tmpStr.length > 0 && tmpStr[tmpStr.length - 1] === '0') { tmpStr = tmpStr.substr(0, tmpStr.length - 1); } else { break; } } else { break; } } if (tmpStr[tmpStr.length - 1] === '.') tmpStr = tmpStr.substr(0, tmpStr.length - 1); rstCell[JV.PROP_VALUE] = tmpStr; } else { rstCell[JV.PROP_VALUE] = parseFloat(rstCell[JV.PROP_VALUE]).toFixed(0); } const commaIdx = formatStr.indexOf(','); if (commaIdx >= 0) { rstCell[JV.PROP_VALUE] = comdify(rstCell[JV.PROP_VALUE].toString()); } } } }, }; function comdify(numStr) { const re = /\d{1,3}(?=(\d{3})+$)/g; return numStr.replace(/^(\d+)((\.\d+)?)$/, function(s, s1, s2) { return s1.replace(re, '$&,') + s2; }); } module.exports = JpcCommonOutputHelper;