vis-chronicle 1.2.1 → 1.2.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vis-chronicle",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
4
4
  "description": "Generates JSON for populating a vis.js timeline from Wikidata queries.",
5
5
  "keywords": [
6
6
  "wikidata",
package/src/fetch.js CHANGED
@@ -131,7 +131,7 @@ entryPoint()
131
131
  {
132
132
  if (item.id)
133
133
  {
134
- if (itemIds.has(item.id))
134
+ if (!itemIds.has(item.id))
135
135
  {
136
136
  itemIds.add(item.id)
137
137
  }
@@ -1,6 +1,7 @@
1
1
 
2
2
  const moment = require('moment')
3
3
  const { toJewishDate, toGregorianDate, getIndexByJewishMonth } = require("jewish-date");
4
+ const wikidata = require('./wikidata');
4
5
 
5
6
  function momentToHDate(inMoment)
6
7
  {
@@ -26,23 +27,19 @@ function durationToWikidataPrecision(duration)
26
27
  // Flattens a relative date string into a hard date string
27
28
  module.exports = function flattenRelativeDate(wikidataCache, dateString)
28
29
  {
30
+ if (dateString == '') return null
31
+
29
32
  // parse out relative date components
30
- const relSplit = []
31
- const relExp = /[\+>][A-Za-z0-9\-]+/g
32
- var lastSep = 0
33
- while (true)
33
+ var relSplit
34
+ var match = dateString.match(wikidata.pathQueryRegex)
35
+ if (match)
34
36
  {
35
- var match = relExp.exec(dateString)
36
- if (match)
37
- {
38
- relSplit.push(dateString.substring(lastSep, match.index))
39
- }
40
- else
41
- {
42
- relSplit.push(dateString.substring(lastSep))
43
- break
44
- }
45
- lastSep = match.index
37
+ relSplit = match.slice(1)
38
+ }
39
+ else
40
+ {
41
+ console.error(`Failed to parse relative date '${dateString}'.`)
42
+ return null
46
43
  }
47
44
 
48
45
  if (!relSplit[0])
@@ -70,7 +67,12 @@ module.exports = function flattenRelativeDate(wikidataCache, dateString)
70
67
  for (var i = 1; i < relSplit.length; i++)
71
68
  {
72
69
  const component = relSplit[i]
73
- if (component[0] == "+")
70
+ if (!component)
71
+ {
72
+ // empty group from regex
73
+ continue
74
+ }
75
+ else if (component[0] == "+")
74
76
  {
75
77
  const momentDelta = moment.duration(component.substring(1))
76
78
  momentDate = momentDate.add(momentDelta)
package/src/wikidata.js CHANGED
@@ -41,6 +41,8 @@ const wikidata = module.exports = {
41
41
  rankNormal: "http://wikiba.se/ontology#NormalRank",
42
42
  rankPreferred: "http://wikiba.se/ontology#PreferredRank",
43
43
 
44
+ pathQueryRegex: /^(Q[0-9]+(?::P[0-9]+)?(?::Q[0-9]+:P[0-9]+)?)([\+>](?:[A-Za-z]+(?:![0-9]+)?|P\-?[0-9A-Z]+)+)*$/,
45
+
44
46
  initialize: function()
45
47
  {
46
48
  const chroniclePackage = require("../package.json")
@@ -499,21 +501,22 @@ const wikidata = module.exports = {
499
501
  const wdQualifiers = new Set()
500
502
  for (const query of queries)
501
503
  {
502
- if (query.startsWith("Q"))
504
+ const match = query.match(/^(Q[0-9]+(?::P[0-9]+)?(?::Q[0-9]+:P[0-9]+)?)([\+>](?:[A-Za-z]+(?:![0-9]+)?|P\-?[0-9A-Z]+)+)*$/)
505
+ if (match && match[1])
503
506
  {
504
- const relSplit = query.split(/[\+>]/) // handle relative date
505
- const split = relSplit[0].split(':')
507
+ const root = match[1]
508
+ const split = root.split(':')
506
509
  if (split.length == 1)
507
510
  {
508
- wdStandaloneIds.add(relSplit[0])
511
+ wdStandaloneIds.add(root)
509
512
  }
510
513
  else if (split.length == 2)
511
514
  {
512
- wdProperties.add(relSplit[0])
515
+ wdProperties.add(root)
513
516
  }
514
517
  else if (split.length == 4)
515
518
  {
516
- wdQualifiers.add(relSplit[0])
519
+ wdQualifiers.add(root)
517
520
  }
518
521
  else
519
522
  {