@baukit/localization-core 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Patrick Koss
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 ADDED
@@ -0,0 +1,39 @@
1
+ # @baukit/localization-core
2
+
3
+ `@baukit/localization-core` provides dependency-free locale resolution, catalog key comparison,
4
+ stable-code localization, and explicit-locale `Intl` formatter factories. Products supply the
5
+ supported locale list, fallback locale, translation catalogs, and copy.
6
+
7
+ The package does not include translations, product catalog IDs, persistence, React providers,
8
+ Expo Localization, or a localization library. It also does not choose plural or unit policy for
9
+ an application.
10
+
11
+ i18next and React composition stays in the consuming app. Resolve and hydrate the locale before
12
+ rendering localized UI, then pass that resolved locale to this package's formatter factories.
13
+
14
+ ## Civil dates
15
+
16
+ A civil date is a `YYYY-MM-DD` calendar day with no time and no offset. Diary entries, plan days,
17
+ and reminder dates are civil dates, and computing one with `Date.getDate()` or an ISO string slice
18
+ gets the wrong answer for any user whose zone disagrees with the host.
19
+
20
+ ```ts
21
+ import { addCivilDays, civilDateForInstant, civilToday } from '@baukit/localization-core';
22
+
23
+ civilToday('Pacific/Auckland'); // '2026-08-25'
24
+ civilDateForInstant('2026-08-25T06:00:00Z', 'America/Los_Angeles'); // '2026-08-24'
25
+ addCivilDays('2026-03-07', 1); // '2026-03-08', DST does not shift a calendar day
26
+ ```
27
+
28
+ - `parseCivilDate(date)` returns `{ ok: true, value }` or `{ ok: false, code: 'invalid_civil_date' }`.
29
+ `isCivilDate`, `civilDateValidationCode`, and `assertCivilDate` are the boolean, code, and
30
+ throwing forms. All of them reject impossible days such as `2026-02-30`.
31
+ - `assertCivilTime(time | null)` accepts `HH:MM` and `HH:MM:SS`.
32
+ - `addCivilDays(date, days)`, `civilDaysBetween(from, to)`, and `compareCivilDates(left, right)`
33
+ work on the calendar, so a DST transition never adds or drops a day.
34
+ - `civilDateForInstant(instant, timeZone)` accepts a `Date`, an ISO string, or epoch
35
+ milliseconds. `civilToday(timeZone)` is the same call against now, and
36
+ `isInstantOnCivilDate(instant, date, timeZone)` answers whether a timestamp belongs to a local
37
+ day.
38
+ - `assertTimeZone(zone)` validates an IANA zone. `resolvedTimeZone()` reads the host zone; every
39
+ other function takes the zone explicitly, so nothing silently depends on where the process runs.
@@ -0,0 +1,7 @@
1
+ export interface CatalogDifference {
2
+ readonly missing: readonly string[];
3
+ readonly extra: readonly string[];
4
+ }
5
+ export declare function catalogKeySet(catalog: unknown): readonly string[];
6
+ export declare function compareCatalogKeys(reference: unknown, candidate: unknown): CatalogDifference;
7
+ //# sourceMappingURL=catalog.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"catalog.d.ts","sourceRoot":"","sources":["../src/catalog.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,iBAAiB;IAChC,QAAQ,CAAC,OAAO,EAAE,SAAS,MAAM,EAAE,CAAC;IACpC,QAAQ,CAAC,KAAK,EAAE,SAAS,MAAM,EAAE,CAAC;CACnC;AAyDD,wBAAgB,aAAa,CAAC,OAAO,EAAE,OAAO,GAAG,SAAS,MAAM,EAAE,CAEjE;AAED,wBAAgB,kBAAkB,CAAC,SAAS,EAAE,OAAO,EAAE,SAAS,EAAE,OAAO,GAAG,iBAAiB,CAQ5F"}
@@ -0,0 +1,57 @@
1
+ function escapeKeySegment(segment) {
2
+ return segment.replaceAll('\\', '\\\\').replaceAll('.', '\\.');
3
+ }
4
+ function enumerableProperties(value) {
5
+ try {
6
+ return Object.entries(Object.getOwnPropertyDescriptors(value))
7
+ .filter(([, descriptor]) => descriptor.enumerable)
8
+ .map(([key, descriptor]) => [key, 'value' in descriptor ? descriptor.value : undefined]);
9
+ }
10
+ catch {
11
+ return null;
12
+ }
13
+ }
14
+ function plainRecordProperties(value) {
15
+ if (value === null || typeof value !== 'object' || Array.isArray(value)) {
16
+ return null;
17
+ }
18
+ try {
19
+ const prototype = Object.getPrototypeOf(value);
20
+ if (prototype !== Object.prototype && prototype !== null) {
21
+ return null;
22
+ }
23
+ }
24
+ catch {
25
+ return null;
26
+ }
27
+ return enumerableProperties(value);
28
+ }
29
+ function collectCatalogKeys(value, prefix, ancestors) {
30
+ const properties = plainRecordProperties(value);
31
+ if (properties === null || properties.length === 0) {
32
+ return prefix === '' ? [] : [prefix];
33
+ }
34
+ if (typeof value !== 'object' || value === null || ancestors.has(value)) {
35
+ return prefix === '' ? [] : [prefix];
36
+ }
37
+ ancestors.add(value);
38
+ const keys = properties.flatMap(([key, child]) => {
39
+ const escapedKey = escapeKeySegment(key);
40
+ const path = prefix === '' ? escapedKey : `${prefix}.${escapedKey}`;
41
+ return collectCatalogKeys(child, path, ancestors);
42
+ });
43
+ ancestors.delete(value);
44
+ return keys;
45
+ }
46
+ export function catalogKeySet(catalog) {
47
+ return [...collectCatalogKeys(catalog, '', new WeakSet())].sort();
48
+ }
49
+ export function compareCatalogKeys(reference, candidate) {
50
+ const referenceKeys = new Set(catalogKeySet(reference));
51
+ const candidateKeys = new Set(catalogKeySet(candidate));
52
+ return {
53
+ missing: [...referenceKeys].filter((key) => !candidateKeys.has(key)).sort(),
54
+ extra: [...candidateKeys].filter((key) => !referenceKeys.has(key)).sort(),
55
+ };
56
+ }
57
+ //# sourceMappingURL=catalog.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"catalog.js","sourceRoot":"","sources":["../src/catalog.ts"],"names":[],"mappings":"AAKA,SAAS,gBAAgB,CAAC,OAAe;IACvC,OAAO,OAAO,CAAC,UAAU,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,UAAU,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;AACjE,CAAC;AAED,SAAS,oBAAoB,CAAC,KAAa;IACzC,IAAI,CAAC;QACH,OAAO,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,yBAAyB,CAAC,KAAK,CAAC,CAAC;aAC3D,MAAM,CAAC,CAAC,CAAC,EAAE,UAAU,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,UAAU,CAAC;aACjD,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,UAAU,CAAC,EAAE,EAAE,CAAC,CAAC,GAAG,EAAE,OAAO,IAAI,UAAU,CAAC,CAAC,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC;IAC7F,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC;AAED,SAAS,qBAAqB,CAAC,KAAc;IAC3C,IAAI,KAAK,KAAK,IAAI,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QACxE,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IAAI,CAAC;QACH,MAAM,SAAS,GAAG,MAAM,CAAC,cAAc,CAAC,KAAK,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OAAO,IAAI,CAAC;AACd,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,OAAgB;IAC5C,OAAO,CAAC,GAAG,kBAAkB,CAAC,OAAO,EAAE,EAAE,EAAE,IAAI,OAAO,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC;AACpE,CAAC;AAED,MAAM,UAAU,kBAAkB,CAAC,SAAkB,EAAE,SAAkB;IACvE,MAAM,aAAa,GAAG,IAAI,GAAG,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,CAAC;IACxD,MAAM,aAAa,GAAG,IAAI,GAAG,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,CAAC;IAExD,OAAO;QACL,OAAO,EAAE,CAAC,GAAG,aAAa,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE;QAC3E,KAAK,EAAE,CAAC,GAAG,aAAa,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,aAAa,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE;KAC1E,CAAC;AACJ,CAAC"}
@@ -0,0 +1,23 @@
1
+ export declare const INVALID_CIVIL_DATE_CODE = "invalid_civil_date";
2
+ export type CivilDateValidationCode = typeof INVALID_CIVIL_DATE_CODE;
3
+ export type CivilDateParseResult = {
4
+ readonly ok: true;
5
+ readonly value: string;
6
+ } | {
7
+ readonly ok: false;
8
+ readonly code: CivilDateValidationCode;
9
+ };
10
+ export declare function parseCivilDate(civilDate: string): CivilDateParseResult;
11
+ export declare function isCivilDate(civilDate: string): boolean;
12
+ export declare function civilDateValidationCode(civilDate: string): CivilDateValidationCode | null;
13
+ export declare function assertCivilDate(civilDate: string): void;
14
+ export declare function assertCivilTime(civilTime: string | null): void;
15
+ export declare function assertTimeZone(timeZone: string): void;
16
+ export declare function addCivilDays(civilDate: string, days: number): string;
17
+ export declare function civilDateForInstant(instant: Date | string | number, timeZone: string): string;
18
+ export declare function civilToday(timeZone: string): string;
19
+ export declare function resolvedTimeZone(): string;
20
+ export declare function isInstantOnCivilDate(instant: Date | string | number, civilDate: string, timeZone: string): boolean;
21
+ export declare function compareCivilDates(left: string, right: string): number;
22
+ export declare function civilDaysBetween(from: string, to: string): number;
23
+ //# sourceMappingURL=civil-date.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"civil-date.d.ts","sourceRoot":"","sources":["../src/civil-date.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,uBAAuB,uBAAuB,CAAC;AAC5D,MAAM,MAAM,uBAAuB,GAAG,OAAO,uBAAuB,CAAC;AAErE,MAAM,MAAM,oBAAoB,GAC5B;IAAE,QAAQ,CAAC,EAAE,EAAE,IAAI,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAA;CAAE,GAC7C;IAAE,QAAQ,CAAC,EAAE,EAAE,KAAK,CAAC;IAAC,QAAQ,CAAC,IAAI,EAAE,uBAAuB,CAAA;CAAE,CAAC;AAEnE,wBAAgB,cAAc,CAAC,SAAS,EAAE,MAAM,GAAG,oBAAoB,CAgBtE;AAED,wBAAgB,WAAW,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAEtD;AAED,wBAAgB,uBAAuB,CAAC,SAAS,EAAE,MAAM,GAAG,uBAAuB,GAAG,IAAI,CAEzF;AAED,wBAAgB,eAAe,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,CAIvD;AAED,wBAAgB,eAAe,CAAC,SAAS,EAAE,MAAM,GAAG,IAAI,GAAG,IAAI,CAI9D;AAED,wBAAgB,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAUrD;AAED,wBAAgB,YAAY,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAYpE;AAED,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAiB7F;AAED,wBAAgB,UAAU,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAEnD;AAED,wBAAgB,gBAAgB,IAAI,MAAM,CAEzC;AAED,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,IAAI,GAAG,MAAM,GAAG,MAAM,EAC/B,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,MAAM,GACf,OAAO,CAGT;AAED,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAOrE;AAED,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,GAAG,MAAM,CAOjE"}
@@ -0,0 +1,96 @@
1
+ const CIVIL_DATE_PATTERN = /^(\d{4})-(\d{2})-(\d{2})$/;
2
+ const CIVIL_TIME_PATTERN = /^(?:[01]\d|2[0-3]):[0-5]\d(?::[0-5]\d)?$/;
3
+ export const INVALID_CIVIL_DATE_CODE = 'invalid_civil_date';
4
+ export function parseCivilDate(civilDate) {
5
+ const match = CIVIL_DATE_PATTERN.exec(civilDate);
6
+ if (match === null) {
7
+ return { ok: false, code: INVALID_CIVIL_DATE_CODE };
8
+ }
9
+ const year = Number(match[1]);
10
+ const month = Number(match[2]);
11
+ const day = Number(match[3]);
12
+ const candidate = new Date(Date.UTC(year, month - 1, day));
13
+ const roundTrips = candidate.getUTCFullYear() === year &&
14
+ candidate.getUTCMonth() === month - 1 &&
15
+ candidate.getUTCDate() === day;
16
+ return roundTrips ? { ok: true, value: civilDate } : { ok: false, code: INVALID_CIVIL_DATE_CODE };
17
+ }
18
+ export function isCivilDate(civilDate) {
19
+ return parseCivilDate(civilDate).ok;
20
+ }
21
+ export function civilDateValidationCode(civilDate) {
22
+ return parseCivilDate(civilDate).ok ? null : INVALID_CIVIL_DATE_CODE;
23
+ }
24
+ export function assertCivilDate(civilDate) {
25
+ if (!parseCivilDate(civilDate).ok) {
26
+ throw new RangeError(`Invalid civil date: ${civilDate}`);
27
+ }
28
+ }
29
+ export function assertCivilTime(civilTime) {
30
+ if (civilTime !== null && !CIVIL_TIME_PATTERN.test(civilTime)) {
31
+ throw new RangeError(`Invalid civil time: ${civilTime}`);
32
+ }
33
+ }
34
+ export function assertTimeZone(timeZone) {
35
+ if (timeZone.trim().length === 0) {
36
+ throw new RangeError('Time zone must be non-empty');
37
+ }
38
+ try {
39
+ new Intl.DateTimeFormat('en-US', { timeZone }).format(new Date(0));
40
+ }
41
+ catch {
42
+ throw new RangeError(`Invalid IANA time zone: ${timeZone}`);
43
+ }
44
+ }
45
+ export function addCivilDays(civilDate, days) {
46
+ assertCivilDate(civilDate);
47
+ if (!Number.isInteger(days)) {
48
+ throw new RangeError('Days must be an integer');
49
+ }
50
+ const [year, month, day] = civilDate.split('-').map(Number);
51
+ const shifted = new Date(Date.UTC(year, month - 1, day + days));
52
+ if (Number.isNaN(shifted.getTime())) {
53
+ throw new RangeError(`Civil date out of range: ${civilDate} + ${String(days)} days`);
54
+ }
55
+ return shifted.toISOString().slice(0, 10);
56
+ }
57
+ export function civilDateForInstant(instant, timeZone) {
58
+ assertTimeZone(timeZone);
59
+ const date = instant instanceof Date ? instant : new Date(instant);
60
+ if (Number.isNaN(date.getTime())) {
61
+ throw new RangeError(`Invalid instant: ${String(instant)}`);
62
+ }
63
+ const parts = new Intl.DateTimeFormat('en-US', {
64
+ timeZone,
65
+ year: 'numeric',
66
+ month: '2-digit',
67
+ day: '2-digit',
68
+ }).formatToParts(date);
69
+ const part = (type) => parts.find((candidate) => candidate.type === type)?.value ?? '';
70
+ return `${part('year')}-${part('month')}-${part('day')}`;
71
+ }
72
+ export function civilToday(timeZone) {
73
+ return civilDateForInstant(new Date(), timeZone);
74
+ }
75
+ export function resolvedTimeZone() {
76
+ return Intl.DateTimeFormat().resolvedOptions().timeZone;
77
+ }
78
+ export function isInstantOnCivilDate(instant, civilDate, timeZone) {
79
+ assertCivilDate(civilDate);
80
+ return civilDateForInstant(instant, timeZone) === civilDate;
81
+ }
82
+ export function compareCivilDates(left, right) {
83
+ assertCivilDate(left);
84
+ assertCivilDate(right);
85
+ if (left === right) {
86
+ return 0;
87
+ }
88
+ return left < right ? -1 : 1;
89
+ }
90
+ export function civilDaysBetween(from, to) {
91
+ assertCivilDate(from);
92
+ assertCivilDate(to);
93
+ const millisecondsPerDay = 86_400_000;
94
+ return Math.round((Date.parse(`${to}T00:00:00Z`) - Date.parse(`${from}T00:00:00Z`)) / millisecondsPerDay);
95
+ }
96
+ //# sourceMappingURL=civil-date.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"civil-date.js","sourceRoot":"","sources":["../src/civil-date.ts"],"names":[],"mappings":"AAAA,MAAM,kBAAkB,GAAG,2BAA2B,CAAC;AACvD,MAAM,kBAAkB,GAAG,0CAA0C,CAAC;AAEtE,MAAM,CAAC,MAAM,uBAAuB,GAAG,oBAAoB,CAAC;AAO5D,MAAM,UAAU,cAAc,CAAC,SAAiB;IAC9C,MAAM,KAAK,GAAG,kBAAkB,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;IACjD,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QACnB,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,uBAAuB,EAAE,CAAC;IACtD,CAAC;IAED,MAAM,IAAI,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IAC9B,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IAC/B,MAAM,GAAG,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;IAC7B,MAAM,SAAS,GAAG,IAAI,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,KAAK,GAAG,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC;IAC3D,MAAM,UAAU,GACd,SAAS,CAAC,cAAc,E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,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAC/B,CAAC;AAED,MAAM,UAAU,gBAAgB,CAAC,IAAY,EAAE,EAAU;IACvD,eAAe,CAAC,IAAI,CAAC,CAAC;IACtB,eAAe,CAAC,EAAE,CAAC,CAAC;IACpB,MAAM,kBAAkB,GAAG,UAAU,CAAC;IACtC,OAAO,IAAI,CAAC,KAAK,CACf,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,YAAY,CAAC,GAAG,IAAI,CAAC,KAAK,CAAC,GAAG,IAAI,YAAY,CAAC,CAAC,GAAG,kBAAkB,CACvF,CAAC;AACJ,CAAC"}
@@ -0,0 +1,7 @@
1
+ export type LocalizedCodeEntry = string | ((details?: unknown) => string | null | undefined);
2
+ export interface CodeResolverOptions<TCode extends string> {
3
+ readonly catalog: Readonly<Partial<Record<TCode, LocalizedCodeEntry>>>;
4
+ readonly fallback: string | ((code: TCode, details?: unknown) => string);
5
+ }
6
+ export declare function createLocalizedCodeResolver<TCode extends string>(options: CodeResolverOptions<TCode>): (code: TCode, details?: unknown) => string;
7
+ //# sourceMappingURL=codes.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codes.d.ts","sourceRoot":"","sources":["../src/codes.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,kBAAkB,GAAG,MAAM,GAAG,CAAC,CAAC,OAAO,CAAC,EAAE,OAAO,KAAK,MAAM,GAAG,IAAI,GAAG,SAAS,CAAC,CAAC;AAE7F,MAAM,WAAW,mBAAmB,CAAC,KAAK,SAAS,MAAM;IACvD,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,KAAK,EAAE,kBAAkB,CAAC,CAAC,CAAC,CAAC;IACvE,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,CAAC,CAAC,IAAI,EAAE,KAAK,EAAE,OAAO,CAAC,EAAE,OAAO,KAAK,MAAM,CAAC,CAAC;CAC1E;AAaD,wBAAgB,2BAA2B,CAAC,KAAK,SAAS,MAAM,EAC9D,OAAO,EAAE,mBAAmB,CAAC,KAAK,CAAC,GAClC,CAAC,IAAI,EAAE,KAAK,EAAE,OAAO,CAAC,EAAE,OAAO,KAAK,MAAM,CAgB5C"}
package/dist/codes.js ADDED
@@ -0,0 +1,26 @@
1
+ function catalogEntry(catalog, code) {
2
+ try {
3
+ return Object.hasOwn(catalog, code) ? catalog[code] : undefined;
4
+ }
5
+ catch {
6
+ return undefined;
7
+ }
8
+ }
9
+ export function createLocalizedCodeResolver(options) {
10
+ return (code, details) => {
11
+ const entry = catalogEntry(options.catalog, code);
12
+ try {
13
+ const localized = typeof entry === 'function' ? entry(details) : entry;
14
+ if (typeof localized === 'string' && localized.trim().length > 0) {
15
+ return localized;
16
+ }
17
+ }
18
+ catch {
19
+ // The safe API message is used when structured details cannot be rendered.
20
+ }
21
+ return typeof options.fallback === 'function'
22
+ ? options.fallback(code, details)
23
+ : options.fallback;
24
+ };
25
+ }
26
+ //# sourceMappingURL=codes.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codes.js","sourceRoot":"","sources":["../src/codes.ts"],"names":[],"mappings":"AAOA,SAAS,YAAY,CACnB,OAA6D,EAC7D,IAAW;IAEX,IAAI,CAAC;QACH,OAAO,MAAM,CAAC,MAAM,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAClE,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC;AAED,MAAM,UAAU,2BAA2B,CACzC,OAAmC;IAEnC,OAAO,CAAC,IAAI,EAAE,OAAO,EAAE,EAAE;QACvB,MAAM,KAAK,GAAG,YAAY,CAAC,OAAO,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;QAClD,IAAI,CAAC;YACH,MAAM,SAAS,GAAG,OAAO,KAAK,KAAK,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;YACvE,IAAI,OAAO,SAAS,KAAK,QAAQ,IAAI,SAAS,CAAC,IAAI,EAAE,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBACjE,OAAO,SAAS,CAAC;YACnB,CAAC;QACH,CAAC;QAAC,MAAM,CAAC;YACP,2EAA2E;QAC7E,CAAC;QAED,OAAO,OAAO,OAAO,CAAC,QAAQ,KAAK,UAAU;YAC3C,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,IAAI,EAAE,OAAO,CAAC;YACjC,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC;IACvB,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,3 @@
1
+ export declare function createNumberFormatter(locale: string, options?: Intl.NumberFormatOptions): Intl.NumberFormat;
2
+ export declare function createDateTimeFormatter(locale: string, options?: Intl.DateTimeFormatOptions): Intl.DateTimeFormat;
3
+ //# sourceMappingURL=formatters.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"formatters.d.ts","sourceRoot":"","sources":["../src/formatters.ts"],"names":[],"mappings":"AAoBA,wBAAgB,qBAAqB,CACnC,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,IAAI,CAAC,mBAAmB,GACjC,IAAI,CAAC,YAAY,CAEnB;AAED,wBAAgB,uBAAuB,CACrC,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,IAAI,CAAC,qBAAqB,GACnC,IAAI,CAAC,cAAc,CAErB"}
@@ -0,0 +1,22 @@
1
+ import { canonicalizeResolvedLocale } from './locale.js';
2
+ function supportedNumberLocale(locale) {
3
+ const canonical = canonicalizeResolvedLocale(locale);
4
+ if (Intl.NumberFormat.supportedLocalesOf([canonical], { localeMatcher: 'lookup' }).length === 0) {
5
+ throw new RangeError(`number formatting does not support locale ${canonical}`);
6
+ }
7
+ return canonical;
8
+ }
9
+ function supportedDateTimeLocale(locale) {
10
+ const canonical = canonicalizeResolvedLocale(locale);
11
+ if (Intl.DateTimeFormat.supportedLocalesOf([canonical], { localeMatcher: 'lookup' }).length === 0) {
12
+ throw new RangeError(`date and time formatting does not support locale ${canonical}`);
13
+ }
14
+ return canonical;
15
+ }
16
+ export function createNumberFormatter(locale, options) {
17
+ return new Intl.NumberFormat(supportedNumberLocale(locale), options);
18
+ }
19
+ export function createDateTimeFormatter(locale, options) {
20
+ return new Intl.DateTimeFormat(supportedDateTimeLocale(locale), options);
21
+ }
22
+ //# sourceMappingURL=formatters.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"formatters.js","sourceRoot":"","sources":["../src/formatters.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,MAAM,aAAa,CAAC;AAEzD,SAAS,qBAAqB,CAAC,MAAc;IAC3C,MAAM,SAAS,GAAG,0BAA0B,CAAC,MAAM,CAAC,CAAC;IACrD,IAAI,IAAI,CAAC,YAAY,CAAC,kBAAkB,CAAC,CAAC,SAAS,CAAC,EAAE,EAAE,aAAa,EAAE,QAAQ,EAAE,CAAC,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;QAChG,MAAM,IAAI,UAAU,CAAC,6CAA6C,SAAS,EAAE,CAAC,CAAC;IACjF,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,SAAS,uBAAuB,CAAC,MAAc;IAC7C,MAAM,SAAS,GAAG,0BAA0B,CAAC,MAAM,CAAC,CAAC;IACrD,IACE,IAAI,CAAC,cAAc,CAAC,kBAAkB,CAAC,CAAC,SAAS,CAAC,EAAE,EAAE,aAAa,EAAE,QAAQ,EAAE,CAAC,CAAC,MAAM,KAAK,CAAC,EAC7F,CAAC;QACD,MAAM,IAAI,UAAU,CAAC,oDAAoD,SAAS,EAAE,CAAC,CAAC;IACxF,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC;AAED,MAAM,UAAU,qBAAqB,CACnC,MAAc,EACd,OAAkC;IAElC,OAAO,IAAI,IAAI,CAAC,YAAY,CAAC,qBAAqB,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,CAAC;AACvE,CAAC;AAED,MAAM,UAAU,uBAAuB,CACrC,MAAc,EACd,OAAoC;IAEpC,OAAO,IAAI,IAAI,CAAC,cAAc,CAAC,uBAAuB,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,CAAC;AAC3E,CAAC"}
@@ -0,0 +1,6 @@
1
+ export { catalogKeySet, compareCatalogKeys, type CatalogDifference } from './catalog.js';
2
+ export { addCivilDays, assertCivilDate, assertCivilTime, assertTimeZone, civilDateForInstant, civilDateValidationCode, civilDaysBetween, civilToday, compareCivilDates, INVALID_CIVIL_DATE_CODE, isCivilDate, isInstantOnCivilDate, parseCivilDate, resolvedTimeZone, type CivilDateParseResult, type CivilDateValidationCode, } from './civil-date.js';
3
+ export { createLocalizedCodeResolver, type CodeResolverOptions, type LocalizedCodeEntry, } from './codes.js';
4
+ export { createDateTimeFormatter, createNumberFormatter } from './formatters.js';
5
+ export { normalizeLocalePreference, resolveLocale, type LocalePreference } from './locale.js';
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,KAAK,iBAAiB,EAAE,MAAM,cAAc,CAAC;AACzF,OAAO,EACL,YAAY,EACZ,eAAe,EACf,eAAe,EACf,cAAc,EACd,mBAAmB,EACnB,uBAAuB,EACvB,gBAAgB,EAChB,UAAU,EACV,iBAAiB,EACjB,uBAAuB,EACvB,WAAW,EACX,oBAAoB,EACpB,cAAc,EACd,gBAAgB,EAChB,KAAK,oBAAoB,EACzB,KAAK,uBAAuB,GAC7B,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,2BAA2B,EAC3B,KAAK,mBAAmB,EACxB,KAAK,kBAAkB,GACxB,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,uBAAuB,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AACjF,OAAO,EAAE,yBAAyB,EAAE,aAAa,EAAE,KAAK,gBAAgB,EAAE,MAAM,aAAa,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ export { catalogKeySet, compareCatalogKeys } from './catalog.js';
2
+ export { addCivilDays, assertCivilDate, assertCivilTime, assertTimeZone, civilDateForInstant, civilDateValidationCode, civilDaysBetween, civilToday, compareCivilDates, INVALID_CIVIL_DATE_CODE, isCivilDate, isInstantOnCivilDate, parseCivilDate, resolvedTimeZone, } from './civil-date.js';
3
+ export { createLocalizedCodeResolver, } from './codes.js';
4
+ export { createDateTimeFormatter, createNumberFormatter } from './formatters.js';
5
+ export { normalizeLocalePreference, resolveLocale } from './locale.js';
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAA0B,MAAM,cAAc,CAAC;AACzF,OAAO,EACL,YAAY,EACZ,eAAe,EACf,eAAe,EACf,cAAc,EACd,mBAAmB,EACnB,uBAAuB,EACvB,gBAAgB,EAChB,UAAU,EACV,iBAAiB,EACjB,uBAAuB,EACvB,WAAW,EACX,oBAAoB,EACpB,cAAc,EACd,gBAAgB,GAGjB,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,2BAA2B,GAG5B,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,uBAAuB,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AACjF,OAAO,EAAE,yBAAyB,EAAE,aAAa,EAAyB,MAAM,aAAa,CAAC"}
@@ -0,0 +1,14 @@
1
+ export type LocalePreference<TLocale extends string> = 'system' | TLocale;
2
+ export declare function normalizeLocalePreference<TLocale extends string>(options: {
3
+ readonly value: unknown;
4
+ readonly supported: readonly TLocale[];
5
+ readonly fallback: TLocale;
6
+ }): LocalePreference<TLocale>;
7
+ export declare function resolveLocale<TLocale extends string>(options: {
8
+ readonly preference: unknown;
9
+ readonly deviceLocales: readonly string[];
10
+ readonly supported: readonly TLocale[];
11
+ readonly fallback: TLocale;
12
+ }): TLocale;
13
+ export declare function canonicalizeResolvedLocale(locale: string): string;
14
+ //# sourceMappingURL=locale.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"locale.d.ts","sourceRoot":"","sources":["../src/locale.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,gBAAgB,CAAC,OAAO,SAAS,MAAM,IAAI,QAAQ,GAAG,OAAO,CAAC;AA8E1E,wBAAgB,yBAAyB,CAAC,OAAO,SAAS,MAAM,EAAE,OAAO,EAAE;IACzE,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;IACxB,QAAQ,CAAC,SAAS,EAAE,SAAS,OAAO,EAAE,CAAC;IACvC,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;CAC5B,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAG5B;AAED,wBAAgB,aAAa,CAAC,OAAO,SAAS,MAAM,EAAE,OAAO,EAAE;IAC7D,QAAQ,CAAC,UAAU,EAAE,OAAO,CAAC;IAC7B,QAAQ,CAAC,aAAa,EAAE,SAAS,MAAM,EAAE,CAAC;IAC1C,QAAQ,CAAC,SAAS,EAAE,SAAS,OAAO,EAAE,CAAC;IACvC,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;CAC5B,GAAG,OAAO,CAsCV;AAED,wBAAgB,0BAA0B,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAOjE"}
package/dist/locale.js ADDED
@@ -0,0 +1,89 @@
1
+ const MAX_LOCALE_TAG_LENGTH = 128;
2
+ function canonicalLocale(value, allowSurroundingWhitespace) {
3
+ if (typeof value !== 'string' || value.length === 0 || value.length > MAX_LOCALE_TAG_LENGTH) {
4
+ return null;
5
+ }
6
+ const trimmed = value.trim();
7
+ if (trimmed.length === 0 ||
8
+ trimmed.length > MAX_LOCALE_TAG_LENGTH ||
9
+ (!allowSurroundingWhitespace && trimmed !== value)) {
10
+ return null;
11
+ }
12
+ try {
13
+ return Intl.getCanonicalLocales(trimmed.replaceAll('_', '-'))[0] ?? null;
14
+ }
15
+ catch {
16
+ return null;
17
+ }
18
+ }
19
+ function localeConfiguration(supported, fallback) {
20
+ const normalized = [];
21
+ for (const locale of supported) {
22
+ const canonical = canonicalLocale(locale, false);
23
+ if (canonical !== null && canonical.toLowerCase() !== 'system') {
24
+ normalized.push({ canonical, locale });
25
+ }
26
+ }
27
+ const canonicalFallback = canonicalLocale(fallback, false);
28
+ const supportedFallback = normalized.find(({ canonical }) => canonical === canonicalFallback);
29
+ if (supportedFallback === undefined) {
30
+ throw new RangeError('fallback must be a valid member of supported locales');
31
+ }
32
+ return { fallback: supportedFallback.locale, supported: normalized };
33
+ }
34
+ function exactSupportedLocale(canonical, supported) {
35
+ return supported.find((entry) => entry.canonical === canonical)?.locale;
36
+ }
37
+ function normalizedPreference(value, configuration) {
38
+ if (value === 'system') {
39
+ return 'system';
40
+ }
41
+ const canonical = canonicalLocale(value, false);
42
+ return canonical === null
43
+ ? configuration.fallback
44
+ : (exactSupportedLocale(canonical, configuration.supported) ?? configuration.fallback);
45
+ }
46
+ export function normalizeLocalePreference(options) {
47
+ const configuration = localeConfiguration(options.supported, options.fallback);
48
+ return normalizedPreference(options.value, configuration);
49
+ }
50
+ export function resolveLocale(options) {
51
+ const configuration = localeConfiguration(options.supported, options.fallback);
52
+ const preference = normalizedPreference(options.preference, configuration);
53
+ if (preference !== 'system') {
54
+ return preference;
55
+ }
56
+ const deviceLocales = options.deviceLocales
57
+ .map((locale) => canonicalLocale(locale, true))
58
+ .filter((locale) => locale !== null);
59
+ for (const deviceLocale of deviceLocales) {
60
+ const exact = exactSupportedLocale(deviceLocale, configuration.supported);
61
+ if (exact !== undefined) {
62
+ return exact;
63
+ }
64
+ }
65
+ for (const deviceLocale of deviceLocales) {
66
+ const base = deviceLocale.split('-', 1)[0];
67
+ if (base === undefined) {
68
+ continue;
69
+ }
70
+ const exactBase = exactSupportedLocale(base, configuration.supported);
71
+ if (exactBase !== undefined) {
72
+ return exactBase;
73
+ }
74
+ const regionalBase = configuration.supported.find(({ canonical }) => canonical.split('-', 1)[0] === base);
75
+ if (regionalBase !== undefined) {
76
+ return regionalBase.locale;
77
+ }
78
+ }
79
+ return configuration.fallback;
80
+ }
81
+ export function canonicalizeResolvedLocale(locale) {
82
+ const canonical = canonicalLocale(locale, false);
83
+ const base = canonical?.split('-', 1)[0]?.toLowerCase();
84
+ if (canonical === null || base === 'system' || base === 'und') {
85
+ throw new RangeError('locale must be an explicit, valid locale tag');
86
+ }
87
+ return canonical;
88
+ }
89
+ //# sourceMappingURL=locale.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"locale.js","sourceRoot":"","sources":["../src/locale.ts"],"names":[],"mappings":"AAEA,MAAM,qBAAqB,GAAG,GAAG,CAAC;AAYlC,SAAS,eAAe,CAAC,KAAc,EAAE,0BAAmC;IAC1E,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,qBAAqB,EAAE,CAAC;QAC5F,OAAO,IAAI,CAAC;IACd,CAAC;IAED,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC;IAC7B,IACE,OAAO,CAAC,MAAM,KAAK,CAAC;QACpB,OAAO,CAAC,MAAM,GAAG,qBAAqB;QACtC,CAAC,CAAC,0BAA0B,IAAI,OAAO,KAAK,KAAK,CAAC,EAClD,CAAC;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAED,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,mBAAmB,CAAC,OAAO,CAAC,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,IAAI,CAAC;IAC3E,CAAC;I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C,CAAC,CAAC,CAAC,CAAC,CAAC;QAC3C,IAAI,IAAI,KAAK,SAAS,EAAE,CAAC;YACvB,SAAS;QACX,CAAC;QAED,MAAM,SAAS,GAAG,oBAAoB,CAAC,IAAI,EAAE,aAAa,CAAC,SAAS,CAAC,CAAC;QACtE,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;YAC5B,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,MAAM,YAAY,GAAG,aAAa,CAAC,SAAS,CAAC,IAAI,CAC/C,CAAC,EAAE,SAAS,EAAE,EAAE,EAAE,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,KAAK,IAAI,CACvD,CAAC;QACF,IAAI,YAAY,KAAK,SAAS,EAAE,CAAC;YAC/B,OAAO,YAAY,CAAC,MAAM,CAAC;QAC7B,CAAC;IACH,CAAC;IAED,OAAO,aAAa,CAAC,QAAQ,CAAC;AAChC,CAAC;AAED,MAAM,UAAU,0BAA0B,CAAC,MAAc;IACvD,MAAM,SAAS,GAAG,eAAe,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;IACjD,MAAM,IAAI,GAAG,SAAS,EAAE,KAAK,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,CAAC;IACxD,IAAI,SAAS,KAAK,IAAI,IAAI,IAAI,KAAK,QAAQ,IAAI,IAAI,KAAK,KAAK,EAAE,CAAC;QAC9D,MAAM,IAAI,UAAU,CAAC,8CAA8C,CAAC,CAAC;IACvE,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@baukit/localization-core",
3
+ "version": "0.1.0",
4
+ "description": "Dependency-free locale resolution, catalog key comparison, stable-code localization, and explicit-locale Intl formatter factories.",
5
+ "license": "MIT",
6
+ "author": "Patrick Koss",
7
+ "homepage": "https://github.com/PatrickKoss/baukit#readme",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/PatrickKoss/baukit.git",
11
+ "directory": "typescript/packages/localization-core"
12
+ },
13
+ "bugs": {
14
+ "url": "https://github.com/PatrickKoss/baukit/issues"
15
+ },
16
+ "publishConfig": {
17
+ "access": "public"
18
+ },
19
+ "type": "module",
20
+ "engines": {
21
+ "node": ">=24.0.0"
22
+ },
23
+ "types": "./dist/index.d.ts",
24
+ "exports": {
25
+ ".": {
26
+ "types": "./dist/index.d.ts",
27
+ "import": "./dist/index.js"
28
+ }
29
+ },
30
+ "files": [
31
+ "dist",
32
+ "README.md",
33
+ "LICENSE"
34
+ ],
35
+ "devDependencies": {
36
+ "typescript": "6.0.3",
37
+ "vitest": "4.1.10"
38
+ },
39
+ "scripts": {
40
+ "build": "tsc -p tsconfig.json",
41
+ "test": "vitest run --config ../../vitest.config.ts",
42
+ "lint": "eslint src --max-warnings=0",
43
+ "format:check": "prettier --check src package.json tsconfig.json"
44
+ }
45
+ }