@sondos_rajeh/piece-relative-time-formatter 0.0.9 → 0.1.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/package.json +5 -11
- package/src/index.d.ts +1 -0
- package/src/index.js +15 -0
- package/src/index.js.map +1 -0
- package/src/lib/actions/format-relative-time-action.d.ts +7 -0
- package/src/lib/actions/format-relative-time-action.js +116 -0
- package/src/lib/actions/format-relative-time-action.js.map +1 -0
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sondos_rajeh/piece-relative-time-formatter",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.1.0",
|
|
4
4
|
"type": "commonjs",
|
|
5
|
-
"main": "
|
|
6
|
-
"types": "
|
|
5
|
+
"main": "src/index.js",
|
|
6
|
+
"types": "src/index.d.ts",
|
|
7
7
|
"dependencies": {
|
|
8
8
|
"@sinclair/typebox": "0.34.11",
|
|
9
9
|
"deepmerge-ts": "7.1.0",
|
|
@@ -17,15 +17,9 @@
|
|
|
17
17
|
"access": "public"
|
|
18
18
|
},
|
|
19
19
|
"files": [
|
|
20
|
-
"
|
|
20
|
+
"src"
|
|
21
21
|
],
|
|
22
22
|
"description": "time formater package",
|
|
23
|
-
"scripts": {
|
|
24
|
-
"test": "npm test"
|
|
25
|
-
},
|
|
26
|
-
"keywords": [
|
|
27
|
-
"relative-time-formatter"
|
|
28
|
-
],
|
|
29
23
|
"author": "sondos",
|
|
30
24
|
"license": "MIT",
|
|
31
25
|
"overrides": {
|
|
@@ -42,4 +36,4 @@
|
|
|
42
36
|
"@opentelemetry/sdk-trace-base": "latest",
|
|
43
37
|
"@opentelemetry/api": "latest"
|
|
44
38
|
}
|
|
45
|
-
}
|
|
39
|
+
}
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const relativeTimeFormatter: import("@activepieces/pieces-framework").Piece<import("@activepieces/pieces-framework").PieceAuthProperty>;
|
package/src/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.relativeTimeFormatter = void 0;
|
|
4
|
+
const pieces_framework_1 = require("@activepieces/pieces-framework");
|
|
5
|
+
const format_relative_time_action_1 = require("./lib/actions/format-relative-time-action");
|
|
6
|
+
exports.relativeTimeFormatter = (0, pieces_framework_1.createPiece)({
|
|
7
|
+
displayName: "Relative-time-formatter",
|
|
8
|
+
auth: pieces_framework_1.PieceAuth.None(),
|
|
9
|
+
minimumSupportedRelease: '0.36.1',
|
|
10
|
+
logoUrl: "https://cdn.activepieces.com/pieces/relative-time-formatter.png",
|
|
11
|
+
authors: [],
|
|
12
|
+
actions: [format_relative_time_action_1.formatRelativeTimeAction],
|
|
13
|
+
triggers: [],
|
|
14
|
+
});
|
|
15
|
+
//# sourceMappingURL=index.js.map
|
package/src/index.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../packages/pieces/community/relative-time-formatter/src/index.ts"],"names":[],"mappings":";;;AACI,qEAAwE;AACxE,2FAAqF;AAExE,QAAA,qBAAqB,GAAG,IAAA,8BAAW,EAAC;IAC/C,WAAW,EAAE,yBAAyB;IACtC,IAAI,EAAE,4BAAS,CAAC,IAAI,EAAE;IACtB,uBAAuB,EAAE,QAAQ;IACjC,OAAO,EAAE,iEAAiE;IAC1E,OAAO,EAAE,EAAE;IACX,OAAO,EAAE,CAAC,sDAAwB,CAAC;IACnC,QAAQ,EAAE,EAAE;CACb,CAAC,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const formatRelativeTimeAction: import("@activepieces/pieces-framework").IAction<import("@activepieces/pieces-framework").PieceAuthProperty, {
|
|
2
|
+
dateToFormat: import("@activepieces/pieces-framework").DateTimeProperty<true>;
|
|
3
|
+
referenceDate: import("@activepieces/pieces-framework").DateTimeProperty<false>;
|
|
4
|
+
locale: import("@activepieces/pieces-framework").ShortTextProperty<false>;
|
|
5
|
+
numericStyle: import("@activepieces/pieces-framework").StaticDropdownProperty<string, false>;
|
|
6
|
+
unitStyle: import("@activepieces/pieces-framework").StaticDropdownProperty<string, false>;
|
|
7
|
+
}>;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.formatRelativeTimeAction = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
const pieces_framework_1 = require("@activepieces/pieces-framework");
|
|
6
|
+
/**
|
|
7
|
+
* --- CORE UTILITY LOGIC (Dependency-Free Time Calculation) ---
|
|
8
|
+
*/
|
|
9
|
+
const MS_PER_SECOND = 1000;
|
|
10
|
+
const MS_PER_MINUTE = 60 * MS_PER_SECOND;
|
|
11
|
+
const MS_PER_HOUR = 60 * MS_PER_MINUTE;
|
|
12
|
+
const MS_PER_DAY = 24 * MS_PER_HOUR;
|
|
13
|
+
const MS_PER_WEEK = 7 * MS_PER_DAY;
|
|
14
|
+
const MS_PER_MONTH = 30 * MS_PER_DAY; // Approximation
|
|
15
|
+
const MS_PER_YEAR = 365 * MS_PER_DAY; // Approximation
|
|
16
|
+
// Defines units and their thresholds (in ms) to switch to the next largest unit.
|
|
17
|
+
const TIME_THRESHOLDS = [
|
|
18
|
+
{ unit: 'second', ms: MS_PER_SECOND, threshold: MS_PER_MINUTE * 0.75 },
|
|
19
|
+
{ unit: 'minute', ms: MS_PER_MINUTE, threshold: MS_PER_HOUR * 0.9 },
|
|
20
|
+
{ unit: 'hour', ms: MS_PER_HOUR, threshold: MS_PER_DAY * 0.92 },
|
|
21
|
+
{ unit: 'day', ms: MS_PER_DAY, threshold: MS_PER_WEEK * 0.9 },
|
|
22
|
+
{ unit: 'week', ms: MS_PER_WEEK, threshold: MS_PER_MONTH * 0.8 },
|
|
23
|
+
{ unit: 'month', ms: MS_PER_MONTH, threshold: MS_PER_YEAR * 0.75 },
|
|
24
|
+
{ unit: 'year', ms: MS_PER_YEAR, threshold: Infinity }
|
|
25
|
+
];
|
|
26
|
+
function calculateTimeDifference(targetDate, referenceDate) {
|
|
27
|
+
const targetTime = targetDate instanceof Date ? targetDate.getTime() : targetDate;
|
|
28
|
+
const refTime = referenceDate instanceof Date ? referenceDate.getTime() : referenceDate;
|
|
29
|
+
const diffMs = targetTime - refTime;
|
|
30
|
+
const absDiffMs = Math.abs(diffMs);
|
|
31
|
+
for (const { unit, ms, threshold } of TIME_THRESHOLDS) {
|
|
32
|
+
if (absDiffMs < threshold) {
|
|
33
|
+
const value = Math.trunc(diffMs / ms);
|
|
34
|
+
if (unit === 'second' && Math.abs(value) < 1) {
|
|
35
|
+
return { value: 0, unit: 'second' };
|
|
36
|
+
}
|
|
37
|
+
return { value: value, unit: unit };
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return { value: Math.trunc(diffMs / MS_PER_YEAR), unit: 'year' };
|
|
41
|
+
}
|
|
42
|
+
// --- ACTIVEPIECES ACTION DEFINITION ---
|
|
43
|
+
exports.formatRelativeTimeAction = (0, pieces_framework_1.createAction)({
|
|
44
|
+
name: 'format_relative_time',
|
|
45
|
+
displayName: 'Format Relative Time (No DayJS)',
|
|
46
|
+
description: 'Converts a date into a localized "time ago" string using native JavaScript Intl APIs.',
|
|
47
|
+
props: {
|
|
48
|
+
dateToFormat: pieces_framework_1.Property.DateTime({
|
|
49
|
+
displayName: 'Target Date/Time',
|
|
50
|
+
description: 'The date/time to format (e.g., an ISO string from a previous step).',
|
|
51
|
+
required: true,
|
|
52
|
+
}),
|
|
53
|
+
referenceDate: pieces_framework_1.Property.DateTime({
|
|
54
|
+
displayName: 'Reference Date (Now)',
|
|
55
|
+
description: 'The date/time to calculate the difference from (defaults to current time if empty).',
|
|
56
|
+
required: false,
|
|
57
|
+
}),
|
|
58
|
+
locale: pieces_framework_1.Property.ShortText({
|
|
59
|
+
displayName: 'Locale',
|
|
60
|
+
description: 'The language code for output (e.g., "en", "fr", "es").',
|
|
61
|
+
required: false,
|
|
62
|
+
defaultValue: 'en',
|
|
63
|
+
}),
|
|
64
|
+
numericStyle: pieces_framework_1.Property.StaticDropdown({
|
|
65
|
+
displayName: 'Numeric Style',
|
|
66
|
+
description: 'Controls if words like "yesterday" or "tomorrow" are used.',
|
|
67
|
+
required: false,
|
|
68
|
+
defaultValue: 'auto',
|
|
69
|
+
options: {
|
|
70
|
+
options: [
|
|
71
|
+
{ label: 'Auto (e.g., "yesterday")', value: 'auto' },
|
|
72
|
+
{ label: 'Always (e.g., "1 day ago")', value: 'always' },
|
|
73
|
+
],
|
|
74
|
+
},
|
|
75
|
+
}),
|
|
76
|
+
unitStyle: pieces_framework_1.Property.StaticDropdown({
|
|
77
|
+
displayName: 'Unit Style',
|
|
78
|
+
description: 'Controls the length of the time unit.',
|
|
79
|
+
required: false,
|
|
80
|
+
defaultValue: 'long',
|
|
81
|
+
options: {
|
|
82
|
+
options: [
|
|
83
|
+
{ label: 'Long (e.g., "minutes")', value: 'long' },
|
|
84
|
+
{ label: 'Short (e.g., "min.")', value: 'short' },
|
|
85
|
+
{ label: 'Narrow (e.g., "m")', value: 'narrow' },
|
|
86
|
+
],
|
|
87
|
+
},
|
|
88
|
+
}),
|
|
89
|
+
},
|
|
90
|
+
run(context) {
|
|
91
|
+
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
92
|
+
const { dateToFormat, referenceDate, locale, numericStyle, unitStyle } = context.props;
|
|
93
|
+
// Ensure properties are treated as strings as they come from the context props
|
|
94
|
+
const targetDateString = dateToFormat;
|
|
95
|
+
const referenceDateString = referenceDate;
|
|
96
|
+
// Convert string inputs to Date objects
|
|
97
|
+
const targetDate = new Date(targetDateString);
|
|
98
|
+
const refDate = referenceDateString ? new Date(referenceDateString) : new Date();
|
|
99
|
+
// 1. Calculate the signed value and unit
|
|
100
|
+
const { value, unit } = calculateTimeDifference(targetDate, refDate);
|
|
101
|
+
// 2. Use the native Intl API for final formatting
|
|
102
|
+
const formatter = new Intl.RelativeTimeFormat(locale || 'en', {
|
|
103
|
+
numeric: numericStyle || 'auto',
|
|
104
|
+
style: unitStyle || 'long',
|
|
105
|
+
});
|
|
106
|
+
const relativeTimeString = formatter.format(value, unit);
|
|
107
|
+
// 3. Return the result object for use in downstream steps
|
|
108
|
+
return {
|
|
109
|
+
relative_time: relativeTimeString,
|
|
110
|
+
value: value,
|
|
111
|
+
unit: unit,
|
|
112
|
+
};
|
|
113
|
+
});
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
//# sourceMappingURL=format-relative-time-action.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"format-relative-time-action.js","sourceRoot":"","sources":["../../../../../../../../packages/pieces/community/relative-time-formatter/src/lib/actions/format-relative-time-action.ts"],"names":[],"mappings":";;;;AAAA,qEAAwE;AAExE;;GAEG;AAEH,MAAM,aAAa,GAAG,IAAI,CAAC;AAC3B,MAAM,aAAa,GAAG,EAAE,GAAG,aAAa,CAAC;AACzC,MAAM,WAAW,GAAG,EAAE,GAAG,aAAa,CAAC;AACvC,MAAM,UAAU,GAAG,EAAE,GAAG,WAAW,CAAC;AACpC,MAAM,WAAW,GAAG,CAAC,GAAG,UAAU,CAAC;AACnC,MAAM,YAAY,GAAG,EAAE,GAAG,UAAU,CAAC,CAAC,gBAAgB;AACtD,MAAM,WAAW,GAAG,GAAG,GAAG,UAAU,CAAC,CAAC,gBAAgB;AAEtD,iFAAiF;AACjF,MAAM,eAAe,GAAG;IACpB,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,EAAE,aAAa,EAAE,SAAS,EAAE,aAAa,GAAG,IAAI,EAAE;IACtE,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE,EAAE,aAAa,EAAE,SAAS,EAAE,WAAW,GAAG,GAAG,EAAE;IACnE,EAAE,IAAI,EAAE,MAAM,EAAE,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