baja-lite 1.0.33 → 1.0.35

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/{cjs/boot-remote.d.ts → boot-remote.d.ts} +1 -1
  2. package/{es/boot-remote.js → boot-remote.js} +2 -2
  3. package/{es/boot.d.ts → boot.d.ts} +1 -1
  4. package/{es/boot.js → boot.js} +2 -2
  5. package/{es/enum.js → enum.js} +1 -1
  6. package/{es/fn.js → fn.js} +2 -2
  7. package/index.d.ts +10 -0
  8. package/index.js +10 -0
  9. package/package.json +11 -12
  10. package/{cjs/sql.d.ts → sql.d.ts} +3 -3
  11. package/{es/sql.js → sql.js} +7 -7
  12. package/{es/sqlite.d.ts → sqlite.d.ts} +1 -1
  13. package/{es/sqlite.js → sqlite.js} +1 -1
  14. package/{es/test-mysql.js → test-mysql.js} +2 -2
  15. package/{es/test-postgresql.js → test-postgresql.js} +2 -2
  16. package/cjs/boot-remote.js +0 -49
  17. package/cjs/boot.d.ts +0 -2
  18. package/cjs/boot.js +0 -183
  19. package/cjs/code.js +0 -379
  20. package/cjs/convert-xml.js +0 -417
  21. package/cjs/enum.js +0 -64
  22. package/cjs/error.js +0 -16
  23. package/cjs/fn.js +0 -179
  24. package/cjs/index.d.ts +0 -10
  25. package/cjs/index.js +0 -26
  26. package/cjs/list.js +0 -60
  27. package/cjs/math.js +0 -474
  28. package/cjs/object.js +0 -242
  29. package/cjs/set-ex.js +0 -345
  30. package/cjs/sql.js +0 -5083
  31. package/cjs/sqlite.d.ts +0 -32
  32. package/cjs/sqlite.js +0 -169
  33. package/cjs/string.js +0 -119
  34. package/cjs/test-mysql.js +0 -138
  35. package/cjs/test-postgresql.js +0 -93
  36. package/cjs/test-sqlite.js +0 -91
  37. package/cjs/test-xml.js +0 -75
  38. package/cjs/test.js +0 -4
  39. package/es/boot-remote.d.ts +0 -2
  40. package/es/code.d.ts +0 -2
  41. package/es/convert-xml.d.ts +0 -10
  42. package/es/enum.d.ts +0 -18
  43. package/es/error.d.ts +0 -5
  44. package/es/fn.d.ts +0 -128
  45. package/es/index.d.ts +0 -10
  46. package/es/index.js +0 -10
  47. package/es/list.d.ts +0 -10
  48. package/es/math.d.ts +0 -83
  49. package/es/object.d.ts +0 -83
  50. package/es/set-ex.d.ts +0 -198
  51. package/es/sql.d.ts +0 -1858
  52. package/es/string.d.ts +0 -17
  53. package/es/test-mysql.d.ts +0 -2
  54. package/es/test-postgresql.d.ts +0 -2
  55. package/es/test-sqlite.d.ts +0 -1
  56. package/es/test-xml.d.ts +0 -1
  57. package/es/test.d.ts +0 -1
  58. package/src/boot-remote.ts +0 -46
  59. package/src/boot.ts +0 -153
  60. package/src/code.ts +0 -377
  61. package/src/convert-xml.ts +0 -460
  62. package/src/enum.ts +0 -71
  63. package/src/error.ts +0 -11
  64. package/src/fn.ts +0 -295
  65. package/src/index.ts +0 -10
  66. package/src/list.ts +0 -57
  67. package/src/math.ts +0 -405
  68. package/src/object.ts +0 -247
  69. package/src/set-ex.ts +0 -374
  70. package/src/sql.ts +0 -5281
  71. package/src/sqlite.ts +0 -161
  72. package/src/string.ts +0 -111
  73. package/src/test-mysql.ts +0 -126
  74. package/src/test-postgresql.ts +0 -79
  75. package/src/test-sqlite.ts +0 -80
  76. package/src/test-xml.ts +0 -70
  77. package/src/test.ts +0 -2
  78. /package/{cjs/code.d.ts → code.d.ts} +0 -0
  79. /package/{es/code.js → code.js} +0 -0
  80. /package/{cjs/convert-xml.d.ts → convert-xml.d.ts} +0 -0
  81. /package/{es/convert-xml.js → convert-xml.js} +0 -0
  82. /package/{cjs/enum.d.ts → enum.d.ts} +0 -0
  83. /package/{cjs/error.d.ts → error.d.ts} +0 -0
  84. /package/{es/error.js → error.js} +0 -0
  85. /package/{cjs/fn.d.ts → fn.d.ts} +0 -0
  86. /package/{cjs/list.d.ts → list.d.ts} +0 -0
  87. /package/{es/list.js → list.js} +0 -0
  88. /package/{cjs/math.d.ts → math.d.ts} +0 -0
  89. /package/{es/math.js → math.js} +0 -0
  90. /package/{cjs/object.d.ts → object.d.ts} +0 -0
  91. /package/{es/object.js → object.js} +0 -0
  92. /package/{cjs/set-ex.d.ts → set-ex.d.ts} +0 -0
  93. /package/{es/set-ex.js → set-ex.js} +0 -0
  94. /package/{cjs/string.d.ts → string.d.ts} +0 -0
  95. /package/{es/string.js → string.js} +0 -0
  96. /package/{cjs/test-mysql.d.ts → test-mysql.d.ts} +0 -0
  97. /package/{cjs/test-postgresql.d.ts → test-postgresql.d.ts} +0 -0
  98. /package/{cjs/test-sqlite.d.ts → test-sqlite.d.ts} +0 -0
  99. /package/{es/test-sqlite.js → test-sqlite.js} +0 -0
  100. /package/{cjs/test-xml.d.ts → test-xml.d.ts} +0 -0
  101. /package/{es/test-xml.js → test-xml.js} +0 -0
  102. /package/{cjs/test.d.ts → test.d.ts} +0 -0
  103. /package/{es/test.js → test.js} +0 -0
@@ -1,417 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.convert = void 0;
7
- const lodash_get_1 = __importDefault(require("lodash.get"));
8
- const convert = function (childrens, param, parentIds, myBatisMapper) {
9
- let statement = '';
10
- for (let i = 0, children; children = childrens[i]; i++) {
11
- // Convert SQL statement recursively
12
- statement += convertChildren(children, param, parentIds, myBatisMapper);
13
- }
14
- // Check not converted Parameters
15
- const regexList = ['\\#{\\S*}', '\\${\\S*}'];
16
- for (let i = 0, regexString; regexString = regexList[i]; i++) {
17
- var checkParam = statement.match(regexString);
18
- if (checkParam != null && checkParam.length > 0) {
19
- throw new Error("Parameter " + checkParam.join(",") + " is not converted.");
20
- }
21
- }
22
- return statement;
23
- };
24
- exports.convert = convert;
25
- const convertChildren = function (children, param, parentIds, myBatisMapper) {
26
- param ?? (param = {});
27
- if (!isDict(param)) {
28
- throw new Error('Parameter argument should be Key-Value type or Null.');
29
- }
30
- if (children.type == 'text') {
31
- // Convert Parameters
32
- return convertParameters(children, param);
33
- }
34
- else if (children.type == 'tag') {
35
- switch (children.name.toLowerCase()) {
36
- case 'if':
37
- return convertIf(children, param, parentIds, myBatisMapper);
38
- case 'choose':
39
- return convertChoose(children, param, parentIds, myBatisMapper);
40
- case 'trim':
41
- case 'where':
42
- return convertTrimWhere(children, param, parentIds, myBatisMapper);
43
- case 'set':
44
- return convertSet(children, param, parentIds, myBatisMapper);
45
- case 'foreach':
46
- return convertForeach(children, param, parentIds, myBatisMapper);
47
- case 'bind':
48
- param = convertBind(children, param);
49
- return '';
50
- case 'include':
51
- return convertInclude(children, param, parentIds, myBatisMapper);
52
- default:
53
- throw new Error('XML is not well-formed character or markup. Consider using CDATA section.');
54
- }
55
- }
56
- else {
57
- return '';
58
- }
59
- };
60
- const convertParameters = function (children, param) {
61
- let convertString = children.content;
62
- try {
63
- convertString = convertParametersInner('#', convertString, param);
64
- convertString = convertParametersInner('$', convertString, param);
65
- }
66
- catch (err) {
67
- throw new Error('Error occurred during convert parameters.');
68
- }
69
- try {
70
- // convert CDATA string
71
- convertString = convertString.replace(/(\&amp\;)/g, '&');
72
- convertString = convertString.replace(/(\&lt\;)/g, '<');
73
- convertString = convertString.replace(/(\&gt\;)/g, '>');
74
- convertString = convertString.replace(/(\&quot\;)/g, '"');
75
- }
76
- catch (err) {
77
- throw new Error('Error occurred during convert CDATA section.');
78
- }
79
- return convertString;
80
- };
81
- const isObject = function (variable) {
82
- return typeof variable === 'object' && variable !== null;
83
- };
84
- const isArray = function (variable) {
85
- return isObject(variable) && variable.hasOwnProperty('length');
86
- };
87
- const convertParametersInner = function (change, convertString, param) {
88
- const stringReg = new RegExp('(\\' + change + '\\{[a-zA-Z0-9._\\$]+\\})', 'g');
89
- let stringTarget = convertString.match(stringReg);
90
- if (stringTarget != null && stringTarget.length > 0) {
91
- const _stringTarget = uniqueArray(stringTarget);
92
- let target;
93
- for (let i = 0; i < _stringTarget.length; i++) {
94
- target = _stringTarget[i];
95
- const t = target.replace(change + '{', '').replace('}', '');
96
- let tempParamKey = (0, lodash_get_1.default)(param, t);
97
- if (tempParamKey !== undefined) {
98
- const reg = new RegExp('\\' + change + '{' + t + '}', 'g');
99
- if (tempParamKey === null) {
100
- tempParamKey = 'NULL';
101
- convertString = convertString.replace(reg, tempParamKey);
102
- }
103
- else {
104
- if (change == '#') {
105
- // processing JSON fields structures
106
- if (isObject(tempParamKey) || isArray(tempParamKey)) {
107
- tempParamKey = JSON.stringify(tempParamKey);
108
- }
109
- else {
110
- tempParamKey = tempParamKey.toString().replace(/"/g, '\\\"');
111
- tempParamKey = mysqlRealEscapeParam(tempParamKey);
112
- }
113
- tempParamKey = tempParamKey.replace(/'/g, "''");
114
- const replaceWith = "'" + tempParamKey + "'";
115
- convertString = convertString.replace(reg, () => replaceWith);
116
- }
117
- else if (change == '$') {
118
- convertString = convertString.replace(reg, tempParamKey);
119
- }
120
- }
121
- }
122
- }
123
- }
124
- return convertString;
125
- };
126
- const convertIf = function (children, param, parentIds, myBatisMapper) {
127
- let evalString = children.attrs['test'];
128
- try {
129
- // Create Evaluate string
130
- evalString = replaceEvalString(evalString, param);
131
- evalString = evalString.replace(/ and /gi, ' && ');
132
- evalString = evalString.replace(/ or /gi, ' || ');
133
- // replace == to === for strict evaluate
134
- // TODO: fix != null & != ''
135
- // evalString = evalString.replace(/==/g, '===');
136
- // evalString = evalString.replace(/!=/g, '!==');
137
- evalString = evalString.replace(/^'(.*?)'\.equalsIgnoreCase\( ([a-zA-Z]+\.[a-zA-Z]+) \)/i, `($2 && $2.toUpperCase() === '$1'.toUpperCase())`);
138
- evalString = evalString.replace(/\('(.*?)'\.equalsIgnoreCase\( ([a-zA-Z]+\.[a-zA-Z]+) \)/i, `(($2 && $2.toUpperCase() === '$1'.toUpperCase())`);
139
- }
140
- catch (err) {
141
- throw new Error('Error occurred during convert <if> element.');
142
- }
143
- // Execute Evaluate string
144
- try {
145
- if (eval(evalString)) {
146
- let convertString = '';
147
- for (let i = 0, nextChildren; nextChildren = children['children'][i]; i++) {
148
- convertString += convertChildren(nextChildren, param, parentIds, myBatisMapper);
149
- }
150
- return convertString;
151
- }
152
- else {
153
- return '';
154
- }
155
- }
156
- catch (e) {
157
- return '';
158
- }
159
- };
160
- const convertForeach = function (children, param, parentIds, myBatisMapper) {
161
- try {
162
- const collection = eval('param.' + children.attrs['collection']);
163
- const item = children.attrs['item'];
164
- const open = (children.attrs['open'] == null) ? '' : children.attrs['open'];
165
- const close = (children.attrs['close'] == null) ? '' : children.attrs['close'];
166
- const separator = (children.attrs['separator'] == null) ? '' : children.attrs['separator'];
167
- const foreachTexts = [];
168
- let coll = null;
169
- for (let j = 0; j < collection.length; j++) {
170
- coll = collection[j];
171
- const foreachParam = param;
172
- foreachParam[item] = coll;
173
- let foreachText = '';
174
- for (let k = 0, nextChildren; nextChildren = children.children[k]; k++) {
175
- let fText = convertChildren(nextChildren, foreachParam, parentIds, myBatisMapper);
176
- fText = fText.replace(/^\s*$/g, '');
177
- if (fText != null && fText.length > 0) {
178
- foreachText += fText;
179
- }
180
- }
181
- if (foreachText != null && foreachText.length > 0) {
182
- foreachTexts.push(foreachText);
183
- }
184
- }
185
- return (open + foreachTexts.join(separator) + close);
186
- }
187
- catch (err) {
188
- throw new Error('Error occurred during convert <foreach> element.');
189
- }
190
- };
191
- const convertChoose = function (children, param, parentIds, myBatisMapper) {
192
- try {
193
- for (let i = 0, whenChildren; whenChildren = children.children[i]; i++) {
194
- if (whenChildren.type == 'tag' && whenChildren.name.toLowerCase() == 'when') {
195
- let evalString = whenChildren.attrs.test;
196
- // Create Evaluate string
197
- evalString = replaceEvalString(evalString, param);
198
- evalString = evalString.replace(/ and /gi, ' && ');
199
- evalString = evalString.replace(/ or /gi, ' || ');
200
- // Execute Evaluate string
201
- try {
202
- if (eval(evalString)) {
203
- // If <when> condition is true, do it.
204
- let convertString = '';
205
- for (let k = 0, nextChildren; nextChildren = whenChildren.children[k]; k++) {
206
- convertString += convertChildren(nextChildren, param, parentIds, myBatisMapper);
207
- }
208
- return convertString;
209
- }
210
- else {
211
- continue;
212
- }
213
- }
214
- catch (e) {
215
- continue;
216
- }
217
- }
218
- else if (whenChildren.type == 'tag' && whenChildren.name.toLowerCase() == 'otherwise') {
219
- // If reached <otherwise> tag, do it.
220
- let convertString = '';
221
- for (let k = 0, nextChildren; nextChildren = whenChildren.children[k]; k++) {
222
- convertString += convertChildren(nextChildren, param, parentIds, myBatisMapper);
223
- }
224
- return convertString;
225
- }
226
- }
227
- // If there is no suitable when and otherwise, just return null.
228
- return '';
229
- }
230
- catch (err) {
231
- throw new Error('Error occurred during convert <choose> element.');
232
- }
233
- };
234
- const convertTrimWhere = function (children, param, parentIds, myBatisMapper) {
235
- let convertString = '';
236
- let prefix;
237
- let prefixOverrides;
238
- let suffix;
239
- let suffixOverrides;
240
- let globalSet;
241
- try {
242
- switch (children.name.toLowerCase()) {
243
- case 'trim':
244
- prefix = children.attrs["prefix"];
245
- prefixOverrides = children.attrs["prefixOverrides"];
246
- suffix = children.attrs["suffix"];
247
- suffixOverrides = children.attrs["suffixOverrides"];
248
- globalSet = 'g';
249
- break;
250
- case 'where':
251
- prefix = 'WHERE';
252
- prefixOverrides = 'and|or';
253
- globalSet = 'gi';
254
- break;
255
- default:
256
- throw new Error('Error occurred during convert <trim/where> element.');
257
- }
258
- // Convert children first.
259
- for (let j = 0, nextChildren; nextChildren = children.children[j]; j++) {
260
- convertString += convertChildren(nextChildren, param, parentIds, myBatisMapper);
261
- }
262
- // Remove prefixOverrides
263
- let trimRegex = new RegExp('(^)([\\s]*?)(' + prefixOverrides + ')', globalSet);
264
- convertString = convertString.replace(trimRegex, '');
265
- // Remove suffixOverrides
266
- trimRegex = new RegExp('(' + suffixOverrides + ')([\\s]*?)($)', globalSet);
267
- convertString = convertString.replace(trimRegex, '');
268
- if (children.name.toLowerCase() != 'trim') {
269
- trimRegex = new RegExp('(' + prefixOverrides + ')([\\s]*?)($)', globalSet);
270
- convertString = convertString.replace(trimRegex, '');
271
- }
272
- // Add Prefix if String is not empty.
273
- trimRegex = new RegExp('([a-zA-Z])', 'g');
274
- const w = convertString.match(trimRegex);
275
- if (w != null && w.length > 0) {
276
- convertString = prefix + ' ' + convertString;
277
- if (suffix) {
278
- convertString = convertString + ' ' + suffix;
279
- }
280
- }
281
- // Remove comma(,) before WHERE
282
- if (children.name.toLowerCase() != 'where') {
283
- const regex = new RegExp('(,)([\\s]*?)(where)', 'gi');
284
- convertString = convertString.replace(regex, ' WHERE ');
285
- }
286
- return convertString;
287
- }
288
- catch (err) {
289
- throw new Error('Error occurred during convert <' + children.name.toLowerCase() + '> element.');
290
- }
291
- };
292
- const convertSet = function (children, param, parentIds, myBatisMapper) {
293
- let convertString = '';
294
- try {
295
- // Convert children first.
296
- for (let j = 0, nextChildren; nextChildren = children.children[j]; j++) {
297
- convertString += convertChildren(nextChildren, param, parentIds, myBatisMapper);
298
- }
299
- // Remove comma repeated more than 2.
300
- let regex = new RegExp('(,)(,|\\s){2,}', 'g');
301
- convertString = convertString.replace(regex, ',\n');
302
- // Remove first comma if exists.
303
- regex = new RegExp('(^)([\\s]*?)(,)', 'g');
304
- convertString = convertString.replace(regex, '');
305
- // Remove last comma if exists.
306
- regex = new RegExp('(,)([\\s]*?)($)', 'g');
307
- convertString = convertString.replace(regex, '');
308
- convertString = ' SET ' + convertString;
309
- return convertString;
310
- }
311
- catch (err) {
312
- throw new Error('Error occurred during convert <set> element.');
313
- }
314
- };
315
- const convertBind = function (children, param) {
316
- let evalString = children.attrs["value"];
317
- // Create Evaluate string
318
- evalString = replaceEvalString(evalString, param);
319
- param[children.attrs["name"]] = eval(evalString);
320
- return param;
321
- };
322
- const convertInclude = function (children, param, parentIds, myBatisMapper) {
323
- try {
324
- // Add Properties to param
325
- for (let j = 0, nextChildren; nextChildren = children.children[j]; j++) {
326
- if (nextChildren.type == 'tag' && nextChildren.name == 'property') {
327
- param[nextChildren.attrs['name']] = nextChildren.attrs['value'];
328
- }
329
- }
330
- }
331
- catch (err) {
332
- throw new Error('Error occurred during read <property> element in <include> element.');
333
- }
334
- try {
335
- let refid = convertParametersInner('#', children.attrs['refid'], param);
336
- refid = convertParametersInner('$', refid, param);
337
- let mapper;
338
- for (const psqlid of parentIds) {
339
- mapper = myBatisMapper[`${psqlid}.${refid}`];
340
- if (mapper) {
341
- break;
342
- }
343
- }
344
- let statement = '';
345
- for (let i = 0, children; children = mapper[i]; i++) {
346
- statement += convertChildren(children, param, parentIds, myBatisMapper);
347
- }
348
- return statement;
349
- }
350
- catch (err) {
351
- throw new Error('Error occurred during convert refid attribute in <include> element.');
352
- }
353
- };
354
- const isDict = function (v) {
355
- return typeof v === 'object' && v !== null && !(v instanceof Array) && !(v instanceof Date);
356
- };
357
- const replaceEvalString = function (evalString, param) {
358
- const keys = Object.keys(param);
359
- for (let i = 0; i < keys.length; i++) {
360
- let replacePrefix = '';
361
- let replacePostfix = '';
362
- let paramRegex;
363
- if (isDict(param[keys[i]])) {
364
- replacePrefix = ' param.';
365
- replacePostfix = '';
366
- paramRegex = new RegExp('(^|[^a-zA-Z0-9_])(' + keys[i] + '\\.)([a-zA-Z0-9_]+)', 'g');
367
- }
368
- else {
369
- replacePrefix = ' param.';
370
- replacePostfix = ' ';
371
- paramRegex = new RegExp('(^|[^a-zA-Z0-9_])(' + keys[i] + ')($|[^a-zA-Z0-9_])', 'g');
372
- }
373
- evalString = evalString.replace(paramRegex, ('$1' + replacePrefix + '$2' + replacePostfix + '$3'));
374
- }
375
- return evalString;
376
- };
377
- const uniqueArray = function (a) {
378
- const seen = {};
379
- const out = [];
380
- const len = a.length;
381
- let j = 0;
382
- for (let i = 0; i < len; i++) {
383
- const item = a[i];
384
- if (seen[item] !== 1) {
385
- seen[item] = 1;
386
- out[j++] = item;
387
- }
388
- }
389
- return out;
390
- };
391
- const mysqlRealEscapeParam = function (param) {
392
- if (typeof param != 'string')
393
- return param;
394
- return param.replace(/[\0\x08\x09\x1a\n\r''\\\%]/g, function (char) {
395
- switch (char) {
396
- case '\0':
397
- return '\\0';
398
- case '\x08':
399
- return '\\b';
400
- case '\x09':
401
- return '\\t';
402
- case '\x1a':
403
- return '\\z';
404
- case '\n':
405
- return '\\n';
406
- case '\r':
407
- return '\\r';
408
- case '\'':
409
- case `'`:
410
- case '\\':
411
- case '%':
412
- return '\\' + char;
413
- default:
414
- return char;
415
- }
416
- });
417
- };
package/cjs/enum.js DELETED
@@ -1,64 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getEnums = exports.Enum = void 0;
4
- const sql_1 = require("./sql");
5
- class Enum {
6
- constructor(value, desc, ...config) {
7
- this._value = value;
8
- this._desc = desc;
9
- this._config = config;
10
- }
11
- eq(value) {
12
- if (value === undefined) {
13
- return false;
14
- }
15
- if (value === null) {
16
- return false;
17
- }
18
- if (typeof value === 'number') {
19
- return this._value === `${value}`;
20
- }
21
- return this._value === `${value}`;
22
- }
23
- value() {
24
- return this._value;
25
- }
26
- desc() {
27
- return this._desc;
28
- }
29
- config() {
30
- return this._config;
31
- }
32
- }
33
- exports.Enum = Enum;
34
- let configData = null;
35
- const getEnums = (GlobalValues) => {
36
- if (!GlobalValues) {
37
- return globalThis[sql_1._enums];
38
- }
39
- if (configData) {
40
- return configData;
41
- }
42
- const result = {
43
- GlobalArray: {},
44
- GlobalMap: {}
45
- };
46
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
47
- Object.keys(GlobalValues).forEach((item) => {
48
- // const guess = /([\w\W]+)_([^_]+)$/.exec(item);
49
- const guess = item.replace(new RegExp(`_${GlobalValues[item].value()}`, 'i'), '');
50
- if (guess) {
51
- if (!result.GlobalArray[guess]) {
52
- result.GlobalArray[guess] = [];
53
- }
54
- result.GlobalArray[guess].push([GlobalValues[item].value(), GlobalValues[item].desc()]);
55
- if (!result.GlobalMap[guess]) {
56
- result.GlobalMap[guess] = {};
57
- }
58
- result.GlobalMap[guess][GlobalValues[item].value()] = GlobalValues[item].desc();
59
- }
60
- });
61
- configData = result;
62
- return result;
63
- };
64
- exports.getEnums = getEnums;
package/cjs/error.js DELETED
@@ -1,16 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Throw = void 0;
4
- exports.Throw = {
5
- if(test, message) {
6
- if (test === true)
7
- this.now(message);
8
- },
9
- ifNot(test, message) {
10
- if (test !== true)
11
- this.now(message);
12
- },
13
- now(message) {
14
- throw typeof message === 'string' ? new Error(message) : message;
15
- }
16
- };
package/cjs/fn.js DELETED
@@ -1,179 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ExcuteSplitMode = exports.sleep = exports.promise = void 0;
4
- exports.dieTrying = dieTrying;
5
- exports.excuteSplit = excuteSplit;
6
- const object_1 = require("./object");
7
- const error_1 = require("./error");
8
- /**
9
- * 回调函数promise化
10
- * 调用示例
11
- * soap.excute(arg1, arg2, function(error, data){});
12
- * 可使用为:
13
- * const soap_excute = promise({
14
- * fn: soap.excute,
15
- * target: soap
16
- * });
17
- * const data = await soap_excute(arg1, arg2);
18
- * @param this
19
- * @param param1
20
- */
21
- const promise = function ({ fn, target, last = true }) {
22
- return (...args) => {
23
- return new Promise((resolve, reject) => {
24
- args[last === true ? 'push' : 'unshift']((err, data) => {
25
- if (err === null || err === undefined) {
26
- return resolve.call(this, data);
27
- }
28
- return reject(err);
29
- });
30
- if (target) {
31
- fn.apply(target, args);
32
- }
33
- else {
34
- fn.apply({}, args);
35
- }
36
- });
37
- };
38
- };
39
- exports.promise = promise;
40
- const sleep = (time = parseInt(`${Math.random()}`) + 200) => new Promise((resolve) => setTimeout(resolve, time));
41
- exports.sleep = sleep;
42
- /**
43
- * 执行器
44
- * @param fn
45
- * @param {
46
- ifFinish?: (result?: T) => boolean; // 是否结束,默认是建议判断 !!result
47
- maxTryTimes?: number; //最多尝试几次,默认是20
48
- onFail?: () => Promise<boolean | undefined> | boolean | undefined; // 失败时的回调,返回false表示停止执行
49
- name?: string; // 执行器名称,用于打印日志
50
- exitIfFail?: boolean; // 失败时是否退出,默认是false. 这里设置true后,onFail返回true,也会停止执行
51
- defVal?: T; // 失败时的默认值
52
- sleepAppend?: number; // 等待延迟MS,默认是1000内随机+200
53
- * }
54
- * @returns
55
- */
56
- async function dieTrying(fn, { ifFinish = (result) => !!result, maxTryTimes = 20, onFail, name = 'dieTrying', exitIfFail = true, defVal, sleepAppend = 0 } = {}) {
57
- let count = 0;
58
- let result = defVal;
59
- while (result = await fn(), !ifFinish(result)) {
60
- await (0, exports.sleep)(parseInt(`${Math.random() * 1000}`) + sleepAppend);
61
- count++;
62
- console.debug(`${name} try ${count} times`);
63
- if (count > maxTryTimes) {
64
- if (onFail) {
65
- const remuseExcute = await onFail();
66
- console.error(`${name} timeout`);
67
- count = 0;
68
- if (remuseExcute === false) {
69
- break;
70
- }
71
- }
72
- if (exitIfFail) {
73
- break;
74
- }
75
- }
76
- }
77
- return result;
78
- }
79
- var ExcuteSplitMode;
80
- (function (ExcuteSplitMode) {
81
- ExcuteSplitMode[ExcuteSplitMode["SyncTrust"] = 0] = "SyncTrust";
82
- ExcuteSplitMode[ExcuteSplitMode["SyncNoTrust"] = 1] = "SyncNoTrust";
83
- ExcuteSplitMode[ExcuteSplitMode["AsyncTrust"] = 2] = "AsyncTrust";
84
- ExcuteSplitMode[ExcuteSplitMode["AsyncNoTrust"] = 3] = "AsyncNoTrust";
85
- })(ExcuteSplitMode || (exports.ExcuteSplitMode = ExcuteSplitMode = {}));
86
- function excuteSplit(sync, datas, fn, { everyLength = 0, groupCount = 0, settled = false, extendParams = new Array() }) {
87
- if (extendParams.length > 0) {
88
- groupCount = extendParams.length;
89
- }
90
- error_1.Throw.if(everyLength === 0 && groupCount === 0, '参数错误!');
91
- const ps = { everyLength, groupCount };
92
- const list = (0, object_1.arraySplit)(datas, ps);
93
- if (sync === ExcuteSplitMode.AsyncTrust) {
94
- return new Promise(async (resolve, reject) => {
95
- try {
96
- const reasons = [];
97
- if (settled) {
98
- const result = await Promise.allSettled(list.map((list, i) => fn(list, i, list.length, extendParams[i])));
99
- for (const item of result) {
100
- if (item.status === 'rejected') {
101
- reject(item.reason);
102
- }
103
- else {
104
- reasons.push(item.value);
105
- }
106
- }
107
- }
108
- else {
109
- for (let i = 0; i < list.length; i++) {
110
- const startIndex = (i - 1) * ps.everyLength;
111
- const endIndex = startIndex + list[i].length - 1;
112
- reasons.push(await fn(list[i], i, list.length, extendParams[i], startIndex, endIndex));
113
- }
114
- }
115
- resolve(reasons);
116
- }
117
- catch (error) {
118
- reject(error);
119
- }
120
- });
121
- }
122
- else if (sync === ExcuteSplitMode.AsyncNoTrust) {
123
- return new Promise(async (resolve, reject) => {
124
- try {
125
- const reasons = { result: [], error: [] };
126
- if (settled) {
127
- const result = await Promise.allSettled(list.map((list, i) => fn(list, i, list.length, extendParams[i])));
128
- for (const item of result) {
129
- if (item.status === 'rejected') {
130
- reasons.error.push(item.reason);
131
- }
132
- else {
133
- reasons.result.push(item.value);
134
- }
135
- }
136
- }
137
- else {
138
- for (let i = 0; i < list.length; i++) {
139
- const startIndex = (i - 1) * ps.everyLength;
140
- const endIndex = startIndex + list[i].length - 1;
141
- try {
142
- reasons.result.push(await fn(list[i], i, list.length, extendParams[i], startIndex, endIndex));
143
- }
144
- catch (error) {
145
- reasons.error.push(error);
146
- }
147
- }
148
- }
149
- resolve(reasons);
150
- }
151
- catch (error) {
152
- reject(error);
153
- }
154
- });
155
- }
156
- else if (sync === ExcuteSplitMode.SyncTrust) {
157
- const reasons = [];
158
- for (let i = 0; i < list.length; i++) {
159
- const startIndex = (i - 1) * ps.everyLength;
160
- const endIndex = startIndex + list[i].length - 1;
161
- reasons.push(fn(list[i], i, list.length, extendParams[i], startIndex, endIndex));
162
- }
163
- return reasons;
164
- }
165
- else {
166
- const reasons = { result: [], error: [] };
167
- for (let i = 0; i < list.length; i++) {
168
- try {
169
- const startIndex = (i - 1) * ps.everyLength;
170
- const endIndex = startIndex + list[i].length - 1;
171
- reasons.result.push(fn(list[i], i, list.length, extendParams[i], startIndex, endIndex));
172
- }
173
- catch (error) {
174
- reasons.error.push(error);
175
- }
176
- }
177
- return reasons;
178
- }
179
- }
package/cjs/index.d.ts DELETED
@@ -1,10 +0,0 @@
1
- import 'reflect-metadata';
2
- export * from './error';
3
- export * from './fn';
4
- export * from './math';
5
- export * from './object';
6
- export * from './set-ex';
7
- export * from './sql';
8
- export * from './string';
9
- export * from './enum';
10
- export * from './list';