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
package/src/math.ts DELETED
@@ -1,405 +0,0 @@
1
- /* eslint-disable @typescript-eslint/no-unsafe-argument */
2
- import Decimal from 'decimal.js';
3
- /** 金钱格式化可用样式 */
4
- export class MoneyOption {
5
- style?: 'currency' | 'decimal' | 'percent' = 'currency';
6
- currency?: string = 'CNY';
7
- prefix?: number = 2;
8
- def?: number = 0;
9
- currencyDisplay?: 'symbol' | 'name' | 'code' = 'symbol';
10
- useGrouping?: boolean = true;
11
- local?: string = 'zh';
12
- }
13
- export interface Point {
14
- latitude: string;
15
- longitude: string;
16
- lat: number;
17
- long: number;
18
- }
19
- // const ONE = new Decimal(1);
20
- const ZERO = new Decimal(0);
21
- function isNum(a: any): boolean {
22
- return a !== '' && a !== null && !isNaN(a);
23
- }
24
- export const num = (val: any, def = 0): number => {
25
- if (val instanceof Bus) {
26
- return val.over();
27
- } else if (!isNum(val)) {
28
- return def;
29
- }
30
- return +val;
31
- };
32
- function filterNumber(array: any[]): number[] {
33
- const res: number[] = [];
34
- array.forEach((element) => {
35
- if (element instanceof Bus) {
36
- res.push(element.over());
37
- } else if (isNum(element)) {
38
- res.push(+element);
39
- }
40
- });
41
- return res;
42
- }
43
- function filterNumber2(array: any[], def?: number): Decimal[] {
44
- const res: Decimal[] = [];
45
- array.forEach((element) => {
46
- if (element instanceof Bus) {
47
- res.push(new Decimal(element.over()));
48
- } else if (isNum(element)) {
49
- res.push(new Decimal(element));
50
- } else if (def !== undefined) {
51
- res.push(new Decimal(def));
52
- }
53
- });
54
- return res;
55
- }
56
-
57
- export const max = (...args: any[]): number => {
58
- const arr = filterNumber(args);
59
- return Math.max.apply(null, arr);
60
- };
61
-
62
- export const min = (...args: any[]): number => {
63
- const arr = filterNumber(args);
64
- return Math.min.apply(null, arr);
65
- };
66
-
67
- export const div = (...args: any[]): number => {
68
- const arr: Decimal[] = filterNumber2(args);
69
- if (arr!.length > 1) {
70
- return arr!.reduce((a, b) => a.div(b)).toNumber();
71
- } else if (arr!.length > 0) {
72
- return arr[0]!.toNumber();
73
- } else {
74
- return 0;
75
- }
76
- };
77
- export const divDef = (def: any, ...args: any[]): number => {
78
- const arr: Decimal[] = filterNumber2(args);
79
- if (arr!.length > 1) {
80
- const zeros = arr!.slice(1).findIndex(i => i.equals(ZERO));
81
- if (zeros > -1) {
82
- return new Decimal(def).toNumber();
83
- }
84
- return arr!.reduce((a, b) => a.div(b)).toNumber();
85
- } else if (arr!.length > 0) {
86
- return arr[0]!.toNumber();
87
- } else {
88
- return 0;
89
- }
90
- };
91
-
92
- export const add = (...args: any[]): number => {
93
- const arr = filterNumber2(args);
94
- if (arr!.length > 1) {
95
- return arr!.reduce((a, b) => a.add(b)).toNumber();
96
- } else if (arr!.length > 0) {
97
- return arr[0]!.toNumber();
98
- } else {
99
- return 0;
100
- }
101
- };
102
-
103
- export const mul = (...args: any[]): number => {
104
- const arr = filterNumber2(args);
105
- if (arr!.length > 1) {
106
- return arr!.reduce((a, b) => a.mul(b)).toNumber();
107
- } else if (arr!.length > 0) {
108
- return arr[0]!.toNumber();
109
- } else {
110
- return 0;
111
- }
112
- };
113
-
114
- export const sub = (...args: any[]): number => {
115
- const arr = filterNumber2(args, 0);
116
- if (arr!.length > 1) {
117
- return arr!.reduce((a, b) => a.sub(b)).toNumber();
118
- } else if (arr!.length > 0) {
119
- return arr[0]!.toNumber();
120
- } else {
121
- return 0;
122
- }
123
- };
124
-
125
- const roundMode = [Decimal.ROUND_HALF_UP, Decimal.ROUND_UP, Decimal.ROUND_DOWN];
126
- export const round = (number: any, numDigits: number, upOrDown = 0): number => {
127
- if (isNum(number)) {
128
- const nu = new Decimal(number);
129
- return nu.toDP(numDigits, roundMode[upOrDown]!).toNumber();
130
- } else {
131
- return 0;
132
- }
133
- };
134
- /** =value.xx,其中xx=number,如number=99,表示修正数字为value.99 */
135
- export const merge = function (value: any, number: any) {
136
- if (isNum(value) && isNum(number)) {
137
- return new Decimal(value).floor().add(`0.${number}`).toNumber();
138
- } else if (isNum(value)) {
139
- return value;
140
- } else {
141
- return 0;
142
- }
143
- };
144
-
145
-
146
- export const money = (
147
- value: any,
148
- option: MoneyOption = {}
149
- ): string => {
150
- // Intl.NumberFormat(option.local ?? 'zh', {
151
- // style: option.style ?? 'currency',
152
- // currency: option.currency ?? 'CNY',
153
- // minimumFractionDigits: option.prefix ?? 2,
154
- // currencyDisplay: option.currencyDisplay ?? 'symbol',
155
- // useGrouping: option.useGrouping ?? true
156
- // }).format(isNum(value) ? value : option.def).replace(/CN|\s/g, '');
157
- return (isNum(value) ? value : option.def).toLocaleString(option.local ?? 'zh', {
158
- style: option.style ?? 'currency',
159
- currency: option.currency ?? 'CNY',
160
- minimumFractionDigits: option.prefix ?? 2,
161
- currencyDisplay: option.currencyDisplay ?? 'symbol',
162
- useGrouping: option.useGrouping ?? true
163
- }).replace(/CN|\s/g, '');
164
- };
165
-
166
- const IF = function () {
167
- return function (_target: any, _propertyKey: string, descriptor: PropertyDescriptor) {
168
- const fn = descriptor!.value;
169
- descriptor!.value = function (this: Bus) {
170
- if (this['ifit'] === true) {
171
- // eslint-disable-next-line prefer-rest-params
172
- const args = Array.from(arguments);
173
- fn.call(this, ...args);
174
- }
175
- this['ifit'] = true;
176
- return this;
177
- };
178
- };
179
- };
180
-
181
- export class Bus {
182
- private result: number;
183
- private ifit = true;
184
- constructor(result: any) {
185
- this.result = num(result);
186
- }
187
- @IF()
188
- add(...args: any[]): this {
189
- this.result = add(this.result, ...args);
190
- return this;
191
- }
192
- @IF()
193
- sub(...args: any[]): this {
194
- this.result = sub(this.result, ...args);
195
- return this;
196
- }
197
- @IF()
198
- div(...args: any[]): this {
199
- this.result = div(this.result, ...args);
200
- return this;
201
- }
202
- @IF()
203
- divDef(def: any, ...args: any[]): this {
204
- this.result = divDef(def, this.result, ...args);
205
- return this;
206
- }
207
-
208
- @IF()
209
- mul(...args: any[]): this {
210
- this.result = mul(this.result, ...args);
211
- return this;
212
- }
213
- @IF()
214
- max(...args: any[]): this {
215
- this.result = max(this.result, ...args);
216
- return this;
217
- }
218
- @IF()
219
- min(...args: any[]): this {
220
- this.result = min(this.result, ...args);
221
- return this;
222
- }
223
- @IF()
224
- ac(): this {
225
- this.result = sub(0, this.result);
226
- return this;
227
- }
228
- @IF()
229
- abs(): this {
230
- this.result = Math.abs(this.result);
231
- return this;
232
- }
233
- @IF()
234
- round(numDigits: number, upOrDown?: number): this {
235
- this.result = round(this.result, numDigits, upOrDown);
236
- return this;
237
- }
238
- @IF()
239
- merge(number: any) {
240
- this.result = merge(this.result, number);
241
- return this;
242
- }
243
- if(condition: boolean) {
244
- this.ifit = condition;
245
- return this;
246
- }
247
- over(): number {
248
- return this.result;
249
- }
250
- money(
251
- option?: MoneyOption
252
- ): string {
253
- return money(this.result, option);
254
- }
255
- lt(data: any): boolean {
256
- const [d, r] = filterNumber2([data, this.result]);
257
- return r!.lessThan(d!);
258
- }
259
- le(data: any): boolean {
260
- const [d, r] = filterNumber2([data, this.result]);
261
- return r!.lessThanOrEqualTo(d!);
262
- }
263
- gt(data: any): boolean {
264
- const [d, r] = filterNumber2([data, this.result]);
265
- return r!.greaterThan(d!);
266
- }
267
- ge(data: any): boolean {
268
- const [d, r] = filterNumber2([data, this.result]);
269
- return r!.greaterThanOrEqualTo(d!);
270
- }
271
- nlt(data: any): boolean {
272
- const [d, r] = filterNumber2([data, this.result]);
273
- return !r!.lessThan(d!);
274
- }
275
- nle(data: any): boolean {
276
- const [d, r] = filterNumber2([data, this.result]);
277
- return !r!.lessThanOrEqualTo(d!);
278
- }
279
- ngt(data: any): boolean {
280
- const [d, r] = filterNumber2([data, this.result]);
281
- return !r!.greaterThan(d!);
282
- }
283
- nge(data: any): boolean {
284
- const [d, r] = filterNumber2([data, this.result]);
285
- return !r!.greaterThanOrEqualTo(d!);
286
- }
287
- eq(data: any): boolean {
288
- const [d, r] = filterNumber2([data, this.result]);
289
- return r!.equals(d!);
290
- }
291
- ne(data: any): boolean {
292
- const [d, r] = filterNumber2([data, this.result]);
293
- return !r!.eq(d!);
294
- }
295
-
296
- ifLt(data: any): this {
297
- const [d, r] = filterNumber2([data, this.result]);
298
- this.ifit = r!.lessThan(d!);
299
- return this;
300
- }
301
- ifLe(data: any): this {
302
- const [d, r] = filterNumber2([data, this.result]);
303
- this.ifit = r!.lessThanOrEqualTo(d!);
304
- return this;
305
- }
306
- ifGt(data: any): this {
307
- const [d, r] = filterNumber2([data, this.result]);
308
- this.ifit = r!.greaterThan(d!);
309
- return this;
310
- }
311
- ifGe(data: any): this {
312
- const [d, r] = filterNumber2([data, this.result]);
313
- this.ifit = r!.greaterThanOrEqualTo(d!);
314
- return this;
315
- }
316
- ifNlt(data: any): this {
317
- const [d, r] = filterNumber2([data, this.result]);
318
- this.ifit = !r!.lessThan(d!);
319
- return this;
320
- }
321
- ifNle(data: any): this {
322
- const [d, r] = filterNumber2([data, this.result]);
323
- this.ifit = !r!.lessThanOrEqualTo(d!);
324
- return this;
325
- }
326
- ifNgt(data: any): this {
327
- const [d, r] = filterNumber2([data, this.result]);
328
- this.ifit = !r!.greaterThan(d!);
329
- return this;
330
- }
331
- ifNge(data: any): this {
332
- const [d, r] = filterNumber2([data, this.result]);
333
- this.ifit = !r!.greaterThanOrEqualTo(d!);
334
- return this;
335
- }
336
- ifEq(data: any): this {
337
- const [d, r] = filterNumber2([data, this.result]);
338
- this.ifit = r!.equals(d!);
339
- return this;
340
- }
341
- ifNe(data: any): this {
342
- const [d, r] = filterNumber2([data, this.result]);
343
- this.ifit = !r!.eq(d!);
344
- return this;
345
- }
346
- }
347
-
348
- export const calc = (result: any) => {
349
- return new Bus(result);
350
- };
351
-
352
- export const getGeo = (p1: Point, p2: Point) => {
353
- p1.lat = calc(p1.latitude).mul(Math.PI).div(180).over();
354
- p1.long = calc(p1.longitude).mul(Math.PI).div(180).over();
355
- p2.lat = calc(p2.latitude).mul(Math.PI).div(180).over();
356
- p2.long = calc(p2.longitude).mul(Math.PI).div(180).over();
357
- return calc(
358
- Math.round(
359
- mul(
360
- Math.asin(
361
- Math.sqrt(
362
- add(Math.pow(Math.sin(div(sub(p1.lat, p2.lat), 2)), 2),
363
- mul(
364
- Math.cos(p1.lat), Math.cos(p2.lat), Math.pow(Math.sin(div(sub(p1.long, p2.long), 2)), 2)
365
- ))
366
- )), 2, 6378.137, 10000))).div(10000).round(2).over();
367
- };
368
-
369
- const ZM = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
370
- /**
371
- * 十进制转换自定义进制
372
- * @param from 数字
373
- * @param to 自定义进制的字符
374
- * @returns
375
- */
376
- export function ten2Any(from: number, to = ZM) {
377
- let result = '';
378
- const length = to.length;
379
- while (from > 0) {
380
- from--;
381
- let remainder = from % length;
382
- result = to.charAt(remainder) + result;
383
- from = Math.floor(from / length);
384
- }
385
- return result;
386
- }
387
- /**
388
- * 自定义进制转换十进制
389
- * @param from
390
- * @param to
391
- * @returns
392
- */
393
- export function any2Ten(from: string, to = ZM) {
394
- let decimal = 0;
395
- from = from.toUpperCase();
396
- for (let i = 0; i < from.length; i++) {
397
- const char = from[from.length - 1 - i]!.toUpperCase();
398
- const index = to.indexOf(char);
399
- if (index > -1) {
400
- const value = to.indexOf(char) + 1;
401
- decimal += value * Math.pow(26, i);
402
- }
403
- }
404
- return decimal;
405
- }
package/src/object.ts DELETED
@@ -1,247 +0,0 @@
1
- import iterate from "iterare";
2
-
3
- /**
4
- * 对象对象(等同与convertBean)
5
- * 仅会将classType有的属性进行转换
6
- * * 相当与一次属性过滤
7
- * @param source
8
- * @param classType
9
- */
10
- export const copyBean = <T>(source: any, classType: any): T => {
11
- const result = {};
12
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
13
- Object.keys(classType).forEach((key) => {
14
- result[key] =
15
- source[key] !== undefined ? source[key] : (result[key] = null);
16
- });
17
- return result as T;
18
- };
19
- /**
20
- * 对象转换(等同与copyBean)
21
- * 仅会将classType有的属性进行转换
22
- * 相当与一次属性过滤
23
- * @param source
24
- * @param classType
25
- */
26
- export const convertBean = copyBean;
27
- /**
28
- * 批量对象转换(等同与copyBean)
29
- * 仅会将classType有的属性进行转换
30
- * 相当与一次属性过滤
31
- * @param source
32
- * @param classType
33
- */
34
- export const convertBeans = <T>(
35
- source: any[],
36
- classType: any,
37
- cb?: (target: T, source: any) => void
38
- ): T[] => {
39
- const result = new Array<T>();
40
- for (const bean of source) {
41
- const data = convertBean<T>(bean, classType);
42
- if (cb) {
43
- cb(data, bean);
44
- }
45
- result.push(data);
46
- }
47
- return result;
48
- };
49
- /**
50
- * 创建一个空对象
51
- * 其内各属性都是null
52
- * @param classType
53
- */
54
- export const emptyBean = <T>(classType: any): T => {
55
- const target = {} as T;
56
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
57
- Object.keys(classType).forEach((key) => {
58
- target[key] = null;
59
- });
60
- return target;
61
- };
62
-
63
- /**
64
- * 将一个json数组提取为一个json对象
65
- * @param source 源数组
66
- * @param key 作为新对象的key的字段
67
- * @param value 作为新对象value的字段,不传则将自身为value
68
- */
69
- export const createBeanFromArray = <F, T = F>(
70
- source: F[],
71
- key: keyof F,
72
- value?: keyof F
73
- ): {
74
- [name: string]: T;
75
- } => {
76
- const result: {
77
- [name: string]: T;
78
- } = {};
79
- if (value) {
80
- source.forEach((item) => {
81
- if (item[key]) {
82
- result[`${item[key]}`] = item[value] as unknown as T;
83
- }
84
- });
85
- } else {
86
- source.forEach((item) => {
87
- if (item[key]) {
88
- result[`${item[key]}`] = item as unknown as T;
89
- }
90
- });
91
- }
92
- return result;
93
- };
94
-
95
- /**
96
- * 转换复合对象为指定bean
97
- * @param source
98
- * @param classType
99
- */
100
- export const coverComplexBean = <T>(
101
- source: any,
102
- classType: any
103
- ): { data: T; array: { [key: string]: any[] } } => {
104
- const result = {};
105
- const arrayData = {};
106
- // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
107
- for (const [key, value] of Object.entries(source)) {
108
- if (value instanceof Array) {
109
- arrayData[key] = value;
110
- } else if (typeof value === 'object') {
111
- Object.assign(result, value);
112
- } else {
113
- result[key] = value;
114
- }
115
- }
116
- return {
117
- data: convertBean<T>(result, classType),
118
- array: arrayData
119
- };
120
- };
121
-
122
- /**
123
- * 将目标对象中为空的字段替换为source中对应key的值或者函数返回值
124
- * @param target
125
- * @param source
126
- */
127
- export const fixEmptyPrototy = async (
128
- target: any,
129
- source: {
130
- [key: string]: any;
131
- }
132
- ) => {
133
- for (const [key, fn] of Object.entries(source)) {
134
- if (!target[key]) {
135
- if (typeof fn === 'function') {
136
- target[key] = await fn();
137
- } else {
138
- target[key] = fn;
139
- }
140
- }
141
- }
142
- };
143
-
144
-
145
- export const mixArray = <T>(array: T[], key: keyof T, defKey?: string): { [key: string]: number } => {
146
- const obj = array.map(item => item[key]);
147
- const result: { [k: string]: number } = {};
148
- for (const i of obj) {
149
- let ki = '';
150
- if (i !== undefined && i !== null) {
151
- ki = `${i}`;
152
- } else if (defKey) {
153
- ki = defKey;
154
- }
155
- if (!result[ki]) {
156
- result[ki] = 0;
157
- }
158
- result[ki]!++;
159
- }
160
- return result;
161
- };
162
-
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[] } = {};
165
- for (const i of array) {
166
- let ki = '';
167
- if (i[key] !== undefined && i[key] !== null) {
168
- ki = `${i[key]}`;
169
- } else if (defKey) {
170
- ki = defKey;
171
- }
172
- if (!result[ki]) {
173
- result[ki] = [];
174
- }
175
- if (value) {
176
- result[ki]!.push(i[value] as any);
177
- } else {
178
- result[ki]!.push(i as any);
179
- }
180
- }
181
- return result;
182
- };
183
-
184
- export const array2map = <T = string | number>(array: string[], v: T): { [key: string]: T } => {
185
- const ot: { [key: string]: T } = {};
186
- for (const item of array) {
187
- ot[item] = v;
188
- }
189
- return ot;
190
- };
191
- /**
192
- * 数组分割
193
- * @param datas
194
- * @param config(二选一) everyLength=每组个数(最后一组可能不足次数), groupCount=拆分几组
195
- * @returns T[][]
196
- */
197
- export const arraySplit = <T = any>(datas: T[], { everyLength = 0, groupCount = 0 } = {}) => {
198
- if (groupCount > 0) {
199
- everyLength = Math.floor(datas.length / groupCount + 0.9);
200
- const result: T[][] = [];
201
- for (let i = 0; i < groupCount; i++) {
202
- result.push(datas.slice(i * everyLength, (i + 1) * everyLength));
203
- }
204
- return result;
205
- } else if (everyLength > 0) {
206
- groupCount = Math.ceil(datas.length / everyLength);
207
- const result: T[][] = [];
208
- for (let i = 0; i < groupCount; i++) {
209
- result.push(datas.slice(i * everyLength, (i + 1) * everyLength));
210
- }
211
- return result;
212
- } else {
213
- throw new Error('参数错误!');
214
- }
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
- }
247
-