vis-chronicle 1.1.0 → 1.2.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/README.md CHANGED
@@ -95,6 +95,8 @@ Item properties:
95
95
  * `general`: General query segment, usually for selecting the item and property.
96
96
  * `start`: Query segment for selecting the start value.
97
97
  * `end`: Query segment for selecting the end value.
98
+ * `startPath`: Another way of writing Wikidata queries. TODO.
99
+ * `endPath`: See `startPath`
98
100
  * `expectedDuration`: Describes the expected duration, for hinting if the start or end is missing.
99
101
  * `min`: The absolute minimum duration.
100
102
  * `max`: The absolute maximum duration.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vis-chronicle",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Generates JSON for populating a vis.js timeline from Wikidata queries.",
5
5
  "keywords": [
6
6
  "wikidata",
@@ -18,7 +18,8 @@
18
18
  },
19
19
  "exports": {
20
20
  "./styles/style.css": "./styles/style.css",
21
- ".": "./src/index.js"
21
+ ".": "./src/index.js",
22
+ "./wikidataToRange.js": "./src/wikidataToRange.js"
22
23
  },
23
24
  "scripts": {},
24
25
  "author": {
package/src/fetch.js CHANGED
@@ -1,4 +1,4 @@
1
- #!/usr/bin/env node
1
+ #!/usr/bin/env node
2
2
 
3
3
  // Uses an input specification file to produce an output file for vis.js Timeline.
4
4
 
@@ -43,6 +43,12 @@ const wikidata = require('./wikidata.js')
43
43
  const renderer = require('./render.js')
44
44
  const mypath = require("./mypath.js");
45
45
  const { flattenRelativeDate } = require('./index.js');
46
+ const wikidataToRange2 = require('./wikidataToRange.js')
47
+
48
+ function wikidataToRange(param)
49
+ {
50
+ return wikidataToRange2(param, wikidata.inputSpec.chronicle.maxUncertainTimePrecision)
51
+ }
46
52
 
47
53
  wikidata.skipCache = values["skip-wd-cache"]
48
54
  wikidata.cacheBuster = values["cachebuster"]
@@ -51,55 +57,6 @@ wikidata.verboseLogging = values["verbose"]
51
57
  wikidata.setLang(values["lang"])
52
58
  wikidata.initialize()
53
59
 
54
- const wikidataToMomentPrecision = [
55
- undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined,
56
- 'year', 'month', 'day', 'hour', 'minute', 'second'
57
- ]
58
-
59
- // produces a range of moments {min,max} from a Wikidata time
60
- function wikidataToRange(inTime)
61
- {
62
- if (!inTime || !inTime.value)
63
- {
64
- // missing value
65
- return undefined
66
- }
67
-
68
- // moment has trouble with negative years unless they're six digits
69
- const date = moment(inTime.value, 'YYYYYY-MM-DDThh:mm:ss')
70
-
71
- switch (inTime.precision)
72
- {
73
- case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
74
- const yearBase = Math.pow(10, 9 - inTime.precision)
75
- var roundedYear = Math.floor(date.year() / yearBase) * yearBase
76
-
77
- // correct for lack of year 0 ("-19" is actually "-20 BC")
78
- // and also for the fact that "-20 BC" is really on the positive side of e.g. -18
79
- if (date.year() < 0) roundedYear += 2
80
-
81
- return {
82
- min: moment({year:roundedYear}),
83
- max: moment({year:roundedYear + yearBase}).subtract(1, 'minute').endOf('year')
84
- }
85
- default:
86
- if (inTime.precision > wikidata.inputSpec.chronicle.maxUncertainTimePrecision)
87
- {
88
- const momentPrecision = wikidataToMomentPrecision[inTime.precision]
89
- return { min: date.clone().startOf(momentPrecision), max: date.clone().startOf(momentPrecision) }
90
- }
91
- else if (inTime.precision < wikidataToMomentPrecision.length)
92
- {
93
- const momentPrecision = wikidataToMomentPrecision[inTime.precision]
94
- return { min: date.clone().startOf(momentPrecision), max: date.clone().endOf(momentPrecision) }
95
- }
96
- else
97
- {
98
- throw `Unrecognized date precision ${inTime.precision}`
99
- }
100
- }
101
- }
102
-
103
60
  function rangeUnionAdv(value, min, max)
104
61
  {
105
62
  var aggregate = {}
@@ -0,0 +1,58 @@
1
+
2
+ const moment = require('moment')
3
+
4
+ const wikidataToMomentPrecision = [
5
+ undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined,
6
+ 'year', 'month', 'day', 'hour', 'minute', 'second'
7
+ ]
8
+
9
+ /**
10
+ * Produces a range of moments from a Wikidata time.
11
+ * @param {*} inTime {value,precision}
12
+ * @param {*} maxUncertainPrecision Max Wikidata precision to treat as uncertain.
13
+ * @returns {*} {min,max}
14
+ */
15
+ module.exports = function wikidataToRange(inTime, maxUncertainPrecision)
16
+ {
17
+ if (!inTime || !inTime.value)
18
+ {
19
+ // missing value
20
+ return undefined
21
+ }
22
+
23
+ if (maxUncertainPrecision === undefined) maxUncertainPrecision = 10
24
+
25
+ // moment has trouble with negative years unless they're six digits
26
+ const date = moment(inTime.value, 'YYYYYY-MM-DDThh:mm:ss')
27
+
28
+ switch (inTime.precision)
29
+ {
30
+ case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
31
+ const yearBase = Math.pow(10, 9 - inTime.precision)
32
+ var roundedYear = Math.floor(date.year() / yearBase) * yearBase
33
+
34
+ // correct for lack of year 0 ("-19" is actually "-20 BC")
35
+ // and also for the fact that "-20 BC" is really on the positive side of e.g. -18
36
+ if (date.year() < 0) roundedYear += 2
37
+
38
+ return {
39
+ min: moment({year:roundedYear}),
40
+ max: moment({year:roundedYear + yearBase}).subtract(1, 'minute').endOf('year')
41
+ }
42
+ default:
43
+ if (inTime.precision > maxUncertainPrecision)
44
+ {
45
+ const momentPrecision = wikidataToMomentPrecision[inTime.precision]
46
+ return { min: date.clone().startOf(momentPrecision), max: date.clone().startOf(momentPrecision) }
47
+ }
48
+ else if (inTime.precision < wikidataToMomentPrecision.length)
49
+ {
50
+ const momentPrecision = wikidataToMomentPrecision[inTime.precision]
51
+ return { min: date.clone().startOf(momentPrecision), max: date.clone().endOf(momentPrecision) }
52
+ }
53
+ else
54
+ {
55
+ throw `Unrecognized date precision ${inTime.precision}`
56
+ }
57
+ }
58
+ }