@sondos_rajeh/piece-relative-time-formatter 0.0.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/.eslintrc.json +33 -0
- package/README.md +7 -0
- package/dist/packages/pieces/community/framework/packages/pieces/community/framework/README.md +11 -0
- package/dist/packages/pieces/community/relative-time-formatter/packages/pieces/community/relative-time-formatter/README.md +7 -0
- package/dist/packages/shared/packages/shared/README.md +7 -0
- package/package.json +14 -0
- package/project.json +51 -0
- package/src/index.ts +14 -0
- package/src/lib/actions/format-relative-time-action.ts +132 -0
- package/tsconfig.json +20 -0
- package/tsconfig.lib.json +9 -0
package/.eslintrc.json
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": [
|
|
3
|
+
"../../../../.eslintrc.base.json"
|
|
4
|
+
],
|
|
5
|
+
"ignorePatterns": [
|
|
6
|
+
"!**/*"
|
|
7
|
+
],
|
|
8
|
+
"overrides": [
|
|
9
|
+
{
|
|
10
|
+
"files": [
|
|
11
|
+
"*.ts",
|
|
12
|
+
"*.tsx",
|
|
13
|
+
"*.js",
|
|
14
|
+
"*.jsx"
|
|
15
|
+
],
|
|
16
|
+
"rules": {}
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"files": [
|
|
20
|
+
"*.ts",
|
|
21
|
+
"*.tsx"
|
|
22
|
+
],
|
|
23
|
+
"rules": {}
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
"files": [
|
|
27
|
+
"*.js",
|
|
28
|
+
"*.jsx"
|
|
29
|
+
],
|
|
30
|
+
"rules": {}
|
|
31
|
+
}
|
|
32
|
+
]
|
|
33
|
+
}
|
package/README.md
ADDED
package/dist/packages/pieces/community/framework/packages/pieces/community/framework/README.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# pieces-framework
|
|
2
|
+
|
|
3
|
+
This library was generated with [Nx](https://nx.dev).
|
|
4
|
+
|
|
5
|
+
## Building
|
|
6
|
+
|
|
7
|
+
Run `nx build pieces-framework` to build the library.
|
|
8
|
+
|
|
9
|
+
## Running unit tests
|
|
10
|
+
|
|
11
|
+
Run `nx test pieces-framework` to execute the unit tests via [Jest](https://jestjs.io).
|
package/package.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sondos_rajeh/piece-relative-time-formatter",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "commonjs",
|
|
5
|
+
"main": "./src/index.js",
|
|
6
|
+
"types": "./src/index.d.ts",
|
|
7
|
+
"dependencies": {
|
|
8
|
+
"tslib": "^2.3.0"
|
|
9
|
+
},
|
|
10
|
+
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public"
|
|
13
|
+
}
|
|
14
|
+
}
|
package/project.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sondos_rajeh/piece-relative-time-formatter",
|
|
3
|
+
"$schema": "../../../../node_modules/nx/schemas/project-schema.json",
|
|
4
|
+
"sourceRoot": "packages/pieces/community/relative-time-formatter/src",
|
|
5
|
+
"projectType": "library",
|
|
6
|
+
"release": {
|
|
7
|
+
"version": {
|
|
8
|
+
"manifestRootsToUpdate": [
|
|
9
|
+
"dist/{projectRoot}"
|
|
10
|
+
],
|
|
11
|
+
"currentVersionResolver": "git-tag",
|
|
12
|
+
"fallbackCurrentVersionResolver": "disk"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"tags": [],
|
|
16
|
+
"targets": {
|
|
17
|
+
"build": {
|
|
18
|
+
"executor": "@nx/js:tsc",
|
|
19
|
+
"outputs": [
|
|
20
|
+
"{options.outputPath}"
|
|
21
|
+
],
|
|
22
|
+
"options": {
|
|
23
|
+
"outputPath": "dist/packages/pieces/community/relative-time-formatter",
|
|
24
|
+
"tsConfig": "packages/pieces/community/relative-time-formatter/tsconfig.lib.json",
|
|
25
|
+
"packageJson": "packages/pieces/community/relative-time-formatter/package.json",
|
|
26
|
+
"main": "packages/pieces/community/relative-time-formatter/src/index.ts",
|
|
27
|
+
"assets": [
|
|
28
|
+
"packages/pieces/community/relative-time-formatter/*.md",
|
|
29
|
+
{
|
|
30
|
+
"input": "packages/pieces/community/relative-time-formatter/src/i18n",
|
|
31
|
+
"output": "./src/i18n",
|
|
32
|
+
"glob": "**/!(i18n.json)"
|
|
33
|
+
}
|
|
34
|
+
],
|
|
35
|
+
"buildableProjectDepsInPackageJsonType": "dependencies",
|
|
36
|
+
"updateBuildableProjectDepsInPackageJson": true
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
"nx-release-publish": {
|
|
40
|
+
"options": {
|
|
41
|
+
"packageRoot": "dist/{projectRoot}"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"lint": {
|
|
45
|
+
"executor": "@nx/eslint:lint",
|
|
46
|
+
"outputs": [
|
|
47
|
+
"{options.outputFile}"
|
|
48
|
+
]
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
|
|
2
|
+
import { createPiece, PieceAuth } from "@activepieces/pieces-framework";
|
|
3
|
+
import { formatRelativeTimeAction } from "./lib/actions/format-relative-time-action";
|
|
4
|
+
|
|
5
|
+
export const relativeTimeFormatter = createPiece({
|
|
6
|
+
displayName: "Relative-time-formatter",
|
|
7
|
+
auth: PieceAuth.None(),
|
|
8
|
+
minimumSupportedRelease: '0.36.1',
|
|
9
|
+
logoUrl: "https://cdn.activepieces.com/pieces/relative-time-formatter.png",
|
|
10
|
+
authors: [],
|
|
11
|
+
actions: [formatRelativeTimeAction],
|
|
12
|
+
triggers: [],
|
|
13
|
+
});
|
|
14
|
+
|
|
@@ -0,0 +1,132 @@
|
|
|
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
|
+
// --- ACTIVEPIECES ACTION DEFINITION ---
|
|
49
|
+
|
|
50
|
+
export const formatRelativeTimeAction = createAction({
|
|
51
|
+
name: 'format_relative_time',
|
|
52
|
+
displayName: 'Format Relative Time (No DayJS)',
|
|
53
|
+
description: 'Converts a date into a localized "time ago" string using native JavaScript Intl APIs.',
|
|
54
|
+
|
|
55
|
+
props: {
|
|
56
|
+
dateToFormat: Property.DateTime({
|
|
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
|
+
}),
|
|
101
|
+
},
|
|
102
|
+
|
|
103
|
+
async run(context: any) { // FIX: Use 'context.props' and cast context to 'any'
|
|
104
|
+
const { dateToFormat, referenceDate, locale, numericStyle, unitStyle } = context.props;
|
|
105
|
+
|
|
106
|
+
// Ensure properties are treated as strings as they come from the context props
|
|
107
|
+
const targetDateString = dateToFormat as string;
|
|
108
|
+
const referenceDateString = referenceDate as string | undefined;
|
|
109
|
+
|
|
110
|
+
// Convert string inputs to Date objects
|
|
111
|
+
const targetDate = new Date(targetDateString);
|
|
112
|
+
const refDate = referenceDateString ? new Date(referenceDateString) : new Date();
|
|
113
|
+
|
|
114
|
+
// 1. Calculate the signed value and unit
|
|
115
|
+
const { value, unit } = calculateTimeDifference(targetDate, refDate);
|
|
116
|
+
|
|
117
|
+
// 2. Use the native Intl API for final formatting
|
|
118
|
+
const formatter = new Intl.RelativeTimeFormat(locale || 'en', {
|
|
119
|
+
numeric: numericStyle || 'auto',
|
|
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
|
|
126
|
+
return {
|
|
127
|
+
relative_time: relativeTimeString,
|
|
128
|
+
value: value,
|
|
129
|
+
unit: unit,
|
|
130
|
+
};
|
|
131
|
+
},
|
|
132
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "../../../../tsconfig.base.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
"forceConsistentCasingInFileNames": true,
|
|
6
|
+
"strict": true,
|
|
7
|
+
"importHelpers": true,
|
|
8
|
+
"noImplicitOverride": true,
|
|
9
|
+
"noImplicitReturns": true,
|
|
10
|
+
"noFallthroughCasesInSwitch": true,
|
|
11
|
+
"noPropertyAccessFromIndexSignature": true
|
|
12
|
+
},
|
|
13
|
+
"files": [],
|
|
14
|
+
"include": [],
|
|
15
|
+
"references": [
|
|
16
|
+
{
|
|
17
|
+
"path": "./tsconfig.lib.json"
|
|
18
|
+
}
|
|
19
|
+
]
|
|
20
|
+
}
|