@fhss-web-team/fuzzy-dates 1.1.1 → 1.2.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/dist/fuzzyDate/collate/collate.d.ts +1 -1
- package/dist/fuzzyDate/collate/collate.js +1 -1
- package/dist/fuzzyDate/fuzzyDate.d.ts +10 -4
- package/dist/fuzzyDate/fuzzyDate.js +39 -5
- package/dist/fuzzyDate/gedcomX/toGedcomX.d.ts +1 -1
- package/dist/fuzzyDate/helpers/constants.d.ts +4 -0
- package/dist/fuzzyDate/helpers/constants.js +20 -0
- package/dist/fuzzyDate/{types.d.ts → helpers/maps.d.ts} +0 -28
- package/dist/fuzzyDate/{types.js → helpers/maps.js} +0 -32
- package/dist/fuzzyDate/helpers/result.d.ts +12 -0
- package/dist/fuzzyDate/helpers/result.js +12 -0
- package/dist/fuzzyDate/helpers/schemas.d.ts +36 -0
- package/dist/fuzzyDate/helpers/schemas.js +12 -0
- package/dist/fuzzyDate/helpers/types.d.ts +16 -0
- package/dist/fuzzyDate/helpers/types.js +1 -0
- package/dist/fuzzyDate/normalize/normalize.d.ts +1 -1
- package/dist/fuzzyDate/normalize/normalize.js +1 -1
- package/dist/fuzzyDate/parse/index.d.ts +2 -2
- package/dist/fuzzyDate/parse/index.js +2 -1
- package/dist/fuzzyDate/parse/inputDateFormats.js +2 -1
- package/dist/fuzzyDate/parse/modifiers.js +2 -1
- package/dist/fuzzyDate/parse/stringToDate.js +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -0
- package/package.json +4 -1
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { FuzzyDateModel } from '../types';
|
|
1
|
+
import { FuzzyDateModel } from '../helpers/types';
|
|
2
2
|
export declare function collate(model: FuzzyDateModel): string;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { FuzzyDateJson } from './helpers/types';
|
|
2
2
|
/**
|
|
3
3
|
* Represents an immutable, parsed fuzzy date suitable for genealogy and
|
|
4
4
|
* historical data contexts.
|
|
@@ -159,13 +159,19 @@ export declare class FuzzyDate {
|
|
|
159
159
|
* The returned object is suitable for long-term storage,
|
|
160
160
|
* transport, and later reconstruction via {@link fromJSON}.
|
|
161
161
|
*/
|
|
162
|
-
toJSON():
|
|
162
|
+
toJSON(): FuzzyDateJson;
|
|
163
163
|
/**
|
|
164
164
|
* Reconstructs a `FuzzyDate` from a previously serialized canonical model.
|
|
165
165
|
*
|
|
166
|
-
* @param
|
|
166
|
+
* @param data A trusted `FuzzyDateModel`, typically loaded from storage.
|
|
167
167
|
*/
|
|
168
|
-
static fromJSON(
|
|
168
|
+
static fromJSON(data: unknown): {
|
|
169
|
+
ok: true;
|
|
170
|
+
value: FuzzyDate;
|
|
171
|
+
} | {
|
|
172
|
+
ok: false;
|
|
173
|
+
error: "Invalid JSON data.";
|
|
174
|
+
};
|
|
169
175
|
/**
|
|
170
176
|
* Parses a human-readable date string into a `FuzzyDate`.
|
|
171
177
|
*
|
|
@@ -2,7 +2,9 @@ import { parse } from './parse/index';
|
|
|
2
2
|
import { normalize } from './normalize/normalize';
|
|
3
3
|
import { collate } from './collate/collate';
|
|
4
4
|
import { toGedcomX } from './gedcomX/toGedcomX';
|
|
5
|
-
import { DATE_NEG_INFINITY, DATE_POS_INFINITY
|
|
5
|
+
import { DATE_NEG_INFINITY, DATE_POS_INFINITY } from './helpers/constants';
|
|
6
|
+
import { ok, err } from './helpers/result';
|
|
7
|
+
import { fuzzyDateJsonSchema } from './helpers/schemas';
|
|
6
8
|
/**
|
|
7
9
|
* Represents an immutable, parsed fuzzy date suitable for genealogy and
|
|
8
10
|
* historical data contexts.
|
|
@@ -180,15 +182,47 @@ export class FuzzyDate {
|
|
|
180
182
|
* transport, and later reconstruction via {@link fromJSON}.
|
|
181
183
|
*/
|
|
182
184
|
toJSON() {
|
|
183
|
-
return
|
|
185
|
+
return {
|
|
186
|
+
modifier: this._model.modifier,
|
|
187
|
+
start: {
|
|
188
|
+
format: this._model.start.format,
|
|
189
|
+
minDate: this._model.start.minDate.toISOString(),
|
|
190
|
+
maxDate: this._model.start.maxDate.toISOString(),
|
|
191
|
+
},
|
|
192
|
+
end: {
|
|
193
|
+
format: this._model.end.format,
|
|
194
|
+
minDate: this._model.end.minDate.toISOString(),
|
|
195
|
+
maxDate: this._model.end.maxDate.toISOString(),
|
|
196
|
+
},
|
|
197
|
+
};
|
|
184
198
|
}
|
|
185
199
|
/**
|
|
186
200
|
* Reconstructs a `FuzzyDate` from a previously serialized canonical model.
|
|
187
201
|
*
|
|
188
|
-
* @param
|
|
202
|
+
* @param data A trusted `FuzzyDateModel`, typically loaded from storage.
|
|
189
203
|
*/
|
|
190
|
-
static fromJSON(
|
|
191
|
-
|
|
204
|
+
static fromJSON(data) {
|
|
205
|
+
try {
|
|
206
|
+
const json = typeof data === 'string' ? JSON.parse(data) : data;
|
|
207
|
+
const safeJson = fuzzyDateJsonSchema.parse(json);
|
|
208
|
+
const model = {
|
|
209
|
+
modifier: safeJson.modifier,
|
|
210
|
+
start: {
|
|
211
|
+
format: safeJson.start.format,
|
|
212
|
+
minDate: new Date(safeJson.start.minDate),
|
|
213
|
+
maxDate: new Date(safeJson.start.maxDate),
|
|
214
|
+
},
|
|
215
|
+
end: {
|
|
216
|
+
format: safeJson.end.format,
|
|
217
|
+
minDate: new Date(safeJson.end.minDate),
|
|
218
|
+
maxDate: new Date(safeJson.end.maxDate),
|
|
219
|
+
},
|
|
220
|
+
};
|
|
221
|
+
return ok(new FuzzyDate(model));
|
|
222
|
+
}
|
|
223
|
+
catch {
|
|
224
|
+
return err('Invalid JSON data.');
|
|
225
|
+
}
|
|
192
226
|
}
|
|
193
227
|
/**
|
|
194
228
|
* Parses a human-readable date string into a `FuzzyDate`.
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { FuzzyDateModel } from '../types';
|
|
1
|
+
import { FuzzyDateModel } from '../helpers/types';
|
|
2
2
|
export declare function toGedcomX(model: FuzzyDateModel): string;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
export declare const DATE_NEG_INFINITY: Date;
|
|
2
|
+
export declare const DATE_POS_INFINITY: Date;
|
|
3
|
+
export declare const FORMAT_ORDER: readonly ["YYYYs", "YYYY", "SEASON_YYYY", "MMMM_YYYY", "D_MMMM_YYYY"];
|
|
4
|
+
export declare const MODIFIER_ORDER: readonly ["BEFORE", "ABOUT", "NONE", "EARLY", "MID", "LATE", "FROM", "BETWEEN", "AFTER"];
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export const DATE_NEG_INFINITY = new Date(-8640000000000000);
|
|
2
|
+
export const DATE_POS_INFINITY = new Date(8640000000000000);
|
|
3
|
+
export const FORMAT_ORDER = [
|
|
4
|
+
'YYYYs',
|
|
5
|
+
'YYYY',
|
|
6
|
+
'SEASON_YYYY',
|
|
7
|
+
'MMMM_YYYY',
|
|
8
|
+
'D_MMMM_YYYY',
|
|
9
|
+
];
|
|
10
|
+
export const MODIFIER_ORDER = [
|
|
11
|
+
'BEFORE',
|
|
12
|
+
'ABOUT',
|
|
13
|
+
'NONE',
|
|
14
|
+
'EARLY',
|
|
15
|
+
'MID',
|
|
16
|
+
'LATE',
|
|
17
|
+
'FROM',
|
|
18
|
+
'BETWEEN',
|
|
19
|
+
'AFTER',
|
|
20
|
+
];
|
|
@@ -1,30 +1,3 @@
|
|
|
1
|
-
type Ok<T> = {
|
|
2
|
-
ok: true;
|
|
3
|
-
value: T;
|
|
4
|
-
};
|
|
5
|
-
type Err<E = unknown> = {
|
|
6
|
-
ok: false;
|
|
7
|
-
error: E;
|
|
8
|
-
};
|
|
9
|
-
export type Result<T, E> = Ok<T> | Err<E>;
|
|
10
|
-
export declare function ok<T = undefined>(value?: T): Ok<T>;
|
|
11
|
-
export declare function err<E>(error: E): Err<E>;
|
|
12
|
-
export declare const DATE_NEG_INFINITY: Date;
|
|
13
|
-
export declare const DATE_POS_INFINITY: Date;
|
|
14
|
-
export declare const FORMAT_ORDER: readonly ["YYYYs", "YYYY", "SEASON_YYYY", "MMMM_YYYY", "D_MMMM_YYYY"];
|
|
15
|
-
export type NormalFormat = (typeof FORMAT_ORDER)[number];
|
|
16
|
-
export declare const MODIFIER_ORDER: readonly ["BEFORE", "ABOUT", "NONE", "EARLY", "MID", "LATE", "FROM", "BETWEEN", "AFTER"];
|
|
17
|
-
export type FuzzyDateModifier = (typeof MODIFIER_ORDER)[number];
|
|
18
|
-
export type FuzzyDateValue = {
|
|
19
|
-
format: NormalFormat;
|
|
20
|
-
minDate: Date;
|
|
21
|
-
maxDate: Date;
|
|
22
|
-
};
|
|
23
|
-
export type FuzzyDateModel = {
|
|
24
|
-
modifier: FuzzyDateModifier;
|
|
25
|
-
start: FuzzyDateValue;
|
|
26
|
-
end: FuzzyDateValue;
|
|
27
|
-
};
|
|
28
1
|
export declare const MONTH_SEASON_MAP: {
|
|
29
2
|
readonly spring: 3;
|
|
30
3
|
readonly summer: 6;
|
|
@@ -75,4 +48,3 @@ export declare const MONTH_NAME_MAP: {
|
|
|
75
48
|
readonly december: 12;
|
|
76
49
|
};
|
|
77
50
|
export declare function isMonth(input: string): input is keyof typeof MONTH_NAME_MAP;
|
|
78
|
-
export {};
|
|
@@ -1,35 +1,3 @@
|
|
|
1
|
-
export function ok(value) {
|
|
2
|
-
return {
|
|
3
|
-
ok: true,
|
|
4
|
-
value,
|
|
5
|
-
};
|
|
6
|
-
}
|
|
7
|
-
export function err(error) {
|
|
8
|
-
return {
|
|
9
|
-
ok: false,
|
|
10
|
-
error,
|
|
11
|
-
};
|
|
12
|
-
}
|
|
13
|
-
export const DATE_NEG_INFINITY = new Date(-8640000000000000);
|
|
14
|
-
export const DATE_POS_INFINITY = new Date(8640000000000000);
|
|
15
|
-
export const FORMAT_ORDER = [
|
|
16
|
-
'YYYYs',
|
|
17
|
-
'YYYY',
|
|
18
|
-
'SEASON_YYYY',
|
|
19
|
-
'MMMM_YYYY',
|
|
20
|
-
'D_MMMM_YYYY',
|
|
21
|
-
];
|
|
22
|
-
export const MODIFIER_ORDER = [
|
|
23
|
-
'BEFORE',
|
|
24
|
-
'ABOUT',
|
|
25
|
-
'NONE',
|
|
26
|
-
'EARLY',
|
|
27
|
-
'MID',
|
|
28
|
-
'LATE',
|
|
29
|
-
'FROM',
|
|
30
|
-
'BETWEEN',
|
|
31
|
-
'AFTER',
|
|
32
|
-
];
|
|
33
1
|
export const MONTH_SEASON_MAP = {
|
|
34
2
|
spring: 3,
|
|
35
3
|
summer: 6,
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
type Ok<T> = {
|
|
2
|
+
ok: true;
|
|
3
|
+
value: T;
|
|
4
|
+
};
|
|
5
|
+
type Err<E = unknown> = {
|
|
6
|
+
ok: false;
|
|
7
|
+
error: E;
|
|
8
|
+
};
|
|
9
|
+
export type Result<T, E> = Ok<T> | Err<E>;
|
|
10
|
+
export declare function ok<T = undefined>(value?: T): Ok<T>;
|
|
11
|
+
export declare function err<E>(error: E): Err<E>;
|
|
12
|
+
export {};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import z from 'zod';
|
|
2
|
+
export declare const fuzzyDateJsonSchema: z.ZodObject<{
|
|
3
|
+
modifier: z.ZodEnum<{
|
|
4
|
+
BEFORE: "BEFORE";
|
|
5
|
+
ABOUT: "ABOUT";
|
|
6
|
+
NONE: "NONE";
|
|
7
|
+
EARLY: "EARLY";
|
|
8
|
+
MID: "MID";
|
|
9
|
+
LATE: "LATE";
|
|
10
|
+
FROM: "FROM";
|
|
11
|
+
BETWEEN: "BETWEEN";
|
|
12
|
+
AFTER: "AFTER";
|
|
13
|
+
}>;
|
|
14
|
+
start: z.ZodObject<{
|
|
15
|
+
format: z.ZodEnum<{
|
|
16
|
+
YYYYs: "YYYYs";
|
|
17
|
+
YYYY: "YYYY";
|
|
18
|
+
SEASON_YYYY: "SEASON_YYYY";
|
|
19
|
+
MMMM_YYYY: "MMMM_YYYY";
|
|
20
|
+
D_MMMM_YYYY: "D_MMMM_YYYY";
|
|
21
|
+
}>;
|
|
22
|
+
minDate: z.ZodISODateTime;
|
|
23
|
+
maxDate: z.ZodISODateTime;
|
|
24
|
+
}, z.core.$strip>;
|
|
25
|
+
end: z.ZodObject<{
|
|
26
|
+
format: z.ZodEnum<{
|
|
27
|
+
YYYYs: "YYYYs";
|
|
28
|
+
YYYY: "YYYY";
|
|
29
|
+
SEASON_YYYY: "SEASON_YYYY";
|
|
30
|
+
MMMM_YYYY: "MMMM_YYYY";
|
|
31
|
+
D_MMMM_YYYY: "D_MMMM_YYYY";
|
|
32
|
+
}>;
|
|
33
|
+
minDate: z.ZodISODateTime;
|
|
34
|
+
maxDate: z.ZodISODateTime;
|
|
35
|
+
}, z.core.$strip>;
|
|
36
|
+
}, z.core.$strip>;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import z from 'zod';
|
|
2
|
+
import { FORMAT_ORDER, MODIFIER_ORDER } from './constants';
|
|
3
|
+
const fuzzyDateValueSchema = z.object({
|
|
4
|
+
format: z.enum(FORMAT_ORDER),
|
|
5
|
+
minDate: z.iso.datetime(),
|
|
6
|
+
maxDate: z.iso.datetime(),
|
|
7
|
+
});
|
|
8
|
+
export const fuzzyDateJsonSchema = z.object({
|
|
9
|
+
modifier: z.enum(MODIFIER_ORDER),
|
|
10
|
+
start: fuzzyDateValueSchema,
|
|
11
|
+
end: fuzzyDateValueSchema,
|
|
12
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import z from 'zod';
|
|
2
|
+
import { FORMAT_ORDER, MODIFIER_ORDER } from './constants';
|
|
3
|
+
import { fuzzyDateJsonSchema } from './schemas';
|
|
4
|
+
export type FuzzyDateFormat = (typeof FORMAT_ORDER)[number];
|
|
5
|
+
export type FuzzyDate = (typeof MODIFIER_ORDER)[number];
|
|
6
|
+
export type FuzzyDateValue = {
|
|
7
|
+
format: FuzzyDateFormat;
|
|
8
|
+
minDate: Date;
|
|
9
|
+
maxDate: Date;
|
|
10
|
+
};
|
|
11
|
+
export type FuzzyDateModel = {
|
|
12
|
+
modifier: FuzzyDate;
|
|
13
|
+
start: FuzzyDateValue;
|
|
14
|
+
end: FuzzyDateValue;
|
|
15
|
+
};
|
|
16
|
+
export type FuzzyDateJson = z.infer<typeof fuzzyDateJsonSchema>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { FuzzyDateModel } from '../types';
|
|
1
|
+
import { FuzzyDateModel } from '../helpers/types';
|
|
2
2
|
export declare function normalize(model: FuzzyDateModel): string;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { FuzzyDateFormat, FuzzyDateValue } from '../helpers/types';
|
|
2
2
|
export declare function parse(input: string): {
|
|
3
3
|
ok: false;
|
|
4
4
|
error: "Year is required.";
|
|
@@ -155,7 +155,7 @@ export declare function getTimes(date: FuzzyDateValue): {
|
|
|
155
155
|
end: number;
|
|
156
156
|
half: number;
|
|
157
157
|
};
|
|
158
|
-
export declare function calculateMaxDate(start: Date, format:
|
|
158
|
+
export declare function calculateMaxDate(start: Date, format: FuzzyDateFormat): Date;
|
|
159
159
|
export declare function parseDateGroups(groups: {
|
|
160
160
|
day?: string;
|
|
161
161
|
month?: string;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { about, after, before, between, early, from, late, mid, none, } from './modifiers';
|
|
2
|
-
import {
|
|
2
|
+
import { err, ok } from '../helpers/result';
|
|
3
|
+
import { isMonth, isSeason, MONTH_NAME_MAP, MONTH_SEASON_MAP, } from '../helpers/maps';
|
|
3
4
|
// Main Parse Function
|
|
4
5
|
export function parse(input) {
|
|
5
6
|
const cleanedInput = input
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { calculateMaxDate, parseDateGroups } from '.';
|
|
2
|
-
import {
|
|
2
|
+
import { isSeason } from '../helpers/maps';
|
|
3
|
+
import { ok } from '../helpers/result';
|
|
3
4
|
const DAY = '(?<day>\\d{1,2})';
|
|
4
5
|
const MONTH_DIGIT = '(?<month>\\d{1,2})';
|
|
5
6
|
const MONTH_STRING = '(?<month>[a-z]+)';
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { getTimes } from '.';
|
|
2
|
-
import {
|
|
2
|
+
import { DATE_NEG_INFINITY, DATE_POS_INFINITY } from '../helpers/constants';
|
|
3
|
+
import { err, ok } from '../helpers/result';
|
|
3
4
|
import { stringToDate } from './stringToDate';
|
|
4
5
|
export const none = (cleanedInput) => {
|
|
5
6
|
const result = stringToDate(cleanedInput);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { err } from '../
|
|
1
|
+
import { err } from '../helpers/result';
|
|
2
2
|
import { dayMonthDigitYear, dayMonthStringYear, decade, monthDigitYear, monthStringDayYear, monthStringYear, year, yearMonthDigitDay, yearDayMonthString, yearMonthDigit, yearMonthString, yearMonthStringDay, } from './inputDateFormats';
|
|
3
3
|
export function stringToDate(dateInput) {
|
|
4
4
|
const parsers = [
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fhss-web-team/fuzzy-dates",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Parses imprecise date strings into a structured form that can be normalized, indexed, searched, and ordered while preserving the original input.",
|
|
5
5
|
"homepage": "https://github.com/FHSS-Web-Team/fuzzy-dates#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -42,5 +42,8 @@
|
|
|
42
42
|
"ts-node": "^10.9.2",
|
|
43
43
|
"typescript": "^5.0.0",
|
|
44
44
|
"vitest": "^4.0.0"
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
47
|
+
"zod": "^4.3.5"
|
|
45
48
|
}
|
|
46
49
|
}
|