baja-lite 1.6.2 → 1.6.4

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 (73) hide show
  1. package/.eslintignore +7 -0
  2. package/.eslintrc.cjs +89 -0
  3. package/.prettierrc +7 -0
  4. package/.vscode/settings.json +9 -0
  5. package/ci.js +33 -0
  6. package/package-cjs.json +17 -0
  7. package/package.json +80 -80
  8. package/pnpm-lock.yaml +2840 -0
  9. package/pnpm-workspace.yaml +2 -0
  10. package/{boot-remote.js → src/boot-remote.ts} +64 -63
  11. package/{boot.js → src/boot.ts} +170 -163
  12. package/{code.js → src/code.ts} +526 -517
  13. package/{convert-xml.js → src/convert-xml.ts} +460 -410
  14. package/src/error.ts +11 -0
  15. package/src/event.ts +34 -0
  16. package/src/fn.ts +295 -0
  17. package/{index.d.ts → src/index.ts} +11 -10
  18. package/src/math.ts +405 -0
  19. package/src/object.ts +342 -0
  20. package/{snowflake.js → src/snowflake.ts} +127 -108
  21. package/src/sql.ts +5529 -0
  22. package/{sqlite.js → src/sqlite.ts} +157 -156
  23. package/src/string.ts +111 -0
  24. package/src/test-mysql.ts +148 -0
  25. package/{test-postgresql.js → src/test-postgresql.ts} +80 -91
  26. package/{test-sqlite.js → src/test-sqlite.ts} +80 -90
  27. package/{test-xml.js → src/test-xml.ts} +70 -70
  28. package/{test.js → src/test.ts} +3 -2
  29. package/src/wx/base.ts +77 -0
  30. package/src/wx/mini.ts +147 -0
  31. package/src/wx/organ.ts +290 -0
  32. package/{wx/types.d.ts → src/wx/types.ts} +549 -560
  33. package/{wx.d.ts → src/wx.ts} +3 -3
  34. package/tsconfig.cjs.json +42 -0
  35. package/tsconfig.json +44 -0
  36. package/xml/event-report.xml +13 -0
  37. package/yarn.lock +1977 -0
  38. package/boot-remote.d.ts +0 -2
  39. package/boot.d.ts +0 -2
  40. package/code.d.ts +0 -2
  41. package/convert-xml.d.ts +0 -10
  42. package/error.d.ts +0 -5
  43. package/error.js +0 -13
  44. package/event.d.ts +0 -10
  45. package/event.js +0 -38
  46. package/fn.d.ts +0 -128
  47. package/fn.js +0 -172
  48. package/index.js +0 -10
  49. package/math.d.ts +0 -83
  50. package/math.js +0 -451
  51. package/object.d.ts +0 -126
  52. package/object.js +0 -321
  53. package/snowflake.d.ts +0 -12
  54. package/sql.d.ts +0 -2148
  55. package/sql.js +0 -5372
  56. package/sqlite.d.ts +0 -32
  57. package/string.d.ts +0 -17
  58. package/string.js +0 -105
  59. package/test-mysql.d.ts +0 -2
  60. package/test-mysql.js +0 -114
  61. package/test-postgresql.d.ts +0 -2
  62. package/test-sqlite.d.ts +0 -1
  63. package/test-xml.d.ts +0 -1
  64. package/test.d.ts +0 -1
  65. package/wx/base.d.ts +0 -11
  66. package/wx/base.js +0 -78
  67. package/wx/mini.d.ts +0 -52
  68. package/wx/mini.js +0 -112
  69. package/wx/organ.d.ts +0 -65
  70. package/wx/organ.js +0 -171
  71. package/wx/types.js +0 -1
  72. package/wx.js +0 -3
  73. /package/{README.md → Readme.md} +0 -0
package/src/math.ts ADDED
@@ -0,0 +1,405 @@
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
+ }