@aureus-core/utils 0.0.0-stage → 0.0.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/LICENSE +21 -0
- package/README.md +76 -2
- package/dist/date-formats/date-formats.d.ts +17 -0
- package/dist/date-formats/date-formats.js +62 -0
- package/dist/date-formats/index.d.ts +4 -0
- package/dist/date-formats/index.js +1 -0
- package/dist/date-formats/types/date-formats-types.d.ts +50 -0
- package/dist/date-formats/types/date-formats-types.js +0 -0
- package/dist/date-formats/types/index.d.ts +2 -0
- package/dist/date-formats/types/index.js +0 -0
- package/dist/date-formats/utils/constants/date-format-catalog.d.ts +105 -0
- package/dist/date-formats/utils/constants/date-format-catalog.js +89 -0
- package/dist/date-formats/utils/constants/date-tokens.d.ts +5 -0
- package/dist/date-formats/utils/constants/date-tokens.js +27 -0
- package/dist/date-formats/utils/constants/index.d.ts +3 -0
- package/dist/date-formats/utils/constants/index.js +3 -0
- package/dist/date-formats/utils/helpers/format-pattern.d.ts +28 -0
- package/dist/date-formats/utils/helpers/format-pattern.js +117 -0
- package/dist/date-formats/utils/helpers/index.d.ts +4 -0
- package/dist/date-formats/utils/helpers/index.js +4 -0
- package/dist/date-formats/utils/helpers/resolve-definition.d.ts +24 -0
- package/dist/date-formats/utils/helpers/resolve-definition.js +26 -0
- package/dist/date-formats/utils/helpers/to-date-value.d.ts +18 -0
- package/dist/date-formats/utils/helpers/to-date-value.js +59 -0
- package/dist/display/display.d.ts +13 -0
- package/dist/display/display.js +42 -0
- package/dist/display/formatters/display-boolean.d.ts +6 -0
- package/dist/display/formatters/display-boolean.js +23 -0
- package/dist/display/formatters/display-date.d.ts +7 -0
- package/dist/display/formatters/display-date.js +13 -0
- package/dist/display/formatters/display-list.d.ts +7 -0
- package/dist/display/formatters/display-list.js +37 -0
- package/dist/display/formatters/display-number.d.ts +7 -0
- package/dist/display/formatters/display-number.js +44 -0
- package/dist/display/formatters/display-text.d.ts +6 -0
- package/dist/display/formatters/display-text.js +28 -0
- package/dist/display/formatters/index.d.ts +6 -0
- package/dist/display/formatters/index.js +6 -0
- package/dist/display/index.d.ts +5 -0
- package/dist/display/index.js +1 -0
- package/dist/display/types/display-types.d.ts +104 -0
- package/dist/display/types/display-types.js +0 -0
- package/dist/display/types/index.d.ts +2 -0
- package/dist/display/types/index.js +0 -0
- package/dist/display/utils/helpers/index.d.ts +3 -0
- package/dist/display/utils/helpers/index.js +3 -0
- package/dist/display/utils/helpers/options.d.ts +12 -0
- package/dist/display/utils/helpers/options.js +27 -0
- package/dist/display/utils/helpers/truncate-graphemes.d.ts +8 -0
- package/dist/display/utils/helpers/truncate-graphemes.js +21 -0
- package/dist/internal/types/index.d.ts +2 -0
- package/dist/internal/types/index.js +0 -0
- package/dist/internal/types/locale-types.d.ts +8 -0
- package/dist/internal/types/locale-types.js +0 -0
- package/dist/internal/utils/constants/index.d.ts +2 -0
- package/dist/internal/utils/constants/index.js +1 -0
- package/dist/internal/utils/constants/is-dev.d.ts +5 -0
- package/dist/internal/utils/constants/is-dev.js +7 -0
- package/dist/internal/utils/helpers/index.d.ts +6 -0
- package/dist/internal/utils/helpers/index.js +6 -0
- package/dist/internal/utils/helpers/intl-cache.d.ts +14 -0
- package/dist/internal/utils/helpers/intl-cache.js +24 -0
- package/dist/internal/utils/helpers/is-nil.d.ts +2 -0
- package/dist/internal/utils/helpers/is-nil.js +6 -0
- package/dist/internal/utils/helpers/iso-date.d.ts +35 -0
- package/dist/internal/utils/helpers/iso-date.js +60 -0
- package/dist/internal/utils/helpers/resolve-locale.d.ts +6 -0
- package/dist/internal/utils/helpers/resolve-locale.js +11 -0
- package/dist/internal/utils/helpers/warn-once.d.ts +7 -0
- package/dist/internal/utils/helpers/warn-once.js +18 -0
- package/package.json +42 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Eddy Javier Cabrera Rodriguez
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,77 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @aureus-core/utils
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Framework-free utilities shared by every Aureus project. ESM only, no dependencies, no React: the code uses only standard JavaScript and `Intl`, so it runs in browsers and in Node.js 24+.
|
|
4
|
+
|
|
5
|
+
**Documentation:** <https://ing-eddy.github.io/aureus-frontend-core/> (section **utils**), in English and Spanish, with a live catalog and a playground.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
pnpm add @aureus-core/utils
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Modules
|
|
14
|
+
|
|
15
|
+
Each module has its own entry point. There is no root import.
|
|
16
|
+
|
|
17
|
+
| Module | Import path | What it does |
|
|
18
|
+
| -------------- | --------------------------------- | ------------------------------------------------------------------- |
|
|
19
|
+
| `date-formats` | `@aureus-core/utils/date-formats` | The single catalog of named date and time formats (`long`, `time`…) |
|
|
20
|
+
| `display` | `@aureus-core/utils/display` | One function per basic type that turns a value into the text shown |
|
|
21
|
+
|
|
22
|
+
## `display`
|
|
23
|
+
|
|
24
|
+
Create it once per app:
|
|
25
|
+
|
|
26
|
+
```ts
|
|
27
|
+
// src/lib/display.ts
|
|
28
|
+
import { createDisplay } from "@aureus-core/utils/display";
|
|
29
|
+
import i18n from "./i18n";
|
|
30
|
+
|
|
31
|
+
export const display = createDisplay({
|
|
32
|
+
empty: "-", // required: shown for null / undefined and for values that cannot be shown
|
|
33
|
+
locale: () => i18n.resolvedLanguage, // read on every call; omitted → the browser's language
|
|
34
|
+
// timeZone: "America/Mexico_City", // optional; omitted → the browser's time zone
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
display.text(user.name, { maxLength: 40, ellipsis: "…" });
|
|
40
|
+
display.number(invoice.total, { decimals: 2, padDecimals: true, grouping: true, currency: "EUR" });
|
|
41
|
+
display.boolean(user.active, { true: t("common.active"), false: t("common.inactive") });
|
|
42
|
+
display.date(order.date, { format: "long" }); // es: 21 de noviembre de 2026 · en: November 21, 2026
|
|
43
|
+
display.time("14:30"); // es: 14:30 · en: 2:30 PM
|
|
44
|
+
display.dateTime(log.at, { format: "shortDateTime" });
|
|
45
|
+
display.list(tags, { maxItems: 2, more: (n) => t("common.more", { count: n }) }); // a, b y 3 más
|
|
46
|
+
display.text(note, { empty: t("notes.none") }); // a per-call empty text
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Every function returns a `string`. Every option is opt-in and independent, and the library ships no texts of its own: `empty`, the boolean labels and `more` are required by TypeScript.
|
|
50
|
+
|
|
51
|
+
## `date-formats`
|
|
52
|
+
|
|
53
|
+
The catalog is defined in this package and is the same in every app. An app picks a name and never writes a format. Each name is written the way the active language writes it.
|
|
54
|
+
|
|
55
|
+
```ts
|
|
56
|
+
import { createDateFormatter } from "@aureus-core/utils/date-formats";
|
|
57
|
+
|
|
58
|
+
const dates = createDateFormatter({ locale: () => i18n.resolvedLanguage });
|
|
59
|
+
dates.format(new Date(2026, 10, 1), "monthYear"); // es: noviembre de 2026 · en: November 2026
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Names:
|
|
63
|
+
|
|
64
|
+
- **Dates:** `short`, `medium`, `long`, `full`, `numericDate`, `dayMonth`, `monthYear`, `weekday`, `month`, `year`.
|
|
65
|
+
- **Times:** `time`, `timeWithSeconds`.
|
|
66
|
+
- **Dates and times:** `shortDateTime`, `mediumDateTime`, `longDateTime`.
|
|
67
|
+
|
|
68
|
+
Values:
|
|
69
|
+
|
|
70
|
+
- **Local ISO strings** (`2026-11-21`, `14:30`, `2026-11-21T14:30`) are shown exactly as written, in any time zone.
|
|
71
|
+
- **Instants** (`Date`, timestamps, ISO with `Z` / an offset) are shown in `timeZone`, or in the browser's time zone.
|
|
72
|
+
|
|
73
|
+
To add or change a format, change the catalog in this package and release it.
|
|
74
|
+
|
|
75
|
+
## Development warnings
|
|
76
|
+
|
|
77
|
+
Invalid values and misuse (`NaN`, an unreadable date, an unknown format name in untyped code) log a `console.warn` once, outside production. The check reads `process.env.NODE_ENV`, which your bundler (Vite) replaces at build time, so production bundles contain no warnings.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { DateFormatName, DateFormatter, DateFormatterConfig } from "./types/date-formats-types.js";
|
|
2
|
+
/** Every name in the catalog, in catalog order. Frozen: the catalog is defined by the library. */
|
|
3
|
+
export declare const dateFormatNames: readonly DateFormatName[];
|
|
4
|
+
/**
|
|
5
|
+
* A formatter for the library's date-format catalog. `locale` may be a getter, evaluated on every
|
|
6
|
+
* call; omitted `locale` / `timeZone` mean the runtime's.
|
|
7
|
+
*
|
|
8
|
+
* An unknown name (untyped code) falls back to `short`, and an unreadable or unfit value gives
|
|
9
|
+
* `null`; both log a development warning. An invalid time zone or locale tag is not caught:
|
|
10
|
+
* `format` throws the `RangeError` of `Intl`.
|
|
11
|
+
*
|
|
12
|
+
* ```ts
|
|
13
|
+
* const dates = createDateFormatter({ locale: () => i18n.resolvedLanguage });
|
|
14
|
+
* dates.format("2026-11-21", "long"); // "21 de noviembre de 2026" · "November 21, 2026"
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
export declare function createDateFormatter(config?: DateFormatterConfig): DateFormatter;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { dateTimeFormat } from "../internal/utils/helpers/intl-cache.js";
|
|
2
|
+
import { isNil } from "../internal/utils/helpers/is-nil.js";
|
|
3
|
+
import { resolveLocale } from "../internal/utils/helpers/resolve-locale.js";
|
|
4
|
+
import { warnOnce } from "../internal/utils/helpers/warn-once.js";
|
|
5
|
+
import { dateFormatCatalog } from "./utils/constants/date-format-catalog.js";
|
|
6
|
+
import { formatPattern } from "./utils/helpers/format-pattern.js";
|
|
7
|
+
import { resolveDefinition } from "./utils/helpers/resolve-definition.js";
|
|
8
|
+
import { toDateValue } from "./utils/helpers/to-date-value.js";
|
|
9
|
+
// src/date-formats/date-formats.ts
|
|
10
|
+
/** The catalog widened to plain string keys, so a name from untyped code can be looked up. */
|
|
11
|
+
const catalog = dateFormatCatalog;
|
|
12
|
+
/** Every name in the catalog, in catalog order. Frozen: the catalog is defined by the library. */
|
|
13
|
+
const dateFormatNames = Object.freeze(Object.keys(dateFormatCatalog));
|
|
14
|
+
/** How a value is written in a warning: strings are quoted, so blank ones are visible. */
|
|
15
|
+
function describe(value) {
|
|
16
|
+
if (value instanceof Date) return "an invalid Date";
|
|
17
|
+
return typeof value === "string" ? JSON.stringify(value) : String(value);
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* A formatter for the library's date-format catalog. `locale` may be a getter, evaluated on every
|
|
21
|
+
* call; omitted `locale` / `timeZone` mean the runtime's.
|
|
22
|
+
*
|
|
23
|
+
* An unknown name (untyped code) falls back to `short`, and an unreadable or unfit value gives
|
|
24
|
+
* `null`; both log a development warning. An invalid time zone or locale tag is not caught:
|
|
25
|
+
* `format` throws the `RangeError` of `Intl`.
|
|
26
|
+
*
|
|
27
|
+
* ```ts
|
|
28
|
+
* const dates = createDateFormatter({ locale: () => i18n.resolvedLanguage });
|
|
29
|
+
* dates.format("2026-11-21", "long"); // "21 de noviembre de 2026" · "November 21, 2026"
|
|
30
|
+
* ```
|
|
31
|
+
*/
|
|
32
|
+
function createDateFormatter(config = {}) {
|
|
33
|
+
return { format(value, name, overrides) {
|
|
34
|
+
if (isNil(value)) return null;
|
|
35
|
+
let formatName = name;
|
|
36
|
+
if (!Object.hasOwn(catalog, formatName)) {
|
|
37
|
+
warnOnce(`date-formats:name:${formatName}`, `date-formats: unknown format "${formatName}"; "short" is used instead.`);
|
|
38
|
+
formatName = "short";
|
|
39
|
+
}
|
|
40
|
+
const definition = catalog[formatName];
|
|
41
|
+
const dateValue = toDateValue(value);
|
|
42
|
+
if (!dateValue) {
|
|
43
|
+
warnOnce(`date-formats:value:${describe(value)}`, `date-formats: ${describe(value)} is not a readable date.`);
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
const hasDate = dateValue.kind !== "time";
|
|
47
|
+
const hasTime = dateValue.kind !== "date";
|
|
48
|
+
const { needs } = definition;
|
|
49
|
+
if (needs !== "time" && !hasDate || needs !== "date" && !hasTime) {
|
|
50
|
+
warnOnce(`date-formats:fit:${formatName}:${describe(value)}`, `date-formats: format "${formatName}" needs a ${needs === "both" ? "date and a time" : needs}, but ${describe(value)} has only a ${hasDate ? "date" : "time"}.`);
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
const locale = resolveLocale(overrides?.locale ?? config.locale);
|
|
54
|
+
const timeZone = dateValue.kind === "instant" ? overrides?.timeZone ?? config.timeZone : "UTC";
|
|
55
|
+
const resolved = resolveDefinition(definition, locale);
|
|
56
|
+
return resolved.kind === "pattern" ? formatPattern(dateValue.date, resolved.pattern, locale, timeZone) : dateTimeFormat(locale, {
|
|
57
|
+
...resolved.options,
|
|
58
|
+
timeZone
|
|
59
|
+
}).format(dateValue.date);
|
|
60
|
+
} };
|
|
61
|
+
}
|
|
62
|
+
export { createDateFormatter, dateFormatNames };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { LocaleInput, LocaleTags } from "../internal/types/locale-types.js";
|
|
2
|
+
import { createDateFormatter, dateFormatNames } from "./date-formats.js";
|
|
3
|
+
import { CalendarFormatName, DateFormatName, DateFormatOverrides, DateFormatter, DateFormatterConfig, DateInput, DateTimeFormatName, TimeFormatName } from "./types/date-formats-types.js";
|
|
4
|
+
export { type CalendarFormatName, type DateFormatName, type DateFormatOverrides, type DateFormatter, type DateFormatterConfig, type DateInput, type DateTimeFormatName, type LocaleInput, type LocaleTags, type TimeFormatName, createDateFormatter, dateFormatNames };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { createDateFormatter, dateFormatNames } from "./date-formats.js";
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { LocaleInput } from "../../internal/types/locale-types.js";
|
|
2
|
+
import { dateFormatCatalog } from "../utils/constants/date-format-catalog.js";
|
|
3
|
+
/** What a format shows: calendar parts, clock parts, or both. */
|
|
4
|
+
export type FormatNeeds = "date" | "time" | "both";
|
|
5
|
+
/** How one catalog name is written. Internal: consumers pick names, never definitions. */
|
|
6
|
+
export interface DateFormatDefinition {
|
|
7
|
+
/** The parts the format shows. A value that lacks one of them is rejected. */
|
|
8
|
+
needs: FormatNeeds;
|
|
9
|
+
/** The locale's conventional form, used for every language without an exact form. */
|
|
10
|
+
intl: Intl.DateTimeFormatOptions;
|
|
11
|
+
/** Exact forms by language tag (`"es"`, `"es-MX"`), written with the internal pattern tokens. */
|
|
12
|
+
patterns?: Readonly<Record<string, string>>;
|
|
13
|
+
}
|
|
14
|
+
/** The catalog's own type, from which the name unions are derived. */
|
|
15
|
+
type Catalog = typeof dateFormatCatalog;
|
|
16
|
+
/** The catalog names whose `needs` is `N`. */
|
|
17
|
+
type NamesWith<N extends FormatNeeds> = { [K in keyof Catalog]: Catalog[K]["needs"] extends N ? K : never; }[keyof Catalog];
|
|
18
|
+
/** Every name in the catalog. */
|
|
19
|
+
export type DateFormatName = keyof Catalog;
|
|
20
|
+
/** Names that show a calendar date only: `short` … `year`. */
|
|
21
|
+
export type CalendarFormatName = NamesWith<"date">;
|
|
22
|
+
/** Names that show a time only: `time`, `timeWithSeconds`. */
|
|
23
|
+
export type TimeFormatName = NamesWith<"time">;
|
|
24
|
+
/** Names that show a date and a time: `shortDateTime`, `mediumDateTime`, `longDateTime`. */
|
|
25
|
+
export type DateTimeFormatName = NamesWith<"both">;
|
|
26
|
+
/**
|
|
27
|
+
* A value `format` can read: a `Date`, milliseconds since the epoch, or an ISO string. Local ISO
|
|
28
|
+
* strings (`YYYY-MM-DD`, `HH:mm[:ss]`, `YYYY-MM-DDTHH:mm[:ss]`) are wall times and are shown as
|
|
29
|
+
* written; a `Date`, a timestamp, or an ISO string with `Z` / `±HH:mm` is an instant.
|
|
30
|
+
*/
|
|
31
|
+
export type DateInput = Date | number | string;
|
|
32
|
+
/** Configuration of `createDateFormatter`. Both settings are optional. */
|
|
33
|
+
export interface DateFormatterConfig {
|
|
34
|
+
/** Tags or a getter evaluated on every call. Omitted → the runtime's default locale. */
|
|
35
|
+
locale?: LocaleInput;
|
|
36
|
+
/** IANA time zone for instants. Omitted → the runtime's time zone. */
|
|
37
|
+
timeZone?: string;
|
|
38
|
+
}
|
|
39
|
+
/** Per-call overrides of the formatter's config. */
|
|
40
|
+
export type DateFormatOverrides = DateFormatterConfig;
|
|
41
|
+
/** Formats dates and times with the names of the library's catalog. */
|
|
42
|
+
export interface DateFormatter {
|
|
43
|
+
/**
|
|
44
|
+
* The value written with a catalog format, or `null` when the value is `null`/`undefined`, is
|
|
45
|
+
* not a readable date, or does not fit the format (a date format on a time-only value, or the
|
|
46
|
+
* reverse). The last two log a development warning.
|
|
47
|
+
*/
|
|
48
|
+
format(value: DateInput | null | undefined, name: DateFormatName, overrides?: DateFormatOverrides): string | null;
|
|
49
|
+
}
|
|
50
|
+
export {};
|
|
File without changes
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { CalendarFormatName, DateFormatDefinition, DateFormatName, DateFormatOverrides, DateFormatter, DateFormatterConfig, DateInput, DateTimeFormatName, FormatNeeds, TimeFormatName } from "./date-formats-types.js";
|
|
2
|
+
export type { CalendarFormatName, DateFormatDefinition, DateFormatName, DateFormatOverrides, DateFormatter, DateFormatterConfig, DateInput, DateTimeFormatName, FormatNeeds, TimeFormatName };
|
|
File without changes
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The single definition of how every Aureus app shows dates and times. Apps pick a name; they
|
|
3
|
+
* never define a format. Each name uses the locale's conventions (`intl`), so it is right in any
|
|
4
|
+
* language; add `patterns` only when a language must look exactly one way, for example
|
|
5
|
+
* `patterns: { es: "dd/MM/yyyy" }`. Changing this file changes every app: add a changeset.
|
|
6
|
+
*/
|
|
7
|
+
export declare const dateFormatCatalog: {
|
|
8
|
+
readonly short: {
|
|
9
|
+
readonly needs: "date";
|
|
10
|
+
readonly intl: {
|
|
11
|
+
readonly dateStyle: "short";
|
|
12
|
+
};
|
|
13
|
+
};
|
|
14
|
+
readonly medium: {
|
|
15
|
+
readonly needs: "date";
|
|
16
|
+
readonly intl: {
|
|
17
|
+
readonly dateStyle: "medium";
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
readonly long: {
|
|
21
|
+
readonly needs: "date";
|
|
22
|
+
readonly intl: {
|
|
23
|
+
readonly dateStyle: "long";
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
readonly full: {
|
|
27
|
+
readonly needs: "date";
|
|
28
|
+
readonly intl: {
|
|
29
|
+
readonly dateStyle: "full";
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
readonly numericDate: {
|
|
33
|
+
readonly needs: "date";
|
|
34
|
+
readonly intl: {
|
|
35
|
+
readonly day: "2-digit";
|
|
36
|
+
readonly month: "2-digit";
|
|
37
|
+
readonly year: "numeric";
|
|
38
|
+
};
|
|
39
|
+
};
|
|
40
|
+
readonly dayMonth: {
|
|
41
|
+
readonly needs: "date";
|
|
42
|
+
readonly intl: {
|
|
43
|
+
readonly day: "numeric";
|
|
44
|
+
readonly month: "short";
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
readonly monthYear: {
|
|
48
|
+
readonly needs: "date";
|
|
49
|
+
readonly intl: {
|
|
50
|
+
readonly month: "long";
|
|
51
|
+
readonly year: "numeric";
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
readonly weekday: {
|
|
55
|
+
readonly needs: "date";
|
|
56
|
+
readonly intl: {
|
|
57
|
+
readonly weekday: "long";
|
|
58
|
+
};
|
|
59
|
+
};
|
|
60
|
+
readonly month: {
|
|
61
|
+
readonly needs: "date";
|
|
62
|
+
readonly intl: {
|
|
63
|
+
readonly month: "long";
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
readonly year: {
|
|
67
|
+
readonly needs: "date";
|
|
68
|
+
readonly intl: {
|
|
69
|
+
readonly year: "numeric";
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
readonly time: {
|
|
73
|
+
readonly needs: "time";
|
|
74
|
+
readonly intl: {
|
|
75
|
+
readonly timeStyle: "short";
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
readonly timeWithSeconds: {
|
|
79
|
+
readonly needs: "time";
|
|
80
|
+
readonly intl: {
|
|
81
|
+
readonly timeStyle: "medium";
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
readonly shortDateTime: {
|
|
85
|
+
readonly needs: "both";
|
|
86
|
+
readonly intl: {
|
|
87
|
+
readonly dateStyle: "short";
|
|
88
|
+
readonly timeStyle: "short";
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
readonly mediumDateTime: {
|
|
92
|
+
readonly needs: "both";
|
|
93
|
+
readonly intl: {
|
|
94
|
+
readonly dateStyle: "medium";
|
|
95
|
+
readonly timeStyle: "short";
|
|
96
|
+
};
|
|
97
|
+
};
|
|
98
|
+
readonly longDateTime: {
|
|
99
|
+
readonly needs: "both";
|
|
100
|
+
readonly intl: {
|
|
101
|
+
readonly dateStyle: "long";
|
|
102
|
+
readonly timeStyle: "short";
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
};
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// src/date-formats/utils/constants/date-format-catalog.ts
|
|
2
|
+
/**
|
|
3
|
+
* The single definition of how every Aureus app shows dates and times. Apps pick a name; they
|
|
4
|
+
* never define a format. Each name uses the locale's conventions (`intl`), so it is right in any
|
|
5
|
+
* language; add `patterns` only when a language must look exactly one way, for example
|
|
6
|
+
* `patterns: { es: "dd/MM/yyyy" }`. Changing this file changes every app: add a changeset.
|
|
7
|
+
*/
|
|
8
|
+
const dateFormatCatalog = {
|
|
9
|
+
short: {
|
|
10
|
+
needs: "date",
|
|
11
|
+
intl: { dateStyle: "short" }
|
|
12
|
+
},
|
|
13
|
+
medium: {
|
|
14
|
+
needs: "date",
|
|
15
|
+
intl: { dateStyle: "medium" }
|
|
16
|
+
},
|
|
17
|
+
long: {
|
|
18
|
+
needs: "date",
|
|
19
|
+
intl: { dateStyle: "long" }
|
|
20
|
+
},
|
|
21
|
+
full: {
|
|
22
|
+
needs: "date",
|
|
23
|
+
intl: { dateStyle: "full" }
|
|
24
|
+
},
|
|
25
|
+
numericDate: {
|
|
26
|
+
needs: "date",
|
|
27
|
+
intl: {
|
|
28
|
+
day: "2-digit",
|
|
29
|
+
month: "2-digit",
|
|
30
|
+
year: "numeric"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
dayMonth: {
|
|
34
|
+
needs: "date",
|
|
35
|
+
intl: {
|
|
36
|
+
day: "numeric",
|
|
37
|
+
month: "short"
|
|
38
|
+
}
|
|
39
|
+
},
|
|
40
|
+
monthYear: {
|
|
41
|
+
needs: "date",
|
|
42
|
+
intl: {
|
|
43
|
+
month: "long",
|
|
44
|
+
year: "numeric"
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
weekday: {
|
|
48
|
+
needs: "date",
|
|
49
|
+
intl: { weekday: "long" }
|
|
50
|
+
},
|
|
51
|
+
month: {
|
|
52
|
+
needs: "date",
|
|
53
|
+
intl: { month: "long" }
|
|
54
|
+
},
|
|
55
|
+
year: {
|
|
56
|
+
needs: "date",
|
|
57
|
+
intl: { year: "numeric" }
|
|
58
|
+
},
|
|
59
|
+
time: {
|
|
60
|
+
needs: "time",
|
|
61
|
+
intl: { timeStyle: "short" }
|
|
62
|
+
},
|
|
63
|
+
timeWithSeconds: {
|
|
64
|
+
needs: "time",
|
|
65
|
+
intl: { timeStyle: "medium" }
|
|
66
|
+
},
|
|
67
|
+
shortDateTime: {
|
|
68
|
+
needs: "both",
|
|
69
|
+
intl: {
|
|
70
|
+
dateStyle: "short",
|
|
71
|
+
timeStyle: "short"
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
mediumDateTime: {
|
|
75
|
+
needs: "both",
|
|
76
|
+
intl: {
|
|
77
|
+
dateStyle: "medium",
|
|
78
|
+
timeStyle: "short"
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
longDateTime: {
|
|
82
|
+
needs: "both",
|
|
83
|
+
intl: {
|
|
84
|
+
dateStyle: "long",
|
|
85
|
+
timeStyle: "short"
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
export { dateFormatCatalog };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// src/date-formats/utils/constants/date-tokens.ts
|
|
2
|
+
/**
|
|
3
|
+
* The tokens of the internal pattern formatter (the Unicode / date-fns letters). A run of the
|
|
4
|
+
* same letter is one token; any other unquoted letter run is written literally, with a warning.
|
|
5
|
+
*/
|
|
6
|
+
const dateTokens = /* @__PURE__ */ new Set([
|
|
7
|
+
"yyyy",
|
|
8
|
+
"yy",
|
|
9
|
+
"M",
|
|
10
|
+
"MM",
|
|
11
|
+
"MMM",
|
|
12
|
+
"MMMM",
|
|
13
|
+
"d",
|
|
14
|
+
"dd",
|
|
15
|
+
"EEE",
|
|
16
|
+
"EEEE",
|
|
17
|
+
"H",
|
|
18
|
+
"HH",
|
|
19
|
+
"h",
|
|
20
|
+
"hh",
|
|
21
|
+
"m",
|
|
22
|
+
"mm",
|
|
23
|
+
"s",
|
|
24
|
+
"ss",
|
|
25
|
+
"a"
|
|
26
|
+
]);
|
|
27
|
+
export { dateTokens };
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** One piece of a pattern: literal text to copy, or a field to fill from the date. */
|
|
2
|
+
type Token = {
|
|
3
|
+
/** Text copied to the output. */
|
|
4
|
+
type: "literal";
|
|
5
|
+
/** The text, with its quotes removed. */
|
|
6
|
+
text: string;
|
|
7
|
+
} | {
|
|
8
|
+
/** A part of the date. */
|
|
9
|
+
type: "field";
|
|
10
|
+
/** The pattern letters, one of `dateTokens` (`"dd"`, `"MMM"`…). */
|
|
11
|
+
token: string;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Splits a pattern into tokens: runs of one letter, and literal text, which may be `'quoted'` (`''`
|
|
15
|
+
* is a quote). A letter run that is not a token is written literally with a development warning,
|
|
16
|
+
* and an unterminated quote makes the rest of the pattern literal.
|
|
17
|
+
*/
|
|
18
|
+
export declare function tokenize(pattern: string): Token[];
|
|
19
|
+
/** A locale as `Intl` accepts it; `undefined` is the runtime's default. */
|
|
20
|
+
type Locale = string | string[] | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* Writes `date` with a pattern. Numbers are always ASCII digits of the Gregorian calendar; month
|
|
23
|
+
* and weekday names and the day period come from `locale`. Each name is formatted on its own, in
|
|
24
|
+
* the locale's default calendar, so a language that inflects names inside a date gets the
|
|
25
|
+
* standalone form. `timeZone` is the zone the parts are read in.
|
|
26
|
+
*/
|
|
27
|
+
export declare function formatPattern(date: Date, pattern: string, locale: Locale, timeZone: string | undefined): string;
|
|
28
|
+
export {};
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { dateTimeFormat } from "../../../internal/utils/helpers/intl-cache.js";
|
|
2
|
+
import { warnOnce } from "../../../internal/utils/helpers/warn-once.js";
|
|
3
|
+
import { dateTokens } from "../constants/date-tokens.js";
|
|
4
|
+
// src/date-formats/utils/helpers/format-pattern.ts
|
|
5
|
+
/**
|
|
6
|
+
* Splits a pattern into tokens: runs of one letter, and literal text, which may be `'quoted'` (`''`
|
|
7
|
+
* is a quote). A letter run that is not a token is written literally with a development warning,
|
|
8
|
+
* and an unterminated quote makes the rest of the pattern literal.
|
|
9
|
+
*/
|
|
10
|
+
function tokenize(pattern) {
|
|
11
|
+
const tokens = [];
|
|
12
|
+
const literal = (text) => {
|
|
13
|
+
const last = tokens.at(-1);
|
|
14
|
+
if (last?.type === "literal") last.text += text;
|
|
15
|
+
else tokens.push({
|
|
16
|
+
type: "literal",
|
|
17
|
+
text
|
|
18
|
+
});
|
|
19
|
+
};
|
|
20
|
+
let index = 0;
|
|
21
|
+
while (index < pattern.length) {
|
|
22
|
+
const char = pattern[index];
|
|
23
|
+
if (char === "'") {
|
|
24
|
+
if (pattern[index + 1] === "'") {
|
|
25
|
+
literal("'");
|
|
26
|
+
index += 2;
|
|
27
|
+
continue;
|
|
28
|
+
}
|
|
29
|
+
let end = index + 1;
|
|
30
|
+
let text = "";
|
|
31
|
+
while (end < pattern.length) if (pattern[end] === "'" && pattern[end + 1] === "'") {
|
|
32
|
+
text += "'";
|
|
33
|
+
end += 2;
|
|
34
|
+
} else if (pattern[end] === "'") break;
|
|
35
|
+
else text += pattern[end++];
|
|
36
|
+
literal(text);
|
|
37
|
+
index = end + 1;
|
|
38
|
+
} else if (/[A-Za-z]/.test(char)) {
|
|
39
|
+
let end = index;
|
|
40
|
+
while (pattern[end] === char) end++;
|
|
41
|
+
const run = pattern.slice(index, end);
|
|
42
|
+
if (dateTokens.has(run)) tokens.push({
|
|
43
|
+
type: "field",
|
|
44
|
+
token: run
|
|
45
|
+
});
|
|
46
|
+
else {
|
|
47
|
+
warnOnce(`date-formats:token:${run}`, `date-formats: "${run}" is not a pattern token; it is written literally. Quote literal text ('…').`);
|
|
48
|
+
literal(run);
|
|
49
|
+
}
|
|
50
|
+
index = end;
|
|
51
|
+
} else {
|
|
52
|
+
literal(char);
|
|
53
|
+
index++;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return tokens;
|
|
57
|
+
}
|
|
58
|
+
/** `value` with leading zeros up to `length` digits. */
|
|
59
|
+
const pad = (value, length) => String(value).padStart(length, "0");
|
|
60
|
+
/** The text of one part of a formatted date, or `""` when the formatter has no such part. */
|
|
61
|
+
function partOf(formatter, date, type) {
|
|
62
|
+
return formatter.formatToParts(date).find((part) => part.type === type)?.value ?? "";
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Writes `date` with a pattern. Numbers are always ASCII digits of the Gregorian calendar; month
|
|
66
|
+
* and weekday names and the day period come from `locale`. Each name is formatted on its own, in
|
|
67
|
+
* the locale's default calendar, so a language that inflects names inside a date gets the
|
|
68
|
+
* standalone form. `timeZone` is the zone the parts are read in.
|
|
69
|
+
*/
|
|
70
|
+
function formatPattern(date, pattern, locale, timeZone) {
|
|
71
|
+
const { year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0 } = dateTimeFormat("en-US", {
|
|
72
|
+
timeZone,
|
|
73
|
+
year: "numeric",
|
|
74
|
+
month: "numeric",
|
|
75
|
+
day: "numeric",
|
|
76
|
+
hour: "numeric",
|
|
77
|
+
minute: "numeric",
|
|
78
|
+
second: "numeric",
|
|
79
|
+
hourCycle: "h23"
|
|
80
|
+
}).formatToParts(date).reduce((parts, part) => {
|
|
81
|
+
if (part.type !== "literal") parts[part.type] = Number(part.value);
|
|
82
|
+
return parts;
|
|
83
|
+
}, {});
|
|
84
|
+
const hour12 = hour % 12 || 12;
|
|
85
|
+
const name = (options, type) => partOf(dateTimeFormat(locale, {
|
|
86
|
+
timeZone,
|
|
87
|
+
...options
|
|
88
|
+
}), date, type);
|
|
89
|
+
return tokenize(pattern).map((token) => {
|
|
90
|
+
if (token.type === "literal") return token.text;
|
|
91
|
+
switch (token.token) {
|
|
92
|
+
case "yyyy": return pad(year, 4);
|
|
93
|
+
case "yy": return pad(year % 100, 2);
|
|
94
|
+
case "M": return String(month);
|
|
95
|
+
case "MM": return pad(month, 2);
|
|
96
|
+
case "MMM": return name({ month: "short" }, "month");
|
|
97
|
+
case "MMMM": return name({ month: "long" }, "month");
|
|
98
|
+
case "d": return String(day);
|
|
99
|
+
case "dd": return pad(day, 2);
|
|
100
|
+
case "EEE": return name({ weekday: "short" }, "weekday");
|
|
101
|
+
case "EEEE": return name({ weekday: "long" }, "weekday");
|
|
102
|
+
case "H": return String(hour);
|
|
103
|
+
case "HH": return pad(hour, 2);
|
|
104
|
+
case "h": return String(hour12);
|
|
105
|
+
case "hh": return pad(hour12, 2);
|
|
106
|
+
case "m": return String(minute);
|
|
107
|
+
case "mm": return pad(minute, 2);
|
|
108
|
+
case "s": return String(second);
|
|
109
|
+
case "ss": return pad(second, 2);
|
|
110
|
+
default: return name({
|
|
111
|
+
hour: "numeric",
|
|
112
|
+
hour12: true
|
|
113
|
+
}, "dayPeriod");
|
|
114
|
+
}
|
|
115
|
+
}).join("");
|
|
116
|
+
}
|
|
117
|
+
export { formatPattern, tokenize };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { formatPattern, tokenize } from "./format-pattern.js";
|
|
2
|
+
import { ResolvedDefinition, resolveDefinition, resolvedTag } from "./resolve-definition.js";
|
|
3
|
+
import { DateValue, toDateValue } from "./to-date-value.js";
|
|
4
|
+
export { type DateValue, type ResolvedDefinition, formatPattern, resolveDefinition, resolvedTag, toDateValue, tokenize };
|