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