@jsb188/app 1.0.4 → 1.1.1
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/constants/app.ts +2 -2
- package/constants/emoji.ts +16 -16
- package/constants/journal.ts +74 -0
- package/i18n/app.ts +2 -0
- package/i18n/auth.ts +12 -6
- package/i18n/datetime.ts +0 -1
- package/i18n/error.ts +11 -6
- package/i18n/form.ts +1 -0
- package/i18n/index.ts +0 -1
- package/i18n/user.ts +6 -3
- package/main.ts +36 -4
- package/package.json +10 -5
- package/platform.ts +125 -0
- package/types/ai.d.ts +30 -0
- package/types/app.d.ts +2 -0
- package/types/auth.d.ts +63 -0
- package/types/journal.d.ts +24 -0
- package/types/organization.d.ts +38 -0
- package/types.d.ts +8 -0
- package/utils/datetime.ts +473 -289
- package/utils/journal.ts +17 -0
- package/utils/logic.ts +81 -78
- package/utils/number.ts +92 -68
- package/utils/object.ts +277 -219
- package/utils/string.ts +219 -107
- package/utils/timeZone.ts +2369 -0
- package/types/datetime.d.ts +0 -10
package/utils/journal.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ColorEnum } from '../types/app.d';
|
|
2
|
+
import type { JournalTypeEnum } from '../types/journal.d';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Map journal type to a color
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export function getJournalTypeColor(type: JournalTypeEnum): ColorEnum {
|
|
9
|
+
const journalTypeToColor = {
|
|
10
|
+
FIELD: 'yellow',
|
|
11
|
+
HARVEST: 'green',
|
|
12
|
+
} as Record<JournalTypeEnum, ColorEnum>;
|
|
13
|
+
|
|
14
|
+
// Default to zinc if type is not found
|
|
15
|
+
const color = journalTypeToColor[type] || 'zinc';
|
|
16
|
+
return color;
|
|
17
|
+
}
|
package/utils/logic.ts
CHANGED
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
type DatabaseAction = 'INSERT' | 'UPDATE' | 'NONE';
|
|
6
6
|
|
|
7
7
|
type DatabaseActionResult = {
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
action: DatabaseAction;
|
|
9
|
+
documentData: any;
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
/**
|
|
@@ -15,56 +15,56 @@ type DatabaseActionResult = {
|
|
|
15
15
|
*/
|
|
16
16
|
|
|
17
17
|
export function getDatabaseAction(
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
18
|
+
currentData: any,
|
|
19
|
+
data: any,
|
|
20
|
+
allowNull?: boolean,
|
|
21
|
+
mergeCurrentData?: boolean,
|
|
22
22
|
): DatabaseActionResult {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
23
|
+
if (!currentData) {
|
|
24
|
+
return {
|
|
25
|
+
action: 'INSERT',
|
|
26
|
+
documentData: data,
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const documentData: Record<string, any> = {};
|
|
31
|
+
|
|
32
|
+
let hasChanges = false;
|
|
33
|
+
for (const key in data) {
|
|
34
|
+
if (Object.prototype.hasOwnProperty.call(data, key) && data[key] !== undefined) {
|
|
35
|
+
// trim() on all string databse fields should be OK;
|
|
36
|
+
// But if not, add an extra param.
|
|
37
|
+
const value = typeof data[key] === 'string' ? data[key].trim() : data[key];
|
|
38
|
+
|
|
39
|
+
const isNullValue = value !== null || allowNull;
|
|
40
|
+
if (isNullValue) {
|
|
41
|
+
documentData[key] = value;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (
|
|
45
|
+
// Doing loose != check instead of !== type check
|
|
46
|
+
currentData[key] != value &&
|
|
47
|
+
isNullValue
|
|
48
|
+
) {
|
|
49
|
+
hasChanges = true;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (mergeCurrentData) {
|
|
55
|
+
for (const key in currentData) {
|
|
56
|
+
if (Object.prototype.hasOwnProperty.call(currentData, key) && documentData[key] === undefined) {
|
|
57
|
+
documentData[key] = currentData[key];
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const action = hasChanges ? 'UPDATE' : 'NONE';
|
|
63
|
+
|
|
64
|
+
return {
|
|
65
|
+
action,
|
|
66
|
+
documentData,
|
|
67
|
+
};
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
/**
|
|
@@ -72,13 +72,13 @@ export function getDatabaseAction(
|
|
|
72
72
|
*/
|
|
73
73
|
|
|
74
74
|
export function checkLastSetTime(time?: number, minsThreshold: number = 5) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
75
|
+
if (!time) {
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
78
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
79
|
+
const now = Date.now();
|
|
80
|
+
const diff = now - time;
|
|
81
|
+
return diff > (minsThreshold * 60 * 1000);
|
|
82
82
|
}
|
|
83
83
|
|
|
84
84
|
/**
|
|
@@ -86,30 +86,33 @@ export function checkLastSetTime(time?: number, minsThreshold: number = 5) {
|
|
|
86
86
|
*/
|
|
87
87
|
|
|
88
88
|
export function delay(ms: number) {
|
|
89
|
-
|
|
89
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
90
90
|
}
|
|
91
91
|
|
|
92
92
|
/**
|
|
93
93
|
* Promise with timeout
|
|
94
94
|
*/
|
|
95
95
|
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
96
|
+
export function timeoutPromise<T>(
|
|
97
|
+
mainPromise: () => any,
|
|
98
|
+
onTimeout: () => any,
|
|
99
|
+
timeoutMs: number,
|
|
100
|
+
): Promise<T> {
|
|
101
|
+
return new Promise<T>((resolve, reject) => {
|
|
102
|
+
const timeout = setTimeout(() => {
|
|
103
|
+
resolve(onTimeout());
|
|
104
|
+
}, timeoutMs);
|
|
105
|
+
|
|
106
|
+
const p = mainPromise();
|
|
107
|
+
if (typeof p?.then === 'function') {
|
|
108
|
+
p.then((result: any) => {
|
|
109
|
+
clearTimeout(timeout);
|
|
110
|
+
resolve(result);
|
|
111
|
+
})
|
|
112
|
+
.catch((err: any) => {
|
|
113
|
+
clearTimeout(timeout);
|
|
114
|
+
reject(err);
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
});
|
|
118
|
+
}
|
package/utils/number.ts
CHANGED
|
@@ -2,30 +2,36 @@
|
|
|
2
2
|
* Check if number
|
|
3
3
|
*/
|
|
4
4
|
|
|
5
|
-
export function isNumber(input: unknown): input is number {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
5
|
+
export function isNumber(input: unknown): input is number | string {
|
|
6
|
+
switch (typeof input) {
|
|
7
|
+
case 'number':
|
|
8
|
+
return input - input === 0;
|
|
9
|
+
case 'string':
|
|
10
|
+
return input.trim() !== '' && !Number.isNaN(Number(input));
|
|
11
|
+
}
|
|
12
|
+
return false;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* Allow double digit numbers to be represented by a letter
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
-
export function numberToLetter(
|
|
20
|
-
|
|
19
|
+
export function numberToLetter(
|
|
20
|
+
num: number,
|
|
21
|
+
alphabet: string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
|
|
22
|
+
) {
|
|
23
|
+
return alphabet.charAt(num);
|
|
21
24
|
}
|
|
22
25
|
|
|
23
26
|
/**
|
|
24
27
|
* Convert the letter (from above fn()) back to a number;
|
|
25
28
|
*/
|
|
26
29
|
|
|
27
|
-
export function letterToNumber(
|
|
28
|
-
|
|
30
|
+
export function letterToNumber(
|
|
31
|
+
letter: string,
|
|
32
|
+
alphabet: string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
|
|
33
|
+
) {
|
|
34
|
+
return alphabet.indexOf(letter);
|
|
29
35
|
}
|
|
30
36
|
|
|
31
37
|
/**
|
|
@@ -33,23 +39,23 @@ export function letterToNumber(letter: string, alphabet: string = '0123456789abc
|
|
|
33
39
|
*/
|
|
34
40
|
|
|
35
41
|
export function versionToNumber(version: string) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
42
|
+
const arr = String(version).split('.');
|
|
43
|
+
return arr.reduce((acc: number, num: string, n) => {
|
|
44
|
+
let val;
|
|
45
|
+
let minorVal;
|
|
46
|
+
if (!isNaN(Number(num))) {
|
|
47
|
+
val = Number(num);
|
|
48
|
+
minorVal = 0;
|
|
49
|
+
} else {
|
|
50
|
+
// NOTE: Use this to make minor version same as without the letter
|
|
51
|
+
val = Number(num.replace(/\D/g, ''));
|
|
52
|
+
minorVal = letterToNumber(num.replace(/[0-9]/g, ''));
|
|
53
|
+
// console.log('->', num, val, minorVal);
|
|
54
|
+
}
|
|
55
|
+
// console.log(acc + Math.pow(100, 4 - n) * val);
|
|
56
|
+
|
|
57
|
+
return acc + Math.pow(100, 4 - n) * val + minorVal;
|
|
58
|
+
}, 0);
|
|
53
59
|
}
|
|
54
60
|
|
|
55
61
|
/**
|
|
@@ -57,16 +63,16 @@ export function versionToNumber(version: string) {
|
|
|
57
63
|
*/
|
|
58
64
|
|
|
59
65
|
export function formatBytes(bytes: number, decimals: number = 0) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
66
|
+
if (!+bytes) {
|
|
67
|
+
return '0 Bytes';
|
|
68
|
+
}
|
|
63
69
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
70
|
+
const k = 1024;
|
|
71
|
+
const dm = decimals < 0 ? 0 : decimals;
|
|
72
|
+
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
|
|
67
73
|
|
|
68
|
-
|
|
69
|
-
|
|
74
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
75
|
+
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
|
|
70
76
|
}
|
|
71
77
|
|
|
72
78
|
/**
|
|
@@ -74,36 +80,36 @@ export function formatBytes(bytes: number, decimals: number = 0) {
|
|
|
74
80
|
*/
|
|
75
81
|
|
|
76
82
|
export function secondsToTime(secs: number) {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
83
|
+
if (secs < 60) {
|
|
84
|
+
return {
|
|
85
|
+
value: secs,
|
|
86
|
+
unit: 's',
|
|
87
|
+
};
|
|
88
|
+
} else if (secs >= 604800) {
|
|
89
|
+
const weeks = Math.floor(secs / 604800);
|
|
90
|
+
return {
|
|
91
|
+
value: weeks,
|
|
92
|
+
unit: 'w',
|
|
93
|
+
};
|
|
94
|
+
} else if (secs >= 86400) {
|
|
95
|
+
const days = Math.floor(secs / 86400);
|
|
96
|
+
return {
|
|
97
|
+
value: days,
|
|
98
|
+
unit: 'd',
|
|
99
|
+
};
|
|
100
|
+
} else if (secs >= 3600) {
|
|
101
|
+
const hours = Math.floor(secs / 3600);
|
|
102
|
+
return {
|
|
103
|
+
value: hours,
|
|
104
|
+
unit: 'h',
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const minutes = Math.floor((secs % 3600) / 60);
|
|
109
|
+
return {
|
|
110
|
+
value: minutes,
|
|
111
|
+
unit: 'm',
|
|
112
|
+
};
|
|
107
113
|
}
|
|
108
114
|
|
|
109
115
|
/**
|
|
@@ -111,5 +117,23 @@ export function secondsToTime(secs: number) {
|
|
|
111
117
|
*/
|
|
112
118
|
|
|
113
119
|
export function calculateDiscount(normalPrice: number, discountedPrice: number) {
|
|
114
|
-
|
|
120
|
+
return Math.round(((normalPrice - discountedPrice) / normalPrice) * 100);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Format decimal number to a string without decimal places or the first non-zero digit
|
|
125
|
+
*
|
|
126
|
+
* @param value - The number to format
|
|
127
|
+
* @param trimDecimals - If true, trims the decimal places to the first non-zero digit
|
|
128
|
+
* @returns Formatted string representation of the number
|
|
129
|
+
*/
|
|
130
|
+
|
|
131
|
+
export function formatDecimal(value_: string | number, trimDecimals: boolean = false): string {
|
|
132
|
+
const value = String(value_);
|
|
133
|
+
if (Number(value) % 1 === 0) {
|
|
134
|
+
return Number(value).toString(); // Whole number
|
|
135
|
+
} else if (trimDecimals) {
|
|
136
|
+
return parseFloat(value.toString()).toString(); // Trims to first non-zero decimal
|
|
137
|
+
}
|
|
138
|
+
return String(value); // Keep existing decimal places
|
|
115
139
|
}
|