'use strict'; export default { isEmptyString: function(str: string) { let rst = false; if (str === null || str === undefined) { rst = true; } else if (typeof str) { let reg = /^\s*$/; rst = reg.test(str); } return rst; }, trim: function(str: string) { return str.replace(/(^\s*)|(\s*$)/g, ""); }, leftTrim: function(str: string) { return str.replace(/(^\s*)/g,""); }, rightTrim: function(str: string) { return str.replace(/(\s*$)/g,""); }, replaceAll: function (targetStr: string, FindText: string, RepText: string) { let regExp = new RegExp(FindText, "gm"); return targetStr.replace(regExp, RepText); }, comdify: function(numStr: string){ let re = /\d{1,3}(?=(\d{3})+$)/g; return numStr.replace(/^(\d+)((\.\d+)?)$/,function(s,s1,s2){return s1.replace(re,"$&,")+s2;}); }, convertToCaptionNum: function(num: any, isCurrency: boolean, isTraditionalCap: boolean) { let me = this, rst = ""; if (/^\d*(\.\d*)?$/.test(num)) { let capChars, unitChars; if (isTraditionalCap) { capChars = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"]; unitChars = ["" , "拾", "佰", "仟", "萬", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万"]; } else { capChars = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"]; unitChars = ["" , "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千", "万"]; } let numSplitArr = ("" + num).replace(/(^0*)/g, "").split("."); if (numSplitArr[0] === "") numSplitArr[0] = "0"; let len = numSplitArr[0].length; let intPartArr = []; if (len <= 13) { for (let idx = 0; idx < len; idx++) { intPartArr.push(capChars[ parseInt(numSplitArr[0].charAt(idx) )] + unitChars[len - idx - 1]); } rst = intPartArr.join(''); rst = me.replaceAll(rst, capChars[0] + unitChars[3], capChars[0]); //零千 -> 零 rst = me.replaceAll(rst, capChars[0] + unitChars[2], capChars[0]); //零百 -> 零 rst = me.replaceAll(rst, capChars[0] + unitChars[1], capChars[0]); //零十 -> 零 // rst = me.replaceAll(me.replaceAll(rst, "零零", "零"), "零零", "零"); rst = me.replaceAll(rst, capChars[0] + unitChars[8], unitChars[8]); //零亿 -> 亿 rst = me.replaceAll(rst, capChars[0] + unitChars[4], unitChars[4]); //零万 -> 万 // rst = me.replaceAll(rst, unitChars[8] + unitChars[4], unitChars[8] + capChars[0]); //亿万 -> 亿零 if (num === 0) { rst = "零"; } else if (rst.length > 1 && rst.charAt(rst.length - 1) === '零') { rst = rst.slice(0, rst.length - 1); } //小数部分处理 if (numSplitArr.length > 1) { len = numSplitArr[1].length; if (parseInt(numSplitArr[1]) === 0) { rst = rst + (isCurrency?"元整":""); } else { if (isCurrency && len > 2) len = 2; let fractionStr = []; for (let idx = 0; idx < len; idx++) { fractionStr.push(capChars[ parseInt(numSplitArr[1].charAt(idx))]+ (isCurrency?((idx === 0)?"角":"分"):"")); } rst = rst + (isCurrency?"元":"点") + fractionStr.join(""); } } else { rst = rst + (isCurrency?"元整":""); } } else { rst = "Number is too big!"; } } else { rst = "Number is wrong!"; } return rst; }, convertStrToBoolean: function(str: string) { let rst = false, me = this; if (!me.isEmptyString(str)) { let upperStr = str.toUpperCase(); if (upperStr === 'T' || upperStr === 'Y' || upperStr === 'YES' || upperStr === 'TRUE') { rst = true; } } return rst; }, formatNumber: function(formatStr: string, val: any) { let rst = val; if (formatStr) { if (!(isNaN(parseFloat(val)))) { let dotIdx = formatStr.indexOf("."); if (dotIdx >= 0) { let tmpStr = parseFloat(val).toFixed(formatStr.length - dotIdx - 1); let 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); rst = tmpStr; } else { rst = parseFloat(val).toFixed(0); } let commaIdx = formatStr.indexOf(","); if (commaIdx >= 0) { rst = comdify(val.toString()); } } } return rst; }, } function comdify(numStr: string){ let re = /\d{1,3}(?=(\d{3})+$)/g; return numStr.replace(/^(\d+)((\.\d+)?)$/, function(s,s1,s2){return s1.replace(re,"$&,")+s2;}); }