@teipublisher/pb-components 1.32.2 → 1.33.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.
@@ -0,0 +1,266 @@
1
+ export class ParseDateService {
2
+ /*
3
+ * PARSE DATE SERVICE
4
+ * whenever the user types a date, it should be allowed
5
+ * to type in a lot of dirrerent date formats. This service
6
+ * should detect all of the supported formats. Some supported formats:
7
+ * - 1. April 1970 => 1970-04-01
8
+ * - 1970 => 1970-01-01
9
+ * - 1970-12-23 => 1970-12-23
10
+ * - 1900 12 23 => 1900-12-23
11
+ * - 1 jan 1970 => 1970-01-01
12
+ * - 2020-W12 => 2020-03-16
13
+ * - 2020-01 => 2020-01-01
14
+ * For all formats check the tests written in AVA `test/parse-date-service-test.js`
15
+ *
16
+ * public methods
17
+ * run()
18
+ */
19
+ constructor() {
20
+ }
21
+
22
+ run (input) {
23
+ this.input = input;
24
+ this.day = "??";
25
+ this.month = "??";
26
+ this.year = "????";
27
+
28
+ const resultIsoMatch = this.input.match(this._isoMatchRegex());
29
+ const resultCustomMatch = this.input.match(this._customRegex());
30
+ const resultWeekMatch = this.input.match(this._weekMatchRegex());
31
+ const resultYearAndMonthMatch = this.input.match(this._yearAndMonthRegex());
32
+ if (resultIsoMatch) {
33
+ const split = resultIsoMatch[0].split(/-|\/|\s/);
34
+ this.year = split[0];
35
+ this.month = this._setWithLeadingZero(split[1]);
36
+ this.day = this._setWithLeadingZero(split[2]);
37
+ } else if (resultYearAndMonthMatch) {
38
+ const split = resultYearAndMonthMatch[0].split("-");
39
+ this.year = split[0];
40
+ this.month = this._setWithLeadingZero(split[1]);
41
+ this.day = "01";
42
+ } else if (resultCustomMatch) {
43
+ const split = resultCustomMatch[0].split(/\.|\s|\/|-/);
44
+ this.day = this._setWithLeadingZero(split[0]);
45
+ this.month = this._setWithLeadingZero(split[1]);
46
+ this.year = split[2];
47
+ } else if (resultWeekMatch) {
48
+ const split = resultWeekMatch[0].split(/\.|\s|\/|-/);
49
+ const year = Number(split[0]);
50
+ const week = Number(split[1].replace("W0", "").replace("W", ""));
51
+ return this._getDateStrOfISOWeek(year, week);
52
+ } else {
53
+ this._findYear();
54
+ this._findMonth();
55
+ this._findDay();
56
+ }
57
+ return this._buildResult();
58
+ }
59
+
60
+ _buildResult() {
61
+ if (this.year != "????" && this.month === "??") {
62
+ this.month = "01";
63
+ }
64
+ if (this.month != "??" && this.day === "??") {
65
+ this.day = "01";
66
+ }
67
+ return `${this.year}-${this.month}-${this.day}`;
68
+ }
69
+
70
+ /*
71
+ * matches ISO string format (with some extensions)
72
+ * | 2012-01-31 | 2012-1-31 | 2012/01/31 |
73
+ * | 2012/1/31 | 2012 1 31 | 2012 01 31 |
74
+ */
75
+ _isoMatchRegex() {
76
+ return /(?<=\s|^)\d{4}(-|\s|\/)([0][1-9]|[1-9]|10|11|12)(-|\s|\/)([0][1-9]|[1-2][0-9]|3[01]|[1-9])(?=\s|$|\.)/;
77
+ /* | | year | 01-09 | 1-9 | 10-12 | |01-09 |10-29 |30,31| 1-9 |
78
+ * | | dash or slash | dash or slash |
79
+ * |preceding with space or start of string end with space endofstr or dot <-|*/
80
+ }
81
+
82
+ /*
83
+ * matches a custom date string format (reversed order, day-month-year)
84
+ * | 12.3.2000 | 1.2.2012 | 1-2-2012 |
85
+ * | 12/3/2000 | 1 2 2012 | 1/2/2012 |
86
+ */
87
+ _customRegex() {
88
+ return /\d{1,2}(\.|\s|\/|-)\d{1,2}(\.|\s|\/|-)\d{4}/;
89
+ /* | day | | month | | year
90
+ * |___________________|
91
+ * |
92
+ * seperated by dot, space, slash or dash */
93
+ }
94
+
95
+ /*
96
+ * matches a date format that specifies the week
97
+ * | 2012-W1 | 2012 W1 | 2012 W01 |
98
+ * | 2020.W2 | 2020-W53 | 2012/W1 |
99
+ */
100
+ _weekMatchRegex() {
101
+ return /\d{4}(\.|\s|\/|-)W\d{1,2}(?=\s|$|\.)/;
102
+ /* |year | |W1-W53 |
103
+ * |___________________|
104
+ * |
105
+ * seperated by dot, space, slash or dash */
106
+ }
107
+
108
+ /*
109
+ * matches a date format with only year and month
110
+ * YYYY-MM (1-12)
111
+ * | 2020-01 | 2020-12 | 2012-1 |
112
+ */
113
+ _yearAndMonthRegex() {
114
+ return /(?<=\s|^)\d{4}-([0][1-9]|[1-9]|10|11|12)(?=\s|$)/;
115
+ }
116
+
117
+ _findYear() {
118
+ let regex = /[1-9]{1}[0-9]{3}/;
119
+ const result = this.input.match(regex)
120
+ if (result) {
121
+ this.year = result[0];
122
+ this._removeMatchFromInput(result);
123
+ }
124
+ }
125
+
126
+ _findMonth() {
127
+ const months = this._monthDictionaryValues();
128
+ months.forEach(month => {
129
+ let re = new RegExp(`(?<=\\s|^)(${month})(?=\\s|$|\\.)`, "i")
130
+ const result = this.input.match(re);
131
+ if (result) { // yes => get dict and value + return
132
+ this.month = this._monthDictionary()[result[0].toLowerCase()];
133
+ this._removeMatchFromInput(result);
134
+ return this.month;
135
+ }
136
+ })
137
+ return undefined;
138
+ }
139
+
140
+ /*
141
+ * find single numbers from 1-31
142
+ */
143
+ _findDay() {
144
+ let regex = /(?<=\s|^)([0][1-9]|[1-2][0-9]|3[01]|[1-9])(?=\s|$|\.|st|nd|rd|th)/;
145
+ /* | | 01-09 | 10-29 |30,31|1-9 | ends with whitespace, endofstr or dot.
146
+ * | starts with whitepace or startoftr | won't be included in match (lookbehind operator)
147
+ * | look behind operator (not included)
148
+ * | https://stackoverflow.com/a/6713378/6272061 */
149
+ const result = this.input.match(regex)
150
+ if (result) {
151
+ this.day = this._setWithLeadingZero(result[0]);
152
+ }
153
+ }
154
+
155
+ /*
156
+ * if a string or number has only 1 digit or char
157
+ * a leading zro is added
158
+ * returns a string
159
+ */
160
+ _setWithLeadingZero(input) {
161
+ input = input.toString();
162
+ if (input.length == 1) {
163
+ return "0" + input;
164
+ } else {
165
+ return input;
166
+ }
167
+ }
168
+
169
+ _removeMatchFromInput(matchObj) {
170
+ if (matchObj && matchObj[0] && matchObj.index) {
171
+ let len = matchObj[0].length;
172
+ let charArr = this.input.split('');
173
+ charArr.splice(matchObj.index, len);
174
+ this.input = charArr.join("");
175
+ }
176
+ }
177
+
178
+ _monthDictionaryValues() {
179
+ return Object.keys(this._monthDictionary());
180
+ }
181
+ _monthDictionary() {
182
+ return {
183
+ // german
184
+ "jan": "01", "januar": "01",
185
+ "feb": "02", "februar": "02",
186
+ "mär": "03", "märz": "03",
187
+ "apr": "04", "april": "04",
188
+ "mai": "05", "mai": "05",
189
+ "jun": "06", "juni": "06",
190
+ "jul": "07", "juli": "07",
191
+ "aug": "08", "august": "08",
192
+ "sep": "09", "september": "09",
193
+ "okt": "10", "oktober": "10",
194
+ "nov": "11", "november": "11",
195
+ "dez": "12", "dezember": "12",
196
+ // english
197
+ "jan": "01", "january": "01",
198
+ "feb": "02", "february": "02",
199
+ "mar": "03", "march": "03",
200
+ "apr": "04", "april": "04",
201
+ "may": "05", "may": "05",
202
+ "jun": "06", "june": "06",
203
+ "jul": "07", "july": "07",
204
+ "aug": "08", "august": "08",
205
+ "sep": "09", "september": "09",
206
+ "oct": "10", "october": "10",
207
+ "nov": "11", "november": "11",
208
+ "dec": "12", "december": "12",
209
+ // french
210
+ "janv":"01", "janvier": "01",
211
+ "févr":"02", "février'": "02",
212
+ "mars":"03", "mars": "03",
213
+ "avr": "04", "avril": "04",
214
+ "mai": "05", "mai": "05",
215
+ "juin":"06", "juin": "06",
216
+ "juil":"07", "juillet": "07",
217
+ "août":"08", "août": "08",
218
+ "sept":"09", "septembre": "09",
219
+ "oct": "10", "octobre": "10",
220
+ "nov": "11", "novembre": "11",
221
+ "déc": "12", "décembre": "12",
222
+ //italian
223
+ "gen": "01", "gennaio": "01",
224
+ "feb": "02", "febbraio": "02",
225
+ "mar": "03", "marzo": "03",
226
+ "apr": "04", "aprile": "04",
227
+ "mag": "05", "maggio": "05",
228
+ "giu": "06", "giugno": "06",
229
+ "lug": "07", "luglio": "07",
230
+ "ago": "08", "agosto": "08",
231
+ "set": "09", "settembre": "09",
232
+ "ott": "10", "ottobre": "10",
233
+ "nov": "11", "novembre": "11",
234
+ "dic": "12", "dicembre": "12",
235
+ }
236
+ }
237
+
238
+ _getDateStrOfISOWeek(y, w) {
239
+ let simple = new Date(y, 0, 1 + (w - 1) * 7);
240
+ let dow = simple.getDay();
241
+ let ISOweekStart = simple;
242
+ if (dow <= 4) {
243
+ ISOweekStart.setDate(simple.getDate() - simple.getDay() + 1);
244
+ } else {
245
+ ISOweekStart.setDate(simple.getDate() + 8 - simple.getDay());
246
+ }
247
+ // do not rollover to next or previous year
248
+ if (ISOweekStart.getFullYear() > y) {
249
+ return `${y}-12-31`;
250
+ } else if (ISOweekStart.getFullYear() < y) {
251
+ return `${y}-01-01`;
252
+ }
253
+ return this._dateToDateStr(ISOweekStart);
254
+ }
255
+
256
+ /*
257
+ * formats date object to dateStr YYYY-MM-DD
258
+ */
259
+ _dateToDateStr(date) {
260
+ let d = new Date(date);
261
+ let month = this._setWithLeadingZero((d.getMonth() + 1));
262
+ let day = this._setWithLeadingZero(d.getDate());
263
+ let year = d.getFullYear();
264
+ return `${year}-${month}-${day}`;
265
+ }
266
+ }
@@ -206,6 +206,7 @@ export class PbBrowseDocs extends PbLoad {
206
206
  }
207
207
  </style>
208
208
  </custom-style>
209
+ <slot name="header"></slot>
209
210
  <div class="toolbar">
210
211
  <paper-dropdown-menu id="sort" label="${translate(this.sortLabel)}" part="sort-dropdown">
211
212
  <paper-listbox id="sort-list" selected="${this.sortBy}" slot="dropdown-content" class="dropdown-content" attr-for-selected="value">
@@ -58,6 +58,7 @@ import './pb-blacklab-results.js';
58
58
  import './pb-blacklab-highlight.js';
59
59
  import './pb-table-grid.js';
60
60
  import './pb-split-list.js';
61
+ import './pb-timeline.js';
61
62
 
62
63
  import '@polymer/iron-icons/editor-icons';
63
64
  import '@polymer/iron-icons/social-icons';