@htilabs/platform-common-models 0.39.0 → 0.41.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/README.md +86 -0
- package/dist/age/index.cjs +136 -0
- package/dist/age/index.cjs.map +1 -0
- package/dist/age/index.d.cts +136 -0
- package/dist/age/index.d.ts +136 -0
- package/dist/age/index.js +105 -0
- package/dist/age/index.js.map +1 -0
- package/dist/index.cjs +159 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +150 -0
- package/dist/index.js.map +1 -1
- package/dist/share-rules/index.cjs +822 -0
- package/dist/share-rules/index.cjs.map +1 -0
- package/dist/share-rules/index.d.cts +804 -0
- package/dist/share-rules/index.d.ts +804 -0
- package/dist/share-rules/index.js +736 -0
- package/dist/share-rules/index.js.map +1 -0
- package/dist/sharing-ceiling/index.cjs +51 -0
- package/dist/sharing-ceiling/index.cjs.map +1 -1
- package/dist/sharing-ceiling/index.d.cts +87 -1
- package/dist/sharing-ceiling/index.d.ts +87 -1
- package/dist/sharing-ceiling/index.js +47 -0
- package/dist/sharing-ceiling/index.js.map +1 -1
- package/package.json +12 -1
- package/src/age/__tests__/ageFromDob.test.ts +192 -0
- package/src/age/ageFromDob.ts +269 -0
- package/src/age/index.ts +8 -0
- package/src/index.ts +4 -0
- package/src/shareRules/__tests__/caseShareLane.test.ts +259 -0
- package/src/shareRules/__tests__/magicLinkAddresses.test.ts +107 -0
- package/src/shareRules/__tests__/platformApps.test.ts +133 -0
- package/src/shareRules/__tests__/releaseTerms.test.ts +358 -0
- package/src/shareRules/__tests__/senderRoiPosture.test.ts +284 -0
- package/src/shareRules/__tests__/sharingMethods.test.ts +105 -0
- package/src/shareRules/caseShareLane.ts +289 -0
- package/src/shareRules/index.ts +31 -0
- package/src/shareRules/magicLinkAddresses.ts +185 -0
- package/src/shareRules/platformApps.ts +168 -0
- package/src/shareRules/releaseTerms.ts +359 -0
- package/src/shareRules/reportPurposes.ts +50 -0
- package/src/shareRules/senderRoiPosture.ts +296 -0
- package/src/shareRules/sharingMethods.ts +204 -0
- package/src/sharingCeiling/__tests__/threadLift.test.ts +353 -0
- package/src/sharingCeiling/coverage.ts +63 -0
- package/src/sharingCeiling/defaults.ts +106 -0
- package/src/sharingCeiling/index.ts +7 -0
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
ageFromDob,
|
|
5
|
+
ageInputFromStoredDob,
|
|
6
|
+
calendarDateOf,
|
|
7
|
+
normalizeDob,
|
|
8
|
+
} from "../ageFromDob.js";
|
|
9
|
+
|
|
10
|
+
/** Local calendar day — the rule reads `today`'s local year / month / day. */
|
|
11
|
+
const day = (y: number, m: number, d: number): Date => new Date(y, m - 1, d, 12);
|
|
12
|
+
|
|
13
|
+
const TODAY = day(2026, 10, 24);
|
|
14
|
+
|
|
15
|
+
describe("ageFromDob — the card's case", () => {
|
|
16
|
+
it("birth year 2008 with no month is 17 on 2026-10-24 (counted younger), not 18", () => {
|
|
17
|
+
expect(ageFromDob({ y: 2008 }, TODAY)).toBe(17);
|
|
18
|
+
expect(ageFromDob({ y: "2008", m: "", d: "" }, TODAY)).toBe(17);
|
|
19
|
+
expect(ageFromDob("//2008", TODAY)).toBe(17);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe("ageFromDob — the partial-date rule", () => {
|
|
24
|
+
it("month unknown: always counts younger, even late in the year", () => {
|
|
25
|
+
expect(ageFromDob({ y: 2000 }, day(2026, 12, 31))).toBe(25);
|
|
26
|
+
expect(ageFromDob({ y: 2000, d: 1 }, day(2026, 12, 31))).toBe(25);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("birth month already past: birthday has happened", () => {
|
|
30
|
+
expect(ageFromDob({ y: 2008, m: 9 }, TODAY)).toBe(18);
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("birth month still ahead: birthday has not happened", () => {
|
|
34
|
+
expect(ageFromDob({ y: 2008, m: 11 }, TODAY)).toBe(17);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("birth month is this month and the day is unknown: counts younger", () => {
|
|
38
|
+
expect(ageFromDob({ y: 2008, m: 10 }, TODAY)).toBe(17);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("birth month is this month and the day is known", () => {
|
|
42
|
+
expect(ageFromDob({ y: 2008, m: 10, d: 23 }, TODAY)).toBe(18);
|
|
43
|
+
expect(ageFromDob({ y: 2008, m: 10, d: 24 }, TODAY)).toBe(18);
|
|
44
|
+
expect(ageFromDob({ y: 2008, m: 10, d: 25 }, TODAY)).toBe(17);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("a full date of birth gives exact whole years", () => {
|
|
48
|
+
expect(ageFromDob({ y: 1987, m: 5, d: 21 }, TODAY)).toBe(39);
|
|
49
|
+
expect(ageFromDob({ y: 2014, m: 1, d: 1 }, day(2026, 12, 31))).toBe(12);
|
|
50
|
+
expect(ageFromDob({ y: 2014, m: 1, d: 1 }, day(2027, 1, 1))).toBe(13);
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it("born on 29 February", () => {
|
|
54
|
+
expect(ageFromDob({ y: 2008, m: 2, d: 29 }, day(2026, 2, 28))).toBe(17);
|
|
55
|
+
expect(ageFromDob({ y: 2008, m: 2, d: 29 }, day(2026, 3, 1))).toBe(18);
|
|
56
|
+
});
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
describe("ageFromDob — no age", () => {
|
|
60
|
+
it("no year, or nothing at all", () => {
|
|
61
|
+
expect(ageFromDob({ m: 5, d: 21 }, TODAY)).toBeNull();
|
|
62
|
+
expect(ageFromDob(null, TODAY)).toBeNull();
|
|
63
|
+
expect(ageFromDob(undefined, TODAY)).toBeNull();
|
|
64
|
+
expect(ageFromDob("", TODAY)).toBeNull();
|
|
65
|
+
expect(ageFromDob({}, TODAY)).toBeNull();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("a year out of range, or a birth after today", () => {
|
|
69
|
+
expect(ageFromDob({ y: 1849 }, TODAY)).toBeNull();
|
|
70
|
+
expect(ageFromDob({ y: 2028 }, TODAY)).toBeNull();
|
|
71
|
+
expect(ageFromDob({ y: 2026, m: 11, d: 1 }, TODAY)).toBeNull();
|
|
72
|
+
expect(ageFromDob({ y: 2027 }, TODAY)).toBeNull();
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("born earlier today's year: 0", () => {
|
|
76
|
+
expect(ageFromDob({ y: 2026, m: 1, d: 5 }, TODAY)).toBe(0);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("an invalid today object", () => {
|
|
80
|
+
expect(ageFromDob("2008-03-01", { y: 2026, m: 13, d: 1 })).toBeNull();
|
|
81
|
+
expect(ageFromDob("2008-03-01", { y: 2026, m: 10, d: 0 })).toBeNull();
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("an unreadable string or an invalid today", () => {
|
|
85
|
+
expect(ageFromDob("March 1990", TODAY)).toBeNull();
|
|
86
|
+
expect(ageFromDob("2008-03-01", new Date(Number.NaN))).toBeNull();
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
describe("normalizeDob", () => {
|
|
91
|
+
it("reads ISO by parts — no time-zone shift", () => {
|
|
92
|
+
expect(normalizeDob("2008-03-01")).toEqual({ y: 2008, m: 3, d: 1 });
|
|
93
|
+
expect(normalizeDob("2008-03-01T00:00:00.000Z")).toEqual({ y: 2008, m: 3, d: 1 });
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("reads MM/DD/YYYY with any part blank, padded or not", () => {
|
|
97
|
+
expect(normalizeDob("03/1/2006")).toEqual({ m: 3, d: 1, y: 2006 });
|
|
98
|
+
expect(normalizeDob("/05/1990")).toEqual({ m: null, d: 5, y: 1990 });
|
|
99
|
+
expect(normalizeDob("//")).toBeNull();
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("drops parts that are not a real month, day or year", () => {
|
|
103
|
+
expect(normalizeDob({ y: 2008, m: 13, d: 0 })).toEqual({ y: 2008, m: null, d: null });
|
|
104
|
+
expect(normalizeDob({ y: "", m: " ", d: null })).toBeNull();
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
describe("ageFromDob — born this year (a known-born person always has an age)", () => {
|
|
109
|
+
it("birth year is this year, month unknown: 0, not null", () => {
|
|
110
|
+
expect(ageFromDob({ y: 2026 }, TODAY)).toBe(0);
|
|
111
|
+
expect(ageFromDob("2026", TODAY)).toBe(0);
|
|
112
|
+
expect(ageFromDob("//2026", TODAY)).toBe(0);
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it("birth month is this month, day unknown: 0, not null", () => {
|
|
116
|
+
expect(ageFromDob({ y: 2026, m: 10 }, TODAY)).toBe(0);
|
|
117
|
+
expect(ageFromDob("2026-10", TODAY)).toBe(0);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("born today: 0", () => {
|
|
121
|
+
expect(ageFromDob({ y: 2026, m: 10, d: 24 }, TODAY)).toBe(0);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("only a date of birth after today has no age", () => {
|
|
125
|
+
expect(ageFromDob({ y: 2026, m: 10, d: 25 }, TODAY)).toBeNull(); // tomorrow
|
|
126
|
+
expect(ageFromDob("2026-10-25", TODAY)).toBeNull();
|
|
127
|
+
expect(ageFromDob({ y: 2026, m: 11 }, TODAY)).toBeNull(); // a later month
|
|
128
|
+
expect(ageFromDob({ y: 2027 }, TODAY)).toBeNull(); // a later year
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
describe("ageFromDob — a JS Date or a partial ISO string as the date of birth", () => {
|
|
133
|
+
it("a Date is a full date (local-constructed or stored as UTC midnight)", () => {
|
|
134
|
+
expect(ageFromDob(new Date(2008, 2, 1), TODAY)).toBe(18);
|
|
135
|
+
expect(ageFromDob(new Date(2008, 9, 25), TODAY)).toBe(17);
|
|
136
|
+
expect(ageFromDob(new Date("2008-10-24T00:00:00.000Z"), TODAY)).toBe(18);
|
|
137
|
+
expect(normalizeDob(new Date("2008-03-01T00:00:00.000Z"))).toEqual({ y: 2008, m: 3, d: 1 });
|
|
138
|
+
expect(normalizeDob(new Date(2008, 2, 1))).toEqual({ y: 2008, m: 3, d: 1 });
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("an invalid Date has no age", () => {
|
|
142
|
+
expect(ageFromDob(new Date(Number.NaN), TODAY)).toBeNull();
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('"YYYY" is year only; "YYYY-MM" is day unknown; "YYYY-MM-DD" is full', () => {
|
|
146
|
+
expect(normalizeDob("2008")).toEqual({ y: 2008, m: null, d: null });
|
|
147
|
+
expect(normalizeDob("2008-05")).toEqual({ y: 2008, m: 5, d: null });
|
|
148
|
+
expect(normalizeDob("2008-13")).toEqual({ y: 2008, m: null, d: null });
|
|
149
|
+
expect(ageFromDob("2008", TODAY)).toBe(17);
|
|
150
|
+
expect(ageFromDob("2008-10", TODAY)).toBe(17);
|
|
151
|
+
expect(ageFromDob("2008-09", TODAY)).toBe(18);
|
|
152
|
+
expect(ageFromDob("2008-10-24", TODAY)).toBe(18);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("an untyped record (any) carrying a Date keeps its age", () => {
|
|
156
|
+
const record: any = { dob: new Date(2008, 2, 1) };
|
|
157
|
+
expect(ageFromDob(record.dob, TODAY)).toBe(18);
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
describe("ageFromDob — today is a calendar date the caller chooses", () => {
|
|
162
|
+
it("{ y, m, d } and a local Date for the same day agree", () => {
|
|
163
|
+
const dob = { y: 2008, m: 10, d: 24 };
|
|
164
|
+
expect(ageFromDob(dob, { y: 2026, m: 10, d: 24 })).toBe(18);
|
|
165
|
+
expect(ageFromDob(dob, { y: 2026, m: 10, d: 23 })).toBe(17);
|
|
166
|
+
expect(ageFromDob(dob, new Date(2026, 9, 24))).toBe(18);
|
|
167
|
+
expect(ageFromDob(dob, new Date(2026, 9, 24, 23, 59))).toBe(18);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("the answer does not depend on the process time zone", () => {
|
|
171
|
+
// The auditor's instant: 2026-10-24T03:00Z. With { y, m, d } the caller
|
|
172
|
+
// decides the day, so TZ=UTC and TZ=America/Los_Angeles cannot split.
|
|
173
|
+
expect(ageFromDob("2008-10-24", { y: 2026, m: 10, d: 24 })).toBe(18);
|
|
174
|
+
expect(ageFromDob("2008-10-24", { y: 2026, m: 10, d: 23 })).toBe(17);
|
|
175
|
+
// A calendar date stored as UTC midnight reads as that day in every zone.
|
|
176
|
+
expect(calendarDateOf(new Date("2026-10-24T00:00:00.000Z"))).toEqual({ y: 2026, m: 10, d: 24 });
|
|
177
|
+
expect(ageFromDob("2008-10-24", new Date("2026-10-24T00:00:00.000Z"))).toBe(18);
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
describe("ageInputFromStoredDob", () => {
|
|
182
|
+
it("maps the stored dobYear / dobMonth / dobDay strings to the ageFromDob input", () => {
|
|
183
|
+
expect(ageInputFromStoredDob({ dobYear: "2008", dobMonth: "10", dobDay: "" })).toEqual({
|
|
184
|
+
y: "2008",
|
|
185
|
+
m: "10",
|
|
186
|
+
d: "",
|
|
187
|
+
});
|
|
188
|
+
expect(ageFromDob(ageInputFromStoredDob({ dobYear: "2008" }), TODAY)).toBe(17);
|
|
189
|
+
expect(ageFromDob(ageInputFromStoredDob({ dobYear: "2008", dobMonth: "10", dobDay: "24" }), TODAY)).toBe(18);
|
|
190
|
+
expect(ageFromDob(ageInputFromStoredDob(null), TODAY)).toBeNull();
|
|
191
|
+
});
|
|
192
|
+
});
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Age from a date of birth — the ONE rule every HTI app uses.
|
|
3
|
+
*
|
|
4
|
+
* Before this module, Thread and PAVE computed age from a date of birth in six
|
|
5
|
+
* hand-kept places with three different answers when the date of birth was
|
|
6
|
+
* incomplete. A person entered with birth year 2008 and no month showed as 17
|
|
7
|
+
* (a minor) on PAVE's profile but as 18 to PAVE's risk calculation, so the
|
|
8
|
+
* minor risk input never fired. Every app now calls {@link ageFromDob}.
|
|
9
|
+
*
|
|
10
|
+
* The rule is the prototype's `deriveYearsFromDob` (HTI-LEADS-PAVE-Thread
|
|
11
|
+
* prototype, Crysta directive 2026-04-28), which PAVE ported verbatim to
|
|
12
|
+
* `packages/domain/src/personAge.ts`:
|
|
13
|
+
*
|
|
14
|
+
* - Whole years between the birth date and `today`.
|
|
15
|
+
* - When we cannot tell whether this year's birthday has happened yet —
|
|
16
|
+
* the month is unknown, or it is the birth month and the day is unknown —
|
|
17
|
+
* count the person YOUNGER (assume the birthday has not happened). At the
|
|
18
|
+
* 12/13 and 17/18 boundaries this picks the cautious, protective reading.
|
|
19
|
+
* - No year → no age.
|
|
20
|
+
* - A person who is known to be born is never "no age": counting younger
|
|
21
|
+
* never takes the age below 0 (an infant entered with only this year as
|
|
22
|
+
* the birth year is 0, a minor). Only a date of birth that is after
|
|
23
|
+
* `today` — at the precision it is known — has no age.
|
|
24
|
+
*
|
|
25
|
+
* `today` is a calendar date the CALLER chooses (Crysta's date rule: moments
|
|
26
|
+
* are stored as UTC, calendar days are local — so which day it is depends on
|
|
27
|
+
* whose day you mean). Pass `{ y, m, d }`; in a server, compute it for the
|
|
28
|
+
* zone you mean rather than passing `new Date()`, so the server and the
|
|
29
|
+
* browser agree on a birthday evening. A `Date` is also accepted and read as
|
|
30
|
+
* a calendar date (see {@link calendarDateOf}).
|
|
31
|
+
*
|
|
32
|
+
* A date-of-birth string is parsed into its year / month / day parts, never
|
|
33
|
+
* through `new Date(string)` — that anchors "YYYY-MM-DD" to UTC midnight and,
|
|
34
|
+
* read back with local getters, lands one day early in every negative-UTC
|
|
35
|
+
* zone (the prototype's DATE-CANON note, 2026-08-25).
|
|
36
|
+
*
|
|
37
|
+
* Pure TypeScript: no mongoose, no I/O. Browser consumers import it from the
|
|
38
|
+
* `@htilabs/platform-common-models/age` subpath.
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
/** A date of birth that may be partial: any of month, day and year can be unknown. */
|
|
42
|
+
export interface PartialDob {
|
|
43
|
+
/** Month 1–12, or `null` when unknown. */
|
|
44
|
+
m: number | null;
|
|
45
|
+
/** Day 1–31, or `null` when unknown. */
|
|
46
|
+
d: number | null;
|
|
47
|
+
/** Four-digit year, or `null` when unknown. */
|
|
48
|
+
y: number | null;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Loose partial-DOB input: any subset of `{ m, d, y }`, as numbers or numeric
|
|
53
|
+
* strings (what a form control hands back). A blank string is "unknown".
|
|
54
|
+
*/
|
|
55
|
+
export interface PartialDobLike {
|
|
56
|
+
m?: number | string | null;
|
|
57
|
+
d?: number | string | null;
|
|
58
|
+
y?: number | string | null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Every date-of-birth shape {@link ageFromDob} accepts: the partial object; a
|
|
63
|
+
* JS `Date` (a full date, read as a calendar date — see {@link calendarDateOf});
|
|
64
|
+
* an ISO string `"YYYY"`, `"YYYY-MM"` or `"YYYY-MM-DD"` (precision by length; a
|
|
65
|
+
* time suffix after the day is ignored); or the app-persisted `"MM/DD/YYYY"`
|
|
66
|
+
* string with any part blank (`"/05/1990"`, `"//2008"`).
|
|
67
|
+
*/
|
|
68
|
+
export type DobLike = PartialDobLike | Date | string | null | undefined;
|
|
69
|
+
|
|
70
|
+
/** A full calendar date: year, month 1–12, day 1–31. */
|
|
71
|
+
export interface CalendarDate {
|
|
72
|
+
y: number;
|
|
73
|
+
m: number;
|
|
74
|
+
d: number;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** The `today` argument of {@link ageFromDob}: a calendar date, or a `Date` read as one. */
|
|
78
|
+
export type CalendarDateLike = CalendarDate | Date;
|
|
79
|
+
|
|
80
|
+
/** Earliest birth year the rule accepts (the prototype's floor). */
|
|
81
|
+
export const DOB_MIN_YEAR = 1850;
|
|
82
|
+
|
|
83
|
+
function toNullableNumber(v: number | string | null | undefined): number | null {
|
|
84
|
+
if (v == null) return null;
|
|
85
|
+
if (typeof v === "string" && v.trim() === "") return null;
|
|
86
|
+
const n = typeof v === "number" ? v : Number(v);
|
|
87
|
+
return Number.isFinite(n) ? n : null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** A month outside 1–12 is not a month. */
|
|
91
|
+
function toNullableMonth(v: number | string | null | undefined): number | null {
|
|
92
|
+
const n = toNullableNumber(v);
|
|
93
|
+
return n != null && n >= 1 && n <= 12 ? n : null;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** A day outside 1–31 is not a day. */
|
|
97
|
+
function toNullableDay(v: number | string | null | undefined): number | null {
|
|
98
|
+
const n = toNullableNumber(v);
|
|
99
|
+
return n != null && n >= 1 && n <= 31 ? n : null;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** A year must be a positive whole number. */
|
|
103
|
+
function toNullableYear(v: number | string | null | undefined): number | null {
|
|
104
|
+
const n = toNullableNumber(v);
|
|
105
|
+
return n != null && Number.isInteger(n) && n > 0 ? n : null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function isDate(v: unknown): v is Date {
|
|
109
|
+
return v instanceof Date || Object.prototype.toString.call(v) === "[object Date]";
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* The calendar date a `Date` stands for, or `null` for an invalid `Date`.
|
|
114
|
+
*
|
|
115
|
+
* A `Date` at exactly UTC midnight is read by its UTC parts: that is how a
|
|
116
|
+
* calendar date is stored (mongoose / JSON `"2008-03-01T00:00:00.000Z"`), and
|
|
117
|
+
* local getters would land a day early west of UTC. Any other `Date` is read
|
|
118
|
+
* by its local parts — `new Date(2008, 2, 1)` is 1 March in every zone, and
|
|
119
|
+
* `new Date()` is the local day of the process that made it.
|
|
120
|
+
*
|
|
121
|
+
* A `Date` date of birth must be stored as UTC midnight. A local-midnight
|
|
122
|
+
* `Date` made in a zone east of UTC is NOT supported: `new Date(2000, 2, 1)`
|
|
123
|
+
* made in Berlin is `2000-02-29T23:00Z` and reads as 29 February on a UTC
|
|
124
|
+
* server — one day early, so the person is counted older the day before the
|
|
125
|
+
* birthday. The zone it was made in is lost, so no rule can recover it; prefer
|
|
126
|
+
* `{ y, m, d }` or {@link ageInputFromStoredDob}.
|
|
127
|
+
*/
|
|
128
|
+
export function calendarDateOf(date: Date): CalendarDate | null {
|
|
129
|
+
if (Number.isNaN(date.getTime())) return null;
|
|
130
|
+
const utcMidnight =
|
|
131
|
+
date.getUTCHours() === 0 &&
|
|
132
|
+
date.getUTCMinutes() === 0 &&
|
|
133
|
+
date.getUTCSeconds() === 0 &&
|
|
134
|
+
date.getUTCMilliseconds() === 0;
|
|
135
|
+
return utcMidnight
|
|
136
|
+
? { y: date.getUTCFullYear(), m: date.getUTCMonth() + 1, d: date.getUTCDate() }
|
|
137
|
+
: { y: date.getFullYear(), m: date.getMonth() + 1, d: date.getDate() };
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* The `{ dobYear, dobMonth, dobDay }` strings as stored on the demographics
|
|
142
|
+
* sub-schema (`schemas/common.ts`), lifted into the `{ y, m, d }` input of
|
|
143
|
+
* {@link ageFromDob}. Use it instead of mapping the fields by hand.
|
|
144
|
+
*/
|
|
145
|
+
export function ageInputFromStoredDob(stored: {
|
|
146
|
+
dobYear?: string | number | null;
|
|
147
|
+
dobMonth?: string | number | null;
|
|
148
|
+
dobDay?: string | number | null;
|
|
149
|
+
} | null | undefined): PartialDobLike {
|
|
150
|
+
return { y: stored?.dobYear ?? null, m: stored?.dobMonth ?? null, d: stored?.dobDay ?? null };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Lifts any accepted date-of-birth shape into `{ m, d, y }` (each part a number
|
|
155
|
+
* or `null`), or `null` when nothing usable is present. An unrecognised string
|
|
156
|
+
* returns `null` rather than risking an engine-dependent `new Date()` parse.
|
|
157
|
+
*/
|
|
158
|
+
export function normalizeDob(dobLike: DobLike): PartialDob | null {
|
|
159
|
+
if (dobLike == null) return null;
|
|
160
|
+
|
|
161
|
+
if (isDate(dobLike)) return calendarDateOf(dobLike);
|
|
162
|
+
|
|
163
|
+
if (typeof dobLike === "object") {
|
|
164
|
+
const m = toNullableMonth(dobLike.m);
|
|
165
|
+
const d = toNullableDay(dobLike.d);
|
|
166
|
+
const y = toNullableYear(dobLike.y);
|
|
167
|
+
if (m == null && d == null && y == null) return null;
|
|
168
|
+
return { m, d, y };
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (typeof dobLike === "string") {
|
|
172
|
+
const s = dobLike.trim();
|
|
173
|
+
if (!s) return null;
|
|
174
|
+
|
|
175
|
+
const iso = /^(\d{4})-(\d{1,2})-(\d{1,2})/.exec(s);
|
|
176
|
+
if (iso) {
|
|
177
|
+
return {
|
|
178
|
+
y: toNullableYear(iso[1]),
|
|
179
|
+
m: toNullableMonth(iso[2]),
|
|
180
|
+
d: toNullableDay(iso[3]),
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Partial ISO: "YYYY-MM" (day unknown) and "YYYY" (month and day unknown).
|
|
185
|
+
const isoPartial = /^(\d{4})(?:-(\d{1,2}))?$/.exec(s);
|
|
186
|
+
if (isoPartial) {
|
|
187
|
+
const y = toNullableYear(isoPartial[1]);
|
|
188
|
+
return y == null ? null : { y, m: toNullableMonth(isoPartial[2]), d: null };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
const slash = /^(\d{0,2})\/(\d{0,2})\/(\d{0,4})$/.exec(s);
|
|
192
|
+
if (slash) {
|
|
193
|
+
const m = toNullableMonth(slash[1]);
|
|
194
|
+
const d = toNullableDay(slash[2]);
|
|
195
|
+
const y = toNullableYear(slash[3]);
|
|
196
|
+
if (m == null && d == null && y == null) return null;
|
|
197
|
+
return { m, d, y };
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Whole years of age on `today`, from a full or partial date of birth — or
|
|
208
|
+
* `null` when there is no usable year, the year is before {@link DOB_MIN_YEAR},
|
|
209
|
+
* or the birth is after `today` at the precision it is known (a later year; this
|
|
210
|
+
* year and a later month; this month and a later day). `null` also for an
|
|
211
|
+
* invalid `today`.
|
|
212
|
+
*
|
|
213
|
+
* When it cannot be told whether this year's birthday has passed (no month, or
|
|
214
|
+
* the birth month with no day), the person is counted younger — but never below
|
|
215
|
+
* 0: a person known to be born is age 0 at least.
|
|
216
|
+
*
|
|
217
|
+
* A `Date` date of birth must be stored as UTC midnight (see
|
|
218
|
+
* {@link calendarDateOf}); prefer `{ y, m, d }` or {@link ageInputFromStoredDob}.
|
|
219
|
+
*
|
|
220
|
+
* `today` is the calendar date the caller means — `{ y, m, d }`, or a `Date`
|
|
221
|
+
* read by {@link calendarDateOf}. In a server, pass `{ y, m, d }` for the zone
|
|
222
|
+
* you mean, not `new Date()` (which is the server's local, usually UTC, day).
|
|
223
|
+
*
|
|
224
|
+
* @example
|
|
225
|
+
* // On 2026-10-24:
|
|
226
|
+
* ageFromDob({ y: 2008 }, today) // 17 — month unknown, count younger
|
|
227
|
+
* ageFromDob({ y: 2008, m: 10 }, today) // 17 — birth month, day unknown
|
|
228
|
+
* ageFromDob({ y: 2008, m: 10, d: 24 }, today) // 18 — birthday is today
|
|
229
|
+
* ageFromDob("2008-03-01", today) // 18
|
|
230
|
+
*/
|
|
231
|
+
export function ageFromDob(dob: DobLike, today: CalendarDateLike): number | null {
|
|
232
|
+
const parts = normalizeDob(dob);
|
|
233
|
+
if (!parts || parts.y == null) return null;
|
|
234
|
+
|
|
235
|
+
const t = isDate(today) ? calendarDateOf(today) : today;
|
|
236
|
+
if (
|
|
237
|
+
t == null ||
|
|
238
|
+
!Number.isInteger(t.y) ||
|
|
239
|
+
toNullableMonth(t.m) == null ||
|
|
240
|
+
toNullableDay(t.d) == null
|
|
241
|
+
) {
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
const todayY = t.y;
|
|
245
|
+
const todayM = t.m;
|
|
246
|
+
const todayD = t.d;
|
|
247
|
+
if (parts.y < DOB_MIN_YEAR) return null;
|
|
248
|
+
|
|
249
|
+
// Born after today, at the precision the date of birth is known → no age.
|
|
250
|
+
if (parts.y > todayY) return null;
|
|
251
|
+
if (parts.y === todayY && parts.m != null) {
|
|
252
|
+
if (parts.m > todayM) return null;
|
|
253
|
+
if (parts.m === todayM && parts.d != null && parts.d > todayD) return null;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
let birthdayPassed: boolean;
|
|
257
|
+
if (parts.m == null) {
|
|
258
|
+
birthdayPassed = false; // month unknown → count younger
|
|
259
|
+
} else if (parts.m < todayM) {
|
|
260
|
+
birthdayPassed = true;
|
|
261
|
+
} else if (parts.m > todayM) {
|
|
262
|
+
birthdayPassed = false;
|
|
263
|
+
} else {
|
|
264
|
+
birthdayPassed = parts.d == null ? false : parts.d <= todayD; // day unknown → count younger
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
// Known to be born (checked above), so counting younger stops at 0.
|
|
268
|
+
return Math.max(0, todayY - parts.y - (birthdayPassed ? 0 : 1));
|
|
269
|
+
}
|
package/src/age/index.ts
ADDED
package/src/index.ts
CHANGED
|
@@ -52,6 +52,10 @@ export * from "./generated/cvc-enums.js";
|
|
|
52
52
|
// Export services (requires mongoose peer dependency)
|
|
53
53
|
export * from "./services/index.js";
|
|
54
54
|
|
|
55
|
+
// The one age-from-date-of-birth rule (pure; no mongoose, no I/O). Browser
|
|
56
|
+
// consumers import it from the `@htilabs/platform-common-models/age` subpath.
|
|
57
|
+
export * from "./age/index.js";
|
|
58
|
+
|
|
55
59
|
// Shared CVC-133 authorization resolver (pure; no mongoose, no I/O). Browser
|
|
56
60
|
// consumers import it from the `@htilabs/platform-common-models/authz` subpath.
|
|
57
61
|
export * from "./authz/index.js";
|