StringUtil.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. var MathUtil_1 = require("./MathUtil");
  4. var Config_1 = require("./Config");
  5. var StringUtil = /** @class */ (function () {
  6. function StringUtil() {
  7. }
  8. StringUtil.getCheckString = function (address, headInfo) {
  9. return "\u914D\u7F6E\u6587\u4EF6:".concat(headInfo.excelPath, ": ").concat(headInfo.sheetname, " :").concat(address);
  10. };
  11. StringUtil.getTsClassString = function (jsobj, filename, TStsr, headInfo) {
  12. var index = filename.indexOf('Object');
  13. if (index >= 0) {
  14. filename = filename.slice(0, index);
  15. }
  16. var s = "export class ".concat(filename);
  17. s += '\r\n{\r\n';
  18. for (var col = 0; col < headInfo.keys.length; col++) {
  19. var cur = headInfo.keys[col];
  20. if (cur.outputclient) {
  21. var many = '';
  22. if (cur.arrayNum == 2) {
  23. many = '[]';
  24. }
  25. else if (cur.arrayNum == 3) {
  26. many = '[][]';
  27. }
  28. s += " public ".concat(cur.name, ": ").concat(cur.type).concat(many, ";\r\n");
  29. }
  30. }
  31. s += '}\r\n';
  32. TStsr += s;
  33. return TStsr;
  34. };
  35. /**
  36. *
  37. * @param jsobj
  38. * @param filename
  39. * @param TStsr
  40. */
  41. StringUtil.getTsFileString = function (jsobj, filename, TStsr, headInfo) {
  42. var s = '';
  43. var headKeys = headInfo.keys.filter(function (val) { return val.outputclient; });
  44. for (var i = 0; i < jsobj.length; i++) {
  45. s += "\r\n ".concat(Config_1.Config.objectKey ? "'".concat(jsobj[i][Config_1.Config.objectKey], "':") : '', "{ ");
  46. var keys = Object.keys(jsobj[i]);
  47. for (var j = 0; j < keys.length; j++) {
  48. // s += '\r\n\t\t\t'
  49. var value = jsobj[i][keys[j]];
  50. var cur = headKeys[j];
  51. if (cur.type == 'string') {
  52. value = "'".concat(value, "'");
  53. }
  54. s += "".concat(keys[j], ": ").concat(value, ", ");
  55. }
  56. if (s.substr(s.length - 2, 2) == ', ') {
  57. s = s.substr(0, s.length - 2);
  58. }
  59. s += ' },';
  60. }
  61. if (Config_1.Config.objectKey) {
  62. s = s.substr(0, s.length - 1); // 单个对象为表去掉多余的数组逗号
  63. return "".concat(TStsr).concat(s);
  64. }
  65. var index = filename.indexOf('Object');
  66. if (index != -1) {
  67. filename = filename.slice(0, index);
  68. s = s.substr(0, s.length - 1); // 单个对象为表去掉多余的数组逗号
  69. return "".concat(TStsr, "\r\n public static ").concat(filename, ": ").concat(filename, " = ").concat(s, ";\r\n");
  70. }
  71. return "".concat(TStsr, "\r\n public static ").concat(filename, ": ").concat(filename, "[] = [").concat(s, "\r\n ];");
  72. };
  73. StringUtil.getTsFileString2 = function (jsobj, filename, TStsr, headInfo) {
  74. var ss = "import { Config } from './Config';";
  75. var s = '';
  76. var headKeys = headInfo.keys.filter(function (val) { return val.outputclient; });
  77. for (var i = 0; i < jsobj.length; i++) {
  78. s += '\r\n {';
  79. var keys = Object.keys(jsobj[i]);
  80. for (var j = 0; j < keys.length; j++) {
  81. var value = jsobj[i][keys[j]];
  82. var cur = headKeys[j];
  83. if (cur.type == 'string' || cur.type == 'String') {
  84. value = "'".concat(value, "'");
  85. }
  86. s += "".concat(keys[j], ": ").concat(value, ", ");
  87. }
  88. if (s.substr(s.length - 2, 2) == ', ') {
  89. s = s.substr(0, s.length - 2);
  90. }
  91. s += '},';
  92. }
  93. var index = filename.indexOf('Object');
  94. if (index != -1) {
  95. filename = filename.slice(0, index);
  96. s = s.substr(0, s.length - 1); // 单个对象为表去掉多余的数组逗号
  97. return "".concat(ss, "\r\n\r\nConfig.").concat(filename, " = ").concat(s, ";\r\n");
  98. }
  99. return "".concat(ss, "\r\n\r\nConfig.").concat(filename, " = [").concat(s, "\r\n];");
  100. };
  101. StringUtil.getTsFileString3 = function (jsobj, headInfo) {
  102. var headKeys = headInfo.keys.filter(function (val) { return val.outputclient; });
  103. for (var i = 0; i < jsobj.length; i++) {
  104. var keys = Object.keys(jsobj[i]);
  105. for (var j = 0; j < keys.length; j++) {
  106. var value = jsobj[i][keys[j]];
  107. var cur = headKeys[j];
  108. if (cur.isArray) {
  109. jsobj[i][keys[j]] = eval(value);
  110. }
  111. }
  112. }
  113. };
  114. StringUtil.getTsFileHead = function (filename, TStsr) {
  115. return "".concat(TStsr, "\r\n public static ").concat(filename, ": ").concat(filename, "[] = [];");
  116. };
  117. /**
  118. *
  119. * @param jsobj
  120. * @param filename
  121. * @param configData
  122. */
  123. StringUtil.getJsFileString = function (jsobj, filename, configData, headInfo) {
  124. var s = '';
  125. var headKeys = headInfo.keys.filter(function (val) { return val.outputclient; });
  126. for (var i = 0; i < jsobj.length; i++) {
  127. s += '\r\n\t\t{';
  128. var keys = Object.keys(jsobj[i]);
  129. for (var j = 0; j < keys.length; j++) {
  130. s += '\r\n\t\t\t';
  131. var value = jsobj[i][keys[j]];
  132. var cur = headKeys[j];
  133. if (cur.type == 'string') {
  134. value = "'".concat(value, "'");
  135. }
  136. s += "".concat(keys[j], ": ").concat(value, ",");
  137. }
  138. s += '\r\n\t\t},';
  139. }
  140. var index = filename.indexOf('Object');
  141. if (index != -1) {
  142. filename = filename.slice(0, index);
  143. return "".concat(configData, "\t").concat(filename, ":").concat(s, "\r\n");
  144. }
  145. return "".concat(configData, "\t").concat(filename, ": [").concat(s, "\r\n\t],\r\n");
  146. };
  147. StringUtil.getCsvFileString = function (csvobj) {
  148. var ret = '';
  149. for (var i = 0; i < csvobj.length; i++) {
  150. var arr = Object.values(csvobj[i]);
  151. for (var j = 0; j < arr.length; j++) {
  152. if (j == arr.length - 1) {
  153. ret = "".concat(ret + arr[j], "\n");
  154. }
  155. else {
  156. ret = "".concat(ret + arr[j], ",");
  157. }
  158. }
  159. }
  160. return ret;
  161. };
  162. StringUtil.getHsvFileString = function (csvobj) {
  163. var ret = '';
  164. for (var i = -1; i < csvobj.length; i++) {
  165. var arr = null;
  166. if (i == -1) {
  167. arr = Object.keys(csvobj[0]);
  168. }
  169. else {
  170. arr = Object.values(csvobj[i]);
  171. }
  172. for (var j = 0; j < arr.length; j++) {
  173. if (j == arr.length - 1) {
  174. ret = "".concat(ret + arr[j], "\n");
  175. }
  176. else {
  177. ret = "".concat(ret + arr[j], ",");
  178. }
  179. }
  180. }
  181. return ret;
  182. };
  183. StringUtil.replaceAll = function (text, oldString, newString) {
  184. return text.replace(new RegExp(oldString, 'g'), newString);
  185. };
  186. ;
  187. StringUtil.replaceDoubleMid = function (text, oldString, newStringCallback, checkIsNumber) {
  188. if (checkIsNumber === void 0) { checkIsNumber = false; }
  189. var globalReg = new RegExp("\\".concat(oldString, "[^\\").concat(oldString, "]+\\").concat(oldString), 'g');
  190. var reg = new RegExp("\\".concat(oldString, "[^\\").concat(oldString, "]+\\").concat(oldString));
  191. var arr = text.match(globalReg);
  192. if (arr) {
  193. for (var i = 0; i < arr.length; i++) {
  194. var mid = arr[i].substr(1, arr[i].length - 2);
  195. if (checkIsNumber) {
  196. if (MathUtil_1.default.isNumber(mid)) {
  197. var newString = newStringCallback(mid); //
  198. text = text.replace(reg, newString); // 从左到右逐个替换
  199. }
  200. else {
  201. // 报错
  202. }
  203. }
  204. else {
  205. var newString = newStringCallback(mid);
  206. text = text.replace(reg, newString); // 从左到右逐个替换
  207. }
  208. }
  209. }
  210. return text;
  211. };
  212. return StringUtil;
  213. }());
  214. exports.default = StringUtil;