@alextheman/utility 5.24.0 → 5.25.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.
@@ -0,0 +1,35 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ //#region src/temporal/formatRelativeDateAndTime.ts
3
+ /**
4
+ * Creates a human-readable string describing the input date relative to the reference date.
5
+ *
6
+ * If no reference date is provided, the current local date and time is used as the reference.
7
+ *
8
+ * @category Temporal
9
+ *
10
+ * @param input - The date and time to format.
11
+ * @param reference - The date and time to compare the input against.
12
+ *
13
+ * @returns A human-readable string describing the input date relative to the reference date.
14
+ *
15
+ * - If the date given is today, the output will be something like `Today at HH:MM`
16
+ * - If the date given happened yesterday, the output will be something like `Yesterday at HH:MM`
17
+ * - For any other date, the output will be something like `DD/MM/YYYY, HH:MM`
18
+ */
19
+ function formatRelativeDateAndTime(input, reference = Temporal.Now.plainDateTimeISO()) {
20
+ const inputPlainDate = input.toPlainDate();
21
+ const referencePlainDate = reference.toPlainDate();
22
+ const formattedTime = input.toLocaleString("en-GB", {
23
+ hour: "2-digit",
24
+ minute: "2-digit"
25
+ });
26
+ if (inputPlainDate.equals(referencePlainDate.subtract({ days: 1 }))) return `Yesterday at ${formattedTime}`;
27
+ if (inputPlainDate.equals(referencePlainDate)) return `Today at ${formattedTime}`;
28
+ return `${input.toLocaleString("en-GB", {
29
+ day: "2-digit",
30
+ month: "2-digit",
31
+ year: "numeric"
32
+ })}, ${formattedTime}`;
33
+ }
34
+ //#endregion
35
+ exports.formatRelativeDateAndTime = formatRelativeDateAndTime;
@@ -0,0 +1,20 @@
1
+ //#region src/temporal/formatRelativeDateAndTime.d.ts
2
+ /**
3
+ * Creates a human-readable string describing the input date relative to the reference date.
4
+ *
5
+ * If no reference date is provided, the current local date and time is used as the reference.
6
+ *
7
+ * @category Temporal
8
+ *
9
+ * @param input - The date and time to format.
10
+ * @param reference - The date and time to compare the input against.
11
+ *
12
+ * @returns A human-readable string describing the input date relative to the reference date.
13
+ *
14
+ * - If the date given is today, the output will be something like `Today at HH:MM`
15
+ * - If the date given happened yesterday, the output will be something like `Yesterday at HH:MM`
16
+ * - For any other date, the output will be something like `DD/MM/YYYY, HH:MM`
17
+ */
18
+ declare function formatRelativeDateAndTime(input: Temporal.PlainDateTime | Temporal.ZonedDateTime, reference?: Temporal.PlainDateTime | Temporal.ZonedDateTime): string;
19
+ //#endregion
20
+ export { formatRelativeDateAndTime };
@@ -0,0 +1,20 @@
1
+ //#region src/temporal/formatRelativeDateAndTime.d.ts
2
+ /**
3
+ * Creates a human-readable string describing the input date relative to the reference date.
4
+ *
5
+ * If no reference date is provided, the current local date and time is used as the reference.
6
+ *
7
+ * @category Temporal
8
+ *
9
+ * @param input - The date and time to format.
10
+ * @param reference - The date and time to compare the input against.
11
+ *
12
+ * @returns A human-readable string describing the input date relative to the reference date.
13
+ *
14
+ * - If the date given is today, the output will be something like `Today at HH:MM`
15
+ * - If the date given happened yesterday, the output will be something like `Yesterday at HH:MM`
16
+ * - For any other date, the output will be something like `DD/MM/YYYY, HH:MM`
17
+ */
18
+ declare function formatRelativeDateAndTime(input: Temporal.PlainDateTime | Temporal.ZonedDateTime, reference?: Temporal.PlainDateTime | Temporal.ZonedDateTime): string;
19
+ //#endregion
20
+ export { formatRelativeDateAndTime };
@@ -0,0 +1,34 @@
1
+ //#region src/temporal/formatRelativeDateAndTime.ts
2
+ /**
3
+ * Creates a human-readable string describing the input date relative to the reference date.
4
+ *
5
+ * If no reference date is provided, the current local date and time is used as the reference.
6
+ *
7
+ * @category Temporal
8
+ *
9
+ * @param input - The date and time to format.
10
+ * @param reference - The date and time to compare the input against.
11
+ *
12
+ * @returns A human-readable string describing the input date relative to the reference date.
13
+ *
14
+ * - If the date given is today, the output will be something like `Today at HH:MM`
15
+ * - If the date given happened yesterday, the output will be something like `Yesterday at HH:MM`
16
+ * - For any other date, the output will be something like `DD/MM/YYYY, HH:MM`
17
+ */
18
+ function formatRelativeDateAndTime(input, reference = Temporal.Now.plainDateTimeISO()) {
19
+ const inputPlainDate = input.toPlainDate();
20
+ const referencePlainDate = reference.toPlainDate();
21
+ const formattedTime = input.toLocaleString("en-GB", {
22
+ hour: "2-digit",
23
+ minute: "2-digit"
24
+ });
25
+ if (inputPlainDate.equals(referencePlainDate.subtract({ days: 1 }))) return `Yesterday at ${formattedTime}`;
26
+ if (inputPlainDate.equals(referencePlainDate)) return `Today at ${formattedTime}`;
27
+ return `${input.toLocaleString("en-GB", {
28
+ day: "2-digit",
29
+ month: "2-digit",
30
+ year: "numeric"
31
+ })}, ${formattedTime}`;
32
+ }
33
+ //#endregion
34
+ export { formatRelativeDateAndTime };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alextheman/utility",
3
- "version": "5.24.0",
3
+ "version": "5.25.0",
4
4
  "description": "Helpful utility functions.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -26,6 +26,11 @@
26
26
  "import": "./dist/node/index.js",
27
27
  "require": "./dist/node/index.cjs"
28
28
  },
29
+ "./temporal": {
30
+ "types": "./dist/temporal/index.d.ts",
31
+ "import": "./dist/temporal/index.js",
32
+ "require": "./dist/temporal/index.cjs"
33
+ },
29
34
  "./v6": {
30
35
  "types": "./dist/v6/index.d.ts",
31
36
  "import": "./dist/v6/index.js",