baja-lite 1.0.4 → 1.0.5

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 (74) hide show
  1. package/cjs/boot-remote.d.ts +2 -0
  2. package/cjs/boot-remote.js +35 -0
  3. package/cjs/boot.d.ts +2 -0
  4. package/cjs/boot.js +152 -0
  5. package/cjs/code.d.ts +1 -0
  6. package/cjs/code.js +345 -1
  7. package/cjs/convert-xml.d.ts +10 -0
  8. package/cjs/convert-xml.js +413 -0
  9. package/cjs/enum.d.ts +10 -0
  10. package/cjs/enum.js +32 -0
  11. package/cjs/error.js +1 -1
  12. package/cjs/index.d.ts +2 -0
  13. package/cjs/index.js +2 -0
  14. package/cjs/object.d.ts +7 -1
  15. package/cjs/object.js +36 -2
  16. package/cjs/sql.d.ts +405 -148
  17. package/cjs/sql.js +1229 -861
  18. package/cjs/sqlite.d.ts +38 -0
  19. package/cjs/sqlite.js +194 -0
  20. package/cjs/test-mysql.d.ts +1 -1
  21. package/cjs/test-mysql.js +72 -63
  22. package/cjs/test-sqlite.d.ts +1 -1
  23. package/cjs/test-sqlite.js +3 -1
  24. package/cjs/test-xml.d.ts +1 -0
  25. package/cjs/test-xml.js +75 -0
  26. package/es/boot-remote.d.ts +2 -0
  27. package/es/boot-remote.js +31 -0
  28. package/es/boot.d.ts +2 -0
  29. package/es/boot.js +125 -0
  30. package/es/code.d.ts +1 -0
  31. package/es/code.js +341 -2
  32. package/es/convert-xml.d.ts +10 -0
  33. package/es/convert-xml.js +409 -0
  34. package/es/enum.d.ts +10 -0
  35. package/es/enum.js +28 -0
  36. package/es/error.js +1 -1
  37. package/es/index.d.ts +2 -0
  38. package/es/index.js +2 -0
  39. package/es/object.d.ts +7 -1
  40. package/es/object.js +28 -1
  41. package/es/sql.d.ts +405 -148
  42. package/es/sql.js +1099 -735
  43. package/es/sqlite.d.ts +38 -0
  44. package/es/sqlite.js +164 -0
  45. package/es/test-mysql.d.ts +1 -1
  46. package/es/test-mysql.js +72 -63
  47. package/es/test-sqlite.d.ts +1 -1
  48. package/es/test-sqlite.js +3 -1
  49. package/es/test-xml.d.ts +1 -0
  50. package/es/test-xml.js +70 -0
  51. package/package.json +10 -7
  52. package/src/boot-remote.ts +31 -0
  53. package/src/boot.ts +129 -0
  54. package/src/code.ts +326 -1
  55. package/src/convert-xml.ts +461 -0
  56. package/src/enum.ts +31 -0
  57. package/src/error.ts +1 -1
  58. package/src/index.ts +3 -1
  59. package/src/object.ts +47 -14
  60. package/src/sql.ts +1145 -787
  61. package/src/sqlite.ts +161 -0
  62. package/src/test-mysql.ts +72 -63
  63. package/src/test-sqlite.ts +3 -1
  64. package/src/test-xml.ts +70 -0
  65. package/cjs/constant.d.ts +0 -13
  66. package/cjs/constant.js +0 -19
  67. package/cjs/redis.d.ts +0 -0
  68. package/cjs/redis.js +0 -1
  69. package/es/constant.d.ts +0 -13
  70. package/es/constant.js +0 -16
  71. package/es/redis.d.ts +0 -0
  72. package/es/redis.js +0 -1
  73. package/src/constant.ts +0 -14
  74. package/src/redis.ts +0 -0
@@ -0,0 +1,461 @@
1
+ export interface XML {
2
+ type: 'tag' | 'text';
3
+ name: string;
4
+ id?: string;
5
+ voidElement: boolean;
6
+ attrs: Record<string, string>;
7
+ children: XML[];
8
+ content: string;
9
+ }
10
+ export const convert = function (childrens: XML[], param: Record<string, any>, parentIds: string[], myBatisMapper: Record<string, XML[]>) {
11
+ let statement = '';
12
+ for (let i = 0, children: XML; children = childrens[i]!; i++) {
13
+ // Convert SQL statement recursively
14
+ statement += convertChildren(children, param, parentIds, myBatisMapper);
15
+ }
16
+ // Check not converted Parameters
17
+ var regexList = ['\\#{\\S*}', '\\${\\S*}'];
18
+ for (var i = 0, regexString: string; regexString = regexList[i]!; i++) {
19
+ var regex = new RegExp(regex, 'g');
20
+ var checkParam = statement.match(regexString);
21
+
22
+ if (checkParam != null && checkParam.length > 0) {
23
+ throw new Error("Parameter " + checkParam.join(",") + " is not converted.");
24
+ }
25
+ }
26
+ return statement;
27
+ }
28
+ const convertChildren = function (children: XML, param: Record<string, any>, parentIds: string[], myBatisMapper: Record<string, XML[]>) {
29
+ param ??= {};
30
+ if (!isDict(param)) {
31
+ throw new Error('Parameter argument should be Key-Value type or Null.');
32
+ }
33
+ if (children.type == 'text') {
34
+ // Convert Parameters
35
+ return convertParameters(children, param);
36
+
37
+ } else if (children.type == 'tag') {
38
+ switch (children.name.toLowerCase()) {
39
+ case 'if':
40
+ return convertIf(children, param, parentIds, myBatisMapper);
41
+ case 'choose':
42
+ return convertChoose(children, param, parentIds, myBatisMapper);
43
+ case 'trim':
44
+ case 'where':
45
+ return convertTrimWhere(children, param, parentIds, myBatisMapper);
46
+ case 'set':
47
+ return convertSet(children, param, parentIds, myBatisMapper);
48
+ case 'foreach':
49
+ return convertForeach(children, param, parentIds, myBatisMapper);
50
+ case 'bind':
51
+ param = convertBind(children, param);
52
+ return '';
53
+ case 'include':
54
+ return convertInclude(children, param, parentIds, myBatisMapper);
55
+ default:
56
+ throw new Error('XML is not well-formed character or markup. Consider using CDATA section.');
57
+ }
58
+ } else {
59
+ return '';
60
+ }
61
+ }
62
+
63
+ const convertParameters = function (children: XML, param: Record<string, any>) {
64
+ let convertString = children.content;
65
+
66
+ try {
67
+ convertString = convertParametersInner('#', convertString, param);
68
+ convertString = convertParametersInner('$', convertString, param);
69
+ } catch (err) {
70
+ throw new Error('Error occurred during convert parameters.');
71
+ }
72
+
73
+ try {
74
+ // convert CDATA string
75
+ convertString = convertString.replace(/(\&amp\;)/g, '&');
76
+ convertString = convertString.replace(/(\&lt\;)/g, '<');
77
+ convertString = convertString.replace(/(\&gt\;)/g, '>');
78
+ convertString = convertString.replace(/(\&quot\;)/g, '"');
79
+ } catch (err) {
80
+ throw new Error('Error occurred during convert CDATA section.');
81
+ }
82
+
83
+ return convertString;
84
+ }
85
+
86
+ const isObject = function (variable: any) {
87
+ return typeof variable === 'object' && variable !== null;
88
+ }
89
+
90
+ const isArray = function (variable: any) {
91
+ return isObject(variable) && variable.hasOwnProperty('length');
92
+ }
93
+
94
+ const convertParametersInner = function (change: string, convertString: string, param: Record<string, any>) {
95
+ const stringReg = new RegExp('(\\' + change + '\\{[a-zA-Z0-9._\\$]+\\})', 'g');
96
+ let stringTarget = convertString.match(stringReg);
97
+
98
+ if (stringTarget != null && stringTarget.length > 0) {
99
+ const _stringTarget = uniqueArray(stringTarget);
100
+
101
+ let target: string | undefined;
102
+ for (let i = 0; i < _stringTarget.length; i++) {
103
+ target = _stringTarget[i];
104
+ const t = target!.replace(change + '{', '').replace('}', '');
105
+ let tempParamKey = eval('param.' + t);
106
+
107
+ if (tempParamKey !== undefined) {
108
+ const reg = new RegExp('\\' + change + '{' + t + '}', 'g');
109
+
110
+ if (tempParamKey === null) {
111
+ tempParamKey = 'NULL';
112
+ convertString = convertString.replace(reg, tempParamKey);
113
+ } else {
114
+ if (change == '#') {
115
+ // processing JSON fields structures
116
+ if (isObject(tempParamKey) || isArray(tempParamKey)) {
117
+ tempParamKey = JSON.stringify(tempParamKey);
118
+ } else {
119
+ tempParamKey = tempParamKey.toString().replace(/"/g, '\\\"');
120
+ tempParamKey = mysqlRealEscapeParam(tempParamKey);
121
+ }
122
+
123
+ tempParamKey = tempParamKey.replace(/'/g, "''");
124
+ const replaceWith = "'" + tempParamKey + "'"
125
+ convertString = convertString.replace(reg, () => replaceWith);
126
+ } else if (change == '$') {
127
+ convertString = convertString.replace(reg, tempParamKey);
128
+ }
129
+ }
130
+ }
131
+ }
132
+ }
133
+ return convertString;
134
+ }
135
+
136
+ const convertIf = function (children: XML, param: Record<string, any>, parentIds: string[], myBatisMapper: Record<string, XML[]>) {
137
+ let evalString = children.attrs['test']!;
138
+ try {
139
+ // Create Evaluate string
140
+ evalString = replaceEvalString(evalString, param);
141
+
142
+ evalString = evalString.replace(/ and /gi, ' && ');
143
+ evalString = evalString.replace(/ or /gi, ' || ');
144
+
145
+ // replace == to === for strict evaluate
146
+ evalString = evalString.replace(/==/g, '===');
147
+ evalString = evalString.replace(/!=/g, '!==');
148
+
149
+ evalString = evalString.replace(/^'(.*?)'\.equalsIgnoreCase\( ([a-zA-Z]+\.[a-zA-Z]+) \)/i, `($2 && $2.toUpperCase() === '$1'.toUpperCase())`);
150
+ evalString = evalString.replace(/\('(.*?)'\.equalsIgnoreCase\( ([a-zA-Z]+\.[a-zA-Z]+) \)/i, `(($2 && $2.toUpperCase() === '$1'.toUpperCase())`);
151
+
152
+ } catch (err) {
153
+ throw new Error('Error occurred during convert <if> element.');
154
+ }
155
+
156
+ // Execute Evaluate string
157
+ try {
158
+ if (eval(evalString)) {
159
+ let convertString = '';
160
+ for (let i = 0, nextChildren: XML; nextChildren = children['children'][i]!; i++) {
161
+ convertString += convertChildren(nextChildren, param, parentIds, myBatisMapper);
162
+ }
163
+ return convertString;
164
+
165
+ } else {
166
+ return '';
167
+ }
168
+ } catch (e) {
169
+ return '';
170
+ }
171
+ }
172
+
173
+ const convertForeach = function (children: XML, param: Record<string, any>, parentIds: string[], myBatisMapper: Record<string, XML[]>) {
174
+ try {
175
+ const collection = eval('param.' + children.attrs['collection']);
176
+ const item = children.attrs['item']!;
177
+ const open = (children.attrs['open'] == null) ? '' : children.attrs['open'];
178
+ const close = (children.attrs['close'] == null) ? '' : children.attrs['close'];
179
+ const separator = (children.attrs['separator'] == null) ? '' : children.attrs['separator'];
180
+
181
+ const foreachTexts: string[] = [];
182
+ let coll = null;
183
+ for (let j = 0; j < collection.length; j++) {
184
+ coll = collection[j];
185
+ const foreachParam = param;
186
+ foreachParam[item] = coll;
187
+
188
+ let foreachText = '';
189
+ for (let k = 0, nextChildren: XML; nextChildren = children.children[k]!; k++) {
190
+ let fText = convertChildren(nextChildren, foreachParam, parentIds, myBatisMapper);
191
+ fText = fText.replace(/^\s*$/g, '');
192
+ if (fText != null && fText.length > 0) {
193
+ foreachText += fText;
194
+ }
195
+ }
196
+
197
+ if (foreachText != null && foreachText.length > 0) {
198
+ foreachTexts.push(foreachText);
199
+ }
200
+ }
201
+
202
+ return (open + foreachTexts.join(separator) + close);
203
+ } catch (err) {
204
+ throw new Error('Error occurred during convert <foreach> element.');
205
+ }
206
+ }
207
+
208
+ const convertChoose = function (children: XML, param: Record<string, any>, parentIds: string[], myBatisMapper: Record<string, XML[]>) {
209
+ try {
210
+ for (let i = 0, whenChildren; whenChildren = children.children[i]; i++) {
211
+ if (whenChildren.type == 'tag' && whenChildren.name.toLowerCase() == 'when') {
212
+ let evalString = whenChildren.attrs.test;
213
+
214
+ // Create Evaluate string
215
+ evalString = replaceEvalString(evalString, param);
216
+
217
+ evalString = evalString.replace(/ and /gi, ' && ');
218
+ evalString = evalString.replace(/ or /gi, ' || ');
219
+
220
+ // Execute Evaluate string
221
+ try {
222
+ if (eval(evalString)) {
223
+ // If <when> condition is true, do it.
224
+ let convertString = '';
225
+ for (let k = 0, nextChildren: XML; nextChildren = whenChildren.children[k]; k++) {
226
+ convertString += convertChildren(nextChildren, param, parentIds, myBatisMapper);
227
+ }
228
+ return convertString;
229
+ } else {
230
+ continue;
231
+ }
232
+ } catch (e) {
233
+ continue;
234
+ }
235
+ } else if (whenChildren.type == 'tag' && whenChildren.name.toLowerCase() == 'otherwise') {
236
+ // If reached <otherwise> tag, do it.
237
+ let convertString = '';
238
+ for (let k = 0, nextChildren: XML; nextChildren = whenChildren.children[k]; k++) {
239
+ convertString += convertChildren(nextChildren, param, parentIds, myBatisMapper);
240
+ }
241
+ return convertString;
242
+ }
243
+ }
244
+
245
+ // If there is no suitable when and otherwise, just return null.
246
+ return '';
247
+
248
+ } catch (err) {
249
+ throw new Error('Error occurred during convert <choose> element.');
250
+ }
251
+ }
252
+
253
+ const convertTrimWhere = function (children: XML, param: Record<string, any>, parentIds: string[], myBatisMapper: Record<string, XML[]>) {
254
+ let convertString = '';
255
+ let prefix: string | undefined;
256
+ let prefixOverrides: string | undefined;
257
+ let suffix: string | undefined;
258
+ let suffixOverrides: string | undefined;
259
+ let globalSet: string | undefined;
260
+
261
+ try {
262
+ switch (children.name.toLowerCase()) {
263
+ case 'trim':
264
+ prefix = children.attrs["prefix"];
265
+ prefixOverrides = children.attrs["prefixOverrides"];
266
+ suffix = children.attrs["suffix"];
267
+ suffixOverrides = children.attrs["suffixOverrides"];
268
+ globalSet = 'g';
269
+ break;
270
+ case 'where':
271
+ prefix = 'WHERE';
272
+ prefixOverrides = 'and|or';
273
+ globalSet = 'gi';
274
+ break;
275
+ default:
276
+ throw new Error('Error occurred during convert <trim/where> element.');
277
+ }
278
+
279
+ // Convert children first.
280
+ for (let j = 0, nextChildren: XML; nextChildren = children.children[j]!; j++) {
281
+ convertString += convertChildren(nextChildren, param, parentIds, myBatisMapper);
282
+ }
283
+
284
+ // Remove prefixOverrides
285
+ let trimRegex = new RegExp('(^)([\\s]*?)(' + prefixOverrides + ')', globalSet);
286
+ convertString = convertString.replace(trimRegex, '');
287
+ // Remove suffixOverrides
288
+ trimRegex = new RegExp('(' + suffixOverrides + ')([\\s]*?)($)', globalSet);
289
+ convertString = convertString.replace(trimRegex, '');
290
+
291
+ if (children.name.toLowerCase() != 'trim') {
292
+ trimRegex = new RegExp('(' + prefixOverrides + ')([\\s]*?)($)', globalSet);
293
+ convertString = convertString.replace(trimRegex, '');
294
+ }
295
+
296
+ // Add Prefix if String is not empty.
297
+ trimRegex = new RegExp('([a-zA-Z])', 'g');
298
+ const w = convertString.match(trimRegex);
299
+
300
+ if (w != null && w.length > 0) {
301
+ convertString = prefix + ' ' + convertString;
302
+ if (suffix) {
303
+ convertString = convertString + ' ' + suffix
304
+ }
305
+ }
306
+
307
+ // Remove comma(,) before WHERE
308
+ if (children.name.toLowerCase() != 'where') {
309
+ const regex = new RegExp('(,)([\\s]*?)(where)', 'gi');
310
+ convertString = convertString.replace(regex, ' WHERE ');
311
+ }
312
+
313
+ return convertString;
314
+ } catch (err) {
315
+ throw new Error('Error occurred during convert <' + children.name.toLowerCase() + '> element.');
316
+ }
317
+ }
318
+
319
+ const convertSet = function (children: XML, param: Record<string, any>, parentIds: string[], myBatisMapper: Record<string, XML[]>) {
320
+ let convertString = '';
321
+
322
+ try {
323
+ // Convert children first.
324
+ for (let j = 0, nextChildren: XML; nextChildren = children.children[j]!; j++) {
325
+ convertString += convertChildren(nextChildren, param, parentIds, myBatisMapper);
326
+ }
327
+
328
+ // Remove comma repeated more than 2.
329
+ let regex = new RegExp('(,)(,|\\s){2,}', 'g');
330
+ convertString = convertString.replace(regex, ',\n');
331
+
332
+ // Remove first comma if exists.
333
+ regex = new RegExp('(^)([\\s]*?)(,)', 'g');
334
+ convertString = convertString.replace(regex, '');
335
+
336
+ // Remove last comma if exists.
337
+ regex = new RegExp('(,)([\\s]*?)($)', 'g');
338
+ convertString = convertString.replace(regex, '');
339
+
340
+ convertString = ' SET ' + convertString;
341
+ return convertString;
342
+ } catch (err) {
343
+ throw new Error('Error occurred during convert <set> element.');
344
+ }
345
+ }
346
+
347
+ const convertBind = function (children: XML, param: Record<string, any>) {
348
+ let evalString = children.attrs["value"]!;
349
+
350
+ // Create Evaluate string
351
+ evalString = replaceEvalString(evalString, param);
352
+
353
+ param[children.attrs["name"]!] = eval(evalString);
354
+
355
+ return param;
356
+ }
357
+
358
+ const convertInclude = function (children: XML, param: Record<string, any>, parentIds: string[], myBatisMapper: Record<string, XML[]>) {
359
+ try {
360
+ // Add Properties to param
361
+ for (let j = 0, nextChildren: XML; nextChildren = children.children[j]!; j++) {
362
+ if (nextChildren.type == 'tag' && nextChildren.name == 'property') {
363
+ param[nextChildren.attrs['name']!] = nextChildren.attrs['value'];
364
+ }
365
+ }
366
+ } catch (err) {
367
+ throw new Error('Error occurred during read <property> element in <include> element.');
368
+ }
369
+
370
+ try {
371
+ let refid = convertParametersInner('#', children.attrs['refid']!, param);
372
+ refid = convertParametersInner('$', refid, param);
373
+ let mapper: XML[] | undefined;
374
+ for (const psqlid of parentIds) {
375
+ mapper = myBatisMapper[`${psqlid}.${refid}`];
376
+ if (mapper) {
377
+ break;
378
+ }
379
+ }
380
+ let statement = '';
381
+ for (let i = 0, children: XML; children = mapper![i]!; i++) {
382
+ statement += convertChildren(children, param, parentIds, myBatisMapper);
383
+ }
384
+ return statement;
385
+ } catch (err) {
386
+ throw new Error('Error occurred during convert refid attribute in <include> element.');
387
+ }
388
+
389
+ }
390
+
391
+ const isDict = function (v: any) {
392
+ return typeof v === 'object' && v !== null && !(v instanceof Array) && !(v instanceof Date);
393
+ }
394
+
395
+ const replaceEvalString = function (evalString: string, param: Record<string, any>) {
396
+ const keys = Object.keys(param);
397
+
398
+ for (let i = 0; i < keys.length; i++) {
399
+ let replacePrefix = '';
400
+ let replacePostfix = '';
401
+ let paramRegex: RegExp | undefined;
402
+
403
+ if (isDict(param[keys[i]!])) {
404
+ replacePrefix = ' param.';
405
+ replacePostfix = '';
406
+
407
+ paramRegex = new RegExp('(^|[^a-zA-Z0-9_])(' + keys[i] + '\\.)([a-zA-Z0-9_]+)', 'g');
408
+ } else {
409
+ replacePrefix = ' param.';
410
+ replacePostfix = ' ';
411
+ paramRegex = new RegExp('(^|[^a-zA-Z0-9_])(' + keys[i] + ')($|[^a-zA-Z0-9_])', 'g');
412
+ }
413
+ evalString = evalString.replace(paramRegex, ('$1' + replacePrefix + '$2' + replacePostfix + '$3'));
414
+ }
415
+ return evalString;
416
+ }
417
+
418
+ const uniqueArray = function (a: RegExpMatchArray) {
419
+ const seen = {};
420
+ const out: string[] = [];
421
+ const len = a.length;
422
+ let j = 0;
423
+ for (let i = 0; i < len; i++) {
424
+ const item = a[i]!;
425
+ if (seen[item] !== 1) {
426
+ seen[item] = 1;
427
+ out[j++] = item;
428
+ }
429
+ }
430
+ return out;
431
+ }
432
+
433
+ const mysqlRealEscapeParam = function (param) {
434
+ if (typeof param != 'string')
435
+ return param;
436
+
437
+ return param.replace(/[\0\x08\x09\x1a\n\r''\\\%]/g, function (char) {
438
+ switch (char) {
439
+ case '\0':
440
+ return '\\0';
441
+ case '\x08':
442
+ return '\\b';
443
+ case '\x09':
444
+ return '\\t';
445
+ case '\x1a':
446
+ return '\\z';
447
+ case '\n':
448
+ return '\\n';
449
+ case '\r':
450
+ return '\\r';
451
+ case '\'':
452
+ case `'`:
453
+ case '\\':
454
+ case '%':
455
+ return '\\' + char;
456
+ default:
457
+ return char;
458
+ }
459
+ });
460
+ }
461
+
package/src/enum.ts ADDED
@@ -0,0 +1,31 @@
1
+ export class Enum {
2
+ private _value: string;
3
+ private _desc: string;
4
+ private _config: string[];
5
+ constructor (value: string, desc: string, ...config: string[]) {
6
+ this._value = value;
7
+ this._desc = desc;
8
+ this._config = config;
9
+ }
10
+ eq(value: string | number | undefined | null): boolean {
11
+ if (value === undefined) {
12
+ return false;
13
+ }
14
+ if (value === null) {
15
+ return false;
16
+ }
17
+ if (typeof value === 'number') {
18
+ return this._value === `${ value }`;
19
+ }
20
+ return this._value === `${ value }`;
21
+ }
22
+ value(): string {
23
+ return this._value;
24
+ }
25
+ desc(): string {
26
+ return this._desc;
27
+ }
28
+ config(): string[] {
29
+ return this._config;
30
+ }
31
+ }
package/src/error.ts CHANGED
@@ -7,5 +7,5 @@ export const Throw = {
7
7
  },
8
8
  now(message: string | Error | any) {
9
9
  throw typeof message === 'string' ? new Error(message) : message;
10
- },
10
+ }
11
11
  };
package/src/index.ts CHANGED
@@ -1,7 +1,9 @@
1
+ import 'reflect-metadata';
1
2
  export * from './error';
2
3
  export * from './fn';
3
4
  export * from './math';
4
5
  export * from './object';
5
6
  export * from './set-ex';
6
7
  export * from './sql';
7
- export * from './string';
8
+ export * from './string';
9
+ export * from './enum';
package/src/object.ts CHANGED
@@ -1,7 +1,9 @@
1
+ import iterate from "iterare";
2
+
1
3
  /**
2
4
  * 对象对象(等同与convertBean)
3
5
  * 仅会将classType有的属性进行转换
4
- * 相当与一次属性过滤
6
+ * * 相当与一次属性过滤
5
7
  * @param source
6
8
  * @param classType
7
9
  */
@@ -77,13 +79,13 @@ export const createBeanFromArray = <F, T = F>(
77
79
  if (value) {
78
80
  source.forEach((item) => {
79
81
  if (item[key]) {
80
- result[`${ item[key] }`] = item[value] as unknown as T;
82
+ result[`${item[key]}`] = item[value] as unknown as T;
81
83
  }
82
84
  });
83
85
  } else {
84
86
  source.forEach((item) => {
85
87
  if (item[key]) {
86
- result[`${ item[key] }`] = item as unknown as T;
88
+ result[`${item[key]}`] = item as unknown as T;
87
89
  }
88
90
  });
89
91
  }
@@ -98,7 +100,7 @@ export const createBeanFromArray = <F, T = F>(
98
100
  export const coverComplexBean = <T>(
99
101
  source: any,
100
102
  classType: any
101
- ): {data: T; array: {[key: string]: any[]}} => {
103
+ ): { data: T; array: { [key: string]: any[] } } => {
102
104
  const result = {};
103
105
  const arrayData = {};
104
106
  // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
@@ -140,13 +142,13 @@ export const fixEmptyPrototy = async (
140
142
  };
141
143
 
142
144
 
143
- export const mixArray = <T>(array: T[], key: keyof T, defKey?: string): {[key: string]: number} => {
145
+ export const mixArray = <T>(array: T[], key: keyof T, defKey?: string): { [key: string]: number } => {
144
146
  const obj = array.map(item => item[key]);
145
- const result: {[k: string]: number} = {};
147
+ const result: { [k: string]: number } = {};
146
148
  for (const i of obj) {
147
149
  let ki = '';
148
150
  if (i !== undefined && i !== null) {
149
- ki = `${ i }`;
151
+ ki = `${i}`;
150
152
  } else if (defKey) {
151
153
  ki = defKey;
152
154
  }
@@ -158,12 +160,12 @@ export const mixArray = <T>(array: T[], key: keyof T, defKey?: string): {[key: s
158
160
  return result;
159
161
  };
160
162
 
161
- export const mixList = <T, V = T>(array: T[], key: keyof T, value?: keyof T, defKey?: string): {[key: string]: V[]} => {
162
- const result: {[k: string]: V[]} = {};
163
+ export const mixList = <T, V = T>(array: T[], key: keyof T, value?: keyof T, defKey?: string): { [key: string]: V[] } => {
164
+ const result: { [k: string]: V[] } = {};
163
165
  for (const i of array) {
164
166
  let ki = '';
165
167
  if (i[key] !== undefined && i[key] !== null) {
166
- ki = `${ i[key] }`;
168
+ ki = `${i[key]}`;
167
169
  } else if (defKey) {
168
170
  ki = defKey;
169
171
  }
@@ -179,8 +181,8 @@ export const mixList = <T, V = T>(array: T[], key: keyof T, value?: keyof T, def
179
181
  return result;
180
182
  };
181
183
 
182
- export const array2map = <T = string | number>(array: string[], v: T): {[key: string]: T} => {
183
- const ot: {[key: string]: T} = {};
184
+ export const array2map = <T = string | number>(array: string[], v: T): { [key: string]: T } => {
185
+ const ot: { [key: string]: T } = {};
184
186
  for (const item of array) {
185
187
  ot[item] = v;
186
188
  }
@@ -192,7 +194,7 @@ export const array2map = <T = string | number>(array: string[], v: T): {[key: st
192
194
  * @param config(二选一) everyLength=每组个数(最后一组可能不足次数), groupCount=拆分几组
193
195
  * @returns T[][]
194
196
  */
195
- export const arraySplit = <T = any>(datas: T[], {everyLength = 0, groupCount = 0} = {}) => {
197
+ export const arraySplit = <T = any>(datas: T[], { everyLength = 0, groupCount = 0 } = {}) => {
196
198
  if (groupCount > 0) {
197
199
  everyLength = Math.floor(datas.length / groupCount + 0.9);
198
200
  const result: T[][] = [];
@@ -210,4 +212,35 @@ export const arraySplit = <T = any>(datas: T[], {everyLength = 0, groupCount = 0
210
212
  } else {
211
213
  throw new Error('参数错误!');
212
214
  }
213
- };
215
+ };
216
+
217
+
218
+ const P2CEX = /[A-Z]/g;
219
+ export const P2C = (pro: string, IF = true) => IF ? pro.replace(P2CEX, (a: string) => `_${a.toLowerCase()}`) : pro;
220
+ const C2PEX = /_([a-z])/g;
221
+ export const C2P = (pro: string, IF = true) => IF ? pro.replace(C2PEX, (a: string, b: string) => `_${b.toUpperCase()}`) : pro;
222
+
223
+
224
+ export function C2P2<T extends Object = any, L extends Object = T>(datas: L[]): T[];
225
+ export function C2P2<T extends Object = any, L extends Object = T>(datas: L): T;
226
+ export function C2P2<T extends Object = any, L extends Object = T>(datas: L | L[]): T | T[] {
227
+ if (datas instanceof Array) {
228
+ return iterate(datas).map((data: L) => Object.fromEntries<T>(Object.entries(data).map(([K, V]) => [C2P(K), V]))).toArray() as unknown as T[];
229
+ } else if (datas) {
230
+ return Object.fromEntries<T>(Object.entries(datas).map(([K, V]) => [C2P(K), V])) as unknown as T;
231
+ } else {
232
+ return datas;
233
+ }
234
+ }
235
+
236
+ export function P2C2<T extends Object = any, L extends Object = T>(datas: L[]): T[];
237
+ export function P2C2<T extends Object = any, L extends Object = T>(datas: L): T;
238
+ export function P2C2<T extends Object = any, L extends Object = T>(datas: L | L[]): T | T[] {
239
+ if (datas instanceof Array) {
240
+ return iterate(datas).map((data: L) => Object.fromEntries<T>(Object.entries(data).map(([K, V]) => [P2C(K), V]))).toArray() as unknown as T[];
241
+ } else if (datas) {
242
+ return Object.fromEntries<T>(Object.entries(datas).map(([K, V]) => [P2C(K), V])) as unknown as T;
243
+ } else {
244
+ return datas;
245
+ }
246
+ }