baja-lite 1.0.0

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 (55) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +1 -0
  3. package/cjs/constant.d.ts +13 -0
  4. package/cjs/constant.js +19 -0
  5. package/cjs/error.d.ts +5 -0
  6. package/cjs/error.js +16 -0
  7. package/cjs/fn.d.ts +128 -0
  8. package/cjs/fn.js +169 -0
  9. package/cjs/index.d.ts +8 -0
  10. package/cjs/index.js +24 -0
  11. package/cjs/math.d.ts +69 -0
  12. package/cjs/math.js +435 -0
  13. package/cjs/now.d.ts +7 -0
  14. package/cjs/now.js +26 -0
  15. package/cjs/object.d.ts +77 -0
  16. package/cjs/object.js +212 -0
  17. package/cjs/set-ex.d.ts +171 -0
  18. package/cjs/set-ex.js +336 -0
  19. package/cjs/sql.d.ts +1216 -0
  20. package/cjs/sql.js +3380 -0
  21. package/cjs/string.d.ts +18 -0
  22. package/cjs/string.js +124 -0
  23. package/cjs/test-mysql.d.ts +1 -0
  24. package/cjs/test-mysql.js +108 -0
  25. package/cjs/test-sqlite.d.ts +1 -0
  26. package/cjs/test-sqlite.js +89 -0
  27. package/cjs/test.d.ts +1 -0
  28. package/cjs/test.js +4 -0
  29. package/es/constant.d.ts +13 -0
  30. package/es/constant.js +16 -0
  31. package/es/error.d.ts +5 -0
  32. package/es/error.js +13 -0
  33. package/es/fn.d.ts +128 -0
  34. package/es/fn.js +162 -0
  35. package/es/index.d.ts +8 -0
  36. package/es/index.js +8 -0
  37. package/es/math.d.ts +69 -0
  38. package/es/math.js +414 -0
  39. package/es/now.d.ts +7 -0
  40. package/es/now.js +16 -0
  41. package/es/object.d.ts +77 -0
  42. package/es/object.js +196 -0
  43. package/es/set-ex.d.ts +171 -0
  44. package/es/set-ex.js +332 -0
  45. package/es/sql.d.ts +1216 -0
  46. package/es/sql.js +3338 -0
  47. package/es/string.d.ts +18 -0
  48. package/es/string.js +109 -0
  49. package/es/test-mysql.d.ts +1 -0
  50. package/es/test-mysql.js +106 -0
  51. package/es/test-sqlite.d.ts +1 -0
  52. package/es/test-sqlite.js +87 -0
  53. package/es/test.d.ts +1 -0
  54. package/es/test.js +2 -0
  55. package/package.json +66 -0
package/es/object.d.ts ADDED
@@ -0,0 +1,77 @@
1
+ /**
2
+ * 对象对象(等同与convertBean)
3
+ * 仅会将classType有的属性进行转换
4
+ * 相当与一次属性过滤
5
+ * @param source
6
+ * @param classType
7
+ */
8
+ export declare const copyBean: <T>(source: any, classType: any) => T;
9
+ /**
10
+ * 对象转换(等同与copyBean)
11
+ * 仅会将classType有的属性进行转换
12
+ * 相当与一次属性过滤
13
+ * @param source
14
+ * @param classType
15
+ */
16
+ export declare const convertBean: <T>(source: any, classType: any) => T;
17
+ /**
18
+ * 批量对象转换(等同与copyBean)
19
+ * 仅会将classType有的属性进行转换
20
+ * 相当与一次属性过滤
21
+ * @param source
22
+ * @param classType
23
+ */
24
+ export declare const convertBeans: <T>(source: any[], classType: any, cb?: (target: T, source: any) => void) => T[];
25
+ /**
26
+ * 创建一个空对象
27
+ * 其内各属性都是null
28
+ * @param classType
29
+ */
30
+ export declare const emptyBean: <T>(classType: any) => T;
31
+ /**
32
+ * 将一个json数组提取为一个json对象
33
+ * @param source 源数组
34
+ * @param key 作为新对象的key的字段
35
+ * @param value 作为新对象value的字段,不传则将自身为value
36
+ */
37
+ export declare const createBeanFromArray: <F, T = F>(source: F[], key: keyof F, value?: keyof F) => {
38
+ [name: string]: T;
39
+ };
40
+ /**
41
+ * 转换复合对象为指定bean
42
+ * @param source
43
+ * @param classType
44
+ */
45
+ export declare const coverComplexBean: <T>(source: any, classType: any) => {
46
+ data: T;
47
+ array: {
48
+ [key: string]: any[];
49
+ };
50
+ };
51
+ /**
52
+ * 将目标对象中为空的字段替换为source中对应key的值或者函数返回值
53
+ * @param target
54
+ * @param source
55
+ */
56
+ export declare const fixEmptyPrototy: (target: any, source: {
57
+ [key: string]: any;
58
+ }) => Promise<void>;
59
+ export declare const mixArray: <T>(array: T[], key: keyof T, defKey?: string) => {
60
+ [key: string]: number;
61
+ };
62
+ export declare const mixList: <T, V = T>(array: T[], key: keyof T, value?: keyof T, defKey?: string) => {
63
+ [key: string]: V[];
64
+ };
65
+ export declare const array2map: <T = string | number>(array: string[], v: T) => {
66
+ [key: string]: T;
67
+ };
68
+ /**
69
+ * 数组分割
70
+ * @param datas
71
+ * @param config(二选一) everyLength=每组个数(最后一组可能不足次数), groupCount=拆分几组
72
+ * @returns T[][]
73
+ */
74
+ export declare const arraySplit: <T = any>(datas: T[], { everyLength, groupCount }?: {
75
+ everyLength?: number | undefined;
76
+ groupCount?: number | undefined;
77
+ }) => T[][];
package/es/object.js ADDED
@@ -0,0 +1,196 @@
1
+ import lodash from 'lodash';
2
+ /**
3
+ * 对象对象(等同与convertBean)
4
+ * 仅会将classType有的属性进行转换
5
+ * 相当与一次属性过滤
6
+ * @param source
7
+ * @param classType
8
+ */
9
+ export const copyBean = (source, classType) => {
10
+ const result = {};
11
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
12
+ Object.keys(classType).forEach((key) => {
13
+ result[key] =
14
+ source[key] !== undefined ? source[key] : (result[key] = null);
15
+ });
16
+ return result;
17
+ };
18
+ /**
19
+ * 对象转换(等同与copyBean)
20
+ * 仅会将classType有的属性进行转换
21
+ * 相当与一次属性过滤
22
+ * @param source
23
+ * @param classType
24
+ */
25
+ export const convertBean = copyBean;
26
+ /**
27
+ * 批量对象转换(等同与copyBean)
28
+ * 仅会将classType有的属性进行转换
29
+ * 相当与一次属性过滤
30
+ * @param source
31
+ * @param classType
32
+ */
33
+ export const convertBeans = (source, classType, cb) => {
34
+ const result = new Array();
35
+ for (const bean of source) {
36
+ const data = convertBean(bean, classType);
37
+ if (cb) {
38
+ cb(data, bean);
39
+ }
40
+ result.push(data);
41
+ }
42
+ return result;
43
+ };
44
+ /**
45
+ * 创建一个空对象
46
+ * 其内各属性都是null
47
+ * @param classType
48
+ */
49
+ export const emptyBean = (classType) => {
50
+ const target = {};
51
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
52
+ Object.keys(classType).forEach((key) => {
53
+ target[key] = null;
54
+ });
55
+ return target;
56
+ };
57
+ /**
58
+ * 将一个json数组提取为一个json对象
59
+ * @param source 源数组
60
+ * @param key 作为新对象的key的字段
61
+ * @param value 作为新对象value的字段,不传则将自身为value
62
+ */
63
+ export const createBeanFromArray = (source, key, value) => {
64
+ const result = {};
65
+ if (value) {
66
+ source.forEach((item) => {
67
+ if (item[key]) {
68
+ result[`${item[key]}`] = item[value];
69
+ }
70
+ });
71
+ }
72
+ else {
73
+ source.forEach((item) => {
74
+ if (item[key]) {
75
+ result[`${item[key]}`] = item;
76
+ }
77
+ });
78
+ }
79
+ return result;
80
+ };
81
+ /**
82
+ * 转换复合对象为指定bean
83
+ * @param source
84
+ * @param classType
85
+ */
86
+ export const coverComplexBean = (source, classType) => {
87
+ const result = {};
88
+ const arrayData = {};
89
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
90
+ for (const [key, value] of Object.entries(source)) {
91
+ if (lodash.isArray(value)) {
92
+ arrayData[key] = value;
93
+ }
94
+ else if (lodash.isObject(value)) {
95
+ lodash.assign(result, value);
96
+ }
97
+ else {
98
+ result[key] = value;
99
+ }
100
+ }
101
+ return {
102
+ data: convertBean(result, classType),
103
+ array: arrayData
104
+ };
105
+ };
106
+ /**
107
+ * 将目标对象中为空的字段替换为source中对应key的值或者函数返回值
108
+ * @param target
109
+ * @param source
110
+ */
111
+ export const fixEmptyPrototy = async (target, source) => {
112
+ for (const [key, fn] of Object.entries(source)) {
113
+ if (!target[key]) {
114
+ if (typeof fn === 'function') {
115
+ target[key] = await fn();
116
+ }
117
+ else {
118
+ target[key] = fn;
119
+ }
120
+ }
121
+ }
122
+ };
123
+ export const mixArray = (array, key, defKey) => {
124
+ const obj = array.map(item => item[key]);
125
+ const result = {};
126
+ for (const i of obj) {
127
+ let ki = '';
128
+ if (i !== undefined && i !== null) {
129
+ ki = `${i}`;
130
+ }
131
+ else if (defKey) {
132
+ ki = defKey;
133
+ }
134
+ if (!result[ki]) {
135
+ result[ki] = 0;
136
+ }
137
+ result[ki]++;
138
+ }
139
+ return result;
140
+ };
141
+ export const mixList = (array, key, value, defKey) => {
142
+ const result = {};
143
+ for (const i of array) {
144
+ let ki = '';
145
+ if (i[key] !== undefined && i[key] !== null) {
146
+ ki = `${i[key]}`;
147
+ }
148
+ else if (defKey) {
149
+ ki = defKey;
150
+ }
151
+ if (!result[ki]) {
152
+ result[ki] = [];
153
+ }
154
+ if (value) {
155
+ result[ki].push(i[value]);
156
+ }
157
+ else {
158
+ result[ki].push(i);
159
+ }
160
+ }
161
+ return result;
162
+ };
163
+ export const array2map = (array, v) => {
164
+ const ot = {};
165
+ for (const item of array) {
166
+ ot[item] = v;
167
+ }
168
+ return ot;
169
+ };
170
+ /**
171
+ * 数组分割
172
+ * @param datas
173
+ * @param config(二选一) everyLength=每组个数(最后一组可能不足次数), groupCount=拆分几组
174
+ * @returns T[][]
175
+ */
176
+ export const arraySplit = (datas, { everyLength = 0, groupCount = 0 } = {}) => {
177
+ if (groupCount > 0) {
178
+ everyLength = Math.floor(datas.length / groupCount + 0.9);
179
+ const result = [];
180
+ for (let i = 0; i < groupCount; i++) {
181
+ result.push(datas.slice(i * everyLength, (i + 1) * everyLength));
182
+ }
183
+ return result;
184
+ }
185
+ else if (everyLength > 0) {
186
+ groupCount = Math.ceil(datas.length / everyLength);
187
+ const result = [];
188
+ for (let i = 0; i < groupCount; i++) {
189
+ result.push(datas.slice(i * everyLength, (i + 1) * everyLength));
190
+ }
191
+ return result;
192
+ }
193
+ else {
194
+ throw new Error('参数错误!');
195
+ }
196
+ };
package/es/set-ex.d.ts ADDED
@@ -0,0 +1,171 @@
1
+ export declare class SetEx<T> extends Set {
2
+ private uniqueKey;
3
+ private whenOnExist?;
4
+ private whenOnNotExist?;
5
+ private replaceItemWhenExits;
6
+ /**
7
+ * @param key 识别是否存在的对象的属性名
8
+ * @param onExist 当存在时作何操作? oldData/newData 哪个将添加到set,由replaceItemWhenExits决定,默认是oldData生效
9
+ * @param onNotExist 当不存在时作何操作?
10
+ * @param replaceWhenExits 当存在时是否覆盖?
11
+ * @param values 初始数组
12
+ */
13
+ constructor(key: keyof T | {
14
+ key: keyof T;
15
+ onExist?: (oldData: T, newData: T) => void;
16
+ onNotExist?: (newData: T) => void;
17
+ replaceWhenExits?: boolean;
18
+ values?: ReadonlyArray<T> | null;
19
+ }, onExist?: (oldData: T, newData: T) => void, replaceWhenExits?: boolean, values?: ReadonlyArray<T> | null, onNotExist?: (newData: T) => void);
20
+ /**
21
+ *
22
+ * 添加返回
23
+ * @param {T} value
24
+ * @returns {this} 当前对象
25
+ */
26
+ add(value: T): this;
27
+ /**
28
+ * 批量添加
29
+ * @param values
30
+ * @returns 当前对象
31
+ */
32
+ addAll(...values: T[]): this;
33
+ /**
34
+ *
35
+ * 添加
36
+ * @param {T} value
37
+ * @returns {T} 添加成功的对象:可能是新加入集合的,也可能是原本存在的
38
+ */
39
+ add2(value: T): T;
40
+ /**
41
+ *
42
+ * 添加并返回添加成功的对象:可能是新加入集合的,也可能是原本存在的
43
+ * @param {T} values
44
+ * @returns {T}
45
+ */
46
+ addAll2(values: T[]): T[];
47
+ /**
48
+ * 用key找到匹配的第一个对象
49
+ * @param {*} value 这是对象的关键属性,而非对象
50
+ * @returns {(T | null)}
51
+ */
52
+ find(value: T[keyof T]): T | null;
53
+ /**
54
+ * 用key找到匹配的所有对象
55
+ * @param {*} value 这是对象的关键属性,而非对象
56
+ * @returns {T[]}
57
+ */
58
+ findAll(value: T[keyof T]): T[];
59
+ /**
60
+ *
61
+ * 用函数回调找到匹配的第一个对象
62
+ * @param {(item: T) => boolean} fn
63
+ * @returns {T[]}
64
+ */
65
+ filter(fn: (item: T) => boolean): T | null;
66
+ /**
67
+ *
68
+ * 用函数回调找到匹配的所有对象
69
+ * @param {(item: T) => boolean} fn
70
+ * @returns {T[]}
71
+ */
72
+ filterAll(fn: (item: T) => boolean): T[];
73
+ /**
74
+ *
75
+ * 是否存在key对应的对象
76
+ * @param {*} value 这是对象的关键属性,而非对象
77
+ * @returns {boolean}
78
+ */
79
+ has(value: T[keyof T]): boolean;
80
+ /**
81
+ * 转为数组
82
+ * @param param0
83
+ * @returns
84
+ */
85
+ toArray({ sort, each, filter, map }?: {
86
+ sort?: (a: T, b: T) => number;
87
+ each?: (a: T) => void;
88
+ filter?: (a: T) => boolean;
89
+ map?: (a: T) => T;
90
+ }): T[];
91
+ /**
92
+ * 转为JSON对象
93
+ * @param key
94
+ * @param value
95
+ * @param param2
96
+ * @returns
97
+ */
98
+ toJSON<L = T>(key: keyof T, value?: keyof T, { sort, each, filter, map }?: {
99
+ sort?: (a: T, b: T) => number;
100
+ each?: (a: T) => void;
101
+ filter?: (a: T) => boolean;
102
+ map?: (a: T) => T;
103
+ }): {
104
+ [k: string]: L;
105
+ };
106
+ /**
107
+ *
108
+ * 删除key对应的对象
109
+ * @param {*} value 这是对象的关键属性,而非对象
110
+ * @returns {boolean}
111
+ */
112
+ delete(value: T[keyof T]): boolean;
113
+ /**
114
+ *
115
+ * 重置
116
+ * @param {keyof T} key
117
+ * @param {(oldData: T, newData: T) => void} [onExist]
118
+ * @param {boolean} [replaceWhenExits=false]
119
+ */
120
+ reset({ key, onExist, onNotExist, replaceWhenExits }: {
121
+ key?: keyof T;
122
+ onExist?: (oldData: T, newData: T) => void | null;
123
+ onNotExist?: (newData: T) => void | null;
124
+ replaceWhenExits?: boolean;
125
+ }): this;
126
+ /**
127
+ *
128
+ * @param param0 转为JSON对象,value可能是数组
129
+ * @returns
130
+ */
131
+ toJSONArray({ sort, each, filter, map }?: {
132
+ sort?: (a: T, b: T) => number;
133
+ each?: (a: T) => void;
134
+ filter?: (a: T) => boolean;
135
+ map?: (a: T) => T;
136
+ }): {
137
+ [k: string]: T[keyof T][];
138
+ };
139
+ /**
140
+ * 转为hot-table支持的数组
141
+ * @param param0
142
+ * @param keys
143
+ * @returns
144
+ */
145
+ toDataGrid({ sort, each, filter, map }?: {
146
+ sort?: (a: T, b: T) => number;
147
+ each?: (a: T) => void;
148
+ filter?: (a: T) => boolean;
149
+ map?: (a: T) => T;
150
+ }, ...keys: (keyof T)[]): (keyof T | T[keyof T])[][];
151
+ /**
152
+ * 转为饼图支持的数组
153
+ * @param param0
154
+ * @param keys
155
+ * @returns
156
+ */
157
+ toPieGrid({ sort, each, filter, map }?: {
158
+ sort?: (a: T, b: T) => number;
159
+ each?: (a: T) => void;
160
+ filter?: (a: T) => boolean;
161
+ map?: (a: T) => T;
162
+ }, ...keys: (keyof T)[]): {
163
+ [k: string]: {
164
+ value: T[keyof T];
165
+ name: T[keyof T];
166
+ }[];
167
+ };
168
+ set onExist(onExist: ((oldData: T, newData: T) => void) | undefined);
169
+ set key(key: keyof T);
170
+ set replaceWhenExits(replaceWhenExits: boolean);
171
+ }