Skip to content

Commit 0443f73

Browse files
slusarzcmouse
authored andcommitted
events: Abstract parsing code out into reusable library/module
1 parent f2a711e commit 0443f73

File tree

2 files changed

+134
-126
lines changed

2 files changed

+134
-126
lines changed

lib/data/events.data.js

Lines changed: 3 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,131 +1,8 @@
1-
import { addWatchPaths, loadData, normalizeArrayData } from '../utility.js'
2-
import { getVitepressMd } from '../markdown.js'
3-
4-
/* Take the events list and normalize entries and process inheritance. */
5-
function processInherit(i, ob) {
6-
let fields = {}
7-
8-
if (i in ob.events) {
9-
fields = { ...fields, ...normalizeEventsFields(ob.events[i].fields) }
10-
if (ob.events[i].inherit) {
11-
fields = { ...fields, ...processInheritList(ob.events[i].inherit, ob) }
12-
}
13-
} else if (i in ob.inherits) {
14-
fields = { ...fields, ...normalizeEventsFields(ob.inherits[i].fields) }
15-
if (ob.inherits[i].inherit) {
16-
fields = { ...fields, ...processInheritList(ob.inherits[i].inherit, ob) }
17-
}
18-
}
19-
20-
return fields
21-
}
22-
23-
function processInheritList(i_list, ob) {
24-
/* Normalize inherit. */
25-
let inherit = i_list
26-
if (typeof i_list === 'string') {
27-
i_list = [ i_list ]
28-
}
29-
30-
let fields = {}
31-
for (const i of i_list.values()) {
32-
fields = { ...fields, ...processInherit(i, ob) }
33-
}
34-
35-
return fields
36-
}
37-
38-
function normalizeEventsFields(fields) {
39-
let f = {}
40-
41-
for (const[k, v] of Object.entries(fields)) {
42-
let v2
43-
if (typeof v === 'string') {
44-
v2 = { text: v }
45-
} else {
46-
v2 = v
47-
}
48-
49-
f[k] = { ...{
50-
// This is a single value
51-
//added: false,
52-
// Need to normalize, as changed can be multiple values.
53-
changed: {},
54-
// This is a single value
55-
//deleted: false,
56-
// This is a single value
57-
//deprecated: false,
58-
text: false
59-
}, ...v2}
60-
}
61-
62-
return f
63-
}
64-
65-
/* Return list of added/changed/deprecated/removed items, this can optionally
66-
* be prefixed with a symbol to mark up an itemization, e.g. '* '. */
67-
function appendListOfUpdates(field, prefix = '') {
68-
let out = ''
69-
for (const f of [ 'added', 'changed', 'deprecated', 'removed' ]) {
70-
for (const update_key of Object.keys(field[f] ?? {})) {
71-
const change_content = field[f][update_key] ?
72-
`: ${field[f][update_key]}` :
73-
''
74-
out += `\n${prefix}[[${f},${update_key}]]${change_content}`
75-
}
76-
}
77-
78-
return out
79-
}
80-
81-
async function normalizeEvents(events, global_inherits, inherits) {
82-
events = normalizeArrayData(
83-
events,
84-
['tags']
85-
)
86-
87-
const md = await getVitepressMd()
88-
89-
for (const [k, v] of Object.entries(events)) {
90-
if (!v) {
91-
delete events[k]
92-
continue
93-
}
94-
95-
/* Process inheritance. */
96-
let fields = normalizeEventsFields(global_inherits)
97-
if (v.fields) {
98-
fields = { ...fields, ...normalizeEventsFields(v.fields) }
99-
}
100-
if (v.inherit) {
101-
const ob = {
102-
events: events,
103-
inherits: inherits,
104-
}
105-
fields = { ...fields, ...processInheritList(v.inherit, ob) }
106-
}
107-
108-
/* Append list of changes as itemization to field's description. */
109-
for (const [k2, v2] of Object.entries(fields)) {
110-
v2.text = md.render(`${v2.text}${appendListOfUpdates(v2, '* ')}`)
111-
}
112-
113-
v.fields = fields
114-
115-
/* Prefix the list of changes to the event's description. */
116-
v.text = v.text ? md.render(`${appendListOfUpdates(v)}\n${v.text}`) : null
117-
}
118-
119-
return events
120-
}
1+
import { addWatchPaths } from '../utility.js'
2+
import { loadEvents } from '../events.js'
1213

1224
export default addWatchPaths({
1235
async load() {
124-
const data = loadData('events')
125-
return await normalizeEvents(
126-
structuredClone(data.events),
127-
structuredClone(data.global_inherits),
128-
structuredClone(data.inherits)
129-
)
6+
return await loadEvents()
1307
}
1318
})

lib/events.js

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
import { loadData, normalizeArrayData } from './utility.js'
2+
import { getVitepressMd } from './markdown.js'
3+
4+
/* Take the events list and normalize entries and process inheritance. */
5+
function processInherit(i, ob) {
6+
let fields = {}
7+
8+
if (i in ob.events) {
9+
fields = { ...fields, ...normalizeEventsFields(ob.events[i].fields) }
10+
if (ob.events[i].inherit) {
11+
fields = { ...fields, ...processInheritList(ob.events[i].inherit, ob) }
12+
}
13+
} else if (i in ob.inherits) {
14+
fields = { ...fields, ...normalizeEventsFields(ob.inherits[i].fields) }
15+
if (ob.inherits[i].inherit) {
16+
fields = { ...fields, ...processInheritList(ob.inherits[i].inherit, ob) }
17+
}
18+
}
19+
20+
return fields
21+
}
22+
23+
function processInheritList(i_list, ob) {
24+
/* Normalize inherit. */
25+
let inherit = i_list
26+
if (typeof i_list === 'string') {
27+
i_list = [ i_list ]
28+
}
29+
30+
let fields = {}
31+
for (const i of i_list.values()) {
32+
fields = { ...fields, ...processInherit(i, ob) }
33+
}
34+
35+
return fields
36+
}
37+
38+
function normalizeEventsFields(fields) {
39+
let f = {}
40+
41+
for (const[k, v] of Object.entries(fields)) {
42+
let v2
43+
if (typeof v === 'string') {
44+
v2 = { text: v }
45+
} else {
46+
v2 = v
47+
}
48+
49+
f[k] = { ...{
50+
// This is a single value
51+
//added: false,
52+
// Need to normalize, as changed can be multiple
53+
// values.
54+
changed: {},
55+
// This is a single value
56+
//deleted: false,
57+
// This is a single value
58+
//deprecated: false,
59+
text: false
60+
}, ...v2}
61+
}
62+
63+
return f
64+
}
65+
66+
/* Return list of added/changed/deprecated/removed items, this can optionally
67+
* be prefixed with a symbol to mark up an itemization, e.g. '* '. */
68+
function appendListOfUpdates(field, prefix = '') {
69+
let out = ''
70+
for (const f of [ 'added', 'changed', 'deprecated', 'removed' ]) {
71+
for (const update_key of Object.keys(field[f] ?? {})) {
72+
const change_content = field[f][update_key] ?
73+
`: ${field[f][update_key]}` :
74+
''
75+
out += `\n${prefix}[[${f},${update_key}]]${change_content}`
76+
}
77+
}
78+
79+
return out
80+
}
81+
82+
async function normalizeEvents(events, global_inherits, inherits) {
83+
events = normalizeArrayData(
84+
events,
85+
['tags']
86+
)
87+
88+
const md = await getVitepressMd()
89+
90+
for (const [k, v] of Object.entries(events)) {
91+
if (!v) {
92+
delete events[k]
93+
continue
94+
}
95+
96+
/* Process inheritance. */
97+
let fields = normalizeEventsFields(global_inherits)
98+
if (v.fields) {
99+
fields = { ...fields, ...normalizeEventsFields(v.fields) }
100+
}
101+
if (v.inherit) {
102+
const ob = {
103+
events: events,
104+
inherits: inherits,
105+
}
106+
fields = { ...fields, ...processInheritList(v.inherit, ob) }
107+
}
108+
109+
/* Append list of changes as itemization to field's
110+
* description. */
111+
for (const [k2, v2] of Object.entries(fields)) {
112+
v2.text = md.render(`${v2.text}${appendListOfUpdates(v2, '* ')}`)
113+
}
114+
115+
v.fields = fields
116+
117+
/* Prefix the list of changes to the event's description. */
118+
v.text = v.text ? md.render(`${appendListOfUpdates(v)}\n${v.text}`) : null
119+
}
120+
121+
return events
122+
}
123+
124+
export async function loadEvents() {
125+
const data = loadData('events')
126+
return await normalizeEvents(
127+
structuredClone(data.events),
128+
structuredClone(data.global_inherits),
129+
structuredClone(data.inherits)
130+
)
131+
}

0 commit comments

Comments
 (0)