chrono-node 2.8.2 → 2.8.4
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 +1 -1
- package/dist/cjs/calculation/duration.d.ts +8 -0
- package/dist/cjs/calculation/duration.js +124 -0
- package/dist/cjs/calculation/duration.js.map +1 -0
- package/dist/cjs/{common/calculation → calculation}/weekdays.d.ts +2 -2
- package/dist/cjs/{common/calculation → calculation}/weekdays.js +3 -3
- package/dist/cjs/calculation/weekdays.js.map +1 -0
- package/dist/cjs/chrono.js +1 -1
- package/dist/cjs/chrono.js.map +1 -1
- package/dist/cjs/common/casualReferences.js +1 -1
- package/dist/cjs/common/casualReferences.js.map +1 -1
- package/dist/cjs/common/refiners/ForwardDateRefiner.js.map +1 -1
- package/dist/cjs/common/refiners/UnlikelyFormatFilter.js +0 -6
- package/dist/cjs/common/refiners/UnlikelyFormatFilter.js.map +1 -1
- package/dist/cjs/index.d.ts +2 -2
- package/dist/cjs/index.js +2 -1
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/locales/de/parsers/DEWeekdayParser.js +1 -1
- package/dist/cjs/locales/de/parsers/DEWeekdayParser.js.map +1 -1
- package/dist/cjs/locales/en/parsers/ENTimeUnitAgoFormatParser.js +4 -5
- package/dist/cjs/locales/en/parsers/ENTimeUnitAgoFormatParser.js.map +1 -1
- package/dist/cjs/locales/en/parsers/ENTimeUnitCasualRelativeFormatParser.js +5 -5
- package/dist/cjs/locales/en/parsers/ENTimeUnitCasualRelativeFormatParser.js.map +1 -1
- package/dist/cjs/locales/en/parsers/ENTimeUnitWithinFormatParser.js +4 -0
- package/dist/cjs/locales/en/parsers/ENTimeUnitWithinFormatParser.js.map +1 -1
- package/dist/cjs/locales/en/parsers/ENWeekdayParser.js +1 -1
- package/dist/cjs/locales/en/parsers/ENWeekdayParser.js.map +1 -1
- package/dist/cjs/locales/en/refiners/ENMergeRelativeAfterDateRefiner.js +1 -1
- package/dist/cjs/locales/en/refiners/ENMergeRelativeAfterDateRefiner.js.map +1 -1
- package/dist/cjs/locales/en/refiners/ENMergeRelativeFollowByDateRefiner.js +4 -4
- package/dist/cjs/locales/en/refiners/ENMergeRelativeFollowByDateRefiner.js.map +1 -1
- package/dist/cjs/locales/es/parsers/ESWeekdayParser.js +1 -1
- package/dist/cjs/locales/es/parsers/ESWeekdayParser.js.map +1 -1
- package/dist/cjs/locales/fr/parsers/FRWeekdayParser.js +1 -1
- package/dist/cjs/locales/fr/parsers/FRWeekdayParser.js.map +1 -1
- package/dist/cjs/locales/it/parsers/ITRelativeDateFormatParser.d.ts +1 -1
- package/dist/cjs/locales/it/parsers/ITRelativeDateFormatParser.js +2 -2
- package/dist/cjs/locales/it/parsers/ITWeekdayParser.js +1 -1
- package/dist/cjs/locales/it/parsers/ITWeekdayParser.js.map +1 -1
- package/dist/cjs/locales/it/refiners/ITMergeRelativeDateRefiner.js +1 -1
- package/dist/cjs/locales/it/refiners/ITMergeRelativeDateRefiner.js.map +1 -1
- package/dist/cjs/locales/ja/parsers/JPWeekdayParser.js +1 -1
- package/dist/cjs/locales/ja/parsers/JPWeekdayParser.js.map +1 -1
- package/dist/cjs/locales/ja/parsers/JPWeekdayWithParenthesesParser.js +1 -1
- package/dist/cjs/locales/ja/parsers/JPWeekdayWithParenthesesParser.js.map +1 -1
- package/dist/cjs/locales/nl/parsers/NLWeekdayParser.js +1 -1
- package/dist/cjs/locales/nl/parsers/NLWeekdayParser.js.map +1 -1
- package/dist/cjs/locales/pt/parsers/PTWeekdayParser.js +1 -1
- package/dist/cjs/locales/pt/parsers/PTWeekdayParser.js.map +1 -1
- package/dist/cjs/locales/ru/parsers/RUWeekdayParser.js +1 -1
- package/dist/cjs/locales/ru/parsers/RUWeekdayParser.js.map +1 -1
- package/dist/cjs/locales/sv/constants.d.ts +25 -0
- package/dist/cjs/locales/sv/constants.js +206 -0
- package/dist/cjs/locales/sv/constants.js.map +1 -0
- package/dist/cjs/locales/sv/index.d.ts +11 -0
- package/dist/cjs/locales/sv/index.js +50 -0
- package/dist/cjs/locales/sv/index.js.map +1 -0
- package/dist/cjs/locales/sv/parsers/SVCasualDateParser.d.ts +7 -0
- package/dist/cjs/locales/sv/parsers/SVCasualDateParser.js +134 -0
- package/dist/cjs/locales/sv/parsers/SVCasualDateParser.js.map +1 -0
- package/dist/cjs/locales/sv/parsers/SVMonthNameLittleEndianParser.d.ts +7 -0
- package/dist/cjs/locales/sv/parsers/SVMonthNameLittleEndianParser.js +49 -0
- package/dist/cjs/locales/sv/parsers/SVMonthNameLittleEndianParser.js.map +1 -0
- package/dist/cjs/locales/sv/parsers/SVWeekdayParser.d.ts +7 -0
- package/dist/cjs/locales/sv/parsers/SVWeekdayParser.js +40 -0
- package/dist/cjs/locales/sv/parsers/SVWeekdayParser.js.map +1 -0
- package/dist/cjs/locales/uk/parsers/UKWeekdayParser.js +1 -1
- package/dist/cjs/locales/uk/parsers/UKWeekdayParser.js.map +1 -1
- package/dist/cjs/results.d.ts +7 -6
- package/dist/cjs/results.js +38 -35
- package/dist/cjs/results.js.map +1 -1
- package/dist/cjs/types.d.ts +1 -0
- package/dist/cjs/types.js.map +1 -1
- package/dist/cjs/utils/dates.d.ts +2 -0
- package/dist/cjs/utils/dates.js +16 -0
- package/dist/cjs/utils/dates.js.map +1 -1
- package/dist/cjs/utils/timeunits.js.map +1 -1
- package/dist/esm/calculation/duration.d.ts +8 -0
- package/dist/esm/calculation/duration.js +119 -0
- package/dist/esm/calculation/duration.js.map +1 -0
- package/dist/esm/{common/calculation → calculation}/weekdays.d.ts +2 -2
- package/dist/esm/{common/calculation → calculation}/weekdays.js +3 -3
- package/dist/esm/calculation/weekdays.js.map +1 -0
- package/dist/esm/chrono.js +1 -1
- package/dist/esm/chrono.js.map +1 -1
- package/dist/esm/common/casualReferences.js +1 -1
- package/dist/esm/common/casualReferences.js.map +1 -1
- package/dist/esm/common/refiners/ForwardDateRefiner.js.map +1 -1
- package/dist/esm/common/refiners/UnlikelyFormatFilter.js +0 -6
- package/dist/esm/common/refiners/UnlikelyFormatFilter.js.map +1 -1
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.js +2 -2
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/locales/de/parsers/DEWeekdayParser.js +1 -1
- package/dist/esm/locales/de/parsers/DEWeekdayParser.js.map +1 -1
- package/dist/esm/locales/en/parsers/ENTimeUnitAgoFormatParser.js +4 -5
- package/dist/esm/locales/en/parsers/ENTimeUnitAgoFormatParser.js.map +1 -1
- package/dist/esm/locales/en/parsers/ENTimeUnitCasualRelativeFormatParser.js +5 -5
- package/dist/esm/locales/en/parsers/ENTimeUnitCasualRelativeFormatParser.js.map +1 -1
- package/dist/esm/locales/en/parsers/ENTimeUnitWithinFormatParser.js +4 -0
- package/dist/esm/locales/en/parsers/ENTimeUnitWithinFormatParser.js.map +1 -1
- package/dist/esm/locales/en/parsers/ENWeekdayParser.js +1 -1
- package/dist/esm/locales/en/parsers/ENWeekdayParser.js.map +1 -1
- package/dist/esm/locales/en/refiners/ENMergeRelativeAfterDateRefiner.js +1 -1
- package/dist/esm/locales/en/refiners/ENMergeRelativeAfterDateRefiner.js.map +1 -1
- package/dist/esm/locales/en/refiners/ENMergeRelativeFollowByDateRefiner.js +4 -4
- package/dist/esm/locales/en/refiners/ENMergeRelativeFollowByDateRefiner.js.map +1 -1
- package/dist/esm/locales/es/parsers/ESWeekdayParser.js +1 -1
- package/dist/esm/locales/es/parsers/ESWeekdayParser.js.map +1 -1
- package/dist/esm/locales/fr/parsers/FRWeekdayParser.js +1 -1
- package/dist/esm/locales/fr/parsers/FRWeekdayParser.js.map +1 -1
- package/dist/esm/locales/it/index.js +2 -2
- package/dist/esm/locales/it/parsers/ITRelativeDateFormatParser.d.ts +1 -1
- package/dist/esm/locales/it/parsers/ITRelativeDateFormatParser.js +1 -1
- package/dist/esm/locales/it/parsers/ITWeekdayParser.js +1 -1
- package/dist/esm/locales/it/parsers/ITWeekdayParser.js.map +1 -1
- package/dist/esm/locales/it/refiners/ITMergeRelativeDateRefiner.js +1 -1
- package/dist/esm/locales/it/refiners/ITMergeRelativeDateRefiner.js.map +1 -1
- package/dist/esm/locales/ja/parsers/JPWeekdayParser.js +1 -1
- package/dist/esm/locales/ja/parsers/JPWeekdayParser.js.map +1 -1
- package/dist/esm/locales/ja/parsers/JPWeekdayWithParenthesesParser.js +1 -1
- package/dist/esm/locales/ja/parsers/JPWeekdayWithParenthesesParser.js.map +1 -1
- package/dist/esm/locales/nl/parsers/NLWeekdayParser.js +1 -1
- package/dist/esm/locales/nl/parsers/NLWeekdayParser.js.map +1 -1
- package/dist/esm/locales/pt/parsers/PTWeekdayParser.js +1 -1
- package/dist/esm/locales/pt/parsers/PTWeekdayParser.js.map +1 -1
- package/dist/esm/locales/ru/parsers/RUWeekdayParser.js +1 -1
- package/dist/esm/locales/ru/parsers/RUWeekdayParser.js.map +1 -1
- package/dist/esm/locales/sv/constants.d.ts +25 -0
- package/dist/esm/locales/sv/constants.js +199 -0
- package/dist/esm/locales/sv/constants.js.map +1 -0
- package/dist/esm/locales/sv/index.d.ts +11 -0
- package/dist/esm/locales/sv/index.js +36 -0
- package/dist/esm/locales/sv/index.js.map +1 -0
- package/dist/esm/locales/sv/parsers/SVCasualDateParser.d.ts +7 -0
- package/dist/esm/locales/sv/parsers/SVCasualDateParser.js +95 -0
- package/dist/esm/locales/sv/parsers/SVCasualDateParser.js.map +1 -0
- package/dist/esm/locales/sv/parsers/SVMonthNameLittleEndianParser.d.ts +7 -0
- package/dist/esm/locales/sv/parsers/SVMonthNameLittleEndianParser.js +46 -0
- package/dist/esm/locales/sv/parsers/SVMonthNameLittleEndianParser.js.map +1 -0
- package/dist/esm/locales/sv/parsers/SVWeekdayParser.d.ts +7 -0
- package/dist/esm/locales/sv/parsers/SVWeekdayParser.js +37 -0
- package/dist/esm/locales/sv/parsers/SVWeekdayParser.js.map +1 -0
- package/dist/esm/locales/uk/parsers/UKWeekdayParser.js +1 -1
- package/dist/esm/locales/uk/parsers/UKWeekdayParser.js.map +1 -1
- package/dist/esm/results.d.ts +7 -6
- package/dist/esm/results.js +35 -32
- package/dist/esm/results.js.map +1 -1
- package/dist/esm/types.d.ts +1 -0
- package/dist/esm/types.js.map +1 -1
- package/dist/esm/utils/dates.d.ts +2 -0
- package/dist/esm/utils/dates.js +14 -0
- package/dist/esm/utils/dates.js.map +1 -1
- package/dist/esm/utils/timeunits.js.map +1 -1
- package/package.json +1 -1
- package/src/calculation/duration.ts +143 -0
- package/src/{common/calculation → calculation}/weekdays.ts +3 -3
- package/src/chrono.ts +2 -2
- package/src/common/casualReferences.ts +1 -1
- package/src/common/refiners/ForwardDateRefiner.ts +0 -1
- package/src/common/refiners/UnlikelyFormatFilter.ts +0 -8
- package/src/index.ts +2 -2
- package/src/locales/de/parsers/DEWeekdayParser.ts +1 -1
- package/src/locales/en/parsers/ENTimeUnitAgoFormatParser.ts +4 -5
- package/src/locales/en/parsers/ENTimeUnitCasualRelativeFormatParser.ts +5 -5
- package/src/locales/en/parsers/ENTimeUnitWithinFormatParser.ts +5 -0
- package/src/locales/en/parsers/ENWeekdayParser.ts +1 -1
- package/src/locales/en/refiners/ENMergeRelativeAfterDateRefiner.ts +1 -1
- package/src/locales/en/refiners/ENMergeRelativeFollowByDateRefiner.ts +5 -5
- package/src/locales/es/parsers/ESWeekdayParser.ts +1 -1
- package/src/locales/fr/parsers/FRWeekdayParser.ts +1 -1
- package/src/locales/it/index.ts +2 -2
- package/src/locales/it/parsers/ITRelativeDateFormatParser.ts +1 -1
- package/src/locales/it/parsers/ITWeekdayParser.ts +1 -1
- package/src/locales/it/refiners/ITMergeRelativeDateRefiner.ts +1 -1
- package/src/locales/ja/parsers/JPWeekdayParser.ts +1 -1
- package/src/locales/ja/parsers/JPWeekdayWithParenthesesParser.ts +1 -1
- package/src/locales/nl/parsers/NLWeekdayParser.ts +1 -1
- package/src/locales/pt/parsers/PTWeekdayParser.ts +1 -1
- package/src/locales/ru/parsers/RUWeekdayParser.ts +1 -1
- package/src/locales/sv/constants.ts +215 -0
- package/src/locales/sv/index.ts +45 -0
- package/src/locales/sv/parsers/SVCasualDateParser.ts +114 -0
- package/src/locales/sv/parsers/SVMonthNameLittleEndianParser.ts +59 -0
- package/src/locales/sv/parsers/SVWeekdayParser.ts +47 -0
- package/src/locales/uk/parsers/UKWeekdayParser.ts +1 -1
- package/src/results.ts +38 -35
- package/src/types.ts +2 -0
- package/src/utils/dates.ts +26 -0
- package/src/utils/timeunits.ts +9 -0
- package/test/calculation_duration.test.ts +165 -0
- package/test/{calculation.test.ts → calculation_weekdays.test.ts} +1 -1
- package/test/en/en_month_name_little_endian.test.ts +19 -0
- package/test/en/en_time_exp.test.ts +14 -0
- package/test/en/en_year.test.ts +38 -0
- package/test/en/negative_cases.test.ts +5 -0
- package/test/result.test.ts +4 -4
- package/test/sv/sv_casual.test.ts +45 -0
- package/test/sv/sv_weekday.test.ts +68 -0
- package/test/system_timezone.test.ts +36 -0
- package/dist/cjs/common/calculation/weekdays.js.map +0 -1
- package/dist/esm/common/calculation/weekdays.js.map +0 -1
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import { OpUnitType, QUnitType } from "dayjs";
|
|
2
|
+
import { matchAnyPattern, repeatedTimeunitPattern } from "../../utils/pattern";
|
|
3
|
+
import { findMostLikelyADYear } from "../../calculation/years";
|
|
4
|
+
import { TimeUnits } from "../../utils/timeunits";
|
|
5
|
+
|
|
6
|
+
export const WEEKDAY_DICTIONARY: { [word: string]: number } = {
|
|
7
|
+
"söndag": 0,
|
|
8
|
+
"sön": 0,
|
|
9
|
+
"so": 0,
|
|
10
|
+
"måndag": 1,
|
|
11
|
+
"mån": 1,
|
|
12
|
+
"må": 1,
|
|
13
|
+
"tisdag": 2,
|
|
14
|
+
"tis": 2,
|
|
15
|
+
"ti": 2,
|
|
16
|
+
"onsdag": 3,
|
|
17
|
+
"ons": 3,
|
|
18
|
+
"on": 3,
|
|
19
|
+
"torsdag": 4,
|
|
20
|
+
"tors": 4,
|
|
21
|
+
"to": 4,
|
|
22
|
+
"fredag": 5,
|
|
23
|
+
"fre": 5,
|
|
24
|
+
"fr": 5,
|
|
25
|
+
"lördag": 6,
|
|
26
|
+
"lör": 6,
|
|
27
|
+
"lö": 6,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const MONTH_DICTIONARY: { [word: string]: number } = {
|
|
31
|
+
"januari": 1,
|
|
32
|
+
"jan": 1,
|
|
33
|
+
"jan.": 1,
|
|
34
|
+
"februari": 2,
|
|
35
|
+
"feb": 2,
|
|
36
|
+
"feb.": 2,
|
|
37
|
+
"mars": 3,
|
|
38
|
+
"mar": 3,
|
|
39
|
+
"mar.": 3,
|
|
40
|
+
"april": 4,
|
|
41
|
+
"apr": 4,
|
|
42
|
+
"apr.": 4,
|
|
43
|
+
"maj": 5,
|
|
44
|
+
"juni": 6,
|
|
45
|
+
"jun": 6,
|
|
46
|
+
"jun.": 6,
|
|
47
|
+
"juli": 7,
|
|
48
|
+
"jul": 7,
|
|
49
|
+
"jul.": 7,
|
|
50
|
+
"augusti": 8,
|
|
51
|
+
"aug": 8,
|
|
52
|
+
"aug.": 8,
|
|
53
|
+
"september": 9,
|
|
54
|
+
"sep": 9,
|
|
55
|
+
"sep.": 9,
|
|
56
|
+
"sept": 9,
|
|
57
|
+
"oktober": 10,
|
|
58
|
+
"okt": 10,
|
|
59
|
+
"okt.": 10,
|
|
60
|
+
"november": 11,
|
|
61
|
+
"nov": 11,
|
|
62
|
+
"nov.": 11,
|
|
63
|
+
"december": 12,
|
|
64
|
+
"dec": 12,
|
|
65
|
+
"dec.": 12,
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
export const ORDINAL_NUMBER_DICTIONARY: { [word: string]: number } = {
|
|
69
|
+
"första": 1,
|
|
70
|
+
"andra": 2,
|
|
71
|
+
"tredje": 3,
|
|
72
|
+
"fjärde": 4,
|
|
73
|
+
"femte": 5,
|
|
74
|
+
"sjätte": 6,
|
|
75
|
+
"sjunde": 7,
|
|
76
|
+
"åttonde": 8,
|
|
77
|
+
"nionde": 9,
|
|
78
|
+
"tionde": 10,
|
|
79
|
+
"elfte": 11,
|
|
80
|
+
"tolfte": 12,
|
|
81
|
+
"trettonde": 13,
|
|
82
|
+
"fjortonde": 14,
|
|
83
|
+
"femtonde": 15,
|
|
84
|
+
"sextonde": 16,
|
|
85
|
+
"sjuttonde": 17,
|
|
86
|
+
"artonde": 18,
|
|
87
|
+
"nittonde": 19,
|
|
88
|
+
"tjugonde": 20,
|
|
89
|
+
"tjugoförsta": 21,
|
|
90
|
+
"tjugoandra": 22,
|
|
91
|
+
"tjugotredje": 23,
|
|
92
|
+
"tjugofjärde": 24,
|
|
93
|
+
"tjugofemte": 25,
|
|
94
|
+
"tjugosjätte": 26,
|
|
95
|
+
"tjugosjunde": 27,
|
|
96
|
+
"tjugoåttonde": 28,
|
|
97
|
+
"tjugonionde": 29,
|
|
98
|
+
"trettionde": 30,
|
|
99
|
+
"trettioförsta": 31,
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
export const INTEGER_WORD_DICTIONARY: { [word: string]: number } = {
|
|
103
|
+
"en": 1,
|
|
104
|
+
"ett": 1,
|
|
105
|
+
"två": 2,
|
|
106
|
+
"tre": 3,
|
|
107
|
+
"fyra": 4,
|
|
108
|
+
"fem": 5,
|
|
109
|
+
"sex": 6,
|
|
110
|
+
"sju": 7,
|
|
111
|
+
"åtta": 8,
|
|
112
|
+
"nio": 9,
|
|
113
|
+
"tio": 10,
|
|
114
|
+
"elva": 11,
|
|
115
|
+
"tolv": 12,
|
|
116
|
+
"tretton": 13,
|
|
117
|
+
"fjorton": 14,
|
|
118
|
+
"femton": 15,
|
|
119
|
+
"sexton": 16,
|
|
120
|
+
"sjutton": 17,
|
|
121
|
+
"arton": 18,
|
|
122
|
+
"nitton": 19,
|
|
123
|
+
"tjugo": 20,
|
|
124
|
+
"trettiо": 30,
|
|
125
|
+
"fyrtio": 40,
|
|
126
|
+
"femtio": 50,
|
|
127
|
+
"sextio": 60,
|
|
128
|
+
"sjuttio": 70,
|
|
129
|
+
"åttio": 80,
|
|
130
|
+
"nittio": 90,
|
|
131
|
+
"hundra": 100,
|
|
132
|
+
"tusen": 1000,
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
export const TIME_UNIT_DICTIONARY: { [word: string]: OpUnitType | QUnitType } = {
|
|
136
|
+
"sekund": "second",
|
|
137
|
+
"sekunder": "second",
|
|
138
|
+
"sek": "second",
|
|
139
|
+
"minut": "minute",
|
|
140
|
+
"minuter": "minute",
|
|
141
|
+
"min": "minute",
|
|
142
|
+
"timme": "hour",
|
|
143
|
+
"timmar": "hour",
|
|
144
|
+
"tim": "hour",
|
|
145
|
+
"dag": "day",
|
|
146
|
+
"dagar": "day",
|
|
147
|
+
"vecka": "week",
|
|
148
|
+
"veckor": "week",
|
|
149
|
+
"månad": "month",
|
|
150
|
+
"månader": "month",
|
|
151
|
+
"mån": "month",
|
|
152
|
+
"år": "year",
|
|
153
|
+
"kvartаl": "quarter",
|
|
154
|
+
"kvartal": "quarter",
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
export function parseTimeUnits(timeunitText): TimeUnits {
|
|
158
|
+
const fragments = {};
|
|
159
|
+
let remainingText = timeunitText;
|
|
160
|
+
let match = SINGLE_TIME_UNIT_REGEX.exec(remainingText);
|
|
161
|
+
while (match) {
|
|
162
|
+
collectDateTimeFragment(fragments, match);
|
|
163
|
+
remainingText = remainingText.substring(match[0].length);
|
|
164
|
+
match = SINGLE_TIME_UNIT_REGEX.exec(remainingText);
|
|
165
|
+
}
|
|
166
|
+
return fragments;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function collectDateTimeFragment(fragments, match) {
|
|
170
|
+
const num = parseNumberPattern(match[1]);
|
|
171
|
+
const unit = TIME_UNIT_DICTIONARY[match[2].toLowerCase()];
|
|
172
|
+
fragments[unit] = num;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export const NUMBER_PATTERN = `(?:${matchAnyPattern(INTEGER_WORD_DICTIONARY)}|\\d+)`;
|
|
176
|
+
export const ORDINAL_NUMBER_PATTERN = `(?:${matchAnyPattern(ORDINAL_NUMBER_DICTIONARY)}|\\d{1,2}(?:e|:e))`;
|
|
177
|
+
export const TIME_UNIT_PATTERN = `(?:${matchAnyPattern(TIME_UNIT_DICTIONARY)})`;
|
|
178
|
+
|
|
179
|
+
const SINGLE_TIME_UNIT_PATTERN = `(${NUMBER_PATTERN})\\s{0,5}(${matchAnyPattern(TIME_UNIT_DICTIONARY)})\\s{0,5}`;
|
|
180
|
+
const SINGLE_TIME_UNIT_REGEX = new RegExp(SINGLE_TIME_UNIT_PATTERN, "i");
|
|
181
|
+
|
|
182
|
+
export const TIME_UNITS_PATTERN = repeatedTimeunitPattern("", SINGLE_TIME_UNIT_PATTERN);
|
|
183
|
+
|
|
184
|
+
export function parseNumberPattern(match: string): number {
|
|
185
|
+
const num = match.toLowerCase();
|
|
186
|
+
if (INTEGER_WORD_DICTIONARY[num] !== undefined) {
|
|
187
|
+
return INTEGER_WORD_DICTIONARY[num];
|
|
188
|
+
}
|
|
189
|
+
return parseInt(num);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
export function parseOrdinalNumberPattern(match: string): number {
|
|
193
|
+
const num = match.toLowerCase();
|
|
194
|
+
if (ORDINAL_NUMBER_DICTIONARY[num] !== undefined) {
|
|
195
|
+
return ORDINAL_NUMBER_DICTIONARY[num];
|
|
196
|
+
}
|
|
197
|
+
return parseInt(num);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export function parseYear(match: string): number {
|
|
201
|
+
if (/\d+/.test(match)) {
|
|
202
|
+
let yearNumber = parseInt(match);
|
|
203
|
+
if (yearNumber < 100) {
|
|
204
|
+
yearNumber = findMostLikelyADYear(yearNumber);
|
|
205
|
+
}
|
|
206
|
+
return yearNumber;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const num = match.toLowerCase();
|
|
210
|
+
if (INTEGER_WORD_DICTIONARY[num] !== undefined) {
|
|
211
|
+
return INTEGER_WORD_DICTIONARY[num];
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return parseInt(match);
|
|
215
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { includeCommonConfiguration } from "../../configurations";
|
|
2
|
+
import { Chrono, Configuration, Parser, Refiner } from "../../chrono";
|
|
3
|
+
import { ParsingResult, ParsingComponents, ReferenceWithTimezone } from "../../results";
|
|
4
|
+
import { Component, ParsedResult, ParsingOption, ParsingReference, Meridiem, Weekday } from "../../types";
|
|
5
|
+
import SlashDateFormatParser from "../../common/parsers/SlashDateFormatParser";
|
|
6
|
+
import ISOFormatParser from "../../common/parsers/ISOFormatParser";
|
|
7
|
+
import SVWeekdayParser from "./parsers/SVWeekdayParser";
|
|
8
|
+
import SVMonthNameLittleEndianParser from "./parsers/SVMonthNameLittleEndianParser";
|
|
9
|
+
import SVCasualDateParser from "./parsers/SVCasualDateParser";
|
|
10
|
+
|
|
11
|
+
export { Chrono, Parser, Refiner, ParsingResult, ParsingComponents, ReferenceWithTimezone };
|
|
12
|
+
export { Component, ParsedResult, ParsingOption, ParsingReference, Meridiem, Weekday };
|
|
13
|
+
|
|
14
|
+
// Shortcuts
|
|
15
|
+
export const casual = new Chrono(createCasualConfiguration());
|
|
16
|
+
export const strict = new Chrono(createConfiguration(true));
|
|
17
|
+
|
|
18
|
+
export function parse(text: string, ref?: ParsingReference | Date, option?: ParsingOption): ParsedResult[] {
|
|
19
|
+
return casual.parse(text, ref, option);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function parseDate(text: string, ref?: ParsingReference | Date, option?: ParsingOption): Date {
|
|
23
|
+
return casual.parseDate(text, ref, option);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function createCasualConfiguration(littleEndian = true): Configuration {
|
|
27
|
+
const option = createConfiguration(false, littleEndian);
|
|
28
|
+
option.parsers.unshift(new SVCasualDateParser());
|
|
29
|
+
return option;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function createConfiguration(strictMode = true, littleEndian = true): Configuration {
|
|
33
|
+
return includeCommonConfiguration(
|
|
34
|
+
{
|
|
35
|
+
parsers: [
|
|
36
|
+
new ISOFormatParser(),
|
|
37
|
+
new SlashDateFormatParser(littleEndian),
|
|
38
|
+
new SVMonthNameLittleEndianParser(),
|
|
39
|
+
new SVWeekdayParser(),
|
|
40
|
+
],
|
|
41
|
+
refiners: [],
|
|
42
|
+
},
|
|
43
|
+
strictMode
|
|
44
|
+
);
|
|
45
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { ParsingContext } from "../../../chrono";
|
|
2
|
+
import { ParsingComponents, ParsingResult } from "../../../results";
|
|
3
|
+
import dayjs from "dayjs";
|
|
4
|
+
import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary";
|
|
5
|
+
import { assignSimilarDate, assignTheNextDay, implySimilarTime } from "../../../utils/dayjs";
|
|
6
|
+
import * as references from "../../../common/casualReferences";
|
|
7
|
+
|
|
8
|
+
const PATTERN = new RegExp(
|
|
9
|
+
`(nu|idag|imorgon|övermorgon|igår|förrgår|i\\s*förrgår)` +
|
|
10
|
+
`(?:\\s*(?:på\\s*)?(morgonen?|förmiddagen?|middagen?|eftermiddagen?|kvällen?|natten?|midnatt))?` +
|
|
11
|
+
`(?=\\W|$)`,
|
|
12
|
+
"i"
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
const DATE_GROUP = 1;
|
|
16
|
+
const TIME_GROUP = 2;
|
|
17
|
+
|
|
18
|
+
export default class SVCasualDateParser extends AbstractParserWithWordBoundaryChecking {
|
|
19
|
+
innerPattern(context: ParsingContext): RegExp {
|
|
20
|
+
return PATTERN;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
innerExtract(context: ParsingContext, match: RegExpMatchArray): ParsingComponents | ParsingResult {
|
|
24
|
+
let targetDate = dayjs(context.refDate);
|
|
25
|
+
const dateKeyword = (match[DATE_GROUP] || "").toLowerCase();
|
|
26
|
+
const timeKeyword = (match[TIME_GROUP] || "").toLowerCase();
|
|
27
|
+
|
|
28
|
+
let component = context.createParsingComponents();
|
|
29
|
+
switch (dateKeyword) {
|
|
30
|
+
case "nu":
|
|
31
|
+
component = references.now(context.reference);
|
|
32
|
+
break;
|
|
33
|
+
|
|
34
|
+
case "idag":
|
|
35
|
+
component = references.today(context.reference);
|
|
36
|
+
break;
|
|
37
|
+
|
|
38
|
+
case "imorgon":
|
|
39
|
+
case "imorn":
|
|
40
|
+
assignTheNextDay(component, targetDate);
|
|
41
|
+
break;
|
|
42
|
+
|
|
43
|
+
case "igår":
|
|
44
|
+
targetDate = targetDate.add(-1, "day");
|
|
45
|
+
assignSimilarDate(component, targetDate);
|
|
46
|
+
implySimilarTime(component, targetDate);
|
|
47
|
+
break;
|
|
48
|
+
|
|
49
|
+
case "förrgår":
|
|
50
|
+
case "i förrgår":
|
|
51
|
+
targetDate = targetDate.add(-2, "day");
|
|
52
|
+
assignSimilarDate(component, targetDate);
|
|
53
|
+
implySimilarTime(component, targetDate);
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
switch (timeKeyword) {
|
|
58
|
+
case "morgon":
|
|
59
|
+
case "morgonen":
|
|
60
|
+
component.imply("hour", 6);
|
|
61
|
+
component.imply("minute", 0);
|
|
62
|
+
component.imply("second", 0);
|
|
63
|
+
component.imply("millisecond", 0);
|
|
64
|
+
break;
|
|
65
|
+
|
|
66
|
+
case "förmiddag":
|
|
67
|
+
case "förmiddagen":
|
|
68
|
+
component.imply("hour", 9);
|
|
69
|
+
component.imply("minute", 0);
|
|
70
|
+
component.imply("second", 0);
|
|
71
|
+
component.imply("millisecond", 0);
|
|
72
|
+
break;
|
|
73
|
+
|
|
74
|
+
case "middag":
|
|
75
|
+
case "middagen":
|
|
76
|
+
component.imply("hour", 12);
|
|
77
|
+
component.imply("minute", 0);
|
|
78
|
+
component.imply("second", 0);
|
|
79
|
+
component.imply("millisecond", 0);
|
|
80
|
+
break;
|
|
81
|
+
|
|
82
|
+
case "eftermiddag":
|
|
83
|
+
case "eftermiddagen":
|
|
84
|
+
component.imply("hour", 15);
|
|
85
|
+
component.imply("minute", 0);
|
|
86
|
+
component.imply("second", 0);
|
|
87
|
+
component.imply("millisecond", 0);
|
|
88
|
+
break;
|
|
89
|
+
|
|
90
|
+
case "kväll":
|
|
91
|
+
case "kvällen":
|
|
92
|
+
component.imply("hour", 20);
|
|
93
|
+
component.imply("minute", 0);
|
|
94
|
+
component.imply("second", 0);
|
|
95
|
+
component.imply("millisecond", 0);
|
|
96
|
+
break;
|
|
97
|
+
|
|
98
|
+
case "natt":
|
|
99
|
+
case "natten":
|
|
100
|
+
case "midnatt":
|
|
101
|
+
if (timeKeyword === "midnatt") {
|
|
102
|
+
component.imply("hour", 0);
|
|
103
|
+
} else {
|
|
104
|
+
component.imply("hour", 2);
|
|
105
|
+
}
|
|
106
|
+
component.imply("minute", 0);
|
|
107
|
+
component.imply("second", 0);
|
|
108
|
+
component.imply("millisecond", 0);
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return component;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { ParsingContext } from "../../../chrono";
|
|
2
|
+
import { ParsingResult } from "../../../results";
|
|
3
|
+
import { findYearClosestToRef } from "../../../calculation/years";
|
|
4
|
+
import { MONTH_DICTIONARY } from "../constants";
|
|
5
|
+
import { parseYear } from "../constants";
|
|
6
|
+
import { matchAnyPattern } from "../../../utils/pattern";
|
|
7
|
+
import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary";
|
|
8
|
+
|
|
9
|
+
const PATTERN = new RegExp(
|
|
10
|
+
"(?:den\\s*?)?" +
|
|
11
|
+
`([0-9]{1,2})` +
|
|
12
|
+
`(?:\\s*(?:till|\\-|\\–|\\s)\\s*([0-9]{1,2}))?\\s*` +
|
|
13
|
+
`(${matchAnyPattern(MONTH_DICTIONARY)})` +
|
|
14
|
+
`(?:(?:-|/|,?\\s*)([0-9]{4}(?![^\\s]\\d)))?` +
|
|
15
|
+
`(?=\\W|$)`,
|
|
16
|
+
"i"
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
const DATE_GROUP = 1;
|
|
20
|
+
const DATE_TO_GROUP = 2;
|
|
21
|
+
const MONTH_NAME_GROUP = 3;
|
|
22
|
+
const YEAR_GROUP = 4;
|
|
23
|
+
|
|
24
|
+
export default class SVMonthNameLittleEndianParser extends AbstractParserWithWordBoundaryChecking {
|
|
25
|
+
innerPattern(): RegExp {
|
|
26
|
+
return PATTERN;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
innerExtract(context: ParsingContext, match: RegExpMatchArray): ParsingResult {
|
|
30
|
+
const result = context.createParsingResult(match.index, match[0]);
|
|
31
|
+
|
|
32
|
+
const month = MONTH_DICTIONARY[match[MONTH_NAME_GROUP].toLowerCase()];
|
|
33
|
+
const day = parseInt(match[DATE_GROUP]);
|
|
34
|
+
if (day > 31) {
|
|
35
|
+
// e.g. "[96 Aug]" => "9[6 Aug]", we need to shift away from the next number
|
|
36
|
+
match.index = match.index + match[DATE_GROUP].length;
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
result.start.assign("month", month);
|
|
41
|
+
result.start.assign("day", day);
|
|
42
|
+
|
|
43
|
+
if (match[YEAR_GROUP]) {
|
|
44
|
+
const yearNumber = parseYear(match[YEAR_GROUP]);
|
|
45
|
+
result.start.assign("year", yearNumber);
|
|
46
|
+
} else {
|
|
47
|
+
const year = findYearClosestToRef(context.refDate, day, month);
|
|
48
|
+
result.start.imply("year", year);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (match[DATE_TO_GROUP]) {
|
|
52
|
+
const endDate = parseInt(match[DATE_TO_GROUP]);
|
|
53
|
+
result.end = result.start.clone();
|
|
54
|
+
result.end.assign("day", endDate);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { ParsingContext } from "../../../chrono";
|
|
2
|
+
import { ParsingComponents } from "../../../results";
|
|
3
|
+
import { WEEKDAY_DICTIONARY } from "../constants";
|
|
4
|
+
import { matchAnyPattern } from "../../../utils/pattern";
|
|
5
|
+
import { AbstractParserWithWordBoundaryChecking } from "../../../common/parsers/AbstractParserWithWordBoundary";
|
|
6
|
+
import { createParsingComponentsAtWeekday } from "../../../calculation/weekdays";
|
|
7
|
+
|
|
8
|
+
const PATTERN = new RegExp(
|
|
9
|
+
"(?:(?:\\,|\\(|\\()\\s*)?" +
|
|
10
|
+
"(?:på\\s*?)?" +
|
|
11
|
+
"(?:(förra|senaste|nästa|kommande)\\s*)?" +
|
|
12
|
+
`(${matchAnyPattern(WEEKDAY_DICTIONARY)})` +
|
|
13
|
+
"(?:\\s*(?:\\,|\\)|\\)))?" +
|
|
14
|
+
"(?:\\s*(förra|senaste|nästa|kommande)\\s*vecka)?" +
|
|
15
|
+
"(?=\\W|$)",
|
|
16
|
+
"i"
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
const PREFIX_GROUP = 1;
|
|
20
|
+
const SUFFIX_GROUP = 3;
|
|
21
|
+
const WEEKDAY_GROUP = 2;
|
|
22
|
+
|
|
23
|
+
export default class SVWeekdayParser extends AbstractParserWithWordBoundaryChecking {
|
|
24
|
+
innerPattern(): RegExp {
|
|
25
|
+
return PATTERN;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
innerExtract(context: ParsingContext, match: RegExpMatchArray): ParsingComponents {
|
|
29
|
+
const dayOfWeek = match[WEEKDAY_GROUP].toLowerCase();
|
|
30
|
+
const offset = WEEKDAY_DICTIONARY[dayOfWeek];
|
|
31
|
+
const prefix = match[PREFIX_GROUP];
|
|
32
|
+
const postfix = match[SUFFIX_GROUP];
|
|
33
|
+
|
|
34
|
+
let modifierWord = prefix || postfix;
|
|
35
|
+
modifierWord = modifierWord || "";
|
|
36
|
+
modifierWord = modifierWord.toLowerCase();
|
|
37
|
+
|
|
38
|
+
let modifier = null;
|
|
39
|
+
if (modifierWord.match(/förra|senaste/)) {
|
|
40
|
+
modifier = "last";
|
|
41
|
+
} else if (modifierWord.match(/nästa|kommande/)) {
|
|
42
|
+
modifier = "next";
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return createParsingComponentsAtWeekday(context.reference, offset, modifier);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
@@ -3,7 +3,7 @@ import { ParsingComponents } from "../../../results";
|
|
|
3
3
|
// TODO: ADD REGEX_PARTS below
|
|
4
4
|
import { WEEKDAY_DICTIONARY } from "../constants";
|
|
5
5
|
import { matchAnyPattern } from "../../../utils/pattern";
|
|
6
|
-
import { createParsingComponentsAtWeekday } from "../../../
|
|
6
|
+
import { createParsingComponentsAtWeekday } from "../../../calculation/weekdays";
|
|
7
7
|
import { AbstractParserWithLeftRightBoundaryChecking } from "./AbstractParserWithWordBoundaryChecking";
|
|
8
8
|
|
|
9
9
|
const PREFIX_GROUP = 1;
|
package/src/results.ts
CHANGED
|
@@ -1,24 +1,32 @@
|
|
|
1
|
-
import { Component, ParsedComponents, ParsedResult, ParsingReference } from "./types";
|
|
1
|
+
import { Component, ParsedComponents, ParsedResult, ParsingReference, TimezoneAbbrMap } from "./types";
|
|
2
2
|
|
|
3
3
|
import quarterOfYear from "dayjs/plugin/quarterOfYear";
|
|
4
4
|
import dayjs, { QUnitType } from "dayjs";
|
|
5
|
-
import { assignSimilarDate, assignSimilarTime, implySimilarTime } from "./utils/
|
|
5
|
+
import { assignSimilarDate, assignSimilarTime, implySimilarTime } from "./utils/dates";
|
|
6
6
|
import { toTimezoneOffset } from "./timezone";
|
|
7
|
+
import { addDuration, Duration } from "./calculation/duration";
|
|
7
8
|
dayjs.extend(quarterOfYear);
|
|
8
9
|
|
|
9
10
|
export class ReferenceWithTimezone {
|
|
10
11
|
readonly instant: Date;
|
|
11
12
|
readonly timezoneOffset?: number | null;
|
|
12
13
|
|
|
13
|
-
constructor(
|
|
14
|
-
|
|
14
|
+
constructor(instant?: Date, timezoneOffset?: number) {
|
|
15
|
+
this.instant = instant ?? new Date();
|
|
16
|
+
this.timezoneOffset = timezoneOffset ?? null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
static fromDate(date: Date): ReferenceWithTimezone {
|
|
20
|
+
return new ReferenceWithTimezone(date);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
static fromInput(input?: ParsingReference | Date, timezoneOverrides?: TimezoneAbbrMap) {
|
|
15
24
|
if (input instanceof Date) {
|
|
16
|
-
|
|
17
|
-
this.timezoneOffset = null;
|
|
18
|
-
} else {
|
|
19
|
-
this.instant = input.instant ?? new Date();
|
|
20
|
-
this.timezoneOffset = toTimezoneOffset(input.timezone, this.instant);
|
|
25
|
+
return ReferenceWithTimezone.fromDate(input);
|
|
21
26
|
}
|
|
27
|
+
const instant: Date = input?.instant ?? new Date();
|
|
28
|
+
const timezoneOffset = toTimezoneOffset(input?.timezone, instant, timezoneOverrides);
|
|
29
|
+
return new ReferenceWithTimezone(instant, timezoneOffset);
|
|
22
30
|
}
|
|
23
31
|
|
|
24
32
|
/**
|
|
@@ -215,18 +223,12 @@ export class ParsingComponents implements ParsedComponents {
|
|
|
215
223
|
return date;
|
|
216
224
|
}
|
|
217
225
|
|
|
218
|
-
static createRelativeFromReference(
|
|
219
|
-
reference
|
|
220
|
-
fragments: { [c in QUnitType]?: number }
|
|
221
|
-
): ParsingComponents {
|
|
222
|
-
let date = dayjs(reference.getDateWithAdjustedTimezone());
|
|
223
|
-
for (const key in fragments) {
|
|
224
|
-
date = date.add(fragments[key as QUnitType], key as QUnitType);
|
|
225
|
-
}
|
|
226
|
+
static createRelativeFromReference(reference: ReferenceWithTimezone, duration: Duration): ParsingComponents {
|
|
227
|
+
let date = addDuration(reference.getDateWithAdjustedTimezone(), duration);
|
|
226
228
|
|
|
227
229
|
const components = new ParsingComponents(reference);
|
|
228
230
|
components.addTag("result/relativeDate");
|
|
229
|
-
if (
|
|
231
|
+
if (duration["hour"] || duration["minute"] || duration["second"]) {
|
|
230
232
|
components.addTag("result/relativeDateAndTime");
|
|
231
233
|
assignSimilarTime(components, date);
|
|
232
234
|
assignSimilarDate(components, date);
|
|
@@ -235,26 +237,27 @@ export class ParsingComponents implements ParsedComponents {
|
|
|
235
237
|
implySimilarTime(components, date);
|
|
236
238
|
components.imply("timezoneOffset", reference.getTimezoneOffset());
|
|
237
239
|
|
|
238
|
-
if (
|
|
239
|
-
components.assign("day", date.
|
|
240
|
-
components.assign("month", date.
|
|
241
|
-
components.assign("year", date.
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
components.assign("
|
|
245
|
-
components.assign("
|
|
246
|
-
components.
|
|
240
|
+
if (duration["day"]) {
|
|
241
|
+
components.assign("day", date.getDate());
|
|
242
|
+
components.assign("month", date.getMonth() + 1);
|
|
243
|
+
components.assign("year", date.getFullYear());
|
|
244
|
+
components.assign("weekday", date.getDay());
|
|
245
|
+
} else if (duration["week"]) {
|
|
246
|
+
components.assign("day", date.getDate());
|
|
247
|
+
components.assign("month", date.getMonth() + 1);
|
|
248
|
+
components.assign("year", date.getFullYear());
|
|
249
|
+
components.imply("weekday", date.getDay());
|
|
247
250
|
} else {
|
|
248
|
-
components.imply("day", date.
|
|
249
|
-
if (
|
|
250
|
-
components.assign("month", date.
|
|
251
|
-
components.assign("year", date.
|
|
251
|
+
components.imply("day", date.getDate());
|
|
252
|
+
if (duration["month"]) {
|
|
253
|
+
components.assign("month", date.getMonth() + 1);
|
|
254
|
+
components.assign("year", date.getFullYear());
|
|
252
255
|
} else {
|
|
253
|
-
components.imply("month", date.
|
|
254
|
-
if (
|
|
255
|
-
components.assign("year", date.
|
|
256
|
+
components.imply("month", date.getMonth() + 1);
|
|
257
|
+
if (duration["year"]) {
|
|
258
|
+
components.assign("year", date.getFullYear());
|
|
256
259
|
} else {
|
|
257
|
-
components.imply("year", date.
|
|
260
|
+
components.imply("year", date.getFullYear());
|
|
258
261
|
}
|
|
259
262
|
}
|
|
260
263
|
}
|
package/src/types.ts
CHANGED
package/src/utils/dates.ts
CHANGED
|
@@ -1,4 +1,29 @@
|
|
|
1
1
|
import { ParsingComponents } from "../results";
|
|
2
|
+
import { Meridiem } from "../types";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Assign (force update) the parsing component to the same day as the `target`.
|
|
6
|
+
* @param component the component to be updated.
|
|
7
|
+
* @param target the target date with timezone adjusted.
|
|
8
|
+
*/
|
|
9
|
+
export function assignSimilarDate(component: ParsingComponents, target: Date) {
|
|
10
|
+
component.assign("day", target.getDate());
|
|
11
|
+
component.assign("month", target.getMonth() + 1);
|
|
12
|
+
component.assign("year", target.getFullYear());
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Assign (force update) the parsing component to the same time as the `target`.
|
|
17
|
+
* @param component the component to be updated.
|
|
18
|
+
* @param target the target date with timezone adjusted.
|
|
19
|
+
*/
|
|
20
|
+
export function assignSimilarTime(component: ParsingComponents, target: Date) {
|
|
21
|
+
component.assign("hour", target.getHours());
|
|
22
|
+
component.assign("minute", target.getMinutes());
|
|
23
|
+
component.assign("second", target.getSeconds());
|
|
24
|
+
component.assign("millisecond", target.getMilliseconds());
|
|
25
|
+
component.assign("meridiem", target.getHours() < 12 ? Meridiem.AM : Meridiem.PM);
|
|
26
|
+
}
|
|
2
27
|
|
|
3
28
|
/**
|
|
4
29
|
* Imply (weakly update) the parsing component to the same day as the `target`.
|
|
@@ -21,4 +46,5 @@ export function implySimilarTime(component: ParsingComponents, target: Date) {
|
|
|
21
46
|
component.imply("minute", target.getMinutes());
|
|
22
47
|
component.imply("second", target.getSeconds());
|
|
23
48
|
component.imply("millisecond", target.getMilliseconds());
|
|
49
|
+
component.imply("meridiem", target.getHours() < 12 ? Meridiem.AM : Meridiem.PM);
|
|
24
50
|
}
|
package/src/utils/timeunits.ts
CHANGED
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
import { OpUnitType, QUnitType } from "dayjs";
|
|
2
2
|
import { ParsingComponents } from "../results";
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* @deprecated Use `calculation.duration.Duration`.
|
|
6
|
+
*/
|
|
4
7
|
export type TimeUnits = { [c in OpUnitType | QUnitType]?: number };
|
|
5
8
|
|
|
9
|
+
/**
|
|
10
|
+
* @deprecated Use `calculation.duration.*`.
|
|
11
|
+
*/
|
|
6
12
|
export function reverseTimeUnits(timeUnits: TimeUnits): TimeUnits {
|
|
7
13
|
const reversed = {};
|
|
8
14
|
for (const key in timeUnits) {
|
|
@@ -13,6 +19,9 @@ export function reverseTimeUnits(timeUnits: TimeUnits): TimeUnits {
|
|
|
13
19
|
return reversed as TimeUnits;
|
|
14
20
|
}
|
|
15
21
|
|
|
22
|
+
/**
|
|
23
|
+
* @deprecated Use `calculation.duration.*`.
|
|
24
|
+
*/
|
|
16
25
|
export function addImpliedTimeUnits(components: ParsingComponents, timeUnits: TimeUnits): ParsingComponents {
|
|
17
26
|
const output = components.clone();
|
|
18
27
|
|