baja-lite 1.0.1 → 1.0.3

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/src/math.ts ADDED
@@ -0,0 +1,367 @@
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
+ };
package/src/object.ts ADDED
@@ -0,0 +1,213 @@
1
+ /**
2
+ * 对象对象(等同与convertBean)
3
+ * 仅会将classType有的属性进行转换
4
+ * 相当与一次属性过滤
5
+ * @param source
6
+ * @param classType
7
+ */
8
+ export const copyBean = <T>(source: any, classType: any): T => {
9
+ const result = {};
10
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
11
+ Object.keys(classType).forEach((key) => {
12
+ result[key] =
13
+ source[key] !== undefined ? source[key] : (result[key] = null);
14
+ });
15
+ return result as T;
16
+ };
17
+ /**
18
+ * 对象转换(等同与copyBean)
19
+ * 仅会将classType有的属性进行转换
20
+ * 相当与一次属性过滤
21
+ * @param source
22
+ * @param classType
23
+ */
24
+ export const convertBean = copyBean;
25
+ /**
26
+ * 批量对象转换(等同与copyBean)
27
+ * 仅会将classType有的属性进行转换
28
+ * 相当与一次属性过滤
29
+ * @param source
30
+ * @param classType
31
+ */
32
+ export const convertBeans = <T>(
33
+ source: any[],
34
+ classType: any,
35
+ cb?: (target: T, source: any) => void
36
+ ): T[] => {
37
+ const result = new Array<T>();
38
+ for (const bean of source) {
39
+ const data = convertBean<T>(bean, classType);
40
+ if (cb) {
41
+ cb(data, bean);
42
+ }
43
+ result.push(data);
44
+ }
45
+ return result;
46
+ };
47
+ /**
48
+ * 创建一个空对象
49
+ * 其内各属性都是null
50
+ * @param classType
51
+ */
52
+ export const emptyBean = <T>(classType: any): T => {
53
+ const target = {} as T;
54
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
55
+ Object.keys(classType).forEach((key) => {
56
+ target[key] = null;
57
+ });
58
+ return target;
59
+ };
60
+
61
+ /**
62
+ * 将一个json数组提取为一个json对象
63
+ * @param source 源数组
64
+ * @param key 作为新对象的key的字段
65
+ * @param value 作为新对象value的字段,不传则将自身为value
66
+ */
67
+ export const createBeanFromArray = <F, T = F>(
68
+ source: F[],
69
+ key: keyof F,
70
+ value?: keyof F
71
+ ): {
72
+ [name: string]: T;
73
+ } => {
74
+ const result: {
75
+ [name: string]: T;
76
+ } = {};
77
+ if (value) {
78
+ source.forEach((item) => {
79
+ if (item[key]) {
80
+ result[`${ item[key] }`] = item[value] as unknown as T;
81
+ }
82
+ });
83
+ } else {
84
+ source.forEach((item) => {
85
+ if (item[key]) {
86
+ result[`${ item[key] }`] = item as unknown as T;
87
+ }
88
+ });
89
+ }
90
+ return result;
91
+ };
92
+
93
+ /**
94
+ * 转换复合对象为指定bean
95
+ * @param source
96
+ * @param classType
97
+ */
98
+ export const coverComplexBean = <T>(
99
+ source: any,
100
+ classType: any
101
+ ): {data: T; array: {[key: string]: any[]}} => {
102
+ const result = {};
103
+ const arrayData = {};
104
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
105
+ for (const [key, value] of Object.entries(source)) {
106
+ if (value instanceof Array) {
107
+ arrayData[key] = value;
108
+ } else if (typeof value === 'object') {
109
+ Object.assign(result, value);
110
+ } else {
111
+ result[key] = value;
112
+ }
113
+ }
114
+ return {
115
+ data: convertBean<T>(result, classType),
116
+ array: arrayData
117
+ };
118
+ };
119
+
120
+ /**
121
+ * 将目标对象中为空的字段替换为source中对应key的值或者函数返回值
122
+ * @param target
123
+ * @param source
124
+ */
125
+ export const fixEmptyPrototy = async (
126
+ target: any,
127
+ source: {
128
+ [key: string]: any;
129
+ }
130
+ ) => {
131
+ for (const [key, fn] of Object.entries(source)) {
132
+ if (!target[key]) {
133
+ if (typeof fn === 'function') {
134
+ target[key] = await fn();
135
+ } else {
136
+ target[key] = fn;
137
+ }
138
+ }
139
+ }
140
+ };
141
+
142
+
143
+ export const mixArray = <T>(array: T[], key: keyof T, defKey?: string): {[key: string]: number} => {
144
+ const obj = array.map(item => item[key]);
145
+ const result: {[k: string]: number} = {};
146
+ for (const i of obj) {
147
+ let ki = '';
148
+ if (i !== undefined && i !== null) {
149
+ ki = `${ i }`;
150
+ } else if (defKey) {
151
+ ki = defKey;
152
+ }
153
+ if (!result[ki]) {
154
+ result[ki] = 0;
155
+ }
156
+ result[ki]++;
157
+ }
158
+ return result;
159
+ };
160
+
161
+ export const mixList = <T, V = T>(array: T[], key: keyof T, value?: keyof T, defKey?: string): {[key: string]: V[]} => {
162
+ const result: {[k: string]: V[]} = {};
163
+ for (const i of array) {
164
+ let ki = '';
165
+ if (i[key] !== undefined && i[key] !== null) {
166
+ ki = `${ i[key] }`;
167
+ } else if (defKey) {
168
+ ki = defKey;
169
+ }
170
+ if (!result[ki]) {
171
+ result[ki] = [];
172
+ }
173
+ if (value) {
174
+ result[ki]!.push(i[value] as any);
175
+ } else {
176
+ result[ki]!.push(i as any);
177
+ }
178
+ }
179
+ return result;
180
+ };
181
+
182
+ export const array2map = <T = string | number>(array: string[], v: T): {[key: string]: T} => {
183
+ const ot: {[key: string]: T} = {};
184
+ for (const item of array) {
185
+ ot[item] = v;
186
+ }
187
+ return ot;
188
+ };
189
+ /**
190
+ * 数组分割
191
+ * @param datas
192
+ * @param config(二选一) everyLength=每组个数(最后一组可能不足次数), groupCount=拆分几组
193
+ * @returns T[][]
194
+ */
195
+ export const arraySplit = <T = any>(datas: T[], {everyLength = 0, groupCount = 0} = {}) => {
196
+ if (groupCount > 0) {
197
+ everyLength = Math.floor(datas.length / groupCount + 0.9);
198
+ const result: T[][] = [];
199
+ for (let i = 0; i < groupCount; i++) {
200
+ result.push(datas.slice(i * everyLength, (i + 1) * everyLength));
201
+ }
202
+ return result;
203
+ } else if (everyLength > 0) {
204
+ groupCount = Math.ceil(datas.length / everyLength);
205
+ const result: T[][] = [];
206
+ for (let i = 0; i < groupCount; i++) {
207
+ result.push(datas.slice(i * everyLength, (i + 1) * everyLength));
208
+ }
209
+ return result;
210
+ } else {
211
+ throw new Error('参数错误!');
212
+ }
213
+ };
package/src/redis.ts ADDED
File without changes