This repository has been archived by the owner on Sep 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
formatDateClaim.js
133 lines (126 loc) · 3.94 KB
/
formatDateClaim.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import moment from "moment/min/moment-with-locales";
import wbk from "wikidata-sdk";
import ordinalize from "ordinalize";
import { SOURCING_CIRCUMSTANCES_ID } from "../constants/properties";
import { CIRCA_ID, PRESUMABLY_ID } from "../constants/entities";
export default function formatDateClaim(claim, languageCode, yearOnly = false) {
if (!claim) return;
let cleanClaims = [];
claim.forEach((c) => {
if (
c.mainsnak &&
c.mainsnak.datavalue &&
c.mainsnak.datavalue.value &&
c.mainsnak.datavalue.value.time
) {
if (c.rank === "normal") cleanClaims.push(c);
if (c.rank === "preferred") cleanClaims.unshift(c);
//What about the deprecated ones?
}
});
const firstDate = cleanClaims[0];
if (!firstDate) return;
const {
mainsnak: {
datavalue: { value },
},
} = firstDate;
let sourcingCircumstances = null;
if (
firstDate.qualifiers &&
firstDate.qualifiers[SOURCING_CIRCUMSTANCES_ID] &&
firstDate.qualifiers[SOURCING_CIRCUMSTANCES_ID][0] &&
firstDate.qualifiers[SOURCING_CIRCUMSTANCES_ID][0].datavalue.value
) {
sourcingCircumstances =
firstDate.qualifiers[SOURCING_CIRCUMSTANCES_ID][0].datavalue.value.id;
}
return parseDate(value, languageCode, yearOnly, sourcingCircumstances);
}
/**
*
* @param wikidatatime
* @returns {{output: null, dateObject: null}|{output: string, dateObject: number}|{output: *, dateObject: null}}
*/
function parseDate(
wikidatatime,
languageCode = "en",
yearOnly = false,
sourcingCircumstances = null
) {
//example of valid object {time: "+1500-07-07T00:00:00Z" ,precision:8}
//https://www.wikidata.org/wiki/Help:Dates
/*
0 - billion years
3 - million years
4 - hundred thousand years
6 - millenium
7 - century
8 - decade
9 - year (only year)
10 - month (only month);
11 - day
*/
let { precision, time } = wikidatatime;
//for precision < 6 this doesn't make sense
const dateISOString = wbk.wikibaseTimeToISOString(time);
const dateOnly = dateISOString.split("T")[0];
let parsedDate = moment(dateOnly);
const year = parsedDate.year();
let eraSuffix = ""; //moment has only BC
if (year <= 0) {
parsedDate.add(1, "year"); //adjust moment wrong year formatting for BCE
eraSuffix = " BCE";
}
if (yearOnly && precision > 9) {
precision = 9;
}
let sourcingPrefix = "";
let sourcingPostfix = "";
if (sourcingCircumstances === CIRCA_ID) {
//circa
sourcingPrefix = "~";
} else if (sourcingCircumstances === PRESUMABLY_ID) {
//presumably/maybe
sourcingPostfix = "?";
}
switch (precision) {
case 0:
let [, byear] = time.split("-");
let bya = parseFloat(+byear / 1e9);
return bya + " Bya";
case 2: //Earth Q2 has precision 2 WTF, not in docs
let [, myear2] = time.split("-");
let mya2 = parseFloat(+myear2 / 1e6);
return mya2 + " Mya";
case 3:
let [, myear] = time.split("-");
let mya = parseFloat(+myear / 1e6);
return mya + " Mya";
case 4:
let [, kyear] = time.split("-");
let kya = parseFloat(+kyear / 1e3); //should be 1e5
return kya + " Kya";
case 6:
let millenniumIndex = Math.abs(Math.floor(year / 1000));
let millenniumNumber = year > 0 ? millenniumIndex + 1 : millenniumIndex;
return ordinalize(millenniumNumber) + " mill." + eraSuffix;
case 7:
let centuryNumber =
year > 0 ? Math.ceil(year / 100) : Math.abs(Math.floor(year / 100));
return ordinalize(centuryNumber) + " cent." + eraSuffix;
case 8:
return Math.floor(year / 10) + "0s" + eraSuffix;
case 9:
return (
sourcingPrefix + parsedDate.format("y") + sourcingPostfix + eraSuffix
);
case 10:
return parsedDate.locale(languageCode).format("MMM y") + eraSuffix;
case 11: {
return parsedDate.locale(languageCode).format("D MMM y") + eraSuffix;
}
default:
return wbk.wikibaseTimeToSimpleDay(wikidatatime);
}
}