stringUtil.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. 'use strict';
  2. export default {
  3. isEmptyString: function(str: string) {
  4. let rst = false;
  5. if (str === null || str === undefined) {
  6. rst = true;
  7. } else if (typeof str) {
  8. let reg = /^\s*$/;
  9. rst = reg.test(str);
  10. }
  11. return rst;
  12. },
  13. trim: function(str: string) {
  14. return str.replace(/(^\s*)|(\s*$)/g, "");
  15. },
  16. leftTrim: function(str: string) {
  17. return str.replace(/(^\s*)/g,"");
  18. },
  19. rightTrim: function(str: string) {
  20. return str.replace(/(\s*$)/g,"");
  21. },
  22. replaceAll: function (targetStr: string, FindText: string, RepText: string) {
  23. let regExp = new RegExp(FindText, "gm");
  24. return targetStr.replace(regExp, RepText);
  25. },
  26. comdify: function(numStr: string){
  27. let re = /\d{1,3}(?=(\d{3})+$)/g;
  28. return numStr.replace(/^(\d+)((\.\d+)?)$/,function(s,s1,s2){return s1.replace(re,"$&,")+s2;});
  29. },
  30. convertToCaptionNum: function(num: any, isCurrency: boolean, isTraditionalCap: boolean) {
  31. let me = this, rst = "";
  32. if (/^\d*(\.\d*)?$/.test(num)) {
  33. let capChars, unitChars;
  34. if (isTraditionalCap) {
  35. capChars = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
  36. unitChars = ["" , "拾", "佰", "仟", "萬", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万"];
  37. } else {
  38. capChars = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
  39. unitChars = ["" , "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千", "万"];
  40. }
  41. let numSplitArr = ("" + num).replace(/(^0*)/g, "").split(".");
  42. if (numSplitArr[0] === "") numSplitArr[0] = "0";
  43. let len = numSplitArr[0].length;
  44. let intPartArr = [];
  45. if (len <= 13) {
  46. for (let idx = 0; idx < len; idx++) {
  47. intPartArr.push(capChars[ parseInt(numSplitArr[0].charAt(idx) )] + unitChars[len - idx - 1]);
  48. }
  49. rst = intPartArr.join('');
  50. rst = me.replaceAll(rst, capChars[0] + unitChars[3], capChars[0]); //零千 -> 零
  51. rst = me.replaceAll(rst, capChars[0] + unitChars[2], capChars[0]); //零百 -> 零
  52. rst = me.replaceAll(rst, capChars[0] + unitChars[1], capChars[0]); //零十 -> 零
  53. //
  54. rst = me.replaceAll(me.replaceAll(rst, "零零", "零"), "零零", "零");
  55. rst = me.replaceAll(rst, capChars[0] + unitChars[8], unitChars[8]); //零亿 -> 亿
  56. rst = me.replaceAll(rst, capChars[0] + unitChars[4], unitChars[4]); //零万 -> 万
  57. //
  58. rst = me.replaceAll(rst, unitChars[8] + unitChars[4], unitChars[8] + capChars[0]); //亿万 -> 亿零
  59. if (num === 0) {
  60. rst = "零";
  61. } else if (rst.length > 1 && rst.charAt(rst.length - 1) === '零') {
  62. rst = rst.slice(0, rst.length - 1);
  63. }
  64. //小数部分处理
  65. if (numSplitArr.length > 1) {
  66. len = numSplitArr[1].length;
  67. if (parseInt(numSplitArr[1]) === 0) {
  68. rst = rst + (isCurrency?"元整":"");
  69. } else {
  70. if (isCurrency && len > 2) len = 2;
  71. let fractionStr = [];
  72. for (let idx = 0; idx < len; idx++) {
  73. fractionStr.push(capChars[ parseInt(numSplitArr[1].charAt(idx))]+ (isCurrency?((idx === 0)?"角":"分"):""));
  74. }
  75. rst = rst + (isCurrency?"元":"点") + fractionStr.join("");
  76. }
  77. } else {
  78. rst = rst + (isCurrency?"元整":"");
  79. }
  80. } else {
  81. rst = "Number is too big!";
  82. }
  83. } else {
  84. rst = "Number is wrong!";
  85. }
  86. return rst;
  87. },
  88. convertStrToBoolean: function(str: string) {
  89. let rst = false, me = this;
  90. if (!me.isEmptyString(str)) {
  91. let upperStr = str.toUpperCase();
  92. if (upperStr === 'T' || upperStr === 'Y' || upperStr === 'YES' || upperStr === 'TRUE') {
  93. rst = true;
  94. }
  95. }
  96. return rst;
  97. },
  98. formatNumber: function(formatStr: string, val: any) {
  99. let rst = val;
  100. if (formatStr) {
  101. if (!(isNaN(parseFloat(val)))) {
  102. let dotIdx = formatStr.indexOf(".");
  103. if (dotIdx >= 0) {
  104. let tmpStr = parseFloat(val).toFixed(formatStr.length - dotIdx - 1);
  105. let digStr = formatStr.substr(dotIdx + 1, formatStr.length - dotIdx);
  106. for (let sIdx = digStr.length - 1; sIdx >= 0; sIdx--) {
  107. if (digStr[sIdx] === '#') {
  108. if (tmpStr.length > 0 && tmpStr[tmpStr.length - 1] === '0') {
  109. tmpStr = tmpStr.substr(0, tmpStr.length - 1);
  110. } else {
  111. break;
  112. }
  113. } else {
  114. break;
  115. }
  116. }
  117. if (tmpStr[tmpStr.length - 1] === '.') tmpStr = tmpStr.substr(0, tmpStr.length - 1);
  118. rst = tmpStr;
  119. } else {
  120. rst = parseFloat(val).toFixed(0);
  121. }
  122. let commaIdx = formatStr.indexOf(",");
  123. if (commaIdx >= 0) {
  124. rst = comdify(val.toString());
  125. }
  126. }
  127. }
  128. return rst;
  129. },
  130. }
  131. function comdify(numStr: string){
  132. let re = /\d{1,3}(?=(\d{3})+$)/g;
  133. return numStr.replace(/^(\d+)((\.\d+)?)$/, function(s,s1,s2){return s1.replace(re,"$&,")+s2;});
  134. }