-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathg7datatypes.js
More file actions
386 lines (367 loc) · 14.8 KB
/
g7datatypes.js
File metadata and controls
386 lines (367 loc) · 14.8 KB
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/**
* @module g7datatypes
*
* Many methods in the classes in this file depend on
* a G7Lookups object named `lookup` (see `g7lookups.js`)
*/
export { G7Age, G7Date, G7DateValue, G7Time, G7Enum, G7Datatype, checkDatatype }
class G7Datatype {
constructor() { throw new Error("Use fromString or fromJSON instead"); }
/**
* Parses a payload encoded as a JSON value into a payload object
*
* @param {Object|string} o - the JSON value to parse
* @param {G7Lookups} lookup
*/
static fromJSON(o, lookup) {
if ('object' != typeof o) return o
if ('calendar' in o) return new G7Date(o, lookup)
if ('type' in o) return new G7DateValue(o, lookup)
if ('hour' in o) return new G7Time(o, lookup)
if ('value' in o) return new G7Enum(o, lookup)
return new G7Age(o, lookup) // might be {} so no "in" check
}
/**
* Parses a payload string into a payload object
*
* @param {object} type - the datatype definition; type.type is a URI, may have other fields for some datatypes
* @param {string} payload - the string to parse
* @param {G7Lookups} lookup
*/
static fromString(pltype, str, lookup) {
if (str && 'string' != typeof str) return str
switch(pltype.type) {
case '?':
return str
case null:
if (str) lookup.err?.(`no payload allowed`)
return undefined
case 'pointer':
if (str !== null) lookup.err?.(`pointer payload expected, not ${JSON.stringify(str)}`)
return null
case 'http://www.w3.org/2001/XMLSchema#nonNegativeInteger':
if (/^[0-9]+$/.test(str)) return Number(str)
lookup.err?.(`integer payload expected, not ${JSON.stringify(str)}`)
return 0
case 'https://gedcom.io/terms/v7/type-Age':
return new G7Age(str, lookup)
case 'https://gedcom.io/terms/v7/type-Date':
return new G7DateValue(str, lookup)
case 'https://gedcom.io/terms/v7/type-Date#period':
{
const ans = new G7DateValue(str, lookup)
if (ans.type != 'DatePeriod' && ans.type != 'empty') {
lookup.err?.(`Expected DatePeriod, not ${ans.type}`)
ans.type = 'empty'
ans.date = undefined
ans.date2 = undefined
}
return ans
}
case 'https://gedcom.io/terms/v7/type-Date#exact':
if (/^[0-9]+ (JAN|FEB|MAR|APR|MAY|JUN|JUL|AUG|SEP|OCT|NOV|DEC) [0-9]+$/.test(str))
return new G7Date(str, lookup)
lookup.err?.(`Invalid DateExact: ${JSON.stringify(str)}`)
return new G7Date("1 JAN 0", lookup)
case 'https://gedcom.io/terms/v7/type-Time':
return new G7Time(str, lookup)
case 'https://gedcom.io/terms/v7/type-List#Text':
{
const ans = str.split(/\s*,\s*/g)
ans.toString = function(){return this.join(', ')}
return ans
}
case 'https://gedcom.io/terms/v7/type-Enum':
return new G7Enum(lookup.enumval(pltype.set, str), lookup)
case 'https://gedcom.io/terms/v7/type-List#Enum':
{
const ans = str.split(/\s*,\s*/g).map(s => new G7Enum(lookup.enumval(pltype.set, s), lookup))
ans.toString = function(){return this.join(', ')}
return ans
}
case 'Y|<NULL>':
if (str && str !== 'Y') lookup.err?.(`Expected "Y" or "", not ${JSON.stringify(str)}`)
return str === 'Y' ? 'Y' : ''
case 'https://gedcom.io/terms/v7/type-Name':
if (!/^[^\0-\x1f\/]*(\/[^\0-\x1f\/]*\/[^\0-\x1f\/]*)?$/.test(str)) {
lookup.err?.(`Invalid personal name ${JSON.stringify(str)}; replacing "/" with "\u2044"`)
return str.replaceAll('/','\u2044')
} else return str
case 'http://www.w3.org/2001/XMLSchema#Language':
if(!/^[a-zA-Z]{2,3}(-[a-zA-Z]{3}){0,3}|[a-zA-Z]{4,8}(-[a-zA-Z]{4})?(-[a-zA-Z]{2}|[0-9]{3})?(-[a-zA-Z]{5,8}|[0-9][a-zA-Z0-9]{3})*(-[0-9A-WY-Za-wy-z](-[a-zA-Z0-9]{2,8})+)*(-x(-[a-zA-Z0-9]{1,8})+)?|x(-[a-zA-Z0-9]{1,8})+|en-GB-oed|i-(ami|bnn|default|enochian|hak|klingon|lux|mingo|navajo|pwn|tao|tay|tsu)|sgn-(BE-(FR|NL)|CH-DE)$/.test(str)) {
lookup.err?.(`Invalid language tag ${JSON.stringify(str)}; using "und" instead`)
return `und`
} else return str
case 'http://www.w3.org/ns/dcat#mediaType':
if (!/^[!-'*-+\--.0-9A-Z^-}]+\/[!-'*-+\--.0-9A-Z^-}]+(;[!-'*-+\--.0-9A-Z^-}]+\=([!-'*-+\--.0-9A-Z^-}]+|("(\\.|[^\\"\r])*")))*$/.test(str)) {
lookup.err?.(`Invalid media type ${JSON.stringify(str)}; using "application/octet-stream" instead`)
return "application/octet-stream"
} else return str
case 'http://www.w3.org/2001/XMLSchema#string':
return str || ''
default:
lookup.warn?.(`parser does not understand datatype ${pltype.type}; leaving as-is`)
return str
}
}
}
/**
* @param payload - a payload of any type
* @plt {Object} - a payload type definition object
*/
function checkDatatype(payload, plt) {
switch(plt.type) {
case '?': return true
case null: return payload === undefined
case 'pointer':
return payload === null || (payload?.type === plt.to)
case 'http://www.w3.org/2001/XMLSchema#nonNegativeInteger': return 'number' === typeof payload && (0|payload) == payload && payload >= 0
case 'https://gedcom.io/terms/v7/type-List#Text': return Array.isArray(payload) && payload.map(e => 'string' == typeof e).reduce((x,y)=>x&&y, true)
case 'https://gedcom.io/terms/v7/type-List#Enum': return Array.isArray(payload) && payload.map(e => e instanceof G7Enum).reduce((x,y)=>x&&y, true)
case 'https://gedcom.io/terms/v7/type-Date': return payload instanceof G7DateValue
case 'https://gedcom.io/terms/v7/type-Date#exact': return payload instanceof G7Date && payload.calendar === 'https://gedcom.io/terms/v7/cal-GREGORIAN' && payload.day !== undefined
case 'https://gedcom.io/terms/v7/type-Date#period': return payload instanceof G7DateValue && (payload.type === 'empty' || payload.type === 'DatePeriod')
case 'https://gedcom.io/terms/v7/type-Enum': return payload instanceof G7Enum
case 'https://gedcom.io/terms/v7/type-Age': return payload instanceof G7Age
case 'https://gedcom.io/terms/v7/type-Time': return payload instanceof G7Time
case 'Y|<NULL>': return !payload || payload === 'Y'
case 'https://gedcom.io/terms/v7/type-Name': return 'string' == typeof payload && payload && /^[^\0-\x1f\/]*(\/[^\0-\x1f\/]*\/[^\0-\x1f\/]*)?$/.test(payload)
case 'http://www.w3.org/2001/XMLSchema#Language': return 'string' == typeof payload && /^[a-zA-Z]{2,3}(-[a-zA-Z]{3}){0,3}|[a-zA-Z]{4,8}(-[a-zA-Z]{4})?(-[a-zA-Z]{2}|[0-9]{3})?(-[a-zA-Z]{5,8}|[0-9][a-zA-Z0-9]{3})*(-[0-9A-WY-Za-wy-z](-[a-zA-Z0-9]{2,8})+)*(-x(-[a-zA-Z0-9]{1,8})+)?|x(-[a-zA-Z0-9]{1,8})+|en-GB-oed|i-(ami|bnn|default|enochian|hak|klingon|lux|mingo|navajo|pwn|tao|tay|tsu)|sgn-(BE-(FR|NL)|CH-DE)$/.test(payload)
case 'http://www.w3.org/ns/dcat#mediaType': return 'string' == typeof payload && /^[!-'*-+\--.0-9A-Z^-}]+\/[!-'*-+\--.0-9A-Z^-}]+(;[!-'*-+\--.0-9A-Z^-}]+\=([!-'*-+\--.0-9A-Z^-}]+|("(\\.|[^\\"\r])*")))*$/.test(payload)
default: return !payload || 'string' == typeof payload
}
}
/**
* An object representing the datatype https://gedcom.io/terms/v7/type-Age
*/
class G7Age {
/** `'<'` or `'>'` or `undefined`; if defined, so is at least one other field */
mod
/** non-negative integer, or `undefined` */
years
/** non-negative integer, or `undefined` */
months
/** non-negative integer, or `undefined` */
weeks
/** non-negative integer, or `undefined` */
days
constructor(payload, lookup) {
if (!payload) return // empty is OK
if ('object' == typeof payload) { // JSON
Object.entries(payload).forEach(([k,v]) => {
if (!(k in this)) lookup.err?.(`Can't set ${k} of a G7Age`)
else this[k] = v
})
return
}
let m = /^(?:([<>]))?(?:([0-9]+)y(?: |$))?(?:([0-9]+)m(?: |$))?(?:([0-9]+)w(?: |$))?(?:([0-9]+)d)?$/.exec(payload)
if (!m || (!m[2] && !m[3] && !m[4] && !m[5])) {
lookup.err?.(`Invalid age: ${JSON.stringify(payload)}`)
this.mod = '>'
this.years = 0
return
}
this.mod = m[1]
this.years = m[2] ? Number(m[2]) : undefined
this.months = m[3] ? Number(m[3]) : undefined
this.weeks = m[4] ? Number(m[4]) : undefined
this.days = m[5] ? Number(m[5]) : undefined
}
toString() {
let ans = this.mod || ''
if (this.years) ans += this.years+'y '
if (this.months) ans += this.months+'m '
if (this.weeks) ans += this.weeks+'w '
if (this.days) ans += this.days+'d'
return ans.trim()
}
isEmpty() { return this.years === undefined && this.months === undefined && this.weeks === undefined && this.days === undefined }
}
/**
* An object representing the `date` production that is part of various date datatypes
*/
class G7Date {
/** URI of a calendar; always set */
calendar
/** non-negative integer; always set */
year
/** URI of a month in the calendar, or `undefined` */
month
/** non-negative integer, or `undefined` */
day
/** tag of an epoch in the calendar, or `undefined` */
epoch
#lookup
constructor(payload, lookup) {
this.#lookup = lookup
if ('object' === typeof payload) {
Object.entries(payload).forEach(([k,v]) => {
if (!(k in this)) lookup.err?.(`Can't set ${k} of a G7Date`)
else this[k] = v
})
return
}
let m = /^(?:(GREGORIAN|JULIAN|FRENCH_R|HEBREW|_[A-Z0-9_]+) )?(?:(?:([0-9]+) )?([A-Z_][A-Z0-9_]+) )?([0-9]+)(?: (BCE|_[A-Z0-9_]+))?$/.exec(payload)
if (!m) {
lookup.err?.(`Invalid date: ${JSON.stringify(payload)}`)
this.calendar = '_ERROR'
this.year = 0
return
}
let cal = lookup.calendar(m[1] || 'GREGORIAN')
this.calendar = cal.type
this.year = Number(m[4])
if (m[3]) {
this.month = lookup.month(cal, m[3])
}
this.day = m[2] && Number(m[2])
if (m[5]) {
if ('epochs' in cal) {
if (cal.epochs.includes(m[5])) this.epoch = m[5]
else lookup.err?.(`Invalid epoch ${m[5]} in calendar ${this.calendar}`)
} else {
this.epoch = m[5]
}
}
}
/**
* @param {bool} showGregorian - if true, use "GREGORIAN" instead of ""
*/
toString(showGregorian) {
let ans = ''
if (showGregorian || this.calendar != 'https://gedcom.io/terms/v7/cal-GREGORIAN')
ans += this.#lookup.tag(this.calendar)+' '
if (this.day !== undefined) ans += this.day+' '
if (this.month !== undefined) ans += this.#lookup.tag(this.month)+' '
ans += this.year
if (this.epoch !== undefined) ans += ' '+this.epoch
return ans
}
}
/**
* An object representing the datatype https://gedcom.io/terms/v7/type-Date
* and the datatype https://gedcom.io/terms/v7/type-Date#period
*/
class G7DateValue {
/** One of: `"dateRange"`, `"DatePeriod"`, `"date"`, `"ABT"`, `"CAL"`, `"EST"`, `"empty"` */
type
/** a G7Date, or `undefined`; the only date, or start dare of a window */
date
/** a G7Date, or `undefined`; the end dare of a window */
date2
constructor(payload, lookup) {
if ('object' === typeof payload) {
this.type = payload.type
if (payload.date) this.date = new G7Date(payload.date, lookup)
if (payload.date2) this.date2 = new G7Date(payload.date2, lookup)
return
}
let m = /^(ABT|CAL|EST) (.*)$|^BET (.*) AND (.*)$|^BEF (.*)$|^AFT (.*)$|^FROM (.*) TO (.*)$|^FROM (.*)$|^TO (.*)$|^(.+)$/.exec(payload)
if (!m) {
if (payload) lookup.err?.(`Invalid date ${JSON.stringify(payload)}`)
this.type = 'empty'
} else if (m[1]) {
this.type = m[1]
this.date = new G7Date(m[2], lookup)
} else if (m[3]) {
this.type = 'dateRange'
this.date = new G7Date(m[3], lookup)
this.date2 = new G7Date(m[4], lookup)
} else if (m[5]) {
this.type = 'dateRange'
this.date2 = new G7Date(m[5], lookup)
} else if (m[6]) {
this.type = 'dateRange'
this.date = new G7Date(m[6], lookup)
} else if (m[7]) {
this.type = 'DatePeriod'
this.date = new G7Date(m[7], lookup)
this.date2 = new G7Date(m[8], lookup)
} else if (m[9]) {
this.type = 'DatePeriod'
this.date = new G7Date(m[9], lookup)
} else if (m[10]) {
this.type = 'DatePeriod'
this.date2 = new G7Date(m[10], lookup)
} else if (m[11]) {
this.type = 'date'
this.date = new G7Date(m[11], lookup)
} else {
if (payload) lookup.err?.(`Invalid date ${JSON.stringify(payload)}`)
this.type = 'empty'
}
}
toString() {
if (this.type == 'empty') return ''
else if (this.type == 'date') return this.date.toString(false)
else if (this.type == 'ABT' || this.type == 'CAL' || this.type == 'EST') {
return this.type+' '+this.date.toString(false)
} else if (this.type == 'dateRange') {
if (this.date && this.date2)
return 'BET '+this.date.toString(this.date.calendar != this.date2.calendar)+' AND '+this.date2.toString(this.date.calendar != this.date2.calendar)
else if (date)
return 'AFT '+this.date.toString(false)
else
return 'BEF '+this.date.toString(false)
} else if (this.type == 'DatePeriod') {
if (this.date && this.date2)
return 'FROM '+this.date.toString(this.date.calendar != this.date2.calendar)+' TO '+this.date2.toString(this.date.calendar != this.date2.calendar)
else if (this.date)
return 'FROM '+this.date.toString(false)
else
return 'TO '+this.date.toString(false)
} else {
throw new Error(`Cannot serialize unknown date type ${this.type}`)
}
}
isEmpty() { return this.type === 'empty' }
}
/**
* An object representing the datatype https://gedcom.io/terms/v7/type-Time
*/
class G7Time {
/** non-negative integer in 24-hour clock */
hour
/** non-negative integer */
minute
/** non-negative real number, or `undefined` */
second
/** `"Z"`, or `undefined` */
tz
constructor(payload, lookup) {
if ('object' == typeof payload) { // JSON
Object.entries(payload).forEach(([k,v]) => {
if (!(k in this)) lookup.err?.(`Can't set ${k} of a G7Age`)
else this[k] = v
})
return
}
let m = /^([01]?[0-9]|2[0-3]):([0-5][0-9])(?::([0-5][0-9](?:[.][0-9]+)?))?(?:(Z))?$/.exec(payload)
if (!m) {
lookup.err?.(`Invalid time: ${JSON.stringify(payload)}`)
this.hour = 0
this.minute = 0
} else {
this.hour = Number(m[1])
this.minute = Number(m[2])
this.second = m[3] && Number(m[3])
this.tz = m[4]
}
}
toString() {
return (100+this.hour).toString().substr(1) + ':' + (100+this.minute).toString().substr(1) + (this.second === undefined ? '' : ':'+(100+this.second).toString().substr(1)) + (this.tz || "")
}
}
/**
* An object representing the datatype https://gedcom.io/terms/v7/type-Enum
*/
class G7Enum {
value
#lookup
constructor(uri, lookup) {
this.#lookup = lookup
if ('object' == typeof uri) this.value = uri.value
else this.value = uri
}
toString() { return this.#lookup.tag(this.value) }
}