polish-dates 0.1.0 → 0.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/README.md +19 -2
- package/dist/index.d.mts +29 -3
- package/dist/index.mjs +156 -52
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# polish-dates
|
|
2
2
|
|
|
3
|
-

|
|
3
|
+
[](https://www.npmjs.com/package/polish-dates)
|
|
4
4
|
[](https://ci.appveyor.com/project/lwojcik/polish-dates/branch/main)
|
|
5
5
|
[](https://codecov.io/github/lwojcik/polish-dates)
|
|
6
6
|
|
|
@@ -46,9 +46,12 @@ import {
|
|
|
46
46
|
clocksIn,
|
|
47
47
|
dateOn,
|
|
48
48
|
dateSpanIn,
|
|
49
|
+
findClocks,
|
|
50
|
+
findDates,
|
|
49
51
|
MONTH_PATTERN,
|
|
50
52
|
monthNumber,
|
|
51
53
|
parseDate,
|
|
54
|
+
withoutDates,
|
|
52
55
|
} from 'polish-dates';
|
|
53
56
|
|
|
54
57
|
monthNumber('listopada'); // 11
|
|
@@ -109,10 +112,24 @@ new RegExp(`(\\d{1,2})\\s+(${MONTH_PATTERN})\\s+(\\d{4})`, 'i');
|
|
|
109
112
|
- a whole month, with no day, in the genitive, the nominative or the locative (`grudnia 2026`, `grudzień 2026`, `w grudniu 2026`), and a range of months (`od listopada do grudnia 2026`, `listopad - grudzień 2026`);
|
|
110
113
|
- a part of a month (`I połowa grudnia 2026`, `druga połowa`, `początek stycznia 2027`, `pod koniec listopada 2026`, `w połowie grudnia`): the first half is the 1st to the 15th and the second half is the 16th to the end, `początek` is the 1st to the 10th, `środek` and `w połowie` the 11th to the 20th and `koniec` the 21st to the end.
|
|
111
114
|
|
|
112
|
-
A range that runs backwards or a day that does not exist is not read. A day always wins over the month around it, so `5 grudnia 2026` is a single day. A month alone with no year (`w grudniu`, `grudzień`, `koniec listopada`, `od listopada do stycznia`) is read with `today`, as the next time it comes; the genitive alone (`grudnia`) is read only with a year. A date without a year is read only when `today` is given as `YYYY-MM-DD` in the second argument `{ today }`, and then it is the next time the date comes (today counts, and a range that is still on counts), skipping to a year that has the day for `29 lutego`. A range across the new year runs into the next year (`30.12–2.01`). The words after the date set the year instead: `br.`, `w tym roku` and `bieżącego roku` for this year, `w przyszłym roku` and `przyszłego roku` for the next, `w zeszłym roku` and `w ubiegłym roku` for the last. A day and a month in digits (`5.12`, `23-27.11`) are read this way too, unless they follow `godz.` or stand before `zł` and the like. A weekday written beside a date (`w sobotę, 5 grudnia`, `pt. 4 – nd. 6 grudnia`, `piątek–niedziela, 4–6 grudnia 2026`) must fit it, or the text gives `null`; for a date without a year it picks the next year that fits. When there is no date but `today` is given, it reads a day by a word (`dziś`, `jutro`, `pojutrze`, `wczoraj`, `przedwczoraj`), by an offset (`za 3 dni`, `za tydzień`, `2 tygodnie temu`), a weekday (`w piątek` is the coming one, `w przyszły piątek` is in the next week, `w zeszły piątek` in the last), a pair of weekdays (`od piątku do niedzieli`, `w sobotę i niedzielę`) and a weekend (`w ten weekend`, `w przyszły weekend`). A date that is written out wins over these words. With `tolerant: true` it also reads words without the Polish letters (`wrzesnia`, `sroda`, `dzis`, `w przyszly piatek`), a month with one letter wrong (`grudznia`, `listpada`, `listopad`, `stycznai`; only a word of five letters or more that follows a number, and only when one month is that near), missing or extra spaces and dots (`5grudnia2026`, `5. grudnia 2026`) and other separators in a date in digits (`17,07.2026`, `5. 12. 2026`, `05-12-2026`). A day that does not exist is still an error. A comma list cannot be told from other text, so `Sala 3, 5 grudnia 2026` reads as `3–5 grudnia`.
|
|
115
|
+
A range that runs backwards or a day that does not exist is not read. A day always wins over the month around it, so `5 grudnia 2026` is a single day. A month alone with no year (`w grudniu`, `grudzień`, `koniec listopada`, `od listopada do stycznia`) is read with `today`, as the next time it comes; the genitive alone (`grudnia`) is read only with a year. A date without a year is read only when `today` is given as `YYYY-MM-DD` in the second argument `{ today }`, and then it is the next time the date comes (today counts, and a range that is still on counts), skipping to a year that has the day for `29 lutego`. A range across the new year runs into the next year (`30.12–2.01`). The words after the date set the year instead: `br.`, `w tym roku` and `bieżącego roku` for this year, `w przyszłym roku` and `przyszłego roku` for the next, `w zeszłym roku` and `w ubiegłym roku` for the last. A day and a month in digits (`5.12`, `23-27.11`) are read this way too, unless they follow `godz.` or stand before `zł` and the like. A weekday written beside a date (`w sobotę, 5 grudnia`, `pt. 4 – nd. 6 grudnia`, `piątek–niedziela, 4–6 grudnia 2026`) must fit it, or the text gives `null`; for a date without a year it picks the next year that fits. When there is no date but `today` is given, it reads a day by a word (`dziś`, `jutro`, `pojutrze`, `wczoraj`, `przedwczoraj`), by an offset (`za 3 dni`, `za tydzień`, `2 tygodnie temu`), a weekday (`w piątek` is the coming one, `w przyszły piątek` is in the next week, `w zeszły piątek` in the last), a pair of weekdays (`od piątku do niedzieli`, `w sobotę i niedzielę`) and a weekend (`w ten weekend`, `w przyszły weekend`). A date that is written out wins over these words. With `tolerant: true` it also reads words without the Polish letters (`wrzesnia`, `sroda`, `dzis`, `w przyszly piatek`), a month with one letter wrong (`grudznia`, `listpada`, `listopad`, `stycznai`; only a word of five letters or more that follows a number, and only when one month is that near), missing or extra spaces and dots (`5grudnia2026`, `5. grudnia 2026`) and other or repeated separators in a date in digits (`17,07.2026`, `5. 12. 2026`, `05-12-2026`, `02..08.2025`; a clock such as `19..00` stays unread). A day that does not exist is still an error. A comma list cannot be told from other text, so `Sala 3, 5 grudnia 2026` reads as `3–5 grudnia`.
|
|
113
116
|
|
|
114
117
|
- **`clockAfterHour(text)`** reads the time after `godz.`, `godzina` or `godzinie` (`godz. 17`, `godz. 17.30`) as `[hour, minute]`, or `null`.
|
|
115
118
|
- **`clocksIn(text)`** gives every `H.MM` or `HH:MM` as `[hour, minute]`, in order, and skips what is not a time of day.
|
|
119
|
+
- **`findDates(text, { today, tolerant })`** gives every date `dateSpanIn` reads, each as `{ start, end, index, length }` with the position in the text you passed (also with `tolerant: true`, which reads a corrected copy). The match includes a weekday written before the date. A date that does not exist is left out. Two dates in one line come out as two matches, in the order they stand:
|
|
120
|
+
|
|
121
|
+
```ts
|
|
122
|
+
findDates('Środa, 9 grudnia 2026, godz. 18.30 oraz 12.12.2026', {
|
|
123
|
+
tolerant: true,
|
|
124
|
+
});
|
|
125
|
+
// [
|
|
126
|
+
// { start: '2026-12-09', end: '2026-12-09', index: 0, length: 21 },
|
|
127
|
+
// { start: '2026-12-12', end: '2026-12-12', index: 40, length: 10 },
|
|
128
|
+
// ]
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
- **`withoutDates(text, options)`** gives the text with every match of `findDates` cut out, so what is left (a place, a title) can be read on its own.
|
|
132
|
+
- **`findClocks(text)`** is `clocksIn` with positions: `{ clock, index, length }` for each match.
|
|
116
133
|
|
|
117
134
|
## Development
|
|
118
135
|
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
export type Clock = [hour: number, minute: number];
|
|
2
|
+
export interface ClockMatch {
|
|
3
|
+
clock: Clock;
|
|
4
|
+
index: number;
|
|
5
|
+
length: number;
|
|
6
|
+
}
|
|
7
|
+
export declare const findClocks: (text: string) => ClockMatch[];
|
|
2
8
|
export declare const clocksIn: (text: string) => Clock[];
|
|
3
9
|
export declare const clockAfterHour: (text: string) => Clock | null;
|
|
4
10
|
export interface DateOnOptions {
|
|
@@ -7,9 +13,6 @@ export interface DateOnOptions {
|
|
|
7
13
|
day: number;
|
|
8
14
|
}
|
|
9
15
|
export declare const dateOn: ({ year, month, day }: DateOnOptions) => string | null;
|
|
10
|
-
export declare const POLISH_MONTHS: readonly ["stycznia", "lutego", "marca", "kwietnia", "maja", "czerwca", "lipca", "sierpnia", "września", "października", "listopada", "grudnia"];
|
|
11
|
-
export declare const MONTH_PATTERN: string;
|
|
12
|
-
export declare const monthNumber: (name: string) => number;
|
|
13
16
|
interface DateSpan {
|
|
14
17
|
start: string;
|
|
15
18
|
end: string;
|
|
@@ -18,7 +21,30 @@ export interface DateSpanOptions {
|
|
|
18
21
|
today?: string;
|
|
19
22
|
tolerant?: boolean;
|
|
20
23
|
}
|
|
24
|
+
export interface Today {
|
|
25
|
+
iso: string;
|
|
26
|
+
year: number;
|
|
27
|
+
}
|
|
28
|
+
export declare const todayOf: (today?: string) => Today | null;
|
|
29
|
+
export interface Found {
|
|
30
|
+
span: DateSpan | null;
|
|
31
|
+
index: number;
|
|
32
|
+
length: number;
|
|
33
|
+
}
|
|
34
|
+
export declare const firstIn: (plain: string, { now, isTolerant }: {
|
|
35
|
+
now: Today | null;
|
|
36
|
+
isTolerant: boolean;
|
|
37
|
+
}) => Found | undefined;
|
|
21
38
|
export declare const dateSpanIn: (text: string, { today, tolerant }?: DateSpanOptions) => DateSpan | null;
|
|
39
|
+
export interface DateMatch extends DateSpan {
|
|
40
|
+
index: number;
|
|
41
|
+
length: number;
|
|
42
|
+
}
|
|
43
|
+
export declare const findDates: (text: string, { today, tolerant }?: DateSpanOptions) => DateMatch[];
|
|
44
|
+
export declare const withoutDates: (text: string, options?: DateSpanOptions) => string;
|
|
45
|
+
export declare const POLISH_MONTHS: readonly ["stycznia", "lutego", "marca", "kwietnia", "maja", "czerwca", "lipca", "sierpnia", "września", "października", "listopada", "grudnia"];
|
|
46
|
+
export declare const MONTH_PATTERN: string;
|
|
47
|
+
export declare const monthNumber: (name: string) => number;
|
|
22
48
|
export interface ParsedDate {
|
|
23
49
|
start: string | null;
|
|
24
50
|
end: string | null;
|
package/dist/index.mjs
CHANGED
|
@@ -2,7 +2,13 @@
|
|
|
2
2
|
const CLOCK = /(\d{1,2})[.:](\d{2})/g;
|
|
3
3
|
const HOURS_PER_DAY = 24;
|
|
4
4
|
const MINUTES_PER_HOUR = 60;
|
|
5
|
-
const
|
|
5
|
+
const isTimeOfDay$1 = ([hour, minute]) => hour < HOURS_PER_DAY && minute < MINUTES_PER_HOUR;
|
|
6
|
+
const findClocks = (text) => text.matchAll(CLOCK).map((match) => ({
|
|
7
|
+
clock: [+match[1], +match[2]],
|
|
8
|
+
index: match.index,
|
|
9
|
+
length: match[0].length
|
|
10
|
+
})).filter(({ clock }) => isTimeOfDay$1(clock)).toArray();
|
|
11
|
+
const clocksIn = (text) => findClocks(text).map(({ clock }) => clock);
|
|
6
12
|
const HOUR = /godz(?:\.|ina|inie)?\s*(\d{1,2})(?:[.:](\d{2}))?/i;
|
|
7
13
|
const clockAfterHour = (text) => {
|
|
8
14
|
const match = HOUR.exec(text);
|
|
@@ -40,25 +46,17 @@ const dateOn = ({ year, month, day }) => {
|
|
|
40
46
|
month
|
|
41
47
|
}) ? `${pad(year, YEAR_DIGITS)}-${pad(month, 2)}-${pad(day, 2)}` : null;
|
|
42
48
|
};
|
|
43
|
-
// src/
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
"
|
|
51
|
-
"
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
"października",
|
|
55
|
-
"listopada",
|
|
56
|
-
"grudnia"
|
|
57
|
-
];
|
|
58
|
-
const MONTH_PATTERN = POLISH_MONTHS.join("|");
|
|
59
|
-
const monthNumber = (name) => POLISH_MONTHS.findIndex((month) => month === name.toLowerCase()) + 1;
|
|
60
|
-
// src/dashes.ts
|
|
61
|
-
const DASHES = /[‐-―]/g;
|
|
49
|
+
// src/fold.ts
|
|
50
|
+
const fold = (text = "") => text.toLowerCase().normalize("NFD").replaceAll(/\p{M}/gu, "").replaceAll("ł", "l");
|
|
51
|
+
const STRICT_DOT = "[./]";
|
|
52
|
+
const TOLERANT_DOT = String.raw`(?:[./:]\s?|,)+`;
|
|
53
|
+
const CLASS_OR_LETTER = /\[[^\]]*]|[óąćęłńśźż]/gi;
|
|
54
|
+
const loosen = (source) => source.replaceAll(CLASS_OR_LETTER, (part) => part.length > 1 ? part : `[${part}${fold(part)}]`);
|
|
55
|
+
const compile = (source, flags = "i") => ({
|
|
56
|
+
strict: new RegExp(source.replaceAll("", () => STRICT_DOT), flags),
|
|
57
|
+
tolerant: new RegExp(loosen(source.replaceAll("", () => TOLERANT_DOT)), flags)
|
|
58
|
+
});
|
|
59
|
+
const choose = ({ strict, tolerant }, isTolerant) => isTolerant ? tolerant : strict;
|
|
62
60
|
// src/zones.ts
|
|
63
61
|
const ZONE_NAMES = "utc|gmt|cest|cet|eest|eet|west|wet|bst|est|edt|cst|cdt|mst|mdt|pst|pdt";
|
|
64
62
|
const ZONE = String.raw`(?:${ZONE_NAMES})(?:[+-]\d{1,2}(?::?\d{2})?)?(?![a-ząćęłńóśźż])`;
|
|
@@ -77,17 +75,23 @@ const WORDS = [
|
|
|
77
75
|
].join("|");
|
|
78
76
|
const NOT_MONEY = String.raw`(?!\s*(?:(?:${WORDS}|${CODE})(?![${LETTER$2}])|[€$£¥%]))`;
|
|
79
77
|
const NOT_AFTER_MONEY = String.raw`(?<![€$£¥]|(?:pln|usd|eur)\s*|(?-i:(?<![A-Za-z])(?!(?:${NOT_A_CODE})(?![A-Z]))[A-Z]{3})\s*)`;
|
|
80
|
-
// src/
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
78
|
+
// src/months.ts
|
|
79
|
+
const POLISH_MONTHS = [
|
|
80
|
+
"stycznia",
|
|
81
|
+
"lutego",
|
|
82
|
+
"marca",
|
|
83
|
+
"kwietnia",
|
|
84
|
+
"maja",
|
|
85
|
+
"czerwca",
|
|
86
|
+
"lipca",
|
|
87
|
+
"sierpnia",
|
|
88
|
+
"września",
|
|
89
|
+
"października",
|
|
90
|
+
"listopada",
|
|
91
|
+
"grudnia"
|
|
92
|
+
];
|
|
93
|
+
const MONTH_PATTERN = POLISH_MONTHS.join("|");
|
|
94
|
+
const monthNumber = (name) => POLISH_MONTHS.findIndex((month) => month === name.toLowerCase()) + 1;
|
|
91
95
|
// src/month-forms.ts
|
|
92
96
|
const LETTER$1 = "a-ząćęłńóśźż";
|
|
93
97
|
const ABBREVIATIONS_FROM = 3;
|
|
@@ -401,10 +405,14 @@ function offsetOf({ count, unit = "" }) {
|
|
|
401
405
|
fallback: Number(count)
|
|
402
406
|
}) : 1) * (/^t/i.test(unit) ? 7 : 1);
|
|
403
407
|
}
|
|
404
|
-
const
|
|
408
|
+
const relativeMatchIn = (text, { today, isTolerant }) => {
|
|
405
409
|
const reader = READERS$1.find(({ pattern }) => choose(pattern, isTolerant).test(text));
|
|
406
|
-
const
|
|
407
|
-
return reader && groups ?
|
|
410
|
+
const found = reader && choose(reader.pattern, isTolerant).exec(text);
|
|
411
|
+
return reader && found?.groups ? {
|
|
412
|
+
span: reader.read(found.groups, today),
|
|
413
|
+
index: found.index,
|
|
414
|
+
length: found[0].length
|
|
415
|
+
} : void 0;
|
|
408
416
|
};
|
|
409
417
|
// src/spans.ts
|
|
410
418
|
const CENTURY = 2e3;
|
|
@@ -423,6 +431,8 @@ const spanOf = (dates) => {
|
|
|
423
431
|
end
|
|
424
432
|
} : null;
|
|
425
433
|
};
|
|
434
|
+
// src/dashes.ts
|
|
435
|
+
const DASHES = /[‐-―]/g;
|
|
426
436
|
// src/tolerance.ts
|
|
427
437
|
const LETTER = "a-ząćęłńóśźż";
|
|
428
438
|
const MONTHS = loosen(MONTH_PATTERN);
|
|
@@ -445,7 +455,57 @@ const correct = (word) => {
|
|
|
445
455
|
const [only = word] = near;
|
|
446
456
|
return near.length === 1 ? only : word;
|
|
447
457
|
};
|
|
448
|
-
const
|
|
458
|
+
const COMBINED = /.[\u{300}-\u{36F}]*/gsu;
|
|
459
|
+
const textOf = (units) => units.map(({ char }) => char).join("");
|
|
460
|
+
const rewrite = (units, { pattern, replace }) => {
|
|
461
|
+
const matches = textOf(units).matchAll(pattern).toArray();
|
|
462
|
+
const starts = new Map(matches.map((match) => [match.index, match]));
|
|
463
|
+
const isCovered = (offset) => matches.some(({ index, 0: whole }) => offset >= index && offset < index + whole.length);
|
|
464
|
+
return units.flatMap((unit, offset) => {
|
|
465
|
+
const match = starts.get(offset);
|
|
466
|
+
if (match) return replace(match).split("").map((char) => ({
|
|
467
|
+
char,
|
|
468
|
+
at: unit.at
|
|
469
|
+
}));
|
|
470
|
+
return isCovered(offset) ? [] : [unit];
|
|
471
|
+
});
|
|
472
|
+
};
|
|
473
|
+
const REWRITES = [
|
|
474
|
+
{
|
|
475
|
+
pattern: WORD_AFTER_NUMBER,
|
|
476
|
+
replace: ([word]) => correct(word)
|
|
477
|
+
},
|
|
478
|
+
{
|
|
479
|
+
pattern: BEFORE_MONTH,
|
|
480
|
+
replace: ([, digit]) => `${digit} `
|
|
481
|
+
},
|
|
482
|
+
{
|
|
483
|
+
pattern: AFTER_MONTH,
|
|
484
|
+
replace: ([, month]) => `${month} `
|
|
485
|
+
}
|
|
486
|
+
];
|
|
487
|
+
const applyAll = ([first, ...others], units) => first ? applyAll(others, rewrite(units, first)) : units;
|
|
488
|
+
const tidyWithOffsets = (text) => {
|
|
489
|
+
const letters = text.matchAll(COMBINED).flatMap(({ 0: cluster, index }) => cluster.normalize("NFC").split("").map((char) => ({
|
|
490
|
+
char,
|
|
491
|
+
at: index
|
|
492
|
+
}))).toArray();
|
|
493
|
+
const units = applyAll(REWRITES, letters);
|
|
494
|
+
return {
|
|
495
|
+
text: textOf(units),
|
|
496
|
+
originalAt: (offset) => units[offset]?.at ?? text.length
|
|
497
|
+
};
|
|
498
|
+
};
|
|
499
|
+
const plainOf = (text, isTolerant) => {
|
|
500
|
+
const { text: tidied, originalAt } = isTolerant ? tidyWithOffsets(text) : {
|
|
501
|
+
text,
|
|
502
|
+
originalAt: (offset) => offset
|
|
503
|
+
};
|
|
504
|
+
return {
|
|
505
|
+
text: tidied.replaceAll(DASHES, "-"),
|
|
506
|
+
originalAt
|
|
507
|
+
};
|
|
508
|
+
};
|
|
449
509
|
// src/whole-month.ts
|
|
450
510
|
const LAST_DAYS = [
|
|
451
511
|
31,
|
|
@@ -653,10 +713,12 @@ const UNDATED = [
|
|
|
653
713
|
];
|
|
654
714
|
const firstOf = (readers, { text, isTolerant }) => {
|
|
655
715
|
const reader = readers.find(({ pattern, isTolerantOnly }) => (isTolerant || !isTolerantOnly) && choose(pattern, isTolerant).test(text));
|
|
656
|
-
const
|
|
657
|
-
return reader && groups ? {
|
|
716
|
+
const found = reader && choose(reader.pattern, isTolerant).exec(text);
|
|
717
|
+
return reader && found?.groups ? {
|
|
658
718
|
reader,
|
|
659
|
-
groups
|
|
719
|
+
groups: found.groups,
|
|
720
|
+
index: found.index,
|
|
721
|
+
length: found[0].length
|
|
660
722
|
} : void 0;
|
|
661
723
|
};
|
|
662
724
|
const todayOf = (today = "") => {
|
|
@@ -673,27 +735,69 @@ const todayOf = (today = "") => {
|
|
|
673
735
|
};
|
|
674
736
|
const shiftOf = (relative) => SHIFTS$1.find(([shift]) => shift.test(fold(relative)))?.[1] ?? 0;
|
|
675
737
|
const resolve = ({ build, groups }, today) => groups.relative ? checked(build(today.year + shiftOf(groups.relative)), groups) : Array.from({ length: SEARCH_YEARS }, (unused, offset) => build(today.year + offset)).find((span) => span && span.end >= today.iso && isFitting(weekdaysOf(groups), span)) ?? null;
|
|
676
|
-
const
|
|
677
|
-
|
|
738
|
+
const blankedHours = (text) => text.replaceAll(AFTER_HOUR, (hour) => " ".repeat(hour.length));
|
|
739
|
+
const firstIn = (plain, { now, isTolerant }) => {
|
|
678
740
|
const dated = firstOf(READERS, {
|
|
679
741
|
text: plain,
|
|
680
|
-
isTolerant
|
|
742
|
+
isTolerant
|
|
681
743
|
});
|
|
682
|
-
if (dated) return
|
|
683
|
-
|
|
684
|
-
|
|
744
|
+
if (dated) return {
|
|
745
|
+
span: dated.reader.read(dated.groups),
|
|
746
|
+
index: dated.index,
|
|
747
|
+
length: dated.length
|
|
748
|
+
};
|
|
749
|
+
if (!now) return;
|
|
685
750
|
const undated = firstOf(UNDATED, {
|
|
686
|
-
text: plain
|
|
687
|
-
isTolerant
|
|
751
|
+
text: blankedHours(plain),
|
|
752
|
+
isTolerant
|
|
688
753
|
});
|
|
689
|
-
return undated ?
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
754
|
+
return undated ? {
|
|
755
|
+
span: resolve({
|
|
756
|
+
build: undated.reader.build(undated.groups),
|
|
757
|
+
groups: undated.groups
|
|
758
|
+
}, now),
|
|
759
|
+
index: undated.index,
|
|
760
|
+
length: undated.length
|
|
761
|
+
} : relativeMatchIn(plain, {
|
|
693
762
|
today: now.iso,
|
|
694
|
-
isTolerant
|
|
763
|
+
isTolerant
|
|
764
|
+
});
|
|
765
|
+
};
|
|
766
|
+
const dateSpanIn = (text, { today, tolerant = false } = {}) => firstIn(plainOf(text, tolerant).text, {
|
|
767
|
+
now: todayOf(today),
|
|
768
|
+
isTolerant: tolerant
|
|
769
|
+
})?.span ?? null;
|
|
770
|
+
// src/find.ts
|
|
771
|
+
const BLANK = "";
|
|
772
|
+
const collect = (rest, { search, found }) => {
|
|
773
|
+
const next = firstIn(rest, search);
|
|
774
|
+
if (!next) return found;
|
|
775
|
+
const { span, index, length } = next;
|
|
776
|
+
const { originalAt } = search;
|
|
777
|
+
const end = index + length;
|
|
778
|
+
const blanked = rest.slice(0, index) + BLANK.repeat(length) + rest.slice(end);
|
|
779
|
+
const match = span ? [{
|
|
780
|
+
...span,
|
|
781
|
+
index: originalAt(index),
|
|
782
|
+
length: originalAt(end) - originalAt(index)
|
|
783
|
+
}] : [];
|
|
784
|
+
return collect(blanked, {
|
|
785
|
+
search,
|
|
786
|
+
found: [...found, ...match]
|
|
695
787
|
});
|
|
696
788
|
};
|
|
789
|
+
const findDates = (text, { today, tolerant = false } = {}) => {
|
|
790
|
+
const { text: plain, originalAt } = plainOf(text, tolerant);
|
|
791
|
+
return collect(plain, {
|
|
792
|
+
search: {
|
|
793
|
+
now: todayOf(today),
|
|
794
|
+
isTolerant: tolerant,
|
|
795
|
+
originalAt
|
|
796
|
+
},
|
|
797
|
+
found: []
|
|
798
|
+
}).toSorted((first, second) => first.index - second.index);
|
|
799
|
+
};
|
|
800
|
+
const withoutDates = (text, options = {}) => findDates(text, options).reduceRight((rest, { index, length }) => rest.slice(0, index) + rest.slice(index + length), text);
|
|
697
801
|
// src/parse.ts
|
|
698
802
|
const MONTHS_PER_YEAR = 12;
|
|
699
803
|
const LINK = String.raw`(?:\s*-\s*|\s+(?:i|oraz|lub|do)\s+)`;
|
|
@@ -726,4 +830,4 @@ const parseDate = (text, options = {}) => {
|
|
|
726
830
|
times: text.replaceAll(DASHES, "-").matchAll(times).flatMap(([token]) => clocksOf(token.replaceAll(",", "."))).toArray()
|
|
727
831
|
};
|
|
728
832
|
};
|
|
729
|
-
export { MONTH_PATTERN, POLISH_MONTHS, clockAfterHour, clocksIn, dateOn, dateSpanIn, monthNumber, parseDate };
|
|
833
|
+
export { MONTH_PATTERN, POLISH_MONTHS, clockAfterHour, clocksIn, dateOn, dateSpanIn, findClocks, findDates, firstIn, monthNumber, parseDate, todayOf, withoutDates };
|
package/package.json
CHANGED