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/cjs/object.js ADDED
@@ -0,0 +1,212 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.arraySplit = exports.array2map = exports.mixList = exports.mixArray = exports.fixEmptyPrototy = exports.coverComplexBean = exports.createBeanFromArray = exports.emptyBean = exports.convertBeans = exports.convertBean = exports.copyBean = void 0;
7
+ const lodash_1 = __importDefault(require("lodash"));
8
+ /**
9
+ * 对象对象(等同与convertBean)
10
+ * 仅会将classType有的属性进行转换
11
+ * 相当与一次属性过滤
12
+ * @param source
13
+ * @param classType
14
+ */
15
+ const copyBean = (source, classType) => {
16
+ const result = {};
17
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
18
+ Object.keys(classType).forEach((key) => {
19
+ result[key] =
20
+ source[key] !== undefined ? source[key] : (result[key] = null);
21
+ });
22
+ return result;
23
+ };
24
+ exports.copyBean = copyBean;
25
+ /**
26
+ * 对象转换(等同与copyBean)
27
+ * 仅会将classType有的属性进行转换
28
+ * 相当与一次属性过滤
29
+ * @param source
30
+ * @param classType
31
+ */
32
+ exports.convertBean = exports.copyBean;
33
+ /**
34
+ * 批量对象转换(等同与copyBean)
35
+ * 仅会将classType有的属性进行转换
36
+ * 相当与一次属性过滤
37
+ * @param source
38
+ * @param classType
39
+ */
40
+ const convertBeans = (source, classType, cb) => {
41
+ const result = new Array();
42
+ for (const bean of source) {
43
+ const data = (0, exports.convertBean)(bean, classType);
44
+ if (cb) {
45
+ cb(data, bean);
46
+ }
47
+ result.push(data);
48
+ }
49
+ return result;
50
+ };
51
+ exports.convertBeans = convertBeans;
52
+ /**
53
+ * 创建一个空对象
54
+ * 其内各属性都是null
55
+ * @param classType
56
+ */
57
+ const emptyBean = (classType) => {
58
+ const target = {};
59
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
60
+ Object.keys(classType).forEach((key) => {
61
+ target[key] = null;
62
+ });
63
+ return target;
64
+ };
65
+ exports.emptyBean = emptyBean;
66
+ /**
67
+ * 将一个json数组提取为一个json对象
68
+ * @param source 源数组
69
+ * @param key 作为新对象的key的字段
70
+ * @param value 作为新对象value的字段,不传则将自身为value
71
+ */
72
+ const createBeanFromArray = (source, key, value) => {
73
+ const result = {};
74
+ if (value) {
75
+ source.forEach((item) => {
76
+ if (item[key]) {
77
+ result[`${item[key]}`] = item[value];
78
+ }
79
+ });
80
+ }
81
+ else {
82
+ source.forEach((item) => {
83
+ if (item[key]) {
84
+ result[`${item[key]}`] = item;
85
+ }
86
+ });
87
+ }
88
+ return result;
89
+ };
90
+ exports.createBeanFromArray = createBeanFromArray;
91
+ /**
92
+ * 转换复合对象为指定bean
93
+ * @param source
94
+ * @param classType
95
+ */
96
+ const coverComplexBean = (source, classType) => {
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 (lodash_1.default.isArray(value)) {
102
+ arrayData[key] = value;
103
+ }
104
+ else if (lodash_1.default.isObject(value)) {
105
+ lodash_1.default.assign(result, value);
106
+ }
107
+ else {
108
+ result[key] = value;
109
+ }
110
+ }
111
+ return {
112
+ data: (0, exports.convertBean)(result, classType),
113
+ array: arrayData
114
+ };
115
+ };
116
+ exports.coverComplexBean = coverComplexBean;
117
+ /**
118
+ * 将目标对象中为空的字段替换为source中对应key的值或者函数返回值
119
+ * @param target
120
+ * @param source
121
+ */
122
+ const fixEmptyPrototy = async (target, source) => {
123
+ for (const [key, fn] of Object.entries(source)) {
124
+ if (!target[key]) {
125
+ if (typeof fn === 'function') {
126
+ target[key] = await fn();
127
+ }
128
+ else {
129
+ target[key] = fn;
130
+ }
131
+ }
132
+ }
133
+ };
134
+ exports.fixEmptyPrototy = fixEmptyPrototy;
135
+ const mixArray = (array, key, defKey) => {
136
+ const obj = array.map(item => item[key]);
137
+ const result = {};
138
+ for (const i of obj) {
139
+ let ki = '';
140
+ if (i !== undefined && i !== null) {
141
+ ki = `${i}`;
142
+ }
143
+ else if (defKey) {
144
+ ki = defKey;
145
+ }
146
+ if (!result[ki]) {
147
+ result[ki] = 0;
148
+ }
149
+ result[ki]++;
150
+ }
151
+ return result;
152
+ };
153
+ exports.mixArray = mixArray;
154
+ const mixList = (array, key, value, defKey) => {
155
+ const result = {};
156
+ for (const i of array) {
157
+ let ki = '';
158
+ if (i[key] !== undefined && i[key] !== null) {
159
+ ki = `${i[key]}`;
160
+ }
161
+ else if (defKey) {
162
+ ki = defKey;
163
+ }
164
+ if (!result[ki]) {
165
+ result[ki] = [];
166
+ }
167
+ if (value) {
168
+ result[ki].push(i[value]);
169
+ }
170
+ else {
171
+ result[ki].push(i);
172
+ }
173
+ }
174
+ return result;
175
+ };
176
+ exports.mixList = mixList;
177
+ const array2map = (array, v) => {
178
+ const ot = {};
179
+ for (const item of array) {
180
+ ot[item] = v;
181
+ }
182
+ return ot;
183
+ };
184
+ exports.array2map = array2map;
185
+ /**
186
+ * 数组分割
187
+ * @param datas
188
+ * @param config(二选一) everyLength=每组个数(最后一组可能不足次数), groupCount=拆分几组
189
+ * @returns T[][]
190
+ */
191
+ const arraySplit = (datas, { everyLength = 0, groupCount = 0 } = {}) => {
192
+ if (groupCount > 0) {
193
+ everyLength = Math.floor(datas.length / groupCount + 0.9);
194
+ const result = [];
195
+ for (let i = 0; i < groupCount; i++) {
196
+ result.push(datas.slice(i * everyLength, (i + 1) * everyLength));
197
+ }
198
+ return result;
199
+ }
200
+ else if (everyLength > 0) {
201
+ groupCount = Math.ceil(datas.length / everyLength);
202
+ const result = [];
203
+ for (let i = 0; i < groupCount; i++) {
204
+ result.push(datas.slice(i * everyLength, (i + 1) * everyLength));
205
+ }
206
+ return result;
207
+ }
208
+ else {
209
+ throw new Error('参数错误!');
210
+ }
211
+ };
212
+ exports.arraySplit = arraySplit;
@@ -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
+ }
package/cjs/set-ex.js ADDED
@@ -0,0 +1,336 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SetEx = void 0;
4
+ class SetEx extends Set {
5
+ /**
6
+ * @param key 识别是否存在的对象的属性名
7
+ * @param onExist 当存在时作何操作? oldData/newData 哪个将添加到set,由replaceItemWhenExits决定,默认是oldData生效
8
+ * @param onNotExist 当不存在时作何操作?
9
+ * @param replaceWhenExits 当存在时是否覆盖?
10
+ * @param values 初始数组
11
+ */
12
+ constructor(key, onExist, replaceWhenExits = false, values, onNotExist) {
13
+ super();
14
+ if (typeof key === 'object') {
15
+ this.whenOnExist = key.onExist;
16
+ this.uniqueKey = key.key;
17
+ this.replaceItemWhenExits = key.replaceWhenExits === true;
18
+ this.whenOnNotExist = key.onNotExist;
19
+ if (key.values) {
20
+ this.addAll(...key.values);
21
+ }
22
+ }
23
+ else {
24
+ this.whenOnExist = onExist;
25
+ this.uniqueKey = key;
26
+ this.replaceItemWhenExits = replaceWhenExits;
27
+ this.whenOnNotExist = onNotExist;
28
+ if (values) {
29
+ this.addAll(...values);
30
+ }
31
+ }
32
+ }
33
+ /**
34
+ *
35
+ * 添加返回
36
+ * @param {T} value
37
+ * @returns {this} 当前对象
38
+ */
39
+ add(value) {
40
+ let flag = false;
41
+ this.forEach((item) => {
42
+ if (item[this.uniqueKey] === value[this.uniqueKey]) {
43
+ flag = true;
44
+ if (this.whenOnExist) {
45
+ this.whenOnExist(item, value);
46
+ }
47
+ if (this.replaceItemWhenExits === true) {
48
+ super.delete(item);
49
+ flag = false;
50
+ }
51
+ return false;
52
+ }
53
+ });
54
+ if (flag === false) {
55
+ super.add(value);
56
+ if (this.whenOnNotExist) {
57
+ this.whenOnNotExist(value);
58
+ }
59
+ }
60
+ return this;
61
+ }
62
+ /**
63
+ * 批量添加
64
+ * @param values
65
+ * @returns 当前对象
66
+ */
67
+ addAll(...values) {
68
+ for (const value of values) {
69
+ this.add(value);
70
+ }
71
+ return this;
72
+ }
73
+ /**
74
+ *
75
+ * 添加
76
+ * @param {T} value
77
+ * @returns {T} 添加成功的对象:可能是新加入集合的,也可能是原本存在的
78
+ */
79
+ add2(value) {
80
+ let flag = false;
81
+ let tmp = value;
82
+ this.forEach((item) => {
83
+ if (item[this.uniqueKey] === value[this.uniqueKey]) {
84
+ flag = true;
85
+ if (this.whenOnExist) {
86
+ this.whenOnExist(item, value);
87
+ }
88
+ if (this.replaceItemWhenExits === true) {
89
+ super.delete(item);
90
+ flag = false;
91
+ }
92
+ else {
93
+ tmp = item;
94
+ }
95
+ return false;
96
+ }
97
+ });
98
+ if (flag === false) {
99
+ super.add(value);
100
+ if (this.whenOnNotExist) {
101
+ this.whenOnNotExist(value);
102
+ }
103
+ }
104
+ return tmp;
105
+ }
106
+ /**
107
+ *
108
+ * 添加并返回添加成功的对象:可能是新加入集合的,也可能是原本存在的
109
+ * @param {T} values
110
+ * @returns {T}
111
+ */
112
+ addAll2(values) {
113
+ const result = [];
114
+ for (const value of values) {
115
+ result.push(this.add2(value));
116
+ }
117
+ return result;
118
+ }
119
+ /**
120
+ * 用key找到匹配的第一个对象
121
+ * @param {*} value 这是对象的关键属性,而非对象
122
+ * @returns {(T | null)}
123
+ */
124
+ find(value) {
125
+ for (const item of this) {
126
+ if (item[this.uniqueKey] === value) {
127
+ return item;
128
+ }
129
+ }
130
+ return null;
131
+ }
132
+ /**
133
+ * 用key找到匹配的所有对象
134
+ * @param {*} value 这是对象的关键属性,而非对象
135
+ * @returns {T[]}
136
+ */
137
+ findAll(value) {
138
+ const res = new Array();
139
+ this.forEach((item) => {
140
+ if (item[this.uniqueKey] === value) {
141
+ res.push(item);
142
+ }
143
+ });
144
+ return res;
145
+ }
146
+ /**
147
+ *
148
+ * 用函数回调找到匹配的第一个对象
149
+ * @param {(item: T) => boolean} fn
150
+ * @returns {T[]}
151
+ */
152
+ filter(fn) {
153
+ for (const item of this) {
154
+ if (fn(item) === true) {
155
+ return item;
156
+ }
157
+ }
158
+ return null;
159
+ }
160
+ /**
161
+ *
162
+ * 用函数回调找到匹配的所有对象
163
+ * @param {(item: T) => boolean} fn
164
+ * @returns {T[]}
165
+ */
166
+ filterAll(fn) {
167
+ const res = new Array();
168
+ this.forEach((item) => {
169
+ if (fn(item) === true) {
170
+ res.push(item);
171
+ }
172
+ });
173
+ return res;
174
+ }
175
+ /**
176
+ *
177
+ * 是否存在key对应的对象
178
+ * @param {*} value 这是对象的关键属性,而非对象
179
+ * @returns {boolean}
180
+ */
181
+ has(value) {
182
+ for (const item of this) {
183
+ if (item[this.uniqueKey] === value) {
184
+ return true;
185
+ }
186
+ }
187
+ return false;
188
+ }
189
+ /**
190
+ * 转为数组
191
+ * @param param0
192
+ * @returns
193
+ */
194
+ toArray({ sort, each, filter, map } = {}) {
195
+ let list = Array.from(this);
196
+ if (filter) {
197
+ list = list.filter(filter);
198
+ }
199
+ if (sort) {
200
+ list.sort(sort);
201
+ }
202
+ if (each) {
203
+ list.forEach(each);
204
+ }
205
+ if (map) {
206
+ list = list.map(map);
207
+ }
208
+ return list;
209
+ }
210
+ /**
211
+ * 转为JSON对象
212
+ * @param key
213
+ * @param value
214
+ * @param param2
215
+ * @returns
216
+ */
217
+ toJSON(key, value, { sort, each, filter, map } = {}) {
218
+ return Object.fromEntries(this.toArray({ sort, each, filter, map }).map(i => [i[key], value ? i[value] : i]));
219
+ }
220
+ /**
221
+ *
222
+ * 删除key对应的对象
223
+ * @param {*} value 这是对象的关键属性,而非对象
224
+ * @returns {boolean}
225
+ */
226
+ delete(value) {
227
+ for (const item of this) {
228
+ if (item[this.uniqueKey] === value) {
229
+ super.delete(item);
230
+ return true;
231
+ }
232
+ }
233
+ return false;
234
+ }
235
+ /**
236
+ *
237
+ * 重置
238
+ * @param {keyof T} key
239
+ * @param {(oldData: T, newData: T) => void} [onExist]
240
+ * @param {boolean} [replaceWhenExits=false]
241
+ */
242
+ reset({ key, onExist, onNotExist, replaceWhenExits }) {
243
+ if (onExist !== undefined) {
244
+ this.whenOnExist = onExist;
245
+ }
246
+ if (onNotExist !== undefined) {
247
+ this.whenOnNotExist = onNotExist;
248
+ }
249
+ if (key) {
250
+ this.uniqueKey = key;
251
+ }
252
+ if (replaceWhenExits !== undefined) {
253
+ this.replaceItemWhenExits = replaceWhenExits;
254
+ }
255
+ this.clear();
256
+ return this;
257
+ }
258
+ /**
259
+ *
260
+ * @param param0 转为JSON对象,value可能是数组
261
+ * @returns
262
+ */
263
+ toJSONArray({ sort, each, filter, map } = {}) {
264
+ const result = {};
265
+ const list = this.toArray({ sort, each, filter, map });
266
+ for (const item of list) {
267
+ for (const key in item) {
268
+ if (!result[key]) {
269
+ result[key] = [];
270
+ }
271
+ result[key].push(item[key]);
272
+ }
273
+ }
274
+ return result;
275
+ }
276
+ /**
277
+ * 转为hot-table支持的数组
278
+ * @param param0
279
+ * @param keys
280
+ * @returns
281
+ */
282
+ toDataGrid({ sort, each, filter, map } = {}, ...keys) {
283
+ if (this.size === 0) {
284
+ return [];
285
+ }
286
+ if (keys.length === 0) {
287
+ keys = Object.keys(this.values().next().value);
288
+ }
289
+ const result = [keys];
290
+ const list = this.toArray({ sort, each, filter, map });
291
+ for (const item of list) {
292
+ const one = [];
293
+ for (const key of keys) {
294
+ one.push(item[key]);
295
+ }
296
+ result.push(one);
297
+ }
298
+ return result;
299
+ }
300
+ /**
301
+ * 转为饼图支持的数组
302
+ * @param param0
303
+ * @param keys
304
+ * @returns
305
+ */
306
+ toPieGrid({ sort, each, filter, map } = {}, ...keys) {
307
+ if (this.size === 0) {
308
+ return {};
309
+ }
310
+ if (keys.length === 0) {
311
+ keys = Object.keys(this.values().next().value);
312
+ }
313
+ const result = {};
314
+ const list = this.toArray({ sort, each, filter, map });
315
+ for (const item of list) {
316
+ const name = item[this.uniqueKey];
317
+ for (const key in item) {
318
+ if (!result[key]) {
319
+ result[key] = [];
320
+ }
321
+ result[key].push({ name, value: item[key] });
322
+ }
323
+ }
324
+ return result;
325
+ }
326
+ set onExist(onExist) {
327
+ this.whenOnExist = onExist;
328
+ }
329
+ set key(key) {
330
+ this.uniqueKey = key;
331
+ }
332
+ set replaceWhenExits(replaceWhenExits) {
333
+ this.replaceItemWhenExits = replaceWhenExits;
334
+ }
335
+ }
336
+ exports.SetEx = SetEx;