ec0daab12adab62901685d46d833f7f56e8952bc.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. System.register(["cc"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, StringFormat, _crd, StringFormatFunction;
  4. return {
  5. setters: [function (_cc) {
  6. _cclegacy = _cc.cclegacy;
  7. }],
  8. execute: function () {
  9. _crd = true;
  10. _cclegacy._RF.push({}, "85fe8Gc6h5Ava+JsdbBs8cR", "StringFormat", undefined);
  11. /**
  12. * 数值格式化函数, 通过语义解析自动设置值的范围
  13. * //整数
  14. * 1:def(0)//显示一个默认值
  15. */
  16. StringFormat = class StringFormat {
  17. deal(value, format) {
  18. if (format === '') return value;
  19. format = format.toLowerCase().trim(); // 不区分大小
  20. var match_func = format.match(/^[a-z|A-Z]+/gi); // 匹配到 format 中的 函数名
  21. var match_num = format.match(/\d+$/gi); // 匹配到 format 中的参数
  22. var func = '';
  23. var num = 0;
  24. var res = '';
  25. if (match_func) func = match_func[0];
  26. if (match_num) num = parseInt(match_num[0]);
  27. if (typeof value == 'number') {
  28. switch (func) {
  29. case 'int':
  30. res = this.int(value);
  31. break;
  32. case 'fix':
  33. res = this.fix(value, num);
  34. break;
  35. case 'kmbt':
  36. res = this.KMBT(value);
  37. break;
  38. case 'per':
  39. res = this.per(value, num);
  40. break;
  41. case 'sep':
  42. res = this.sep(value);
  43. break;
  44. default:
  45. break;
  46. }
  47. } else {
  48. switch (func) {
  49. case 'limit':
  50. res = this.limit(value, num);
  51. break;
  52. default:
  53. break;
  54. }
  55. res = value;
  56. }
  57. return res;
  58. } // 将数字按分号显示
  59. sep(value) {
  60. var num = Math.round(value).toString();
  61. return num.replace(new RegExp('(\\d)(?=(\\d{3})+$)', 'ig'), "$1,");
  62. } // 将数字按分显示 00:00 显示 (ms制)
  63. time_m(value) {//todo
  64. } // 将数字按秒显示 00:00:00 显示 (ms制)
  65. time_s(value) {//todo
  66. } // 将数字按 0:00:00:000 显示 (ms制)
  67. time_ms(value) {//todo
  68. } // 将时间戳显示为详细的内容
  69. timeStamp(value) {
  70. //todo
  71. return new Date(value).toString();
  72. }
  73. /** [value:int] 将取值0~1 变成 1~100,可以指定修饰的小数位数 */
  74. per(value, fd) {
  75. return Math.round(value * 100).toFixed(fd);
  76. }
  77. /** [value:int] 将取值变成整数 */
  78. int(value) {
  79. return Math.round(value);
  80. }
  81. /** [value:fix2]数值转换为小数*/
  82. fix(value, fd) {
  83. return value.toFixed(fd);
  84. }
  85. /** [value:limit3]字符串长度限制 */
  86. limit(value, count) {
  87. return value.substring(0, count);
  88. }
  89. /** 将数字缩短显示为KMBT单位 大写,目前只支持英文 */
  90. KMBT(value, lang) {
  91. if (lang === void 0) {
  92. lang = 'en';
  93. }
  94. //10^4=万, 10^8=亿,10^12=兆,10^16=京,
  95. var counts = [1000, 1000000, 1000000000, 1000000000000];
  96. var units = ['', 'K', 'M', 'B', 'T'];
  97. switch (lang) {
  98. case 'zh':
  99. //10^4=万, 10^8=亿,10^12=兆,10^16=京,
  100. var _counts = [10000, 100000000, 1000000000000, 10000000000000000];
  101. var _units = ['', '万', '亿', '兆', '京'];
  102. break;
  103. default:
  104. break;
  105. }
  106. return this.compressUnit(value, counts, units, 2);
  107. } //压缩任意单位的数字,后缀加上单位文字
  108. compressUnit(value, valueArr, unitArr, fixNum) {
  109. if (fixNum === void 0) {
  110. fixNum = 2;
  111. }
  112. var counts = valueArr;
  113. var units = unitArr;
  114. var res = "";
  115. var index;
  116. for (index = 0; index < counts.length; index++) {
  117. var e = counts[index];
  118. if (value < e) {
  119. if (index > 0) {
  120. res = (value / counts[index - 1]).toFixed(fixNum);
  121. } else {
  122. res = value.toFixed(0);
  123. }
  124. break;
  125. }
  126. }
  127. return res + units[index];
  128. }
  129. };
  130. /**格式化处理函数 */
  131. _export("StringFormatFunction", StringFormatFunction = new StringFormat());
  132. _cclegacy._RF.pop();
  133. _crd = false;
  134. }
  135. };
  136. });
  137. //# sourceMappingURL=ec0daab12adab62901685d46d833f7f56e8952bc.js.map