@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.
@@ -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
- action: DatabaseAction;
9
- documentData: any;
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
- currentData: any,
19
- data: any,
20
- allowNull?: boolean,
21
- mergeCurrentData?: boolean,
18
+ currentData: any,
19
+ data: any,
20
+ allowNull?: boolean,
21
+ mergeCurrentData?: boolean,
22
22
  ): DatabaseActionResult {
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
- };
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
- if (!time) {
76
- return true;
77
- }
75
+ if (!time) {
76
+ return true;
77
+ }
78
78
 
79
- const now = Date.now();
80
- const diff = now - time;
81
- return diff > (minsThreshold * 60 * 1000);
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
- return new Promise((resolve) => setTimeout(resolve, ms));
89
+ return new Promise((resolve) => setTimeout(resolve, ms));
90
90
  }
91
91
 
92
92
  /**
93
93
  * Promise with timeout
94
94
  */
95
95
 
96
- // export function timeoutPromise(promiseFn: Function, ms = 10000) {
97
- // return new Promise((resolve, reject) => {
98
- // // Set up the timeout
99
- // const timer = setTimeout(() => {
100
- // reject(new Error(`Promise timed out after ${ms} ms`));
101
- // }, ms);
102
-
103
- // // Set up the real work
104
- // promiseFn(
105
- // (value: any) => {
106
- // clearTimeout(timer);
107
- // resolve(value);
108
- // },
109
- // (error: any) => {
110
- // clearTimeout(timer);
111
- // reject(error);
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
- 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;
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(num: number, alphabet: string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') {
20
- return alphabet.charAt(num);
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(letter: string, alphabet: string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') {
28
- return alphabet.indexOf(letter);
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
- const arr = String(version).split('.');
37
- return arr.reduce((acc: number, num: string, n) => {
38
- let val;
39
- let minorVal;
40
- if (!isNaN(Number(num))) {
41
- val = Number(num);
42
- minorVal = 0;
43
- } else {
44
- // NOTE: Use this to make minor version same as without the letter
45
- val = Number(num.replace(/\D/g, ''));
46
- minorVal = letterToNumber(num.replace(/[0-9]/g, ''));
47
- // console.log('->', num, val, minorVal);
48
- }
49
- // console.log(acc + Math.pow(100, 4 - n) * val);
50
-
51
- return acc + Math.pow(100, 4 - n) * val + minorVal;
52
- }, 0);
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
- if (!+bytes) {
61
- return '0 Bytes';
62
- }
66
+ if (!+bytes) {
67
+ return '0 Bytes';
68
+ }
63
69
 
64
- const k = 1024;
65
- const dm = decimals < 0 ? 0 : decimals;
66
- const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
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
- const i = Math.floor(Math.log(bytes) / Math.log(k));
69
- return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
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
- if (secs < 60) {
78
- return {
79
- value: secs,
80
- unit: 's',
81
- };
82
- } else if (secs >= 604800) {
83
- const weeks = Math.floor(secs / 604800);
84
- return {
85
- value: weeks,
86
- unit: 'w',
87
- };
88
- } else if (secs >= 86400) {
89
- const days = Math.floor(secs / 86400);
90
- return {
91
- value: days,
92
- unit: 'd',
93
- };
94
- } else if (secs >= 3600) {
95
- const hours = Math.floor(secs / 3600);
96
- return {
97
- value: hours,
98
- unit: 'h',
99
- };
100
- }
101
-
102
- const minutes = Math.floor((secs % 3600) / 60);
103
- return {
104
- value: minutes,
105
- unit: 'm',
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
- return Math.round(((normalPrice - discountedPrice) / normalPrice) * 100);
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
  }