wedance-shared 1.0.23 → 1.0.25
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/dist/utils/date.d.ts +7 -6
- package/dist/utils/date.js +28 -31
- package/dist/utils/date.js.map +1 -1
- package/package.json +1 -1
- package/src/utils/date.ts +40 -45
package/dist/utils/date.d.ts
CHANGED
|
@@ -4,13 +4,14 @@ import { Timestamp } from "firebase/firestore";
|
|
|
4
4
|
* @param timestamp Firestore Timestamp or null/undefined
|
|
5
5
|
* @returns JavaScript Date object or null if input is null/undefined
|
|
6
6
|
*/
|
|
7
|
-
export declare const timestampToDate: (timestamp: Timestamp
|
|
7
|
+
export declare const timestampToDate: (timestamp: Timestamp) => Date;
|
|
8
|
+
export declare function timestampToIsoString(timestamp: Timestamp): string;
|
|
8
9
|
/**
|
|
9
10
|
* Converts a JavaScript Date to a Firestore Timestamp
|
|
10
11
|
* @param date JavaScript Date object or null/undefined
|
|
11
12
|
* @returns Firestore Timestamp or null if input is null/undefined
|
|
12
13
|
*/
|
|
13
|
-
export declare const dateToTimestamp: (date: Date
|
|
14
|
+
export declare const dateToTimestamp: (date: Date) => Timestamp;
|
|
14
15
|
/**
|
|
15
16
|
* Formats a Firestore Timestamp to a string using the provided format
|
|
16
17
|
* Uses day.js library - install with: npm install dayjs
|
|
@@ -20,23 +21,23 @@ export declare const dateToTimestamp: (date: Date | null | undefined) => Timesta
|
|
|
20
21
|
* @param defaultValue Value to return if timestamp is null/undefined
|
|
21
22
|
* @returns Formatted date string or defaultValue if timestamp is null/undefined
|
|
22
23
|
*/
|
|
23
|
-
export declare const formatTimestamp: (timestamp: Timestamp
|
|
24
|
+
export declare const formatTimestamp: (timestamp: Timestamp, formatStr?: string) => string;
|
|
24
25
|
/**
|
|
25
26
|
* Checks if a Firestore Timestamp is before current time
|
|
26
27
|
* @param timestamp Firestore Timestamp to check
|
|
27
28
|
* @returns boolean indicating if timestamp is in the past
|
|
28
29
|
*/
|
|
29
|
-
export declare const isTimestampInPast: (timestamp: Timestamp
|
|
30
|
+
export declare const isTimestampInPast: (timestamp: Timestamp) => boolean;
|
|
30
31
|
/**
|
|
31
32
|
* Checks if a Firestore Timestamp is after current time
|
|
32
33
|
* @param timestamp Firestore Timestamp to check
|
|
33
34
|
* @returns boolean indicating if timestamp is in the future
|
|
34
35
|
*/
|
|
35
|
-
export declare const isTimestampInFuture: (timestamp: Timestamp
|
|
36
|
+
export declare const isTimestampInFuture: (timestamp: Timestamp) => boolean;
|
|
36
37
|
/**
|
|
37
38
|
* Gets the relative time between now and a Firestore Timestamp (e.g. "2 hours ago", "in 3 days")
|
|
38
39
|
* Uses day.js with relativeTime plugin for localized relative time formatting
|
|
39
40
|
* @param timestamp Firestore Timestamp
|
|
40
41
|
* @returns String representation of relative time
|
|
41
42
|
*/
|
|
42
|
-
export declare const getRelativeTime: (timestamp: Timestamp
|
|
43
|
+
export declare const getRelativeTime: (timestamp: Timestamp) => string;
|
package/dist/utils/date.js
CHANGED
|
@@ -9,18 +9,23 @@ dayjs.extend(relativeTime);
|
|
|
9
9
|
* @returns JavaScript Date object or null if input is null/undefined
|
|
10
10
|
*/
|
|
11
11
|
export const timestampToDate = (timestamp) => {
|
|
12
|
-
if (!timestamp)
|
|
13
|
-
|
|
12
|
+
if (!timestamp) {
|
|
13
|
+
throw new Error("Timestamp is null or undefined");
|
|
14
|
+
}
|
|
14
15
|
return new Date(timestamp.seconds * 1000 + timestamp.nanoseconds / 1000000);
|
|
15
16
|
};
|
|
17
|
+
export function timestampToIsoString(timestamp) {
|
|
18
|
+
return new Date(timestamp.seconds * 1000).toISOString();
|
|
19
|
+
}
|
|
16
20
|
/**
|
|
17
21
|
* Converts a JavaScript Date to a Firestore Timestamp
|
|
18
22
|
* @param date JavaScript Date object or null/undefined
|
|
19
23
|
* @returns Firestore Timestamp or null if input is null/undefined
|
|
20
24
|
*/
|
|
21
25
|
export const dateToTimestamp = (date) => {
|
|
22
|
-
if (!date)
|
|
23
|
-
|
|
26
|
+
if (!date) {
|
|
27
|
+
throw new Error("Date is null or undefined");
|
|
28
|
+
}
|
|
24
29
|
return Timestamp.fromDate(date);
|
|
25
30
|
};
|
|
26
31
|
/**
|
|
@@ -32,19 +37,14 @@ export const dateToTimestamp = (date) => {
|
|
|
32
37
|
* @param defaultValue Value to return if timestamp is null/undefined
|
|
33
38
|
* @returns Formatted date string or defaultValue if timestamp is null/undefined
|
|
34
39
|
*/
|
|
35
|
-
export const formatTimestamp = (timestamp, formatStr = "MMM D, YYYY"
|
|
36
|
-
if (!timestamp)
|
|
37
|
-
|
|
38
|
-
try {
|
|
39
|
-
// Convert timestamp to Date
|
|
40
|
-
const date = new Date(timestamp.seconds * 1000 + timestamp.nanoseconds / 1000000);
|
|
41
|
-
// Use dayjs to format the date according to the format string
|
|
42
|
-
return dayjs(date).format(formatStr);
|
|
43
|
-
}
|
|
44
|
-
catch (error) {
|
|
45
|
-
console.error("Error formatting timestamp:", error);
|
|
46
|
-
return defaultValue;
|
|
40
|
+
export const formatTimestamp = (timestamp, formatStr = "MMM D, YYYY") => {
|
|
41
|
+
if (!timestamp) {
|
|
42
|
+
throw new Error("Timestamp is null or undefined");
|
|
47
43
|
}
|
|
44
|
+
// Convert timestamp to Date
|
|
45
|
+
const date = new Date(timestamp.seconds * 1000 + timestamp.nanoseconds / 1000000);
|
|
46
|
+
// Use dayjs to format the date according to the format string
|
|
47
|
+
return dayjs(date).format(formatStr);
|
|
48
48
|
};
|
|
49
49
|
/**
|
|
50
50
|
* Checks if a Firestore Timestamp is before current time
|
|
@@ -52,8 +52,9 @@ export const formatTimestamp = (timestamp, formatStr = "MMM D, YYYY", defaultVal
|
|
|
52
52
|
* @returns boolean indicating if timestamp is in the past
|
|
53
53
|
*/
|
|
54
54
|
export const isTimestampInPast = (timestamp) => {
|
|
55
|
-
if (!timestamp)
|
|
56
|
-
|
|
55
|
+
if (!timestamp) {
|
|
56
|
+
throw new Error("Timestamp is null or undefined");
|
|
57
|
+
}
|
|
57
58
|
// Convert timestamp to milliseconds manually
|
|
58
59
|
const timestampMillis = timestamp.seconds * 1000 + timestamp.nanoseconds / 1000000;
|
|
59
60
|
return timestampMillis < Date.now();
|
|
@@ -64,8 +65,9 @@ export const isTimestampInPast = (timestamp) => {
|
|
|
64
65
|
* @returns boolean indicating if timestamp is in the future
|
|
65
66
|
*/
|
|
66
67
|
export const isTimestampInFuture = (timestamp) => {
|
|
67
|
-
if (!timestamp)
|
|
68
|
-
|
|
68
|
+
if (!timestamp) {
|
|
69
|
+
throw new Error("Timestamp is null or undefined");
|
|
70
|
+
}
|
|
69
71
|
// Convert timestamp to milliseconds manually
|
|
70
72
|
const timestampMillis = timestamp.seconds * 1000 + timestamp.nanoseconds / 1000000;
|
|
71
73
|
return timestampMillis > Date.now();
|
|
@@ -77,17 +79,12 @@ export const isTimestampInFuture = (timestamp) => {
|
|
|
77
79
|
* @returns String representation of relative time
|
|
78
80
|
*/
|
|
79
81
|
export const getRelativeTime = (timestamp) => {
|
|
80
|
-
if (!timestamp)
|
|
81
|
-
|
|
82
|
-
try {
|
|
83
|
-
// Convert timestamp to milliseconds manually
|
|
84
|
-
const timestampMillis = timestamp.seconds * 1000 + timestamp.nanoseconds / 1000000;
|
|
85
|
-
// Use dayjs to get relative time
|
|
86
|
-
return dayjs(timestampMillis).fromNow();
|
|
87
|
-
}
|
|
88
|
-
catch (error) {
|
|
89
|
-
console.error("Error generating relative time:", error);
|
|
90
|
-
return "";
|
|
82
|
+
if (!timestamp) {
|
|
83
|
+
throw new Error("Timestamp is null or undefined");
|
|
91
84
|
}
|
|
85
|
+
// Convert timestamp to milliseconds manually
|
|
86
|
+
const timestampMillis = timestamp.seconds * 1000 + timestamp.nanoseconds / 1000000;
|
|
87
|
+
// Use dayjs to get relative time
|
|
88
|
+
return dayjs(timestampMillis).fromNow();
|
|
92
89
|
};
|
|
93
90
|
//# sourceMappingURL=date.js.map
|
package/dist/utils/date.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"date.js","sourceRoot":"","sources":["../../src/utils/date.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,YAAY,MAAM,2BAA2B,CAAC;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAE/C,mCAAmC;AACnC,KAAK,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;AAE3B;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,
|
|
1
|
+
{"version":3,"file":"date.js","sourceRoot":"","sources":["../../src/utils/date.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAC1B,OAAO,YAAY,MAAM,2BAA2B,CAAC;AACrD,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAE/C,mCAAmC;AACnC,KAAK,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;AAE3B;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,SAAoB,EAAQ,EAAE;IAC5D,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;IACpD,CAAC;IACD,OAAO,IAAI,IAAI,CAAC,SAAS,CAAC,OAAO,GAAG,IAAI,GAAG,SAAS,CAAC,WAAW,GAAG,OAAO,CAAC,CAAC;AAC9E,CAAC,CAAC;AAEF,MAAM,UAAU,oBAAoB,CAAC,SAAoB;IACvD,OAAO,IAAI,IAAI,CAAC,SAAS,CAAC,OAAO,GAAG,IAAI,CAAC,CAAC,WAAW,EAAE,CAAC;AAC1D,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,MAAM,eAAe,GAAG,CAAC,IAAU,EAAa,EAAE;IACvD,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,MAAM,IAAI,KAAK,CAAC,2BAA2B,CAAC,CAAC;IAC/C,CAAC;IACD,OAAO,SAAS,CAAC,QAAQ,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C,MAAM,eAAe,GAAG,CAAC,SAAoB,EAAU,EAAE;IAC9D,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;IACpD,CAAC;IAED,6CAA6C;IAC7C,MAAM,eAAe,GACnB,SAAS,CAAC,OAAO,GAAG,IAAI,GAAG,SAAS,CAAC,WAAW,GAAG,OAAO,CAAC;IAE7D,iCAAiC;IACjC,OAAO,KAAK,CAAC,eAAe,CAAC,CAAC,OAAO,EAAE,CAAC;AAC1C,CAAC,CAAC"}
|
package/package.json
CHANGED
package/src/utils/date.ts
CHANGED
|
@@ -10,22 +10,26 @@ dayjs.extend(relativeTime);
|
|
|
10
10
|
* @param timestamp Firestore Timestamp or null/undefined
|
|
11
11
|
* @returns JavaScript Date object or null if input is null/undefined
|
|
12
12
|
*/
|
|
13
|
-
export const timestampToDate = (
|
|
14
|
-
timestamp
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
export const timestampToDate = (timestamp: Timestamp): Date => {
|
|
14
|
+
if (!timestamp) {
|
|
15
|
+
throw new Error("Timestamp is null or undefined");
|
|
16
|
+
}
|
|
17
17
|
return new Date(timestamp.seconds * 1000 + timestamp.nanoseconds / 1000000);
|
|
18
18
|
};
|
|
19
19
|
|
|
20
|
+
export function timestampToIsoString(timestamp: Timestamp) {
|
|
21
|
+
return new Date(timestamp.seconds * 1000).toISOString();
|
|
22
|
+
}
|
|
23
|
+
|
|
20
24
|
/**
|
|
21
25
|
* Converts a JavaScript Date to a Firestore Timestamp
|
|
22
26
|
* @param date JavaScript Date object or null/undefined
|
|
23
27
|
* @returns Firestore Timestamp or null if input is null/undefined
|
|
24
28
|
*/
|
|
25
|
-
export const dateToTimestamp = (
|
|
26
|
-
date
|
|
27
|
-
|
|
28
|
-
|
|
29
|
+
export const dateToTimestamp = (date: Date): Timestamp => {
|
|
30
|
+
if (!date) {
|
|
31
|
+
throw new Error("Date is null or undefined");
|
|
32
|
+
}
|
|
29
33
|
return Timestamp.fromDate(date);
|
|
30
34
|
};
|
|
31
35
|
|
|
@@ -39,24 +43,20 @@ export const dateToTimestamp = (
|
|
|
39
43
|
* @returns Formatted date string or defaultValue if timestamp is null/undefined
|
|
40
44
|
*/
|
|
41
45
|
export const formatTimestamp = (
|
|
42
|
-
timestamp: Timestamp
|
|
43
|
-
formatStr: string = "MMM D, YYYY"
|
|
44
|
-
defaultValue: string = ""
|
|
46
|
+
timestamp: Timestamp,
|
|
47
|
+
formatStr: string = "MMM D, YYYY"
|
|
45
48
|
): string => {
|
|
46
|
-
if (!timestamp)
|
|
49
|
+
if (!timestamp) {
|
|
50
|
+
throw new Error("Timestamp is null or undefined");
|
|
51
|
+
}
|
|
47
52
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
);
|
|
53
|
+
// Convert timestamp to Date
|
|
54
|
+
const date = new Date(
|
|
55
|
+
timestamp.seconds * 1000 + timestamp.nanoseconds / 1000000
|
|
56
|
+
);
|
|
53
57
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
} catch (error) {
|
|
57
|
-
console.error("Error formatting timestamp:", error);
|
|
58
|
-
return defaultValue;
|
|
59
|
-
}
|
|
58
|
+
// Use dayjs to format the date according to the format string
|
|
59
|
+
return dayjs(date).format(formatStr);
|
|
60
60
|
};
|
|
61
61
|
|
|
62
62
|
/**
|
|
@@ -64,10 +64,10 @@ export const formatTimestamp = (
|
|
|
64
64
|
* @param timestamp Firestore Timestamp to check
|
|
65
65
|
* @returns boolean indicating if timestamp is in the past
|
|
66
66
|
*/
|
|
67
|
-
export const isTimestampInPast = (
|
|
68
|
-
timestamp
|
|
69
|
-
|
|
70
|
-
|
|
67
|
+
export const isTimestampInPast = (timestamp: Timestamp): boolean => {
|
|
68
|
+
if (!timestamp) {
|
|
69
|
+
throw new Error("Timestamp is null or undefined");
|
|
70
|
+
}
|
|
71
71
|
|
|
72
72
|
// Convert timestamp to milliseconds manually
|
|
73
73
|
const timestampMillis =
|
|
@@ -80,10 +80,10 @@ export const isTimestampInPast = (
|
|
|
80
80
|
* @param timestamp Firestore Timestamp to check
|
|
81
81
|
* @returns boolean indicating if timestamp is in the future
|
|
82
82
|
*/
|
|
83
|
-
export const isTimestampInFuture = (
|
|
84
|
-
timestamp
|
|
85
|
-
|
|
86
|
-
|
|
83
|
+
export const isTimestampInFuture = (timestamp: Timestamp): boolean => {
|
|
84
|
+
if (!timestamp) {
|
|
85
|
+
throw new Error("Timestamp is null or undefined");
|
|
86
|
+
}
|
|
87
87
|
|
|
88
88
|
// Convert timestamp to milliseconds manually
|
|
89
89
|
const timestampMillis =
|
|
@@ -97,20 +97,15 @@ export const isTimestampInFuture = (
|
|
|
97
97
|
* @param timestamp Firestore Timestamp
|
|
98
98
|
* @returns String representation of relative time
|
|
99
99
|
*/
|
|
100
|
-
export const getRelativeTime = (
|
|
101
|
-
timestamp
|
|
102
|
-
|
|
103
|
-
|
|
100
|
+
export const getRelativeTime = (timestamp: Timestamp): string => {
|
|
101
|
+
if (!timestamp) {
|
|
102
|
+
throw new Error("Timestamp is null or undefined");
|
|
103
|
+
}
|
|
104
104
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
timestamp.seconds * 1000 + timestamp.nanoseconds / 1000000;
|
|
105
|
+
// Convert timestamp to milliseconds manually
|
|
106
|
+
const timestampMillis =
|
|
107
|
+
timestamp.seconds * 1000 + timestamp.nanoseconds / 1000000;
|
|
109
108
|
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
} catch (error) {
|
|
113
|
-
console.error("Error generating relative time:", error);
|
|
114
|
-
return "";
|
|
115
|
-
}
|
|
109
|
+
// Use dayjs to get relative time
|
|
110
|
+
return dayjs(timestampMillis).fromNow();
|
|
116
111
|
};
|