@internetarchive/histogram-date-range 1.3.1-alpha-webdev7745.0 → 1.3.1

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.
@@ -1,52 +1,52 @@
1
- import type dayjs from 'dayjs/esm';
2
-
3
- /**
4
- * As with the Date(y, m, ...) constructor, dayjs interprets years 0-99 as offsets
5
- * from the year 1900 instead of the actual first-century years.
6
- * We don't want that weird legacy behavior; we want years parsed literally.
7
- *
8
- * The maintainer of dayjs apparently refuses to address this:
9
- * - https://github.com/iamkun/dayjs/pull/548#issuecomment-477660947
10
- * - https://github.com/iamkun/dayjs/issues/1237
11
- *
12
- * So this plugin tries to detect the anomalous cases where the date format
13
- * contains a YYYY block and the parsed date has a year in the 1900-1999 range,
14
- * by checking whether the parsed year actually occurred in the original string.
15
- * If not, then we assume it was parsed incorrectly as an offset, and adjust.
16
- *
17
- * In practice this assumption could fail if the input date is invalid in some
18
- * way (e.g. having overflow, like a YYYY-MM-DD of "1950-22-33", which might be
19
- * converted to 1951-11-02 and produce a false positive). Essentially, we trade away
20
- * leniency for overflow dates to ensure that we handle all valid ones correctly.
21
- * This seems a reasonable tradeoff for our present use cases. But realistically we
22
- * should probably explore moving to a date lib that handles these cases properly.
23
- */
24
- export default function fixFirstCenturyYears(
25
- _: unknown,
26
- dayjsClass: typeof dayjs.Dayjs
27
- ) {
28
- const proto = dayjsClass.prototype;
29
- const oldParse = proto.parse;
30
- proto.parse = function (cfg) {
31
- const inputDate = cfg.date;
32
- const format = cfg.args[1];
33
- oldParse.call(this, cfg);
34
-
35
- const year = this.year();
36
- const isProblemDateRange = year >= 1900 && year < 2000;
37
- const isProblemStringFormat =
38
- typeof format === 'string' && format.includes('YYYY');
39
- const isProblemArrayFormat =
40
- Array.isArray(format) &&
41
- typeof format[0] === 'string' &&
42
- format[0].includes('YYYY');
43
- const isProblemFormat = isProblemStringFormat || isProblemArrayFormat;
44
- const missingParsedYear =
45
- typeof inputDate === 'string' && !inputDate.includes(`${year}`);
46
-
47
- if (isProblemDateRange && isProblemFormat && missingParsedYear) {
48
- this.$d.setFullYear(year - 1900);
49
- this.init(); // Re-initialize with the new date
50
- }
51
- };
52
- }
1
+ import type dayjs from 'dayjs/esm';
2
+
3
+ /**
4
+ * As with the Date(y, m, ...) constructor, dayjs interprets years 0-99 as offsets
5
+ * from the year 1900 instead of the actual first-century years.
6
+ * We don't want that weird legacy behavior; we want years parsed literally.
7
+ *
8
+ * The maintainer of dayjs apparently refuses to address this:
9
+ * - https://github.com/iamkun/dayjs/pull/548#issuecomment-477660947
10
+ * - https://github.com/iamkun/dayjs/issues/1237
11
+ *
12
+ * So this plugin tries to detect the anomalous cases where the date format
13
+ * contains a YYYY block and the parsed date has a year in the 1900-1999 range,
14
+ * by checking whether the parsed year actually occurred in the original string.
15
+ * If not, then we assume it was parsed incorrectly as an offset, and adjust.
16
+ *
17
+ * In practice this assumption could fail if the input date is invalid in some
18
+ * way (e.g. having overflow, like a YYYY-MM-DD of "1950-22-33", which might be
19
+ * converted to 1951-11-02 and produce a false positive). Essentially, we trade away
20
+ * leniency for overflow dates to ensure that we handle all valid ones correctly.
21
+ * This seems a reasonable tradeoff for our present use cases. But realistically we
22
+ * should probably explore moving to a date lib that handles these cases properly.
23
+ */
24
+ export default function fixFirstCenturyYears(
25
+ _: unknown,
26
+ dayjsClass: typeof dayjs.Dayjs
27
+ ) {
28
+ const proto = dayjsClass.prototype;
29
+ const oldParse = proto.parse;
30
+ proto.parse = function (cfg) {
31
+ const inputDate = cfg.date;
32
+ const format = cfg.args[1];
33
+ oldParse.call(this, cfg);
34
+
35
+ const year = this.year();
36
+ const isProblemDateRange = year >= 1900 && year < 2000;
37
+ const isProblemStringFormat =
38
+ typeof format === 'string' && format.includes('YYYY');
39
+ const isProblemArrayFormat =
40
+ Array.isArray(format) &&
41
+ typeof format[0] === 'string' &&
42
+ format[0].includes('YYYY');
43
+ const isProblemFormat = isProblemStringFormat || isProblemArrayFormat;
44
+ const missingParsedYear =
45
+ typeof inputDate === 'string' && !inputDate.includes(`${year}`);
46
+
47
+ if (isProblemDateRange && isProblemFormat && missingParsedYear) {
48
+ this.$d.setFullYear(year - 1900);
49
+ this.init(); // Re-initialize with the new date
50
+ }
51
+ };
52
+ }