@internetarchive/histogram-date-range 1.3.1 → 1.3.2-alpha-webdev7713.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/demo/index.css +6 -0
- package/demo/index.html +15 -0
- package/dist/demo/js/app-root.d.ts +19 -19
- package/dist/demo/js/app-root.js +46 -46
- package/dist/demo/js/app-root.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/src/histogram-date-range copy.d.ts +214 -0
- package/dist/src/histogram-date-range copy.js +1018 -0
- package/dist/src/histogram-date-range copy.js.map +1 -0
- package/dist/src/histogram-date-range.d.ts +214 -213
- package/dist/src/histogram-date-range.js +1024 -1008
- package/dist/src/histogram-date-range.js.map +1 -1
- package/dist/src/plugins/fix-first-century-years.d.ts +23 -23
- package/dist/src/plugins/fix-first-century-years.js +42 -42
- package/dist/src/plugins/fix-first-century-years.js.map +1 -1
- package/dist/test/histogram-date-range.test.d.ts +1 -1
- package/dist/test/histogram-date-range.test.js +698 -697
- package/dist/test/histogram-date-range.test.js.map +1 -1
- package/docs/_snowpack/pkg/import-map.json +2 -1
- package/docs/_snowpack/pkg/lit/decorators.js +14 -1
- package/docs/_snowpack/pkg/lit/directives/style-map.js +10 -0
- package/docs/demo/index.css +6 -0
- package/docs/demo/index.html +15 -0
- package/docs/dist/src/histogram-date-range.js +31 -22
- package/package.json +11 -11
- package/src/histogram-date-range.ts +1110 -1094
- package/src/plugins/fix-first-century-years.ts +52 -52
- package/test/histogram-date-range.test.ts +935 -934
- package/types/dayjs.d.ts +10 -10
|
@@ -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
|
+
}
|