stringUtil.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import Pinyin from './pinyin';
  2. import { ReportDate } from './ReportDate'
  3. const pinyin = new Pinyin();
  4. const isEmptyString = (str: string) => {
  5. let rst = false;
  6. if (str === null || str === undefined) {
  7. rst = true;
  8. } else if (typeof str) {
  9. let reg = /^\s*$/;
  10. rst = reg.test(str);
  11. }
  12. return rst;
  13. }
  14. const trim = (str: string) => {
  15. return str.replace(/(^\s*)|(\s*$)/g, "");
  16. }
  17. const leftTrim = (str: string) => {
  18. return str.replace(/(^\s*)/g, "");
  19. }
  20. const rightTrim = (str: string) => {
  21. return str.replace(/(\s*$)/g, "");
  22. }
  23. const replaceAll = (targetStr: string, FindText: string, RepText: string) => {
  24. let regExp = new RegExp(FindText, "gm");
  25. return targetStr.replace(regExp, RepText);
  26. }
  27. const comdify = (numStr: string) => {
  28. let re = /\d{1,3}(?=(\d{3})+$)/g;
  29. return numStr.replace(/^(\d+)((\.\d+)?)$/, function (s, s1, s2) { return s1.replace(re, "$&,") + s2; });
  30. }
  31. const convertToCaptionNum = (num: any, isCurrency: boolean, isTraditionalCap: boolean) => {
  32. let rst = "";
  33. if (/^\d*(\.\d*)?$/.test(num)) {
  34. let capChars, unitChars;
  35. if (isTraditionalCap) {
  36. capChars = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
  37. unitChars = ["", "拾", "佰", "仟", "萬", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万"];
  38. } else {
  39. capChars = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
  40. unitChars = ["", "十", "百", "千", "万", "十", "百", "千", "亿", "十", "百", "千", "万"];
  41. }
  42. let numSplitArr = ("" + num).replace(/(^0*)/g, "").split(".");
  43. if (numSplitArr[0] === "") numSplitArr[0] = "0";
  44. let len = numSplitArr[0].length;
  45. let intPartArr = [];
  46. if (len <= 13) {
  47. for (let idx = 0; idx < len; idx++) {
  48. intPartArr.push(capChars[parseInt(numSplitArr[0].charAt(idx))] + unitChars[len - idx - 1]);
  49. }
  50. rst = intPartArr.join('');
  51. rst = replaceAll(rst, capChars[0] + unitChars[3], capChars[0]); //零千 -> 零
  52. rst = replaceAll(rst, capChars[0] + unitChars[2], capChars[0]); //零百 -> 零
  53. rst = replaceAll(rst, capChars[0] + unitChars[1], capChars[0]); //零十 -> 零
  54. //
  55. rst = replaceAll(replaceAll(rst, "零零", "零"), "零零", "零");
  56. rst = replaceAll(rst, capChars[0] + unitChars[8], unitChars[8]); //零亿 -> 亿
  57. rst = replaceAll(rst, capChars[0] + unitChars[4], unitChars[4]); //零万 -> 万
  58. //
  59. rst = replaceAll(rst, unitChars[8] + unitChars[4], unitChars[8] + capChars[0]); //亿万 -> 亿零
  60. if (num === 0) {
  61. rst = "零";
  62. } else if (rst.length > 1 && rst.charAt(rst.length - 1) === '零') {
  63. rst = rst.slice(0, rst.length - 1);
  64. }
  65. //小数部分处理
  66. if (numSplitArr.length > 1) {
  67. len = numSplitArr[1].length;
  68. if (parseInt(numSplitArr[1]) === 0) {
  69. rst = rst + (isCurrency ? "元整" : "");
  70. } else {
  71. if (isCurrency && len > 2) len = 2;
  72. let fractionStr = [];
  73. for (let idx = 0; idx < len; idx++) {
  74. fractionStr.push(capChars[parseInt(numSplitArr[1].charAt(idx))] + (isCurrency ? ((idx === 0) ? "角" : "分") : ""));
  75. }
  76. rst = rst + (isCurrency ? "元" : "点") + fractionStr.join("");
  77. }
  78. } else {
  79. rst = rst + (isCurrency ? "元整" : "");
  80. }
  81. } else {
  82. rst = "Number is too big!";
  83. }
  84. } else {
  85. rst = "Number is wrong!";
  86. }
  87. return rst;
  88. }
  89. const convertStrToBoolean = (str: string) => {
  90. let rst = false;
  91. if (!isEmptyString(str)) {
  92. let upperStr = str.toUpperCase();
  93. if (upperStr === 'T' || upperStr === 'Y' || upperStr === 'YES' || upperStr === 'TRUE') {
  94. rst = true;
  95. }
  96. }
  97. return rst;
  98. }
  99. const getPinYinFullChars=(value: string) =>{
  100. return pinyin.getFullChars(value);
  101. }
  102. const getPinYinCamelChars=(value: string)=> {
  103. return pinyin.getCamelChars(value);
  104. }
  105. const formatNumber = (formatStr: string, val: any) => {
  106. let rst = val;
  107. if (formatStr) {
  108. if (!(isNaN(parseFloat(val)))) {
  109. let dotIdx = formatStr.indexOf(".");
  110. if (dotIdx >= 0) {
  111. let tmpStr = parseFloat(val).toFixed(formatStr.length - dotIdx - 1);
  112. let digStr = formatStr.substr(dotIdx + 1, formatStr.length - dotIdx);
  113. for (let sIdx = digStr.length - 1; sIdx >= 0; sIdx--) {
  114. if (digStr[sIdx] === '#') {
  115. if (tmpStr.length > 0 && tmpStr[tmpStr.length - 1] === '0') {
  116. tmpStr = tmpStr.substr(0, tmpStr.length - 1);
  117. } else {
  118. break;
  119. }
  120. } else {
  121. break;
  122. }
  123. }
  124. if (tmpStr[tmpStr.length - 1] === '.') tmpStr = tmpStr.substr(0, tmpStr.length - 1);
  125. rst = tmpStr;
  126. } else {
  127. rst = parseFloat(val).toFixed(0);
  128. }
  129. let commaIdx = formatStr.indexOf(",");
  130. if (commaIdx >= 0) {
  131. rst = comdify(val.toString());
  132. }
  133. }
  134. }
  135. return rst;
  136. }
  137. const setupDateFormat=()=> {
  138. // 原本是直接向Date赋值的,但是现在ts不允许这么做,因此定义一个继承了Date的类使用
  139. const newReportDate = new ReportDate();
  140. Object.assign(Date, newReportDate);
  141. }
  142. export {
  143. isEmptyString,
  144. trim,
  145. leftTrim,
  146. rightTrim,
  147. replaceAll,
  148. comdify,
  149. convertToCaptionNum,
  150. convertStrToBoolean,
  151. formatNumber,
  152. getPinYinFullChars,
  153. getPinYinCamelChars,
  154. setupDateFormat
  155. }