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.
- package/LICENSE +21 -0
- package/README.md +1 -0
- package/cjs/constant.d.ts +13 -0
- package/cjs/constant.js +19 -0
- package/cjs/error.d.ts +5 -0
- package/cjs/error.js +16 -0
- package/cjs/fn.d.ts +128 -0
- package/cjs/fn.js +169 -0
- package/cjs/index.d.ts +8 -0
- package/cjs/index.js +24 -0
- package/cjs/math.d.ts +69 -0
- package/cjs/math.js +435 -0
- package/cjs/now.d.ts +7 -0
- package/cjs/now.js +26 -0
- package/cjs/object.d.ts +77 -0
- package/cjs/object.js +212 -0
- package/cjs/set-ex.d.ts +171 -0
- package/cjs/set-ex.js +336 -0
- package/cjs/sql.d.ts +1216 -0
- package/cjs/sql.js +3380 -0
- package/cjs/string.d.ts +18 -0
- package/cjs/string.js +124 -0
- package/cjs/test-mysql.d.ts +1 -0
- package/cjs/test-mysql.js +108 -0
- package/cjs/test-sqlite.d.ts +1 -0
- package/cjs/test-sqlite.js +89 -0
- package/cjs/test.d.ts +1 -0
- package/cjs/test.js +4 -0
- package/es/constant.d.ts +13 -0
- package/es/constant.js +16 -0
- package/es/error.d.ts +5 -0
- package/es/error.js +13 -0
- package/es/fn.d.ts +128 -0
- package/es/fn.js +162 -0
- package/es/index.d.ts +8 -0
- package/es/index.js +8 -0
- package/es/math.d.ts +69 -0
- package/es/math.js +414 -0
- package/es/now.d.ts +7 -0
- package/es/now.js +16 -0
- package/es/object.d.ts +77 -0
- package/es/object.js +196 -0
- package/es/set-ex.d.ts +171 -0
- package/es/set-ex.js +332 -0
- package/es/sql.d.ts +1216 -0
- package/es/sql.js +3338 -0
- package/es/string.d.ts +18 -0
- package/es/string.js +109 -0
- package/es/test-mysql.d.ts +1 -0
- package/es/test-mysql.js +106 -0
- package/es/test-sqlite.d.ts +1 -0
- package/es/test-sqlite.js +87 -0
- package/es/test.d.ts +1 -0
- package/es/test.js +2 -0
- package/package.json +66 -0
package/es/fn.js
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { arraySplit } from './object';
|
|
2
|
+
import { Throw } from './error';
|
|
3
|
+
/**
|
|
4
|
+
* 回调函数promise化
|
|
5
|
+
* 调用示例
|
|
6
|
+
* soap.excute(arg1, arg2, function(error, data){});
|
|
7
|
+
* 可使用为:
|
|
8
|
+
* const soap_excute = promise({
|
|
9
|
+
* fn: soap.excute,
|
|
10
|
+
* target: soap
|
|
11
|
+
* });
|
|
12
|
+
* const data = await soap_excute(arg1, arg2);
|
|
13
|
+
* @param this
|
|
14
|
+
* @param param1
|
|
15
|
+
*/
|
|
16
|
+
export const promise = function ({ fn, target, last = true }) {
|
|
17
|
+
return (...args) => {
|
|
18
|
+
return new Promise((resolve, reject) => {
|
|
19
|
+
args[last === true ? 'push' : 'unshift']((err, data) => {
|
|
20
|
+
if (err === null || err === undefined) {
|
|
21
|
+
return resolve.call(this, data);
|
|
22
|
+
}
|
|
23
|
+
return reject(err);
|
|
24
|
+
});
|
|
25
|
+
if (target) {
|
|
26
|
+
fn.apply(target, args);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
fn.apply({}, args);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
export const sleep = (time = parseInt(`${Math.random()}`) + 200) => new Promise((resolve) => setTimeout(resolve, time));
|
|
35
|
+
/**
|
|
36
|
+
* 执行器
|
|
37
|
+
* @param fn
|
|
38
|
+
* @param {
|
|
39
|
+
ifFinish?: (result?: T) => boolean; // 是否结束,默认是建议判断 !!result
|
|
40
|
+
maxTryTimes?: number; //最多尝试几次,默认是20
|
|
41
|
+
onFail?: () => Promise<boolean | undefined> | boolean | undefined; // 失败时的回调,返回false表示停止执行
|
|
42
|
+
name?: string; // 执行器名称,用于打印日志
|
|
43
|
+
exitIfFail?: boolean; // 失败时是否退出,默认是false. 这里设置true后,onFail返回true,也会停止执行
|
|
44
|
+
defVal?: T; // 失败时的默认值
|
|
45
|
+
sleepAppend?: number; // 等待延迟MS,默认是1000内随机+200
|
|
46
|
+
* }
|
|
47
|
+
* @returns
|
|
48
|
+
*/
|
|
49
|
+
export async function dieTrying(fn, { ifFinish = (result) => !!result, maxTryTimes = 20, onFail, name = 'dieTrying', exitIfFail = true, defVal, sleepAppend = 0 } = {}) {
|
|
50
|
+
let count = 0;
|
|
51
|
+
let result = defVal;
|
|
52
|
+
while (result = await fn(), !ifFinish(result)) {
|
|
53
|
+
await sleep(parseInt(`${Math.random() * 1000}`) + sleepAppend);
|
|
54
|
+
count++;
|
|
55
|
+
console.debug(`${name} try ${count} times`);
|
|
56
|
+
if (count > maxTryTimes) {
|
|
57
|
+
if (onFail) {
|
|
58
|
+
const remuseExcute = await onFail();
|
|
59
|
+
console.error(`${name} timeout`);
|
|
60
|
+
count = 0;
|
|
61
|
+
if (remuseExcute === false) {
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (exitIfFail) {
|
|
66
|
+
break;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return result;
|
|
71
|
+
}
|
|
72
|
+
export var ExcuteSplitMode;
|
|
73
|
+
(function (ExcuteSplitMode) {
|
|
74
|
+
ExcuteSplitMode[ExcuteSplitMode["SyncTrust"] = 0] = "SyncTrust";
|
|
75
|
+
ExcuteSplitMode[ExcuteSplitMode["SyncNoTrust"] = 1] = "SyncNoTrust";
|
|
76
|
+
ExcuteSplitMode[ExcuteSplitMode["AsyncTrust"] = 2] = "AsyncTrust";
|
|
77
|
+
ExcuteSplitMode[ExcuteSplitMode["AsyncNoTrust"] = 3] = "AsyncNoTrust";
|
|
78
|
+
})(ExcuteSplitMode || (ExcuteSplitMode = {}));
|
|
79
|
+
export function excuteSplit(sync, datas, fn, { everyLength = 0, groupCount = 0, settled = false, extendParams = new Array() }) {
|
|
80
|
+
if (extendParams.length > 0) {
|
|
81
|
+
groupCount = extendParams.length;
|
|
82
|
+
}
|
|
83
|
+
Throw.if(everyLength === 0 && groupCount === 0, '参数错误!');
|
|
84
|
+
const ps = { everyLength, groupCount };
|
|
85
|
+
const list = arraySplit(datas, ps);
|
|
86
|
+
if (sync === ExcuteSplitMode.AsyncTrust) {
|
|
87
|
+
return new Promise(async (resolve, reject) => {
|
|
88
|
+
const reasons = [];
|
|
89
|
+
if (settled) {
|
|
90
|
+
const result = await Promise.allSettled(list.map((list, i) => fn(list, i, list.length, extendParams[i])));
|
|
91
|
+
for (const item of result) {
|
|
92
|
+
if (item.status === 'rejected') {
|
|
93
|
+
reject(item.reason);
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
reasons.push(item.value);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
for (let i = 0; i < list.length; i++) {
|
|
102
|
+
const startIndex = (i - 1) * ps.everyLength;
|
|
103
|
+
const endIndex = startIndex + list[i].length - 1;
|
|
104
|
+
reasons.push(await fn(list[i], i, list.length, extendParams[i], startIndex, endIndex));
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
resolve(reasons);
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
else if (sync === ExcuteSplitMode.AsyncNoTrust) {
|
|
111
|
+
return new Promise(async (resolve, reject) => {
|
|
112
|
+
const reasons = { result: [], error: [] };
|
|
113
|
+
if (settled) {
|
|
114
|
+
const result = await Promise.allSettled(list.map((list, i) => fn(list, i, list.length, extendParams[i])));
|
|
115
|
+
for (const item of result) {
|
|
116
|
+
if (item.status === 'rejected') {
|
|
117
|
+
reasons.error.push(item.reason);
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
reasons.result.push(item.value);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
for (let i = 0; i < list.length; i++) {
|
|
126
|
+
const startIndex = (i - 1) * ps.everyLength;
|
|
127
|
+
const endIndex = startIndex + list[i].length - 1;
|
|
128
|
+
try {
|
|
129
|
+
reasons.result.push(await fn(list[i], i, list.length, extendParams[i], startIndex, endIndex));
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
reasons.error.push(error);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
resolve(reasons);
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
else if (sync === ExcuteSplitMode.SyncTrust) {
|
|
140
|
+
const reasons = [];
|
|
141
|
+
for (let i = 0; i < list.length; i++) {
|
|
142
|
+
const startIndex = (i - 1) * ps.everyLength;
|
|
143
|
+
const endIndex = startIndex + list[i].length - 1;
|
|
144
|
+
reasons.push(fn(list[i], i, list.length, extendParams[i], startIndex, endIndex));
|
|
145
|
+
}
|
|
146
|
+
return reasons;
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
const reasons = { result: [], error: [] };
|
|
150
|
+
for (let i = 0; i < list.length; i++) {
|
|
151
|
+
try {
|
|
152
|
+
const startIndex = (i - 1) * ps.everyLength;
|
|
153
|
+
const endIndex = startIndex + list[i].length - 1;
|
|
154
|
+
reasons.result.push(fn(list[i], i, list.length, extendParams[i], startIndex, endIndex));
|
|
155
|
+
}
|
|
156
|
+
catch (error) {
|
|
157
|
+
reasons.error.push(error);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return reasons;
|
|
161
|
+
}
|
|
162
|
+
}
|
package/es/index.d.ts
ADDED
package/es/index.js
ADDED
package/es/math.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/** 金钱格式化可用样式 */
|
|
2
|
+
export declare class MoneyOption {
|
|
3
|
+
style?: 'currency' | 'decimal' | 'percent';
|
|
4
|
+
currency?: string;
|
|
5
|
+
prefix?: number;
|
|
6
|
+
def?: number;
|
|
7
|
+
currencyDisplay?: 'symbol' | 'name' | 'code';
|
|
8
|
+
useGrouping?: boolean;
|
|
9
|
+
local?: string;
|
|
10
|
+
}
|
|
11
|
+
export interface Point {
|
|
12
|
+
latitude: string;
|
|
13
|
+
longitude: string;
|
|
14
|
+
lat: number;
|
|
15
|
+
long: number;
|
|
16
|
+
}
|
|
17
|
+
export declare const num: (val: any, def?: number) => number;
|
|
18
|
+
export declare const max: (...args: any[]) => number;
|
|
19
|
+
export declare const min: (...args: any[]) => number;
|
|
20
|
+
export declare const div: (...args: any[]) => number;
|
|
21
|
+
export declare const divDef: (def: any, ...args: any[]) => number;
|
|
22
|
+
export declare const add: (...args: any[]) => number;
|
|
23
|
+
export declare const mul: (...args: any[]) => number;
|
|
24
|
+
export declare const sub: (...args: any[]) => number;
|
|
25
|
+
export declare const round: (number: any, numDigits: number, upOrDown?: number) => number;
|
|
26
|
+
/** =value.xx,其中xx=number,如number=99,表示修正数字为value.99 */
|
|
27
|
+
export declare const merge: (value: any, number: any) => any;
|
|
28
|
+
export declare const money: (value: any, option?: MoneyOption) => string;
|
|
29
|
+
export declare class Bus {
|
|
30
|
+
private result;
|
|
31
|
+
private ifit;
|
|
32
|
+
constructor(result: any);
|
|
33
|
+
add(...args: any[]): this;
|
|
34
|
+
sub(...args: any[]): this;
|
|
35
|
+
div(...args: any[]): this;
|
|
36
|
+
divDef(def: any, ...args: any[]): this;
|
|
37
|
+
mul(...args: any[]): this;
|
|
38
|
+
max(...args: any[]): this;
|
|
39
|
+
min(...args: any[]): this;
|
|
40
|
+
ac(): this;
|
|
41
|
+
abs(): this;
|
|
42
|
+
round(numDigits: number, upOrDown?: number): this;
|
|
43
|
+
merge(number: any): this;
|
|
44
|
+
if(condition: boolean): this;
|
|
45
|
+
over(): number;
|
|
46
|
+
money(option?: MoneyOption): string;
|
|
47
|
+
lt(data: any): boolean;
|
|
48
|
+
le(data: any): boolean;
|
|
49
|
+
gt(data: any): boolean;
|
|
50
|
+
ge(data: any): boolean;
|
|
51
|
+
nlt(data: any): boolean;
|
|
52
|
+
nle(data: any): boolean;
|
|
53
|
+
ngt(data: any): boolean;
|
|
54
|
+
nge(data: any): boolean;
|
|
55
|
+
eq(data: any): boolean;
|
|
56
|
+
ne(data: any): boolean;
|
|
57
|
+
ifLt(data: any): this;
|
|
58
|
+
ifLe(data: any): this;
|
|
59
|
+
ifGt(data: any): this;
|
|
60
|
+
ifGe(data: any): this;
|
|
61
|
+
ifNlt(data: any): this;
|
|
62
|
+
ifNle(data: any): this;
|
|
63
|
+
ifNgt(data: any): this;
|
|
64
|
+
ifNge(data: any): this;
|
|
65
|
+
ifEq(data: any): this;
|
|
66
|
+
ifNe(data: any): this;
|
|
67
|
+
}
|
|
68
|
+
export declare const calc: (result: any) => Bus;
|
|
69
|
+
export declare const getGeo: (p1: Point, p2: Point) => number;
|
package/es/math.js
ADDED
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
/* eslint-disable @typescript-eslint/no-unsafe-argument */
|
|
11
|
+
import Decimal from 'decimal.js';
|
|
12
|
+
/** 金钱格式化可用样式 */
|
|
13
|
+
export class MoneyOption {
|
|
14
|
+
constructor() {
|
|
15
|
+
this.style = 'currency';
|
|
16
|
+
this.currency = 'CNY';
|
|
17
|
+
this.prefix = 2;
|
|
18
|
+
this.def = 0;
|
|
19
|
+
this.currencyDisplay = 'symbol';
|
|
20
|
+
this.useGrouping = true;
|
|
21
|
+
this.local = 'zh';
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
// const ONE = new Decimal(1);
|
|
25
|
+
const ZERO = new Decimal(0);
|
|
26
|
+
function isNum(a) {
|
|
27
|
+
return a !== '' && a !== null && !isNaN(a);
|
|
28
|
+
}
|
|
29
|
+
export const num = (val, def = 0) => {
|
|
30
|
+
if (val instanceof Bus) {
|
|
31
|
+
return val.over();
|
|
32
|
+
}
|
|
33
|
+
else if (!isNum(val)) {
|
|
34
|
+
return def;
|
|
35
|
+
}
|
|
36
|
+
return +val;
|
|
37
|
+
};
|
|
38
|
+
function filterNumber(array) {
|
|
39
|
+
const res = [];
|
|
40
|
+
array.forEach((element) => {
|
|
41
|
+
if (element instanceof Bus) {
|
|
42
|
+
res.push(element.over());
|
|
43
|
+
}
|
|
44
|
+
else if (isNum(element)) {
|
|
45
|
+
res.push(+element);
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
return res;
|
|
49
|
+
}
|
|
50
|
+
function filterNumber2(array, def) {
|
|
51
|
+
const res = [];
|
|
52
|
+
array.forEach((element) => {
|
|
53
|
+
if (element instanceof Bus) {
|
|
54
|
+
res.push(new Decimal(element.over()));
|
|
55
|
+
}
|
|
56
|
+
else if (isNum(element)) {
|
|
57
|
+
res.push(new Decimal(element));
|
|
58
|
+
}
|
|
59
|
+
else if (def !== undefined) {
|
|
60
|
+
res.push(new Decimal(def));
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
return res;
|
|
64
|
+
}
|
|
65
|
+
export const max = (...args) => {
|
|
66
|
+
const arr = filterNumber(args);
|
|
67
|
+
return Math.max.apply(null, arr);
|
|
68
|
+
};
|
|
69
|
+
export const min = (...args) => {
|
|
70
|
+
const arr = filterNumber(args);
|
|
71
|
+
return Math.min.apply(null, arr);
|
|
72
|
+
};
|
|
73
|
+
export const div = (...args) => {
|
|
74
|
+
const arr = filterNumber2(args);
|
|
75
|
+
if (arr.length > 1) {
|
|
76
|
+
return arr.reduce((a, b) => a.div(b)).toNumber();
|
|
77
|
+
}
|
|
78
|
+
else if (arr.length > 0) {
|
|
79
|
+
return arr[0].toNumber();
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
return 0;
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
export const divDef = (def, ...args) => {
|
|
86
|
+
const arr = filterNumber2(args);
|
|
87
|
+
if (arr.length > 1) {
|
|
88
|
+
const zeros = arr.slice(1).findIndex(i => i.equals(ZERO));
|
|
89
|
+
if (zeros > -1) {
|
|
90
|
+
return new Decimal(def).toNumber();
|
|
91
|
+
}
|
|
92
|
+
return arr.reduce((a, b) => a.div(b)).toNumber();
|
|
93
|
+
}
|
|
94
|
+
else if (arr.length > 0) {
|
|
95
|
+
return arr[0].toNumber();
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
return 0;
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
export const add = (...args) => {
|
|
102
|
+
const arr = filterNumber2(args);
|
|
103
|
+
if (arr.length > 1) {
|
|
104
|
+
return arr.reduce((a, b) => a.add(b)).toNumber();
|
|
105
|
+
}
|
|
106
|
+
else if (arr.length > 0) {
|
|
107
|
+
return arr[0].toNumber();
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
return 0;
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
export const mul = (...args) => {
|
|
114
|
+
const arr = filterNumber2(args);
|
|
115
|
+
if (arr.length > 1) {
|
|
116
|
+
return arr.reduce((a, b) => a.mul(b)).toNumber();
|
|
117
|
+
}
|
|
118
|
+
else if (arr.length > 0) {
|
|
119
|
+
return arr[0].toNumber();
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
return 0;
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
export const sub = (...args) => {
|
|
126
|
+
const arr = filterNumber2(args, 0);
|
|
127
|
+
if (arr.length > 1) {
|
|
128
|
+
return arr.reduce((a, b) => a.sub(b)).toNumber();
|
|
129
|
+
}
|
|
130
|
+
else if (arr.length > 0) {
|
|
131
|
+
return arr[0].toNumber();
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
return 0;
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
const roundMode = [Decimal.ROUND_HALF_UP, Decimal.ROUND_UP, Decimal.ROUND_DOWN];
|
|
138
|
+
export const round = (number, numDigits, upOrDown = 0) => {
|
|
139
|
+
if (isNum(number)) {
|
|
140
|
+
const nu = new Decimal(number);
|
|
141
|
+
return nu.toDP(numDigits, roundMode[upOrDown]).toNumber();
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
return 0;
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
/** =value.xx,其中xx=number,如number=99,表示修正数字为value.99 */
|
|
148
|
+
export const merge = function (value, number) {
|
|
149
|
+
if (isNum(value) && isNum(number)) {
|
|
150
|
+
return new Decimal(value).floor().add(`0.${number}`).toNumber();
|
|
151
|
+
}
|
|
152
|
+
else if (isNum(value)) {
|
|
153
|
+
return value;
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
return 0;
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
export const money = (value, option = {}) => {
|
|
160
|
+
// Intl.NumberFormat(option.local ?? 'zh', {
|
|
161
|
+
// style: option.style ?? 'currency',
|
|
162
|
+
// currency: option.currency ?? 'CNY',
|
|
163
|
+
// minimumFractionDigits: option.prefix ?? 2,
|
|
164
|
+
// currencyDisplay: option.currencyDisplay ?? 'symbol',
|
|
165
|
+
// useGrouping: option.useGrouping ?? true
|
|
166
|
+
// }).format(isNum(value) ? value : option.def).replace(/CN|\s/g, '');
|
|
167
|
+
return (isNum(value) ? value : option.def).toLocaleString(option.local ?? 'zh', {
|
|
168
|
+
style: option.style ?? 'currency',
|
|
169
|
+
currency: option.currency ?? 'CNY',
|
|
170
|
+
minimumFractionDigits: option.prefix ?? 2,
|
|
171
|
+
currencyDisplay: option.currencyDisplay ?? 'symbol',
|
|
172
|
+
useGrouping: option.useGrouping ?? true
|
|
173
|
+
}).replace(/CN|\s/g, '');
|
|
174
|
+
};
|
|
175
|
+
const IF = function () {
|
|
176
|
+
return function (_target, _propertyKey, descriptor) {
|
|
177
|
+
const fn = descriptor.value;
|
|
178
|
+
descriptor.value = function () {
|
|
179
|
+
if (this['ifit'] === true) {
|
|
180
|
+
// eslint-disable-next-line prefer-rest-params
|
|
181
|
+
const args = Array.from(arguments);
|
|
182
|
+
fn.call(this, ...args);
|
|
183
|
+
}
|
|
184
|
+
this['ifit'] = true;
|
|
185
|
+
return this;
|
|
186
|
+
};
|
|
187
|
+
};
|
|
188
|
+
};
|
|
189
|
+
export class Bus {
|
|
190
|
+
constructor(result) {
|
|
191
|
+
this.ifit = true;
|
|
192
|
+
this.result = num(result);
|
|
193
|
+
}
|
|
194
|
+
add(...args) {
|
|
195
|
+
this.result = add(this.result, ...args);
|
|
196
|
+
return this;
|
|
197
|
+
}
|
|
198
|
+
sub(...args) {
|
|
199
|
+
this.result = sub(this.result, ...args);
|
|
200
|
+
return this;
|
|
201
|
+
}
|
|
202
|
+
div(...args) {
|
|
203
|
+
this.result = div(this.result, ...args);
|
|
204
|
+
return this;
|
|
205
|
+
}
|
|
206
|
+
divDef(def, ...args) {
|
|
207
|
+
this.result = divDef(def, this.result, ...args);
|
|
208
|
+
return this;
|
|
209
|
+
}
|
|
210
|
+
mul(...args) {
|
|
211
|
+
this.result = mul(this.result, ...args);
|
|
212
|
+
return this;
|
|
213
|
+
}
|
|
214
|
+
max(...args) {
|
|
215
|
+
this.result = max(this.result, ...args);
|
|
216
|
+
return this;
|
|
217
|
+
}
|
|
218
|
+
min(...args) {
|
|
219
|
+
this.result = min(this.result, ...args);
|
|
220
|
+
return this;
|
|
221
|
+
}
|
|
222
|
+
ac() {
|
|
223
|
+
this.result = sub(0, this.result);
|
|
224
|
+
return this;
|
|
225
|
+
}
|
|
226
|
+
abs() {
|
|
227
|
+
this.result = Math.abs(this.result);
|
|
228
|
+
return this;
|
|
229
|
+
}
|
|
230
|
+
round(numDigits, upOrDown) {
|
|
231
|
+
this.result = round(this.result, numDigits, upOrDown);
|
|
232
|
+
return this;
|
|
233
|
+
}
|
|
234
|
+
merge(number) {
|
|
235
|
+
this.result = merge(this.result, number);
|
|
236
|
+
return this;
|
|
237
|
+
}
|
|
238
|
+
if(condition) {
|
|
239
|
+
this.ifit = condition;
|
|
240
|
+
return this;
|
|
241
|
+
}
|
|
242
|
+
over() {
|
|
243
|
+
return this.result;
|
|
244
|
+
}
|
|
245
|
+
money(option) {
|
|
246
|
+
return money(this.result, option);
|
|
247
|
+
}
|
|
248
|
+
lt(data) {
|
|
249
|
+
const [d, r] = filterNumber2([data, this.result]);
|
|
250
|
+
return r.lessThan(d);
|
|
251
|
+
}
|
|
252
|
+
le(data) {
|
|
253
|
+
const [d, r] = filterNumber2([data, this.result]);
|
|
254
|
+
return r.lessThanOrEqualTo(d);
|
|
255
|
+
}
|
|
256
|
+
gt(data) {
|
|
257
|
+
const [d, r] = filterNumber2([data, this.result]);
|
|
258
|
+
return r.greaterThan(d);
|
|
259
|
+
}
|
|
260
|
+
ge(data) {
|
|
261
|
+
const [d, r] = filterNumber2([data, this.result]);
|
|
262
|
+
return r.greaterThanOrEqualTo(d);
|
|
263
|
+
}
|
|
264
|
+
nlt(data) {
|
|
265
|
+
const [d, r] = filterNumber2([data, this.result]);
|
|
266
|
+
return !r.lessThan(d);
|
|
267
|
+
}
|
|
268
|
+
nle(data) {
|
|
269
|
+
const [d, r] = filterNumber2([data, this.result]);
|
|
270
|
+
return !r.lessThanOrEqualTo(d);
|
|
271
|
+
}
|
|
272
|
+
ngt(data) {
|
|
273
|
+
const [d, r] = filterNumber2([data, this.result]);
|
|
274
|
+
return !r.greaterThan(d);
|
|
275
|
+
}
|
|
276
|
+
nge(data) {
|
|
277
|
+
const [d, r] = filterNumber2([data, this.result]);
|
|
278
|
+
return !r.greaterThanOrEqualTo(d);
|
|
279
|
+
}
|
|
280
|
+
eq(data) {
|
|
281
|
+
const [d, r] = filterNumber2([data, this.result]);
|
|
282
|
+
return r.equals(d);
|
|
283
|
+
}
|
|
284
|
+
ne(data) {
|
|
285
|
+
const [d, r] = filterNumber2([data, this.result]);
|
|
286
|
+
return !r.eq(d);
|
|
287
|
+
}
|
|
288
|
+
ifLt(data) {
|
|
289
|
+
const [d, r] = filterNumber2([data, this.result]);
|
|
290
|
+
this.ifit = r.lessThan(d);
|
|
291
|
+
return this;
|
|
292
|
+
}
|
|
293
|
+
ifLe(data) {
|
|
294
|
+
const [d, r] = filterNumber2([data, this.result]);
|
|
295
|
+
this.ifit = r.lessThanOrEqualTo(d);
|
|
296
|
+
return this;
|
|
297
|
+
}
|
|
298
|
+
ifGt(data) {
|
|
299
|
+
const [d, r] = filterNumber2([data, this.result]);
|
|
300
|
+
this.ifit = r.greaterThan(d);
|
|
301
|
+
return this;
|
|
302
|
+
}
|
|
303
|
+
ifGe(data) {
|
|
304
|
+
const [d, r] = filterNumber2([data, this.result]);
|
|
305
|
+
this.ifit = r.greaterThanOrEqualTo(d);
|
|
306
|
+
return this;
|
|
307
|
+
}
|
|
308
|
+
ifNlt(data) {
|
|
309
|
+
const [d, r] = filterNumber2([data, this.result]);
|
|
310
|
+
this.ifit = !r.lessThan(d);
|
|
311
|
+
return this;
|
|
312
|
+
}
|
|
313
|
+
ifNle(data) {
|
|
314
|
+
const [d, r] = filterNumber2([data, this.result]);
|
|
315
|
+
this.ifit = !r.lessThanOrEqualTo(d);
|
|
316
|
+
return this;
|
|
317
|
+
}
|
|
318
|
+
ifNgt(data) {
|
|
319
|
+
const [d, r] = filterNumber2([data, this.result]);
|
|
320
|
+
this.ifit = !r.greaterThan(d);
|
|
321
|
+
return this;
|
|
322
|
+
}
|
|
323
|
+
ifNge(data) {
|
|
324
|
+
const [d, r] = filterNumber2([data, this.result]);
|
|
325
|
+
this.ifit = !r.greaterThanOrEqualTo(d);
|
|
326
|
+
return this;
|
|
327
|
+
}
|
|
328
|
+
ifEq(data) {
|
|
329
|
+
const [d, r] = filterNumber2([data, this.result]);
|
|
330
|
+
this.ifit = r.equals(d);
|
|
331
|
+
return this;
|
|
332
|
+
}
|
|
333
|
+
ifNe(data) {
|
|
334
|
+
const [d, r] = filterNumber2([data, this.result]);
|
|
335
|
+
this.ifit = !r.eq(d);
|
|
336
|
+
return this;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
__decorate([
|
|
340
|
+
IF(),
|
|
341
|
+
__metadata("design:type", Function),
|
|
342
|
+
__metadata("design:paramtypes", [Object]),
|
|
343
|
+
__metadata("design:returntype", Object)
|
|
344
|
+
], Bus.prototype, "add", null);
|
|
345
|
+
__decorate([
|
|
346
|
+
IF(),
|
|
347
|
+
__metadata("design:type", Function),
|
|
348
|
+
__metadata("design:paramtypes", [Object]),
|
|
349
|
+
__metadata("design:returntype", Object)
|
|
350
|
+
], Bus.prototype, "sub", null);
|
|
351
|
+
__decorate([
|
|
352
|
+
IF(),
|
|
353
|
+
__metadata("design:type", Function),
|
|
354
|
+
__metadata("design:paramtypes", [Object]),
|
|
355
|
+
__metadata("design:returntype", Object)
|
|
356
|
+
], Bus.prototype, "div", null);
|
|
357
|
+
__decorate([
|
|
358
|
+
IF(),
|
|
359
|
+
__metadata("design:type", Function),
|
|
360
|
+
__metadata("design:paramtypes", [Object, Object]),
|
|
361
|
+
__metadata("design:returntype", Object)
|
|
362
|
+
], Bus.prototype, "divDef", null);
|
|
363
|
+
__decorate([
|
|
364
|
+
IF(),
|
|
365
|
+
__metadata("design:type", Function),
|
|
366
|
+
__metadata("design:paramtypes", [Object]),
|
|
367
|
+
__metadata("design:returntype", Object)
|
|
368
|
+
], Bus.prototype, "mul", null);
|
|
369
|
+
__decorate([
|
|
370
|
+
IF(),
|
|
371
|
+
__metadata("design:type", Function),
|
|
372
|
+
__metadata("design:paramtypes", [Object]),
|
|
373
|
+
__metadata("design:returntype", Object)
|
|
374
|
+
], Bus.prototype, "max", null);
|
|
375
|
+
__decorate([
|
|
376
|
+
IF(),
|
|
377
|
+
__metadata("design:type", Function),
|
|
378
|
+
__metadata("design:paramtypes", [Object]),
|
|
379
|
+
__metadata("design:returntype", Object)
|
|
380
|
+
], Bus.prototype, "min", null);
|
|
381
|
+
__decorate([
|
|
382
|
+
IF(),
|
|
383
|
+
__metadata("design:type", Function),
|
|
384
|
+
__metadata("design:paramtypes", []),
|
|
385
|
+
__metadata("design:returntype", Object)
|
|
386
|
+
], Bus.prototype, "ac", null);
|
|
387
|
+
__decorate([
|
|
388
|
+
IF(),
|
|
389
|
+
__metadata("design:type", Function),
|
|
390
|
+
__metadata("design:paramtypes", []),
|
|
391
|
+
__metadata("design:returntype", Object)
|
|
392
|
+
], Bus.prototype, "abs", null);
|
|
393
|
+
__decorate([
|
|
394
|
+
IF(),
|
|
395
|
+
__metadata("design:type", Function),
|
|
396
|
+
__metadata("design:paramtypes", [Number, Number]),
|
|
397
|
+
__metadata("design:returntype", Object)
|
|
398
|
+
], Bus.prototype, "round", null);
|
|
399
|
+
__decorate([
|
|
400
|
+
IF(),
|
|
401
|
+
__metadata("design:type", Function),
|
|
402
|
+
__metadata("design:paramtypes", [Object]),
|
|
403
|
+
__metadata("design:returntype", void 0)
|
|
404
|
+
], Bus.prototype, "merge", null);
|
|
405
|
+
export const calc = (result) => {
|
|
406
|
+
return new Bus(result);
|
|
407
|
+
};
|
|
408
|
+
export const getGeo = (p1, p2) => {
|
|
409
|
+
p1.lat = calc(p1.latitude).mul(Math.PI).div(180).over();
|
|
410
|
+
p1.long = calc(p1.longitude).mul(Math.PI).div(180).over();
|
|
411
|
+
p2.lat = calc(p2.latitude).mul(Math.PI).div(180).over();
|
|
412
|
+
p2.long = calc(p2.longitude).mul(Math.PI).div(180).over();
|
|
413
|
+
return calc(Math.round(mul(Math.asin(Math.sqrt(add(Math.pow(Math.sin(div(sub(p1.lat, p2.lat), 2)), 2), mul(Math.cos(p1.lat), Math.cos(p2.lat), Math.pow(Math.sin(div(sub(p1.long, p2.long), 2)), 2))))), 2, 6378.137, 10000))).div(10000).round(2).over();
|
|
414
|
+
};
|
package/es/now.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const dateTime = "YYYY-MM-DD HH:mm:ss";
|
|
2
|
+
export declare const dateXSDTime = "YYYY-MM-DDTHH:mm:ss";
|
|
3
|
+
export declare const date = "YYYY-MM-DD";
|
|
4
|
+
export declare const nowTime: () => string;
|
|
5
|
+
export declare const nowDate: () => string;
|
|
6
|
+
export declare const nowTimeXSD: () => string;
|
|
7
|
+
export declare const dateFormat: (str: any, format?: string) => any;
|
package/es/now.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import day from 'dayjs';
|
|
2
|
+
export const dateTime = 'YYYY-MM-DD HH:mm:ss';
|
|
3
|
+
export const dateXSDTime = 'YYYY-MM-DDTHH:mm:ss';
|
|
4
|
+
export const date = 'YYYY-MM-DD';
|
|
5
|
+
export const nowTime = () => day().format(dateTime);
|
|
6
|
+
export const nowDate = () => day().format(date);
|
|
7
|
+
export const nowTimeXSD = () => day().format(dateXSDTime);
|
|
8
|
+
export const dateFormat = (str, format) => {
|
|
9
|
+
if (str) {
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
11
|
+
return day(str).format(format || dateTime);
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
return str;
|
|
15
|
+
}
|
|
16
|
+
};
|