baja-lite 1.6.4 → 1.6.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.
- package/boot-remote.d.ts +2 -0
- package/{src/boot-remote.ts → boot-remote.js} +63 -64
- package/boot.d.ts +2 -0
- package/{src/boot.ts → boot.js} +163 -170
- package/code.d.ts +2 -0
- package/{src/code.ts → code.js} +405 -414
- package/convert-xml.d.ts +10 -0
- package/{src/convert-xml.ts → convert-xml.js} +410 -460
- package/error.d.ts +5 -0
- package/error.js +13 -0
- package/event.d.ts +10 -0
- package/event.js +38 -0
- package/fn.d.ts +128 -0
- package/fn.js +172 -0
- package/{src/index.ts → index.d.ts} +10 -11
- package/index.js +10 -0
- package/math.d.ts +83 -0
- package/math.js +451 -0
- package/object.d.ts +126 -0
- package/object.js +321 -0
- package/package.json +1 -1
- package/snowflake.d.ts +12 -0
- package/{src/snowflake.ts → snowflake.js} +108 -127
- package/sql.d.ts +2148 -0
- package/sql.js +5370 -0
- package/sqlite.d.ts +32 -0
- package/{src/sqlite.ts → sqlite.js} +156 -157
- package/string.d.ts +17 -0
- package/string.js +105 -0
- package/test-mysql.d.ts +2 -0
- package/test-mysql.js +114 -0
- package/test-postgresql.d.ts +2 -0
- package/{src/test-postgresql.ts → test-postgresql.js} +91 -80
- package/test-sqlite.d.ts +1 -0
- package/{src/test-sqlite.ts → test-sqlite.js} +90 -80
- package/test-xml.d.ts +1 -0
- package/{src/test-xml.ts → test-xml.js} +2 -2
- package/test.d.ts +1 -0
- package/{src/test.ts → test.js} +2 -3
- package/wx/base.d.ts +11 -0
- package/wx/base.js +78 -0
- package/wx/mini.d.ts +52 -0
- package/wx/mini.js +112 -0
- package/wx/organ.d.ts +65 -0
- package/wx/organ.js +171 -0
- package/{src/wx/types.ts → wx/types.d.ts} +560 -549
- package/wx/types.js +1 -0
- package/{src/wx.ts → wx.d.ts} +3 -3
- package/wx.js +3 -0
- package/.eslintignore +0 -7
- package/.eslintrc.cjs +0 -89
- package/.prettierrc +0 -7
- package/.vscode/settings.json +0 -9
- package/ci.js +0 -33
- package/package-cjs.json +0 -17
- package/pnpm-lock.yaml +0 -2840
- package/pnpm-workspace.yaml +0 -2
- package/src/error.ts +0 -11
- package/src/event.ts +0 -34
- package/src/fn.ts +0 -295
- package/src/math.ts +0 -405
- package/src/object.ts +0 -342
- package/src/sql.ts +0 -5529
- package/src/string.ts +0 -111
- package/src/test-mysql.ts +0 -148
- package/src/wx/base.ts +0 -77
- package/src/wx/mini.ts +0 -147
- package/src/wx/organ.ts +0 -290
- package/tsconfig.cjs.json +0 -42
- package/tsconfig.json +0 -44
- package/xml/event-report.xml +0 -13
- package/yarn.lock +0 -1977
- /package/{Readme.md → README.md} +0 -0
package/src/object.ts
DELETED
|
@@ -1,342 +0,0 @@
|
|
|
1
|
-
import { SetEx } from 'baja-lite-field';
|
|
2
|
-
import * as ite from 'iterare';
|
|
3
|
-
const iterate = ite.iterate;
|
|
4
|
-
/**
|
|
5
|
-
* 对象对象(等同与convertBean)
|
|
6
|
-
* 仅会将classType有的属性进行转换
|
|
7
|
-
* * 相当与一次属性过滤
|
|
8
|
-
* @param source
|
|
9
|
-
* @param classType
|
|
10
|
-
*/
|
|
11
|
-
export const copyBean = <T>(source: any, classType: any): T => {
|
|
12
|
-
const result = {};
|
|
13
|
-
Object.keys(classType).forEach((key) => {
|
|
14
|
-
result[key] = source[key] !== undefined ? source[key] : (result[key] = null);
|
|
15
|
-
});
|
|
16
|
-
return result as T;
|
|
17
|
-
};
|
|
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>(source: any[], classType: any, cb?: (target: T, source: any) => void): T[] => {
|
|
35
|
-
const result = new Array<T>();
|
|
36
|
-
for (const bean of source) {
|
|
37
|
-
const data = convertBean<T>(bean, classType);
|
|
38
|
-
if (cb) {
|
|
39
|
-
cb(data, bean);
|
|
40
|
-
}
|
|
41
|
-
result.push(data);
|
|
42
|
-
}
|
|
43
|
-
return result;
|
|
44
|
-
};
|
|
45
|
-
/**
|
|
46
|
-
* 创建一个空对象
|
|
47
|
-
* 其内各属性都是null
|
|
48
|
-
* @param classType
|
|
49
|
-
*/
|
|
50
|
-
export const emptyBean = <T>(classType: any): T => {
|
|
51
|
-
const target = {} as T;
|
|
52
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
53
|
-
Object.keys(classType).forEach((key) => {
|
|
54
|
-
target[key] = null;
|
|
55
|
-
});
|
|
56
|
-
return target;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* 将一个json数组提取为一个json对象
|
|
61
|
-
* @param source 源数组
|
|
62
|
-
* @param key 作为新对象的key的字段
|
|
63
|
-
* @param value 作为新对象value的字段,不传则将自身为value
|
|
64
|
-
*/
|
|
65
|
-
export const createBeanFromArray = <F, T = F>(
|
|
66
|
-
source: F[],
|
|
67
|
-
key: keyof F,
|
|
68
|
-
value?: keyof F,
|
|
69
|
-
): {
|
|
70
|
-
[name: string]: T;
|
|
71
|
-
} => {
|
|
72
|
-
const result: {
|
|
73
|
-
[name: string]: T;
|
|
74
|
-
} = {};
|
|
75
|
-
if (value) {
|
|
76
|
-
source.forEach((item) => {
|
|
77
|
-
if (item[key]) {
|
|
78
|
-
result[`${item[key]}`] = item[value] as unknown as T;
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
} else {
|
|
82
|
-
source.forEach((item) => {
|
|
83
|
-
if (item[key]) {
|
|
84
|
-
result[`${item[key]}`] = item as unknown as T;
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
return result;
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* 转换复合对象为指定bean
|
|
93
|
-
* @param source
|
|
94
|
-
* @param classType
|
|
95
|
-
*/
|
|
96
|
-
export const coverComplexBean = <T>(source: any, classType: any): { data: T; array: { [key: string]: any[] } } => {
|
|
97
|
-
const result = {};
|
|
98
|
-
const arrayData = {};
|
|
99
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
100
|
-
for (const [key, value] of Object.entries(source)) {
|
|
101
|
-
if (value instanceof Array) {
|
|
102
|
-
arrayData[key] = value;
|
|
103
|
-
} else if (typeof value === 'object') {
|
|
104
|
-
Object.assign(result, value);
|
|
105
|
-
} else {
|
|
106
|
-
result[key] = value;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
return {
|
|
110
|
-
data: convertBean<T>(result, classType),
|
|
111
|
-
array: arrayData,
|
|
112
|
-
};
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
/**
|
|
116
|
-
* 将目标对象中为空的字段替换为source中对应key的值或者函数返回值
|
|
117
|
-
* @param target
|
|
118
|
-
* @param source
|
|
119
|
-
*/
|
|
120
|
-
export const fixEmptyPrototy = async (
|
|
121
|
-
target: any,
|
|
122
|
-
source: {
|
|
123
|
-
[key: string]: any;
|
|
124
|
-
},
|
|
125
|
-
) => {
|
|
126
|
-
for (const [key, fn] of Object.entries(source)) {
|
|
127
|
-
if (!target[key]) {
|
|
128
|
-
if (typeof fn === 'function') {
|
|
129
|
-
target[key] = await fn();
|
|
130
|
-
} else {
|
|
131
|
-
target[key] = fn;
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* 1. 统计array中某个字段key的数量:{ [k: string]: number }
|
|
139
|
-
*
|
|
140
|
-
* @param array T组成的array,数据源
|
|
141
|
-
* @param key 返回结果{ [k: string]: number }的key
|
|
142
|
-
* @param defKey 如果array中某个对象没有key字段,则归类到默认key中
|
|
143
|
-
* @returns
|
|
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
|
-
* 1. 将T组成的array按照某个字段(key)提取为{ [key: string]: V[] }
|
|
164
|
-
* @param array T组成的array,数据源
|
|
165
|
-
* @param key 返回结果{ [key: string]: V[] }的key
|
|
166
|
-
* @param value 返回结果{ [key: string]: V[] }中的V可以是T,也可以是T的某个字段(指定value参数)
|
|
167
|
-
* @param defKey 如果array中某个对象没有key字段,则归类到默认key中
|
|
168
|
-
* @returns
|
|
169
|
-
*/
|
|
170
|
-
export const mixList = <T, V = T>(array: T[], key: keyof T, value?: keyof T, defKey?: string): { [key: string]: V[] } => {
|
|
171
|
-
const result: { [k: string]: V[] } = {};
|
|
172
|
-
for (const i of array) {
|
|
173
|
-
let ki = '';
|
|
174
|
-
if (i[key] !== undefined && i[key] !== null) {
|
|
175
|
-
ki = `${i[key]}`;
|
|
176
|
-
} else if (defKey) {
|
|
177
|
-
ki = defKey;
|
|
178
|
-
}
|
|
179
|
-
if (!result[ki]) {
|
|
180
|
-
result[ki] = [];
|
|
181
|
-
}
|
|
182
|
-
if (value) {
|
|
183
|
-
result[ki]!.push(i[value] as any);
|
|
184
|
-
} else {
|
|
185
|
-
result[ki]!.push(i as any);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
return result;
|
|
189
|
-
};
|
|
190
|
-
|
|
191
|
-
/**
|
|
192
|
-
* ## 仿照Object.assign的数组方法
|
|
193
|
-
* ### 1. 用法参照
|
|
194
|
-
* ```
|
|
195
|
-
* // ID 是 数组中对象的关键字段,用来区分同一条记录.支持多个字段(联合主键场景)
|
|
196
|
-
* // 与assign的逻辑相同,后面的数组会覆盖前面数组的相同对象的同名字段
|
|
197
|
-
* const result = assignArray('ID', Array1, Array2, Array3...);
|
|
198
|
-
* ```
|
|
199
|
-
* @param key
|
|
200
|
-
* @param arrays
|
|
201
|
-
* @returns
|
|
202
|
-
*/
|
|
203
|
-
export const assignArray = <T>(key: keyof T | (keyof T)[] | ((t1: T, t2: T) => boolean), ...arrays: T[][]): T[] => {
|
|
204
|
-
const result: T[] = [];
|
|
205
|
-
const match = key instanceof Function ? key : key instanceof Array ? (t1: T, t2: T) => key.every((k) => t1[k] === t2[k]) : (t1: T, t2: T) => t1[key] === t2[key];
|
|
206
|
-
for (const array of arrays) {
|
|
207
|
-
for (const item of array) {
|
|
208
|
-
const find = result.find((i) => match(i, item));
|
|
209
|
-
if (find) {
|
|
210
|
-
Object.assign(find, item);
|
|
211
|
-
} else {
|
|
212
|
-
result.push(item);
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
return result;
|
|
217
|
-
};
|
|
218
|
-
|
|
219
|
-
export const array2map = <T = string | number>(array: string[], v: T): { [key: string]: T } => {
|
|
220
|
-
const ot: { [key: string]: T } = {};
|
|
221
|
-
for (const item of array) {
|
|
222
|
-
ot[item] = v;
|
|
223
|
-
}
|
|
224
|
-
return ot;
|
|
225
|
-
};
|
|
226
|
-
/**
|
|
227
|
-
* 数组分割
|
|
228
|
-
* @param datas
|
|
229
|
-
* @param config(二选一) everyLength=每组个数(最后一组可能不足次数), groupCount=拆分几组
|
|
230
|
-
* @returns T[][]
|
|
231
|
-
*/
|
|
232
|
-
export const arraySplit = <T = any>(datas: T[], { everyLength = 0, groupCount = 0 } = {}) => {
|
|
233
|
-
if (groupCount > 0) {
|
|
234
|
-
everyLength = Math.floor(datas.length / groupCount + 0.9);
|
|
235
|
-
const result: T[][] = [];
|
|
236
|
-
for (let i = 0; i < groupCount; i++) {
|
|
237
|
-
result.push(datas.slice(i * everyLength, (i + 1) * everyLength));
|
|
238
|
-
}
|
|
239
|
-
return result;
|
|
240
|
-
} else if (everyLength > 0) {
|
|
241
|
-
groupCount = Math.ceil(datas.length / everyLength);
|
|
242
|
-
const result: T[][] = [];
|
|
243
|
-
for (let i = 0; i < groupCount; i++) {
|
|
244
|
-
result.push(datas.slice(i * everyLength, (i + 1) * everyLength));
|
|
245
|
-
}
|
|
246
|
-
return result;
|
|
247
|
-
} else {
|
|
248
|
-
throw new Error('参数错误!');
|
|
249
|
-
}
|
|
250
|
-
};
|
|
251
|
-
/**
|
|
252
|
-
* 合并对象(浅),忽略后续参数的null、undefined、空字符串
|
|
253
|
-
* @param source
|
|
254
|
-
* @param os
|
|
255
|
-
* @returns
|
|
256
|
-
*/
|
|
257
|
-
export const assginObject = <T extends Object>(source: T, ...os: T[]) => {
|
|
258
|
-
os.forEach(o => Object.entries(o).forEach(([key, value]) => value !== null && value !== undefined && `${value}`.trim() !== '' && (source[key] = value)));
|
|
259
|
-
}
|
|
260
|
-
/**
|
|
261
|
-
* 去除数组中重复数据,同时可以通过each函数为每个数据执行某项操作
|
|
262
|
-
* @param source
|
|
263
|
-
* @param each
|
|
264
|
-
*/
|
|
265
|
-
export const distinctArray = <T extends Object>(
|
|
266
|
-
source: T[],
|
|
267
|
-
keys: (keyof T)[],
|
|
268
|
-
each?: (data: T) => void
|
|
269
|
-
): T[] => {
|
|
270
|
-
const set = new SetEx<T & { ___id: string }>({ key: '___id' });
|
|
271
|
-
for (const item of source) {
|
|
272
|
-
const _item = item as T & { ___id: string };
|
|
273
|
-
_item.___id = keys.map(key => item[key]).join('_');
|
|
274
|
-
if (each) {
|
|
275
|
-
each(item);
|
|
276
|
-
}
|
|
277
|
-
set.add(_item);
|
|
278
|
-
}
|
|
279
|
-
return set.toArray();
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
const P2CEX = /[A-Z]/g;
|
|
283
|
-
export const P2C = (pro: string, IF = true) => (IF ? pro.replace(P2CEX, (a: string) => `_${a.toLowerCase()}`) : pro);
|
|
284
|
-
const C2PEX = /_([a-z])/g;
|
|
285
|
-
export const C2P = (pro: string, IF = true) => (IF ? pro.replace(C2PEX, (a: string, b: string) => `${b.toUpperCase()}`) : pro);
|
|
286
|
-
|
|
287
|
-
export function C2P2<T extends Object = any, L extends Object = T>(datas: L[], hump?: boolean, convert?: Record<string, (data: any) => any>): T[];
|
|
288
|
-
export function C2P2<T extends Object = any, L extends Object = T>(datas: L, hump?: boolean, convert?: Record<string, (data: any) => any>): T;
|
|
289
|
-
export function C2P2<T extends Object = any, L extends Object = T>(datas: L | L[], hump?: boolean, convert?: Record<string, (data: any) => any>): T | T[] {
|
|
290
|
-
if (datas instanceof Array) {
|
|
291
|
-
return iterate(datas)
|
|
292
|
-
.map((data: L) =>
|
|
293
|
-
Object.fromEntries(
|
|
294
|
-
Object.entries(data).map(([K, V]) => {
|
|
295
|
-
if (hump) {
|
|
296
|
-
K = C2P(K);
|
|
297
|
-
}
|
|
298
|
-
if (convert && convert[K]) {
|
|
299
|
-
return [K, convert[K]!(V)];
|
|
300
|
-
} else {
|
|
301
|
-
return [K, V];
|
|
302
|
-
}
|
|
303
|
-
}),
|
|
304
|
-
),
|
|
305
|
-
)
|
|
306
|
-
.toArray() as unknown as T[];
|
|
307
|
-
} else if (datas) {
|
|
308
|
-
return Object.fromEntries(
|
|
309
|
-
Object.entries(datas).map(([K, V]) => {
|
|
310
|
-
if (hump) {
|
|
311
|
-
K = C2P(K);
|
|
312
|
-
}
|
|
313
|
-
if (convert && convert[K]) {
|
|
314
|
-
return [K, convert[K]!(V)];
|
|
315
|
-
} else {
|
|
316
|
-
return [K, V];
|
|
317
|
-
}
|
|
318
|
-
}),
|
|
319
|
-
) as unknown as T;
|
|
320
|
-
} else {
|
|
321
|
-
return datas;
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
export function P2C2<T extends Object = any, L extends Object = T>(datas: L[]): T[];
|
|
326
|
-
export function P2C2<T extends Object = any, L extends Object = T>(datas: L): T;
|
|
327
|
-
export function P2C2<T extends Object = any, L extends Object = T>(datas: L | L[]): T | T[] {
|
|
328
|
-
if (datas instanceof Array) {
|
|
329
|
-
return iterate(datas)
|
|
330
|
-
.map((data: L) => Object.fromEntries<T>(Object.entries(data).map(([K, V]) => [P2C(K), V])))
|
|
331
|
-
.toArray() as unknown as T[];
|
|
332
|
-
} else if (datas) {
|
|
333
|
-
return Object.fromEntries<T>(Object.entries(datas).map(([K, V]) => [P2C(K), V])) as unknown as T;
|
|
334
|
-
} else {
|
|
335
|
-
return datas;
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
export function fillArrayToMinLength<T>(arr: T[], minLength: number, fillValue: T): T[] {
|
|
340
|
-
while (arr.length < minLength) arr.push(fillValue);
|
|
341
|
-
return arr;
|
|
342
|
-
}
|