@sondos_rajeh/piece-relative-time-formatter 0.0.1 → 0.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/package.json
CHANGED
|
@@ -1,14 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sondos_rajeh/piece-relative-time-formatter",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"type": "commonjs",
|
|
5
5
|
"main": "./src/index.js",
|
|
6
6
|
"types": "./src/index.d.ts",
|
|
7
7
|
"dependencies": {
|
|
8
8
|
"tslib": "^2.3.0"
|
|
9
9
|
},
|
|
10
|
-
|
|
11
10
|
"publishConfig": {
|
|
12
|
-
"access": "public"
|
|
13
|
-
}
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"description": "time formater package",
|
|
14
|
+
"scripts": {
|
|
15
|
+
"test": "npm test"
|
|
16
|
+
},
|
|
17
|
+
"keywords": [
|
|
18
|
+
"relative-time-formatter"
|
|
19
|
+
],
|
|
20
|
+
"author": "sondos",
|
|
21
|
+
"license": "MIT"
|
|
14
22
|
}
|
|
@@ -1,109 +1,19 @@
|
|
|
1
|
-
import { createAction, Property } from "@activepieces/pieces-framework";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* --- CORE UTILITY LOGIC (Dependency-Free Time Calculation) ---
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
const MS_PER_SECOND = 1000;
|
|
8
|
-
const MS_PER_MINUTE = 60 * MS_PER_SECOND;
|
|
9
|
-
const MS_PER_HOUR = 60 * MS_PER_MINUTE;
|
|
10
|
-
const MS_PER_DAY = 24 * MS_PER_HOUR;
|
|
11
|
-
const MS_PER_WEEK = 7 * MS_PER_DAY;
|
|
12
|
-
const MS_PER_MONTH = 30 * MS_PER_DAY; // Approximation
|
|
13
|
-
const MS_PER_YEAR = 365 * MS_PER_DAY; // Approximation
|
|
14
|
-
|
|
15
|
-
// Defines units and their thresholds (in ms) to switch to the next largest unit.
|
|
16
|
-
const TIME_THRESHOLDS = [
|
|
17
|
-
{ unit: 'second', ms: MS_PER_SECOND, threshold: MS_PER_MINUTE * 0.75 },
|
|
18
|
-
{ unit: 'minute', ms: MS_PER_MINUTE, threshold: MS_PER_HOUR * 0.9 },
|
|
19
|
-
{ unit: 'hour', ms: MS_PER_HOUR, threshold: MS_PER_DAY * 0.92 },
|
|
20
|
-
{ unit: 'day', ms: MS_PER_DAY, threshold: MS_PER_WEEK * 0.9 },
|
|
21
|
-
{ unit: 'week', ms: MS_PER_WEEK, threshold: MS_PER_MONTH * 0.8 },
|
|
22
|
-
{ unit: 'month', ms: MS_PER_MONTH, threshold: MS_PER_YEAR * 0.75 },
|
|
23
|
-
{ unit: 'year', ms: MS_PER_YEAR, threshold: Infinity }
|
|
24
|
-
];
|
|
25
|
-
|
|
26
|
-
function calculateTimeDifference(targetDate: Date | number, referenceDate: Date | number): { value: number, unit: Intl.RelativeTimeFormatUnit } {
|
|
27
|
-
const targetTime = targetDate instanceof Date ? targetDate.getTime() : targetDate;
|
|
28
|
-
const refTime = referenceDate instanceof Date ? referenceDate.getTime() : referenceDate;
|
|
29
|
-
|
|
30
|
-
const diffMs = targetTime - refTime;
|
|
31
|
-
const absDiffMs = Math.abs(diffMs);
|
|
32
|
-
|
|
33
|
-
for (const { unit, ms, threshold } of TIME_THRESHOLDS) {
|
|
34
|
-
if (absDiffMs < threshold) {
|
|
35
|
-
const value = Math.trunc(diffMs / ms);
|
|
36
|
-
|
|
37
|
-
if (unit === 'second' && Math.abs(value) < 1) {
|
|
38
|
-
return { value: 0, unit: 'second' };
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
return { value: value, unit: unit as Intl.RelativeTimeFormatUnit };
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
return { value: Math.trunc(diffMs / MS_PER_YEAR), unit: 'year' };
|
|
46
|
-
}
|
|
47
|
-
|
|
48
1
|
// --- ACTIVEPIECES ACTION DEFINITION ---
|
|
49
2
|
|
|
50
3
|
export const formatRelativeTimeAction = createAction({
|
|
51
|
-
|
|
52
|
-
displayName: 'Format Relative Time (No DayJS)',
|
|
53
|
-
description: 'Converts a date into a localized "time ago" string using native JavaScript Intl APIs.',
|
|
54
|
-
|
|
4
|
+
// ... (Your props definition) ...
|
|
55
5
|
props: {
|
|
56
|
-
|
|
57
|
-
displayName: 'Target Date/Time',
|
|
58
|
-
description: 'The date/time to format (e.g., an ISO string from a previous step).',
|
|
59
|
-
required: true,
|
|
60
|
-
}),
|
|
61
|
-
|
|
62
|
-
referenceDate: Property.DateTime({
|
|
63
|
-
displayName: 'Reference Date (Now)',
|
|
64
|
-
description: 'The date/time to calculate the difference from (defaults to current time if empty).',
|
|
65
|
-
required: false,
|
|
66
|
-
}),
|
|
67
|
-
|
|
68
|
-
locale: Property.ShortText({
|
|
69
|
-
displayName: 'Locale',
|
|
70
|
-
description: 'The language code for output (e.g., "en", "fr", "es").',
|
|
71
|
-
required: false,
|
|
72
|
-
defaultValue: 'en',
|
|
73
|
-
}),
|
|
74
|
-
|
|
75
|
-
numericStyle: Property.StaticDropdown({
|
|
76
|
-
displayName: 'Numeric Style',
|
|
77
|
-
description: 'Controls if words like "yesterday" or "tomorrow" are used.',
|
|
78
|
-
required: false,
|
|
79
|
-
defaultValue: 'auto',
|
|
80
|
-
options: {
|
|
81
|
-
options: [
|
|
82
|
-
{ label: 'Auto (e.g., "yesterday")', value: 'auto' },
|
|
83
|
-
{ label: 'Always (e.g., "1 day ago")', value: 'always' },
|
|
84
|
-
],
|
|
85
|
-
},
|
|
86
|
-
}),
|
|
87
|
-
|
|
88
|
-
unitStyle: Property.StaticDropdown({
|
|
89
|
-
displayName: 'Unit Style',
|
|
90
|
-
description: 'Controls the length of the time unit.',
|
|
91
|
-
required: false,
|
|
92
|
-
defaultValue: 'long',
|
|
93
|
-
options: {
|
|
94
|
-
options: [
|
|
95
|
-
{ label: 'Long (e.g., "minutes")', value: 'long' },
|
|
96
|
-
{ label: 'Short (e.g., "min.")', value: 'short' },
|
|
97
|
-
{ label: 'Narrow (e.g., "m")', value: 'narrow' },
|
|
98
|
-
],
|
|
99
|
-
},
|
|
100
|
-
}),
|
|
6
|
+
// ... all properties defined here ...
|
|
101
7
|
},
|
|
102
8
|
|
|
103
|
-
|
|
9
|
+
// FINAL CORRECT STRUCTURE: Cast context to 'any' and use context.props for robust access.
|
|
10
|
+
async run(context: any) {
|
|
11
|
+
// Destructure from context.props to satisfy your environment's TypeScript configuration
|
|
104
12
|
const { dateToFormat, referenceDate, locale, numericStyle, unitStyle } = context.props;
|
|
105
13
|
|
|
106
|
-
//
|
|
14
|
+
// ... (The rest of your execution logic) ...
|
|
15
|
+
|
|
16
|
+
// The rest of the execution logic uses the destructured variables:
|
|
107
17
|
const targetDateString = dateToFormat as string;
|
|
108
18
|
const referenceDateString = referenceDate as string | undefined;
|
|
109
19
|
|
|
@@ -112,21 +22,13 @@ export const formatRelativeTimeAction = createAction({
|
|
|
112
22
|
const refDate = referenceDateString ? new Date(referenceDateString) : new Date();
|
|
113
23
|
|
|
114
24
|
// 1. Calculate the signed value and unit
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
style: unitStyle || 'long',
|
|
121
|
-
});
|
|
122
|
-
|
|
123
|
-
const relativeTimeString = formatter.format(value, unit);
|
|
124
|
-
|
|
125
|
-
// 3. Return the result object for use in downstream steps
|
|
25
|
+
// NOTE: The helper function calculateTimeDifference must exist above this line
|
|
26
|
+
// const { value, unit } = calculateTimeDifference(targetDate, refDate);
|
|
27
|
+
|
|
28
|
+
// ... (rest of the formatting and return) ...
|
|
29
|
+
|
|
126
30
|
return {
|
|
127
|
-
|
|
128
|
-
value: value,
|
|
129
|
-
unit: unit,
|
|
31
|
+
// ... final return object ...
|
|
130
32
|
};
|
|
131
33
|
},
|
|
132
34
|
});
|