polish-dates 0.0.0-stage → 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 +21 -0
- package/README.md +128 -2
- package/dist/index.d.mts +28 -0
- package/dist/index.mjs +729 -0
- package/package.json +68 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Łukasz Wójcik
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,129 @@
|
|
|
1
|
-
#
|
|
1
|
+
# polish-dates
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+

|
|
4
|
+
[](https://ci.appveyor.com/project/lwojcik/polish-dates/branch/main)
|
|
5
|
+
[](https://codecov.io/github/lwojcik/polish-dates)
|
|
6
|
+
|
|
7
|
+
Read dates and clock times written in Polish text, such as `5 i 6 grudnia 2026`, `23–27 listopada 2026`, `17,07.2026` or `godz. 17.30`. It has no dependencies and runs on Node 24 or newer.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm install polish-dates
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Use
|
|
16
|
+
|
|
17
|
+
Paste any text and read the answer with `parseDate`:
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { parseDate } from 'polish-dates';
|
|
21
|
+
|
|
22
|
+
parseDate('W dniach 23–27 listopada 2026 r., godz. 9.00 – 17:30');
|
|
23
|
+
// { start: '2026-11-23', end: '2026-11-27', times: [[9, 0], [17, 30]] }
|
|
24
|
+
|
|
25
|
+
parseDate('jutro o godz. 18:30', { today: '2026-11-18' });
|
|
26
|
+
// { start: '2026-11-19', end: '2026-11-19', times: [[18, 30]] }
|
|
27
|
+
|
|
28
|
+
parseDate('wkrótce'); // { start: null, end: null, times: [] }
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
By default it reads only what is written correctly. Pass `{ tolerant: true }` to forgive the slips of a hasty typist:
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
parseDate('Sroda, 9 grudznia 2026, godz. 18,30', { tolerant: true });
|
|
35
|
+
// { start: '2026-12-09', end: '2026-12-09', times: [[18, 30]] }
|
|
36
|
+
|
|
37
|
+
parseDate('17,07.2026', { tolerant: true }); // { start: '2026-07-17', ... }
|
|
38
|
+
parseDate('17,07.2026'); // { start: null, end: null, times: [] }
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
The functions it is made of can be used on their own:
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
import {
|
|
45
|
+
clockAfterHour,
|
|
46
|
+
clocksIn,
|
|
47
|
+
dateOn,
|
|
48
|
+
dateSpanIn,
|
|
49
|
+
MONTH_PATTERN,
|
|
50
|
+
monthNumber,
|
|
51
|
+
parseDate,
|
|
52
|
+
} from 'polish-dates';
|
|
53
|
+
|
|
54
|
+
monthNumber('listopada'); // 11
|
|
55
|
+
monthNumber('Września'); // 9, in any case
|
|
56
|
+
monthNumber('wrzesień'); // 0, only the genitive is a date's month
|
|
57
|
+
|
|
58
|
+
dateOn({ year: 2026, month: 11, day: 9 }); // '2026-11-09'
|
|
59
|
+
dateOn({ year: 2026, month: 2, day: 30 }); // null, the month has no such day
|
|
60
|
+
|
|
61
|
+
clocksIn('godz. 10.00 -11:30 i 9.05'); // [[10, 0], [11, 30], [9, 5]]
|
|
62
|
+
clocksIn('24.00 12.60'); // [], not times of day
|
|
63
|
+
|
|
64
|
+
clockAfterHour('o godz. 17.30'); // [17, 30]
|
|
65
|
+
clockAfterHour('o 17.30'); // null, no `godz.`
|
|
66
|
+
|
|
67
|
+
dateSpanIn('W dniach 23–27 listopada 2026'); // { start: '2026-11-23', end: '2026-11-27' }
|
|
68
|
+
dateSpanIn('5 i 6 grudnia 2026'); // { start: '2026-12-05', end: '2026-12-06' }
|
|
69
|
+
dateSpanIn('30 listopada - 2 grudnia 2026'); // { start: '2026-11-30', end: '2026-12-02' }
|
|
70
|
+
dateSpanIn('od 23 do 27 listopada 2026'); // { start: '2026-11-23', end: '2026-11-27' }
|
|
71
|
+
dateSpanIn('5, 6 i 7 grudnia 2026'); // { start: '2026-12-05', end: '2026-12-07' }
|
|
72
|
+
dateSpanIn('23.11–27.11.2026'); // { start: '2026-11-23', end: '2026-11-27' }
|
|
73
|
+
dateSpanIn('30 XI – 2 XII 2026'); // { start: '2026-11-30', end: '2026-12-02' }
|
|
74
|
+
dateSpanIn('04.12.2026'); // { start: '2026-12-04', end: '2026-12-04' }
|
|
75
|
+
dateSpanIn('2026-12-05'); // { start: '2026-12-05', end: '2026-12-05' }
|
|
76
|
+
dateSpanIn('5 i 6 grudnia'); // null, a missing year is never guessed
|
|
77
|
+
|
|
78
|
+
const today = { today: '2026-11-18' };
|
|
79
|
+
|
|
80
|
+
dateSpanIn('5 i 6 grudnia', today); // { start: '2026-12-05', end: '2026-12-06' }
|
|
81
|
+
dateSpanIn('17 listopada', today); // { start: '2027-11-17', end: '2027-11-17' }, the next time it comes
|
|
82
|
+
dateSpanIn('17 listopada br.', today); // { start: '2026-11-17', end: '2026-11-17' }
|
|
83
|
+
dateSpanIn('30.12–2.01', today); // { start: '2026-12-30', end: '2027-01-02' }
|
|
84
|
+
dateSpanIn('niedziela 5 grudnia', today); // { start: '2027-12-05', end: '2027-12-05' }, the next year it is a Sunday
|
|
85
|
+
dateSpanIn('pt. 4 – nd. 6 grudnia 2026'); // { start: '2026-12-04', end: '2026-12-06' }
|
|
86
|
+
dateSpanIn('w niedzielę 5 grudnia 2026'); // null, 5 December 2026 is a Saturday
|
|
87
|
+
dateSpanIn('w grudniu 2026'); // { start: '2026-12-01', end: '2026-12-31' }
|
|
88
|
+
dateSpanIn('II połowa grudnia 2026'); // { start: '2026-12-16', end: '2026-12-31' }
|
|
89
|
+
dateSpanIn('koniec listopada', today); // { start: '2026-11-21', end: '2026-11-30' }
|
|
90
|
+
dateSpanIn('5 grud. 2026'); // { start: '2026-12-05', end: '2026-12-05' }
|
|
91
|
+
dateSpanIn('jutro', today); // { start: '2026-11-19', end: '2026-11-19' }
|
|
92
|
+
dateSpanIn('w przyszły piątek', today); // { start: '2026-11-27', end: '2026-11-27' }
|
|
93
|
+
dateSpanIn('w ten weekend', today); // { start: '2026-11-21', end: '2026-11-22' }
|
|
94
|
+
dateSpanIn('za 2 tygodnie', today); // { start: '2026-12-02', end: '2026-12-02' }
|
|
95
|
+
|
|
96
|
+
new RegExp(`(\\d{1,2})\\s+(${MONTH_PATTERN})\\s+(\\d{4})`, 'i');
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
- **`POLISH_MONTHS`** lists the months as they stand in a date, in the genitive (`stycznia`, `lutego`, ...), and `MONTH_PATTERN` joins them for a regular expression.
|
|
100
|
+
- **`monthNumber(name)`** gives 1 to 12, or 0 for a word that is not a month in the genitive.
|
|
101
|
+
- **`dateOn({ year, month, day })`** gives `YYYY-MM-DD`, or `null` for a day that does not exist. It knows the leap years and uses no time zone.
|
|
102
|
+
- **`parseDate(text, { today })`** gives `{ start, end, times }` for any form below: the dates come from `dateSpanIn` and the times from the clock readers. `start` and `end` are `null` when there is no date, and `times` is empty when there is no clock. A time is read after `godz.` (`godz. 18`, and with `tolerant: true` a comma too, `godz. 18,30`) or on its own as `H:MM` or `H.MM` (`o 17.30`, `od 9.00 do 17.30`, `godz. 10.00 -11:30 i 9.05`). A `H.MM` that could as well be a day and a month (`o 9.05`, `5.12`, `od 5.12 do 7.12`) is skipped, unless it is in a chain with a time that cannot be one, as in `od 9.05 do 17.30`; so is a price (`12.50 zł`, `12.50 PLN`, `$12.50`, `12.50 dolarów`; zloty, dollar, euro, pound and franc, with or without the Polish letters, and any code of three capitals such as `SEK` or `NOK`, whatever it is; a code in small letters is not told from a word). A time zone after a number settles that it is a time: `12.50 UTC`, `12.05 GMT` and `9.00 CET` are times, never a price or a date. `clocksIn` reads them all.
|
|
103
|
+
- **`dateSpanIn(text, { today, tolerant })`** finds the first day or range of days in a sentence and gives `{ start, end }` (the same day twice for one day), or `null`. It reads:
|
|
104
|
+
- a range inside a month (`23–27 listopada 2026`, `od 23 do 27 listopada 2026`) and across months (`30 listopada - 2 grudnia 2026`, `od 30 listopada do 2 grudnia 2026`), with any dash a typist may use;
|
|
105
|
+
- a list of days (`5 i 6 grudnia 2026`, `5, 6 i 7 grudnia 2026`), as the span from the first day to the last;
|
|
106
|
+
- a numeric range (`23-27.11.2026`, `23.11–27.11.2026`, `30.11.2025 – 2.12.2026`);
|
|
107
|
+
- one day in words (`9 listopada 2026 r.`), in digits (`4.12.2026`, `04/12/2026`, `5.12.26`, where two digits of a year mean 20xx) or as ISO (`2026-12-05`);
|
|
108
|
+
- a month in Roman numerals (`5 XII 2026`, `23–27 XI 2026`) or abbreviated (`5 gru 2026`, `5 grud. 2026`, `23–27 lis 2026`);
|
|
109
|
+
- 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
|
+
- 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
|
+
|
|
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`.
|
|
113
|
+
|
|
114
|
+
- **`clockAfterHour(text)`** reads the time after `godz.`, `godzina` or `godzinie` (`godz. 17`, `godz. 17.30`) as `[hour, minute]`, or `null`.
|
|
115
|
+
- **`clocksIn(text)`** gives every `H.MM` or `HH:MM` as `[hour, minute]`, in order, and skips what is not a time of day.
|
|
116
|
+
|
|
117
|
+
## Development
|
|
118
|
+
|
|
119
|
+
```sh
|
|
120
|
+
npm ci
|
|
121
|
+
npm test
|
|
122
|
+
npm run typecheck
|
|
123
|
+
npm run lint
|
|
124
|
+
npm run build
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Licence
|
|
128
|
+
|
|
129
|
+
[MIT](LICENSE)
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
export type Clock = [hour: number, minute: number];
|
|
2
|
+
export declare const clocksIn: (text: string) => Clock[];
|
|
3
|
+
export declare const clockAfterHour: (text: string) => Clock | null;
|
|
4
|
+
export interface DateOnOptions {
|
|
5
|
+
year: number;
|
|
6
|
+
month: number;
|
|
7
|
+
day: number;
|
|
8
|
+
}
|
|
9
|
+
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
|
+
interface DateSpan {
|
|
14
|
+
start: string;
|
|
15
|
+
end: string;
|
|
16
|
+
}
|
|
17
|
+
export interface DateSpanOptions {
|
|
18
|
+
today?: string;
|
|
19
|
+
tolerant?: boolean;
|
|
20
|
+
}
|
|
21
|
+
export declare const dateSpanIn: (text: string, { today, tolerant }?: DateSpanOptions) => DateSpan | null;
|
|
22
|
+
export interface ParsedDate {
|
|
23
|
+
start: string | null;
|
|
24
|
+
end: string | null;
|
|
25
|
+
times: Clock[];
|
|
26
|
+
}
|
|
27
|
+
export declare const parseDate: (text: string, options?: DateSpanOptions) => ParsedDate;
|
|
28
|
+
export type { DateSpan };
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,729 @@
|
|
|
1
|
+
// src/clock.ts
|
|
2
|
+
const CLOCK = /(\d{1,2})[.:](\d{2})/g;
|
|
3
|
+
const HOURS_PER_DAY = 24;
|
|
4
|
+
const MINUTES_PER_HOUR = 60;
|
|
5
|
+
const clocksIn = (text) => text.matchAll(CLOCK).map((match) => [+match[1], +match[2]]).filter(([hour, minute]) => hour < HOURS_PER_DAY && minute < MINUTES_PER_HOUR).toArray();
|
|
6
|
+
const HOUR = /godz(?:\.|ina|inie)?\s*(\d{1,2})(?:[.:](\d{2}))?/i;
|
|
7
|
+
const clockAfterHour = (text) => {
|
|
8
|
+
const match = HOUR.exec(text);
|
|
9
|
+
if (!match) return null;
|
|
10
|
+
const clock = [+match[1], +(match[2] ?? 0)];
|
|
11
|
+
return clock[0] < HOURS_PER_DAY && clock[1] < MINUTES_PER_HOUR ? clock : null;
|
|
12
|
+
};
|
|
13
|
+
// src/date.ts
|
|
14
|
+
const MONTHS_PER_YEAR$1 = 12;
|
|
15
|
+
const YEAR_DIGITS = 4;
|
|
16
|
+
const LEAP_CYCLE = 4;
|
|
17
|
+
const CENTURY$1 = 100;
|
|
18
|
+
const LEAP_CENTURY_CYCLE = 400;
|
|
19
|
+
const FEBRUARY = 2;
|
|
20
|
+
const DAYS_IN_MONTH = [
|
|
21
|
+
31,
|
|
22
|
+
28,
|
|
23
|
+
31,
|
|
24
|
+
30,
|
|
25
|
+
31,
|
|
26
|
+
30,
|
|
27
|
+
31,
|
|
28
|
+
31,
|
|
29
|
+
30,
|
|
30
|
+
31,
|
|
31
|
+
30,
|
|
32
|
+
31
|
|
33
|
+
];
|
|
34
|
+
const isLeapYear = (year) => year % LEAP_CYCLE === 0 && year % CENTURY$1 !== 0 || year % LEAP_CENTURY_CYCLE === 0;
|
|
35
|
+
const daysIn$1 = ({ year, month }) => DAYS_IN_MONTH[month - 1] + Number(month === FEBRUARY && isLeapYear(year));
|
|
36
|
+
const pad = (value, length) => String(value).padStart(length, "0");
|
|
37
|
+
const dateOn = ({ year, month, day }) => {
|
|
38
|
+
return Number.isSafeInteger(year) && Number.isSafeInteger(month) && Number.isSafeInteger(day) && year >= 0 && month >= 1 && month <= MONTHS_PER_YEAR$1 && day >= 1 && day <= daysIn$1({
|
|
39
|
+
year,
|
|
40
|
+
month
|
|
41
|
+
}) ? `${pad(year, YEAR_DIGITS)}-${pad(month, 2)}-${pad(day, 2)}` : null;
|
|
42
|
+
};
|
|
43
|
+
// src/months.ts
|
|
44
|
+
const POLISH_MONTHS = [
|
|
45
|
+
"stycznia",
|
|
46
|
+
"lutego",
|
|
47
|
+
"marca",
|
|
48
|
+
"kwietnia",
|
|
49
|
+
"maja",
|
|
50
|
+
"czerwca",
|
|
51
|
+
"lipca",
|
|
52
|
+
"sierpnia",
|
|
53
|
+
"września",
|
|
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;
|
|
62
|
+
// src/zones.ts
|
|
63
|
+
const ZONE_NAMES = "utc|gmt|cest|cet|eest|eet|west|wet|bst|est|edt|cst|cdt|mst|mdt|pst|pdt";
|
|
64
|
+
const ZONE = String.raw`(?:${ZONE_NAMES})(?:[+-]\d{1,2}(?::?\d{2})?)?(?![a-ząćęłńóśźż])`;
|
|
65
|
+
const NOT_ZONE = String.raw`(?!\s*${ZONE})`;
|
|
66
|
+
// src/money.ts
|
|
67
|
+
const LETTER$2 = "a-ząćęłńóśźż";
|
|
68
|
+
const NOT_A_CODE = `${ZONE_NAMES.toUpperCase()}|LUB`;
|
|
69
|
+
const CODE = `(?-i:(?!(?:${NOT_A_CODE})(?![A-Z]))[A-Z]{3})`;
|
|
70
|
+
const WORDS = [
|
|
71
|
+
"z[łl](?:ot(?:y|ych|e|ego))?",
|
|
72
|
+
"pln|usd|eur|euro",
|
|
73
|
+
"dolar(?:y|ów|a|ami)?",
|
|
74
|
+
"funt(?:y|ów|a|ami)?",
|
|
75
|
+
"frank(?:i|ów|a|ami)?",
|
|
76
|
+
"groszy"
|
|
77
|
+
].join("|");
|
|
78
|
+
const NOT_MONEY = String.raw`(?!\s*(?:(?:${WORDS}|${CODE})(?![${LETTER$2}])|[€$£¥%]))`;
|
|
79
|
+
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/fold.ts
|
|
81
|
+
const fold = (text = "") => text.toLowerCase().normalize("NFD").replaceAll(/\p{M}/gu, "").replaceAll("ł", "l");
|
|
82
|
+
const STRICT_DOT = "[./]";
|
|
83
|
+
const TOLERANT_DOT = String.raw`(?:[./]\s?|,)`;
|
|
84
|
+
const CLASS_OR_LETTER = /\[[^\]]*]|[óąćęłńśźż]/gi;
|
|
85
|
+
const loosen = (source) => source.replaceAll(CLASS_OR_LETTER, (part) => part.length > 1 ? part : `[${part}${fold(part)}]`);
|
|
86
|
+
const compile = (source, flags = "i") => ({
|
|
87
|
+
strict: new RegExp(source.replaceAll("", () => STRICT_DOT), flags),
|
|
88
|
+
tolerant: new RegExp(loosen(source.replaceAll("", () => TOLERANT_DOT)), flags)
|
|
89
|
+
});
|
|
90
|
+
const choose = ({ strict, tolerant }, isTolerant) => isTolerant ? tolerant : strict;
|
|
91
|
+
// src/month-forms.ts
|
|
92
|
+
const LETTER$1 = "a-ząćęłńóśźż";
|
|
93
|
+
const ABBREVIATIONS_FROM = 3;
|
|
94
|
+
const TABLE = [
|
|
95
|
+
[
|
|
96
|
+
"stycznia",
|
|
97
|
+
"styczeń",
|
|
98
|
+
"styczniu",
|
|
99
|
+
"sty",
|
|
100
|
+
"stycz"
|
|
101
|
+
],
|
|
102
|
+
[
|
|
103
|
+
"lutego",
|
|
104
|
+
"luty",
|
|
105
|
+
"lutym",
|
|
106
|
+
"lut"
|
|
107
|
+
],
|
|
108
|
+
[
|
|
109
|
+
"marca",
|
|
110
|
+
"marzec",
|
|
111
|
+
"marcu",
|
|
112
|
+
"mar",
|
|
113
|
+
"marz"
|
|
114
|
+
],
|
|
115
|
+
[
|
|
116
|
+
"kwietnia",
|
|
117
|
+
"kwiecień",
|
|
118
|
+
"kwietniu",
|
|
119
|
+
"kwi",
|
|
120
|
+
"kwiet"
|
|
121
|
+
],
|
|
122
|
+
[
|
|
123
|
+
"maja",
|
|
124
|
+
"maj",
|
|
125
|
+
"maju",
|
|
126
|
+
"maj"
|
|
127
|
+
],
|
|
128
|
+
[
|
|
129
|
+
"czerwca",
|
|
130
|
+
"czerwiec",
|
|
131
|
+
"czerwcu",
|
|
132
|
+
"cze",
|
|
133
|
+
"czer",
|
|
134
|
+
"czerw"
|
|
135
|
+
],
|
|
136
|
+
[
|
|
137
|
+
"lipca",
|
|
138
|
+
"lipiec",
|
|
139
|
+
"lipcu",
|
|
140
|
+
"lip"
|
|
141
|
+
],
|
|
142
|
+
[
|
|
143
|
+
"sierpnia",
|
|
144
|
+
"sierpień",
|
|
145
|
+
"sierpniu",
|
|
146
|
+
"sie",
|
|
147
|
+
"sierp"
|
|
148
|
+
],
|
|
149
|
+
[
|
|
150
|
+
"września",
|
|
151
|
+
"wrzesień",
|
|
152
|
+
"wrześniu",
|
|
153
|
+
"wrz",
|
|
154
|
+
"wrzes",
|
|
155
|
+
"wrześ"
|
|
156
|
+
],
|
|
157
|
+
[
|
|
158
|
+
"października",
|
|
159
|
+
"październik",
|
|
160
|
+
"październiku",
|
|
161
|
+
"paź",
|
|
162
|
+
"paźdz"
|
|
163
|
+
],
|
|
164
|
+
[
|
|
165
|
+
"listopada",
|
|
166
|
+
"listopad",
|
|
167
|
+
"listopadzie",
|
|
168
|
+
"lis",
|
|
169
|
+
"listop"
|
|
170
|
+
],
|
|
171
|
+
[
|
|
172
|
+
"grudnia",
|
|
173
|
+
"grudzień",
|
|
174
|
+
"grudniu",
|
|
175
|
+
"gru",
|
|
176
|
+
"grud"
|
|
177
|
+
]
|
|
178
|
+
];
|
|
179
|
+
const alternatives = (forms) => forms.toSorted((first, second) => second.length - first.length).join("|");
|
|
180
|
+
const INDEX = new Map(TABLE.flatMap((forms, index) => forms.map((form) => [fold(form), index + 1])));
|
|
181
|
+
const OTHER_FORMS = alternatives(TABLE.flatMap(([, nominative, locative]) => [nominative, locative]));
|
|
182
|
+
const ABBREVIATIONS = alternatives(TABLE.flatMap((forms) => forms.slice(ABBREVIATIONS_FROM)));
|
|
183
|
+
const monthIndex = (word) => INDEX.get(fold(word.replace(/\.$/, ""))) ?? 0;
|
|
184
|
+
const ABBREVIATION = String.raw`(?:${ABBREVIATIONS})(?![${LETTER$1}])\.?`;
|
|
185
|
+
const GENITIVE_OR_ABBREVIATION = `(?:(?:${MONTH_PATTERN})(?![${LETTER$1}])|${ABBREVIATION})`;
|
|
186
|
+
const NOMINATIVE_OR_LOCATIVE = `(?:${OTHER_FORMS})(?![${LETTER$1}])`;
|
|
187
|
+
const ANY_MONTH = `(?:(?:${MONTH_PATTERN}|${OTHER_FORMS})(?![${LETTER$1}])|${ABBREVIATION})`;
|
|
188
|
+
// src/weekdays.ts
|
|
189
|
+
const LETTERS = "a-ząćęłńóśźż";
|
|
190
|
+
const MONDAY_FIRST = 6;
|
|
191
|
+
const MS_PER_DAY = 864e5;
|
|
192
|
+
const ISO_LENGTH = 10;
|
|
193
|
+
const WEEKDAY_NAMES = [
|
|
194
|
+
[
|
|
195
|
+
"poniedziałek",
|
|
196
|
+
"poniedziałku",
|
|
197
|
+
"poniedziałkiem",
|
|
198
|
+
"pon",
|
|
199
|
+
"pn"
|
|
200
|
+
],
|
|
201
|
+
[
|
|
202
|
+
"wtorek",
|
|
203
|
+
"wtorku",
|
|
204
|
+
"wtorkiem",
|
|
205
|
+
"wt"
|
|
206
|
+
],
|
|
207
|
+
[
|
|
208
|
+
"środa",
|
|
209
|
+
"środę",
|
|
210
|
+
"środy",
|
|
211
|
+
"środzie",
|
|
212
|
+
"środą",
|
|
213
|
+
"śr"
|
|
214
|
+
],
|
|
215
|
+
[
|
|
216
|
+
"czwartek",
|
|
217
|
+
"czwartku",
|
|
218
|
+
"czwartkiem",
|
|
219
|
+
"czw"
|
|
220
|
+
],
|
|
221
|
+
[
|
|
222
|
+
"piątek",
|
|
223
|
+
"piątku",
|
|
224
|
+
"piątkiem",
|
|
225
|
+
"pt"
|
|
226
|
+
],
|
|
227
|
+
[
|
|
228
|
+
"sobota",
|
|
229
|
+
"sobotę",
|
|
230
|
+
"soboty",
|
|
231
|
+
"sobocie",
|
|
232
|
+
"sobotą",
|
|
233
|
+
"sob",
|
|
234
|
+
"sb"
|
|
235
|
+
],
|
|
236
|
+
[
|
|
237
|
+
"niedziela",
|
|
238
|
+
"niedzielę",
|
|
239
|
+
"niedzieli",
|
|
240
|
+
"niedzielą",
|
|
241
|
+
"niedz",
|
|
242
|
+
"ndz",
|
|
243
|
+
"nd"
|
|
244
|
+
]
|
|
245
|
+
];
|
|
246
|
+
const WORD_START = `(?<![${LETTERS}])`;
|
|
247
|
+
const WORD_END = `(?![${LETTERS}])`;
|
|
248
|
+
const WEEKDAY = `${WORD_START}(?:${WEEKDAY_NAMES.flat().toSorted((first, second) => second.length - first.length).join("|")})${WORD_END}`;
|
|
249
|
+
const WEEKDAY_WORD = String.raw`${WEEKDAY}\.?`;
|
|
250
|
+
const FOLDED_NAMES = WEEKDAY_NAMES.map((forms) => forms.map(fold));
|
|
251
|
+
const WEEKDAYS = compile(WEEKDAY, "gi");
|
|
252
|
+
const weekdayOf = (word) => FOLDED_NAMES.findIndex((forms) => forms.includes(fold(word)));
|
|
253
|
+
const weekdaysIn = (text) => text.matchAll(choose(WEEKDAYS, true)).map(([word]) => weekdayOf(word)).toArray();
|
|
254
|
+
const dayOfWeek = (iso) => (new Date(Date.parse(iso)).getUTCDay() + MONDAY_FIRST) % 7;
|
|
255
|
+
const addDays = (iso, days) => new Date(Date.parse(iso) + days * MS_PER_DAY).toISOString().slice(0, ISO_LENGTH);
|
|
256
|
+
const ROMAN = [
|
|
257
|
+
"I",
|
|
258
|
+
"II",
|
|
259
|
+
"III",
|
|
260
|
+
"IV",
|
|
261
|
+
"V",
|
|
262
|
+
"VI",
|
|
263
|
+
"VII",
|
|
264
|
+
"VIII",
|
|
265
|
+
"IX",
|
|
266
|
+
"X",
|
|
267
|
+
"XI",
|
|
268
|
+
"XII"
|
|
269
|
+
];
|
|
270
|
+
const DAY = String.raw`(?<![\d.])\d{1,2}(?!\d)`;
|
|
271
|
+
const MONTH_NUMBER = String.raw`(?:0?[1-9]|1[0-2])(?!\d)`;
|
|
272
|
+
const NUMBER = String.raw`\d{1,2}(?!\d)`;
|
|
273
|
+
const WORD_YEAR = String.raw`\d{4}(?!\d)`;
|
|
274
|
+
const NUMERIC_YEAR = String.raw`(?:\d{4}|\d{2})(?!\d)`;
|
|
275
|
+
const MONTH = `(?:${MONTH_PATTERN}|${ABBREVIATION}|${`${ROMAN.slice(1).toReversed().join("|")}|(?-i:I)`})`;
|
|
276
|
+
const RANGE = String.raw`(?:\s*-\s*|\s+do\s+)`;
|
|
277
|
+
const LIST = String.raw`(?:\s*-\s*|\s+do\s+|\s*,\s*|\s+i\s+)`;
|
|
278
|
+
const DOT_ONLY = String.raw`\.`;
|
|
279
|
+
const QUALIFIER = [
|
|
280
|
+
String.raw`br(?![a-ząćęłńóśźż])\.?`,
|
|
281
|
+
String.raw`(?:w\s+)?(?:tym|przyszłym|zeszłym|ubiegłym)\s+roku`,
|
|
282
|
+
String.raw`(?:bieżącego|przyszłego|zeszłego|ubiegłego)\s+roku`
|
|
283
|
+
].join("|");
|
|
284
|
+
const RELATIVE = String.raw`(?:\s+(?<relative>${QUALIFIER}))?`;
|
|
285
|
+
const END_OF_DAY = String.raw`(?!\.?\d)\.?${NOT_MONEY}${NOT_ZONE}`;
|
|
286
|
+
const AFTER_HOUR = new RegExp(String.raw`godz(?:\.|ina|inie)?\s*\d{1,2}(?:[.:]\d{2})?(?:\s*-\s*\d{1,2}(?:[.:]\d{2})?)?`, "gi");
|
|
287
|
+
const TODAY = /^(\d{4})-(\d{2})-(\d{2})$/;
|
|
288
|
+
const SHIFTS$1 = [[/przyszl/, 1], [/zeszl|ubiegl/, -1]];
|
|
289
|
+
const WEEKDAY_SEPARATOR = String.raw`(?:\s*-\s*|\s+i\s+|\s+do\s+)`;
|
|
290
|
+
const LEAD = String.raw`(?:${WEEKDAY_WORD}(?:${WEEKDAY_SEPARATOR}${WEEKDAY_WORD})*[,\s]*)?`;
|
|
291
|
+
const ITEM = `${LEAD}${DAY}`;
|
|
292
|
+
const WORD_ACROSS = compile(String.raw`(?<lead>${LEAD})(?<d1>${DAY})\s+(?<m1>${MONTH})(?:\s+(?<y1>${WORD_YEAR}))?${RANGE}(?<mid>${LEAD})(?<d2>${DAY})\s+(?<m2>${MONTH})\s+(?<y2>${WORD_YEAR})`);
|
|
293
|
+
const WORD_DAYS = compile(String.raw`(?<days>${ITEM}(?:${LIST}${ITEM})*)\s+(?<m>${MONTH})\s+(?<y>${WORD_YEAR})`);
|
|
294
|
+
const NUMERIC_ACROSS = compile(`(?<lead>${LEAD})(?<d1>${DAY})(?<m1>${NUMBER})(?:(?<y1>${NUMERIC_YEAR}))?${RANGE}(?<mid>${LEAD})(?<d2>${DAY})(?<m2>${NUMBER})(?<y2>${NUMERIC_YEAR})`);
|
|
295
|
+
const NUMERIC_DAYS = compile(`(?<days>${ITEM}(?:${RANGE}${ITEM})+)(?<m>${NUMBER})(?<y>${NUMERIC_YEAR})`);
|
|
296
|
+
const NUMERIC = compile(`(?<lead>${LEAD})(?<d>${DAY})(?<m>${NUMBER})(?<y>${NUMERIC_YEAR})`);
|
|
297
|
+
const WORD_ACROSS_UNDATED = compile(String.raw`(?<lead>${LEAD})(?<d1>${DAY})\s+(?<m1>${MONTH})${RANGE}(?<mid>${LEAD})(?<d2>${DAY})\s+(?<m2>${MONTH})${RELATIVE}`);
|
|
298
|
+
const WORD_DAYS_UNDATED = compile(String.raw`(?<days>${ITEM}(?:${LIST}${ITEM})*)\s+(?<m>${MONTH})${RELATIVE}`);
|
|
299
|
+
const NUMERIC_ACROSS_UNDATED = compile(`(?<lead>${LEAD})(?<d1>${DAY})${DOT_ONLY}(?<m1>${MONTH_NUMBER})${RANGE}(?<mid>${LEAD})(?<d2>${DAY})${DOT_ONLY}(?<m2>${MONTH_NUMBER})${END_OF_DAY}${RELATIVE}`);
|
|
300
|
+
const NUMERIC_DAYS_UNDATED = compile(`(?<days>${ITEM}(?:${RANGE}${ITEM})+)${DOT_ONLY}(?<m>${MONTH_NUMBER})${END_OF_DAY}${RELATIVE}`);
|
|
301
|
+
const NUMERIC_UNDATED = compile(`(?<lead>${LEAD})${NOT_AFTER_MONEY}(?<d>${DAY})${DOT_ONLY}(?<m>${MONTH_NUMBER})${END_OF_DAY}${RELATIVE}`);
|
|
302
|
+
const NUMERIC_DASHED = compile(String.raw`(?<lead>${LEAD})(?<![\d.-])(?<d>\d{1,2})-(?<m>\d{1,2})-(?<y>\d{4})(?!\d)`);
|
|
303
|
+
const ISO = compile(String.raw`(?<lead>${LEAD})(?<![\d-])(?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})(?!\d)`);
|
|
304
|
+
const NOT_MID_WORD = String.raw`(?<![a-ząćęłńóśźż])(?<!\d[.\s]*)`;
|
|
305
|
+
const MONTHS_LINK = String.raw`(?:\s*-\s*|\s+do\s+|\s+i\s+)`;
|
|
306
|
+
const HALF = String.raw`(?<half>(?-i:II|I)|pierwsz(?:a|ej|ą)|drug(?:a|iej|ą))\s+połow(?:a|ie|y|ę|ą)`;
|
|
307
|
+
const PART_OF = String.raw`(?<![a-ząćęłńóśźż])(?:${HALF}|${"(?<part>początek|początku|początkiem|koniec|końca|końcu|końcem|środek|środka|środku|połowa|połowie|połowy)"})\s+(?<m>${GENITIVE_OR_ABBREVIATION})`;
|
|
308
|
+
const WHOLE_MONTH = compile(String.raw`${NOT_MID_WORD}(?<m>${ANY_MONTH})\s+(?<y>${WORD_YEAR})`);
|
|
309
|
+
const MONTHS_ACROSS = compile(String.raw`${NOT_MID_WORD}(?<m1>${ANY_MONTH})(?:\s+(?<y1>${WORD_YEAR}))?${MONTHS_LINK}(?<m2>${ANY_MONTH})\s+(?<y2>${WORD_YEAR})`);
|
|
310
|
+
const PART_OF_MONTH = compile(String.raw`${PART_OF}\s+(?<y>${WORD_YEAR})`);
|
|
311
|
+
const WHOLE_MONTH_UNDATED = compile(`${NOT_MID_WORD}(?<m>${NOMINATIVE_OR_LOCATIVE})${RELATIVE}`);
|
|
312
|
+
const MONTHS_ACROSS_UNDATED = compile(`${NOT_MID_WORD}(?<m1>${ANY_MONTH})${MONTHS_LINK}(?<m2>${ANY_MONTH})${RELATIVE}`);
|
|
313
|
+
const PART_OF_MONTH_UNDATED = compile(`${PART_OF}${RELATIVE}`);
|
|
314
|
+
// src/relative.ts
|
|
315
|
+
const DAY_WORDS = {
|
|
316
|
+
przedwczoraj: -2,
|
|
317
|
+
wczoraj: -1,
|
|
318
|
+
dziś: 0,
|
|
319
|
+
dzisiaj: 0,
|
|
320
|
+
jutro: 1,
|
|
321
|
+
pojutrze: 2
|
|
322
|
+
};
|
|
323
|
+
const NUMBER_WORDS = {
|
|
324
|
+
dwa: 2,
|
|
325
|
+
dwie: 2,
|
|
326
|
+
trzy: 3,
|
|
327
|
+
cztery: 4,
|
|
328
|
+
pięć: 5,
|
|
329
|
+
sześć: 6,
|
|
330
|
+
siedem: 7,
|
|
331
|
+
osiem: 8,
|
|
332
|
+
dziewięć: 9,
|
|
333
|
+
dziesięć: 10
|
|
334
|
+
};
|
|
335
|
+
const SHIFTS = [[/przyszl|nastepn/, 1], [/zeszl|ubiegl|poprzedni/, -1]];
|
|
336
|
+
const COUNT = String.raw`(?:(?<count>\d{1,3}|${Object.keys(NUMBER_WORDS).join("|")})\s+)?`;
|
|
337
|
+
const UNIT = "(?<unit>dzień|dni|tydzień|tygodnie|tygodni)";
|
|
338
|
+
const WHICH = "(?:ten|tym|tę|tą|najbliższ(?:y|ym|ą)|przyszł(?:y|ym|ą)|następn(?:y|ym|ą)|zeszł(?:y|ym|ą)|ubiegł(?:y|ym|ą)|poprzedni(?:m)?)";
|
|
339
|
+
const SEPARATOR = String.raw`(?:\s+do\s+|\s+i\s+|\s*-\s*)`;
|
|
340
|
+
const valueOf = (table, { word, fallback }) => Object.entries(table).find(([key]) => fold(key) === fold(word))?.[1] ?? fallback;
|
|
341
|
+
const one = (date) => ({
|
|
342
|
+
start: date,
|
|
343
|
+
end: date
|
|
344
|
+
});
|
|
345
|
+
const shiftOf$1 = (which = "") => SHIFTS.find(([pattern]) => pattern.test(fold(which)))?.[1] ?? 0;
|
|
346
|
+
const upcoming = (today, weekday) => addDays(today, (weekday - dayOfWeek(today) + 7) % 7 || 7);
|
|
347
|
+
const inWeek = ({ today, weekday, shift }) => addDays(today, weekday - dayOfWeek(today) + shift * 7);
|
|
348
|
+
const READERS$1 = [
|
|
349
|
+
{
|
|
350
|
+
pattern: compile(`${WORD_START}(?:(?<which>${WHICH})\\s+)?weekend(?:u|zie)?${WORD_END}`),
|
|
351
|
+
read: ({ which }, today) => {
|
|
352
|
+
const day = dayOfWeek(today);
|
|
353
|
+
const saturday = addDays(today, day === 6 ? -1 : 5 - day);
|
|
354
|
+
const start = addDays(saturday, shiftOf$1(which) * 7);
|
|
355
|
+
return {
|
|
356
|
+
start,
|
|
357
|
+
end: addDays(start, 1)
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
},
|
|
361
|
+
{
|
|
362
|
+
pattern: compile(String.raw`${WORD_START}(?:w|we|od)\s+(?<first>${WEEKDAY})${SEPARATOR}(?<last>${WEEKDAY})`),
|
|
363
|
+
read: ({ first = "", last = "" }, today) => {
|
|
364
|
+
const start = upcoming(today, weekdayOf(first));
|
|
365
|
+
return {
|
|
366
|
+
start,
|
|
367
|
+
end: addDays(start, (weekdayOf(last) - weekdayOf(first) + 7) % 7)
|
|
368
|
+
};
|
|
369
|
+
}
|
|
370
|
+
},
|
|
371
|
+
{
|
|
372
|
+
pattern: compile(String.raw`${WORD_START}(?:w|we|na)\s+(?:(?<which>${WHICH})\s+)?(?<weekday>${WEEKDAY})`),
|
|
373
|
+
read: ({ which, weekday = "" }, today) => {
|
|
374
|
+
const shift = shiftOf$1(which);
|
|
375
|
+
return one(shift ? inWeek({
|
|
376
|
+
today,
|
|
377
|
+
weekday: weekdayOf(weekday),
|
|
378
|
+
shift
|
|
379
|
+
}) : upcoming(today, weekdayOf(weekday)));
|
|
380
|
+
}
|
|
381
|
+
},
|
|
382
|
+
{
|
|
383
|
+
pattern: compile(String.raw`${WORD_START}za\s+${COUNT}${UNIT}${WORD_END}`),
|
|
384
|
+
read: (groups, today) => one(addDays(today, offsetOf(groups)))
|
|
385
|
+
},
|
|
386
|
+
{
|
|
387
|
+
pattern: compile(String.raw`${WORD_START}${COUNT}${UNIT}\s+temu${WORD_END}`),
|
|
388
|
+
read: (groups, today) => one(addDays(today, -offsetOf(groups)))
|
|
389
|
+
},
|
|
390
|
+
{
|
|
391
|
+
pattern: compile(`${WORD_START}(?<word>${Object.keys(DAY_WORDS).join("|")})${WORD_END}`),
|
|
392
|
+
read: ({ word = "" }, today) => one(addDays(today, valueOf(DAY_WORDS, {
|
|
393
|
+
word,
|
|
394
|
+
fallback: 0
|
|
395
|
+
})))
|
|
396
|
+
}
|
|
397
|
+
];
|
|
398
|
+
function offsetOf({ count, unit = "" }) {
|
|
399
|
+
return (count ? valueOf(NUMBER_WORDS, {
|
|
400
|
+
word: count,
|
|
401
|
+
fallback: Number(count)
|
|
402
|
+
}) : 1) * (/^t/i.test(unit) ? 7 : 1);
|
|
403
|
+
}
|
|
404
|
+
const relativeSpanIn = (text, { today, isTolerant }) => {
|
|
405
|
+
const reader = READERS$1.find(({ pattern }) => choose(pattern, isTolerant).test(text));
|
|
406
|
+
const groups = reader && choose(reader.pattern, isTolerant).exec(text)?.groups;
|
|
407
|
+
return reader && groups ? reader.read(groups, today) : null;
|
|
408
|
+
};
|
|
409
|
+
// src/spans.ts
|
|
410
|
+
const CENTURY = 2e3;
|
|
411
|
+
const monthOf = (word = "") => {
|
|
412
|
+
if (/^\d+$/.test(word)) return Number(word);
|
|
413
|
+
return monthIndex(word) || ROMAN.indexOf(word.toUpperCase()) + 1;
|
|
414
|
+
};
|
|
415
|
+
const yearOf = (year = "") => year.length === 2 ? CENTURY + Number(year) : Number(year);
|
|
416
|
+
const spanOf = (dates) => {
|
|
417
|
+
const valid = dates.filter((date) => date !== null);
|
|
418
|
+
const [start] = valid;
|
|
419
|
+
const end = valid.at(-1);
|
|
420
|
+
const isOrdered = valid.every((date, index) => date >= (valid[index - 1] ?? date));
|
|
421
|
+
return valid.length === dates.length && isOrdered && start && end ? {
|
|
422
|
+
start,
|
|
423
|
+
end
|
|
424
|
+
} : null;
|
|
425
|
+
};
|
|
426
|
+
// src/tolerance.ts
|
|
427
|
+
const LETTER = "a-ząćęłńóśźż";
|
|
428
|
+
const MONTHS = loosen(MONTH_PATTERN);
|
|
429
|
+
const WORD_AFTER_NUMBER = new RegExp(`(?<=\\d\\.?\\s{0,3})[${LETTER}]{5,}(?![${LETTER}])`, "gi");
|
|
430
|
+
const BEFORE_MONTH = new RegExp(String.raw`(\d)\.?\s*(?=(?:${MONTHS})(?![${LETTER}]))`, "gi");
|
|
431
|
+
const AFTER_MONTH = new RegExp(String.raw`(?<![${LETTER}])(${MONTHS})(?=\d)`, "gi");
|
|
432
|
+
const isNear = (word, month) => {
|
|
433
|
+
const prefix = [...word].findIndex((letter, index) => letter !== month[index]);
|
|
434
|
+
const at = prefix === -1 ? word.length : prefix;
|
|
435
|
+
const gap = word.length - month.length;
|
|
436
|
+
const rest = {
|
|
437
|
+
word: word.slice(at),
|
|
438
|
+
month: month.slice(at)
|
|
439
|
+
};
|
|
440
|
+
return gap === 0 && (rest.word.slice(1) === rest.month.slice(1) || rest.word[0] === rest.month[1] && rest.word[1] === rest.month[0] && rest.word.slice(2) === rest.month.slice(2)) || gap === 1 && rest.word.slice(1) === rest.month || gap === -1 && rest.word === rest.month.slice(1);
|
|
441
|
+
};
|
|
442
|
+
const correct = (word) => {
|
|
443
|
+
const folded = fold(word);
|
|
444
|
+
const near = POLISH_MONTHS.filter((month) => isNear(folded, fold(month)));
|
|
445
|
+
const [only = word] = near;
|
|
446
|
+
return near.length === 1 ? only : word;
|
|
447
|
+
};
|
|
448
|
+
const tidy = (text) => text.normalize("NFC").replaceAll(WORD_AFTER_NUMBER, (word) => correct(word)).replaceAll(BEFORE_MONTH, (match, digit) => `${digit} `).replaceAll(AFTER_MONTH, (match, month) => `${month} `);
|
|
449
|
+
// src/whole-month.ts
|
|
450
|
+
const LAST_DAYS = [
|
|
451
|
+
31,
|
|
452
|
+
30,
|
|
453
|
+
29,
|
|
454
|
+
28
|
|
455
|
+
];
|
|
456
|
+
const PARTS = [
|
|
457
|
+
[
|
|
458
|
+
/^(?:i$|pierwsz)/,
|
|
459
|
+
1,
|
|
460
|
+
15
|
|
461
|
+
],
|
|
462
|
+
[
|
|
463
|
+
/^(?:ii$|drug)/,
|
|
464
|
+
16,
|
|
465
|
+
null
|
|
466
|
+
],
|
|
467
|
+
[
|
|
468
|
+
/^pocz/,
|
|
469
|
+
1,
|
|
470
|
+
10
|
|
471
|
+
],
|
|
472
|
+
[
|
|
473
|
+
/^(?:srod|polow)/,
|
|
474
|
+
11,
|
|
475
|
+
20
|
|
476
|
+
],
|
|
477
|
+
[
|
|
478
|
+
/^kon/,
|
|
479
|
+
21,
|
|
480
|
+
null
|
|
481
|
+
]
|
|
482
|
+
];
|
|
483
|
+
const lastDayOf = (year, month) => Math.max(0, ...LAST_DAYS.filter((day) => dateOn({
|
|
484
|
+
year,
|
|
485
|
+
month,
|
|
486
|
+
day
|
|
487
|
+
})));
|
|
488
|
+
const endOf = (year, month) => dateOn({
|
|
489
|
+
year,
|
|
490
|
+
month,
|
|
491
|
+
day: lastDayOf(year, month)
|
|
492
|
+
});
|
|
493
|
+
const startOf = (year, month) => dateOn({
|
|
494
|
+
year,
|
|
495
|
+
month,
|
|
496
|
+
day: 1
|
|
497
|
+
});
|
|
498
|
+
const wholeIn = (groups) => (year) => {
|
|
499
|
+
const month = monthOf(groups.m);
|
|
500
|
+
return spanOf([startOf(year, month), endOf(year, month)]);
|
|
501
|
+
};
|
|
502
|
+
const partIn = (groups) => (year) => {
|
|
503
|
+
const month = monthOf(groups.m);
|
|
504
|
+
const word = fold(groups.half ?? groups.part);
|
|
505
|
+
const [[, from = 1, to = null] = []] = PARTS.filter(([pattern]) => pattern.test(word));
|
|
506
|
+
return spanOf([dateOn({
|
|
507
|
+
year,
|
|
508
|
+
month,
|
|
509
|
+
day: from
|
|
510
|
+
}), to === null ? endOf(year, month) : dateOn({
|
|
511
|
+
year,
|
|
512
|
+
month,
|
|
513
|
+
day: to
|
|
514
|
+
})]);
|
|
515
|
+
};
|
|
516
|
+
const monthsAcrossIn = (groups) => (year) => {
|
|
517
|
+
const first = monthOf(groups.m1);
|
|
518
|
+
const last = monthOf(groups.m2);
|
|
519
|
+
const endYear = last < first ? year + 1 : year;
|
|
520
|
+
return spanOf([startOf(year, first), endOf(endYear, last)]);
|
|
521
|
+
};
|
|
522
|
+
const readWhole = (groups) => wholeIn(groups)(yearOf(groups.y));
|
|
523
|
+
const readPart = (groups) => partIn(groups)(yearOf(groups.y));
|
|
524
|
+
const readMonths = (groups) => {
|
|
525
|
+
const endYear = yearOf(groups.y2);
|
|
526
|
+
const startYear = groups.y1 ? yearOf(groups.y1) : endYear;
|
|
527
|
+
return spanOf([startOf(startYear, monthOf(groups.m1)), endOf(endYear, monthOf(groups.m2))]);
|
|
528
|
+
};
|
|
529
|
+
// src/span.ts
|
|
530
|
+
const SEARCH_YEARS = 9;
|
|
531
|
+
const digitsIn = (text = "") => Array.from(text.matchAll(/\d+/g), ([digits]) => digits);
|
|
532
|
+
const weekdaysOf = ({ lead, mid, days }) => weekdaysIn([
|
|
533
|
+
lead,
|
|
534
|
+
mid,
|
|
535
|
+
days
|
|
536
|
+
].join(" "));
|
|
537
|
+
const isFitting = (weekdays, { start, end }) => weekdays.length === 0 || weekdays[0] === dayOfWeek(start) && (weekdays.length === 1 || weekdays.at(-1) === dayOfWeek(end));
|
|
538
|
+
const checked = (span, groups) => span && isFitting(weekdaysOf(groups), span) ? span : null;
|
|
539
|
+
const readAcross = (groups) => {
|
|
540
|
+
const end = yearOf(groups.y2);
|
|
541
|
+
const start = groups.y1 ? yearOf(groups.y1) : end;
|
|
542
|
+
return checked(spanOf([dateOn({
|
|
543
|
+
year: start,
|
|
544
|
+
month: monthOf(groups.m1),
|
|
545
|
+
day: Number(groups.d1)
|
|
546
|
+
}), dateOn({
|
|
547
|
+
year: end,
|
|
548
|
+
month: monthOf(groups.m2),
|
|
549
|
+
day: Number(groups.d2)
|
|
550
|
+
})]), groups);
|
|
551
|
+
};
|
|
552
|
+
const daysIn = (groups) => (year) => spanOf(digitsIn(groups.days).map((day) => dateOn({
|
|
553
|
+
year,
|
|
554
|
+
month: monthOf(groups.m),
|
|
555
|
+
day: Number(day)
|
|
556
|
+
})));
|
|
557
|
+
const oneIn = (groups) => (year) => spanOf([dateOn({
|
|
558
|
+
year,
|
|
559
|
+
month: monthOf(groups.m),
|
|
560
|
+
day: Number(groups.d)
|
|
561
|
+
})]);
|
|
562
|
+
const acrossIn = (groups) => (year) => {
|
|
563
|
+
const first = monthOf(groups.m1);
|
|
564
|
+
const last = monthOf(groups.m2);
|
|
565
|
+
return spanOf([dateOn({
|
|
566
|
+
year,
|
|
567
|
+
month: first,
|
|
568
|
+
day: Number(groups.d1)
|
|
569
|
+
}), dateOn({
|
|
570
|
+
year: last < first ? year + 1 : year,
|
|
571
|
+
month: last,
|
|
572
|
+
day: Number(groups.d2)
|
|
573
|
+
})]);
|
|
574
|
+
};
|
|
575
|
+
const readDays = (groups) => checked(daysIn(groups)(yearOf(groups.y)), groups);
|
|
576
|
+
const readOne = (groups) => checked(oneIn(groups)(yearOf(groups.y)), groups);
|
|
577
|
+
const READERS = [
|
|
578
|
+
{
|
|
579
|
+
pattern: WORD_ACROSS,
|
|
580
|
+
read: readAcross
|
|
581
|
+
},
|
|
582
|
+
{
|
|
583
|
+
pattern: WORD_DAYS,
|
|
584
|
+
read: readDays
|
|
585
|
+
},
|
|
586
|
+
{
|
|
587
|
+
pattern: NUMERIC_ACROSS,
|
|
588
|
+
read: readAcross
|
|
589
|
+
},
|
|
590
|
+
{
|
|
591
|
+
pattern: NUMERIC_DAYS,
|
|
592
|
+
read: readDays
|
|
593
|
+
},
|
|
594
|
+
{
|
|
595
|
+
pattern: NUMERIC,
|
|
596
|
+
read: readOne
|
|
597
|
+
},
|
|
598
|
+
{
|
|
599
|
+
pattern: ISO,
|
|
600
|
+
read: readOne
|
|
601
|
+
},
|
|
602
|
+
{
|
|
603
|
+
pattern: NUMERIC_DASHED,
|
|
604
|
+
read: readOne,
|
|
605
|
+
isTolerantOnly: true
|
|
606
|
+
},
|
|
607
|
+
{
|
|
608
|
+
pattern: MONTHS_ACROSS,
|
|
609
|
+
read: readMonths
|
|
610
|
+
},
|
|
611
|
+
{
|
|
612
|
+
pattern: PART_OF_MONTH,
|
|
613
|
+
read: readPart
|
|
614
|
+
},
|
|
615
|
+
{
|
|
616
|
+
pattern: WHOLE_MONTH,
|
|
617
|
+
read: readWhole
|
|
618
|
+
}
|
|
619
|
+
];
|
|
620
|
+
const UNDATED = [
|
|
621
|
+
{
|
|
622
|
+
pattern: WORD_ACROSS_UNDATED,
|
|
623
|
+
build: acrossIn
|
|
624
|
+
},
|
|
625
|
+
{
|
|
626
|
+
pattern: WORD_DAYS_UNDATED,
|
|
627
|
+
build: daysIn
|
|
628
|
+
},
|
|
629
|
+
{
|
|
630
|
+
pattern: NUMERIC_ACROSS_UNDATED,
|
|
631
|
+
build: acrossIn
|
|
632
|
+
},
|
|
633
|
+
{
|
|
634
|
+
pattern: NUMERIC_DAYS_UNDATED,
|
|
635
|
+
build: daysIn
|
|
636
|
+
},
|
|
637
|
+
{
|
|
638
|
+
pattern: NUMERIC_UNDATED,
|
|
639
|
+
build: oneIn
|
|
640
|
+
},
|
|
641
|
+
{
|
|
642
|
+
pattern: MONTHS_ACROSS_UNDATED,
|
|
643
|
+
build: monthsAcrossIn
|
|
644
|
+
},
|
|
645
|
+
{
|
|
646
|
+
pattern: PART_OF_MONTH_UNDATED,
|
|
647
|
+
build: partIn
|
|
648
|
+
},
|
|
649
|
+
{
|
|
650
|
+
pattern: WHOLE_MONTH_UNDATED,
|
|
651
|
+
build: wholeIn
|
|
652
|
+
}
|
|
653
|
+
];
|
|
654
|
+
const firstOf = (readers, { text, isTolerant }) => {
|
|
655
|
+
const reader = readers.find(({ pattern, isTolerantOnly }) => (isTolerant || !isTolerantOnly) && choose(pattern, isTolerant).test(text));
|
|
656
|
+
const groups = reader && choose(reader.pattern, isTolerant).exec(text)?.groups;
|
|
657
|
+
return reader && groups ? {
|
|
658
|
+
reader,
|
|
659
|
+
groups
|
|
660
|
+
} : void 0;
|
|
661
|
+
};
|
|
662
|
+
const todayOf = (today = "") => {
|
|
663
|
+
const [, year, month, day] = TODAY.exec(today) ?? [];
|
|
664
|
+
const iso = dateOn({
|
|
665
|
+
year: Number(year),
|
|
666
|
+
month: Number(month),
|
|
667
|
+
day: Number(day)
|
|
668
|
+
});
|
|
669
|
+
return iso ? {
|
|
670
|
+
iso,
|
|
671
|
+
year: Number(year)
|
|
672
|
+
} : null;
|
|
673
|
+
};
|
|
674
|
+
const shiftOf = (relative) => SHIFTS$1.find(([shift]) => shift.test(fold(relative)))?.[1] ?? 0;
|
|
675
|
+
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 dateSpanIn = (text, { today, tolerant = false } = {}) => {
|
|
677
|
+
const plain = (tolerant ? tidy(text) : text).replaceAll(DASHES, "-");
|
|
678
|
+
const dated = firstOf(READERS, {
|
|
679
|
+
text: plain,
|
|
680
|
+
isTolerant: tolerant
|
|
681
|
+
});
|
|
682
|
+
if (dated) return dated.reader.read(dated.groups);
|
|
683
|
+
const now = todayOf(today);
|
|
684
|
+
if (!now) return null;
|
|
685
|
+
const undated = firstOf(UNDATED, {
|
|
686
|
+
text: plain.replaceAll(AFTER_HOUR, " "),
|
|
687
|
+
isTolerant: tolerant
|
|
688
|
+
});
|
|
689
|
+
return undated ? resolve({
|
|
690
|
+
build: undated.reader.build(undated.groups),
|
|
691
|
+
groups: undated.groups
|
|
692
|
+
}, now) : relativeSpanIn(plain, {
|
|
693
|
+
today: now.iso,
|
|
694
|
+
isTolerant: tolerant
|
|
695
|
+
});
|
|
696
|
+
};
|
|
697
|
+
// src/parse.ts
|
|
698
|
+
const MONTHS_PER_YEAR = 12;
|
|
699
|
+
const LINK = String.raw`(?:\s*-\s*|\s+(?:i|oraz|lub|do)\s+)`;
|
|
700
|
+
const NOT_PART_OF_DATE = String.raw`(?!\d|[.:]\d)`;
|
|
701
|
+
const ZONE_AFTER = new RegExp(`${ZONE}$`, "i");
|
|
702
|
+
const CLOCK_PARTS = /\d{1,2}(?<separator>[.:])(?<minute>\d{2})/g;
|
|
703
|
+
const timesAt = (separator) => {
|
|
704
|
+
const after = String.raw`\d{1,2}${separator}\d{2}(?!\d)`;
|
|
705
|
+
const bare = String.raw`\d{1,2}[.:]\d{2}${NOT_PART_OF_DATE}`;
|
|
706
|
+
return new RegExp(String.raw`godz(?:\.|ina|inie)?\s*\d{1,2}(?:${separator}\d{2})?(?:${LINK}${after})*|(?<![\d.:,])${NOT_AFTER_MONEY}${bare}${NOT_MONEY}(?:${LINK}${bare})*(?:\s*${ZONE})?`, "gi");
|
|
707
|
+
};
|
|
708
|
+
const STRICT_TIMES = timesAt("[.:]");
|
|
709
|
+
const TOLERANT_TIMES = timesAt("[.:,]");
|
|
710
|
+
const isTimeOfDay = (token) => ZONE_AFTER.test(token) || token.matchAll(CLOCK_PARTS).some(({ groups }) => {
|
|
711
|
+
const minute = Number(groups?.minute);
|
|
712
|
+
return groups?.separator === ":" || !(minute > 0 && minute <= MONTHS_PER_YEAR);
|
|
713
|
+
});
|
|
714
|
+
const clocksOf = (token) => {
|
|
715
|
+
const clocks = clocksIn(token);
|
|
716
|
+
const hour = clockAfterHour(token);
|
|
717
|
+
if (/^godz/i.test(token)) return clocks.length === 0 && hour ? [hour] : clocks;
|
|
718
|
+
return isTimeOfDay(token) ? clocks : [];
|
|
719
|
+
};
|
|
720
|
+
const parseDate = (text, options = {}) => {
|
|
721
|
+
const span = dateSpanIn(text, options);
|
|
722
|
+
const times = options.tolerant ? TOLERANT_TIMES : STRICT_TIMES;
|
|
723
|
+
return {
|
|
724
|
+
start: span?.start ?? null,
|
|
725
|
+
end: span?.end ?? null,
|
|
726
|
+
times: text.replaceAll(DASHES, "-").matchAll(times).flatMap(([token]) => clocksOf(token.replaceAll(",", "."))).toArray()
|
|
727
|
+
};
|
|
728
|
+
};
|
|
729
|
+
export { MONTH_PATTERN, POLISH_MONTHS, clockAfterHour, clocksIn, dateOn, dateSpanIn, monthNumber, parseDate };
|
package/package.json
CHANGED
|
@@ -1,6 +1,70 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "polish-dates",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Read dates and clock times written in Polish text: \"5 i 6 grudnia 2026\", \"23–27 listopada 2026\", \"17,07.2026\", \"godz. 17.30\".",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"polish",
|
|
7
|
+
"poland",
|
|
8
|
+
"dates",
|
|
9
|
+
"date-parser",
|
|
10
|
+
"parser",
|
|
11
|
+
"text",
|
|
12
|
+
"nlp",
|
|
13
|
+
"pl"
|
|
14
|
+
],
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"author": "Łukasz Wójcik",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "git+https://github.com/lwojcik/polish-dates.git"
|
|
20
|
+
},
|
|
21
|
+
"bugs": "https://github.com/lwojcik/polish-dates/issues",
|
|
22
|
+
"homepage": "https://github.com/lwojcik/polish-dates#readme",
|
|
23
|
+
"type": "module",
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=24"
|
|
26
|
+
},
|
|
27
|
+
"files": [
|
|
28
|
+
"dist"
|
|
29
|
+
],
|
|
30
|
+
"main": "./dist/index.mjs",
|
|
31
|
+
"types": "./dist/index.d.mts",
|
|
32
|
+
"exports": {
|
|
33
|
+
".": {
|
|
34
|
+
"types": "./dist/index.d.mts",
|
|
35
|
+
"import": "./dist/index.mjs"
|
|
36
|
+
},
|
|
37
|
+
"./package.json": "./package.json"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"prepare": "husky",
|
|
41
|
+
"commit": "czg",
|
|
42
|
+
"build": "tsdown",
|
|
43
|
+
"typecheck": "tsc --noEmit",
|
|
44
|
+
"lint": "eslint .",
|
|
45
|
+
"format": "prettier --write .",
|
|
46
|
+
"format:check": "prettier --check .",
|
|
47
|
+
"test": "TZ=UTC vitest run",
|
|
48
|
+
"test:coverage": "TZ=UTC vitest run --coverage",
|
|
49
|
+
"prepublishOnly": "npm run build",
|
|
50
|
+
"release": "HUSKY=0 GITHUB_TOKEN=$(gh auth token) release-it --no-increment --no-git.commit",
|
|
51
|
+
"release:prepare": "scripts/release-prepare.sh"
|
|
52
|
+
},
|
|
53
|
+
"devDependencies": {
|
|
54
|
+
"@commitlint/cli": "21.2.4",
|
|
55
|
+
"@commitlint/config-conventional": "21.2.4",
|
|
56
|
+
"@lwojcik/eslint-config": "0.2.0",
|
|
57
|
+
"@release-it/conventional-changelog": "^12.0.2",
|
|
58
|
+
"@types/node": "26.6.6",
|
|
59
|
+
"@vitest/coverage-v8": "5.1.0",
|
|
60
|
+
"czg": "1.14.0",
|
|
61
|
+
"eslint": "^10.15.0",
|
|
62
|
+
"husky": "9.1.7",
|
|
63
|
+
"prettier": "3.10.1",
|
|
64
|
+
"release-it": "^21.1.2",
|
|
65
|
+
"tsdown": "0.23.7",
|
|
66
|
+
"typescript": "6.0.3",
|
|
67
|
+
"typescript-eslint": "^8.73.0",
|
|
68
|
+
"vitest": "5.1.0"
|
|
69
|
+
}
|
|
70
|
+
}
|