baja-lite 1.0.4 → 1.0.6

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