-
Notifications
You must be signed in to change notification settings - Fork 0
/
to_aml.js
359 lines (335 loc) · 9.69 KB
/
to_aml.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
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
/**
* @file to_aml.js
* @author Brandon Kalinowski (@brandonkal)
* @copyright 2021 Brandon Kalinowski. All Rights Reserved.
* @description Converts JSON structure to ArchieML text string.
* @license Contact for license
*/
//@ts-nocheck -- this code works.
/* understand/
* We know we want to show the end "leaves" of the object as ArchieML.
* Any intermediate objects are just the "path/structure". For example:
*
* { a: { b: { c: "leaf" } } }
*
* Should become
*
* a.b.c: leaf
*
* outcome/
* We first convert the object into a 'leafy' structure and
*
* then use that to output the ArchieML.
* @param {string} [obj] - Somebody's name.
*/
/**
* @param {object} obj
*/
export function archieml(obj) {
const leafy = toLeaf(obj)
return toArchieML(leafy)
}
/* problem/
* Given an object we want to return a "flattened" array of it's
* end/leaf values.
*
* way/
* We "walk" the object recursively, keeping track of the current 'path'
* so that everytime we hit an end value we can add it to a "leafy"
* accumulator.
*/
function toLeaf(obj) {
const leafy = []
to_leaf_1(leafy, [], obj)
return leafy
function to_leaf_1(acc, curpath, obj) {
if (is_leaf_1(obj)) add_to_accum_1(acc, curpath, obj)
else if (Array.isArray(obj)) {
for (let i = 0; i < obj.length; i++) {
to_leaf_1(acc, curpath, obj[i])
}
} else if (typeof obj === 'object') {
for (const k in obj) {
curpath.push(path_elem_1(obj, k))
to_leaf_1(acc, curpath, obj[k])
curpath.pop(k)
}
}
}
function add_to_accum_1(acc, curpath, v) {
if (typeof v === 'number') v = v.toString()
acc.push({ path: curpath.slice(0), value: v })
}
/* understand/
* We only support string values
*/
function is_leaf_1(v) {
return typeof v === 'string' || typeof v === 'number'
}
/* problem/
* Because ArchieML handles objects and arrays differently, we need
* the path to distinguish between the two.
*
* way/
* If the key is an array, we store it as '[key]', otherwise we
* store it as 'key'.
*/
function path_elem_1(obj, k) {
if (Array.isArray(obj[k])) return `[${k}]`
else return k
}
}
const rxArray = /^\[(.*)\]$/
/* problem/
* Support the conversion of leafy values to ArchieML in the following
* cases:
* (a) object.path, value ===> object.path: value
* (b) object.path.a, value {object.path}
* object.path.b, value ===> a: value
* object.path.c, value b: value
* c: value
* (c) [array], value [array]
* [array], value ===> * value
* * value
* []
* (d) object.path.[array], value [object.path.array]
* * value
* []
* (e) object.path.[array].a, value [object.path.array]
* object.path.[array].b, value a: value
* b: value
* []
* (f) object.path.[array].a.[another], value
* object.path.[array].a.[another], value
* ===v
* [object.path.array]
* [.a.another]
* * value
* []
* []
*
* Remember to exit objects that need it
*
* way/
* Exit any existing arrays if our path is different
* Enter any existing object path if needed and looks nice.
* If the path is an object path, resolve it's value with the current
* object context (exiting if required) and show it.
* Otherwise enter all the arrays needed and show the remaining value
*/
function toArchieML(leafy) {
const state = {
curobject: null,
curarrays: [],
firstelem: null,
moreelems: false,
}
const r = []
for (let i = 0; i < leafy.length; i++) {
exit_arrays_needed_1(leafy[i], state, r)
resolve_object_path_1(leafy, i, state, r)
if (is_obj_path_1(leafy[i])) show_obj_1(leafy[i], state, r)
else {
enter_arrays_needed_1(leafy[i], state, r)
show_array_elem_1(leafy[i], state, r)
}
}
return r.join('\n')
/* outcome/
* Enter all the arrays needed for the current leaf by outputting
* [array.path]
* for the first element and
* [.subarray.paths]
* for all other elements while keeping track of them all in the
* state. We know the arrays needed are additional arrays to the
* current arrays because additional arrays have been removed
* earlier during the `exit_arrays_needed_1` call.
*/
function enter_arrays_needed_1(leaf, state, r) {
const arrs = leafArraySplit(leaf.path)
for (let i = state.curarrays.length; i < arrs.length; i++) {
if (i == 0) r.push(`[${arrs[i]}]`)
else r.push(`[.${arrs[i]}]`)
state.curarrays.push(arrs[i])
state.firstelem = null
state.moreelems = false
}
}
/* outcome/
* Look for the ending path beyond the last array element:
* a.b.[c].d ===> d
* a.b.[c].d.e ===> d.e
* a.b.[c] ===> <null>
* If there is no ending path then it is a simple string element and
* should be output as:
* * value
* Otherwise output it as a subobject value:
* d.e: value
* If it is the first array element, output an empty line before it
* to make it look nicer.
*/
function show_array_elem_1(leaf, state, r) {
const p = array_path_1(leaf.path)
if (!p) {
r.push(`* ${leafVal(leaf)}`)
} else {
if (!state.firstelem) state.firstelem = p
else if (state.firstelem == p && state.moreelems) r.push('')
else state.moreelems = true
r.push(`${p}: ${leafVal(leaf)}`)
}
function array_path_1(path) {
for (let i = path.length - 1; i >= 0; i--) {
if (isPathElemArray(path[i])) {
return path.slice(i + 1).join('.')
}
}
}
}
/* outcome/
* If the leaf value has multiple lines it must end with an ':end'
* marker, otherwise it can just be a simple value.
*/
function leafVal(leaf) {
const v = leaf.value
if (v.match(/[\n\r]/)) return `${v}\n:end`
else return v
}
/* outcome/
* Check if the current leaf belongs to the currently active object.
* If it does, output it's remaining path and value. If it doesn't,
* exit the current object and then output the leaf path and value.
*/
function show_obj_1(leaf, state, r) {
const path = as_obj_path_1(leaf.path)
if (!state.curobject) {
r.push(`${path}: ${leafVal(leaf)}`)
} else {
if (path.startsWith(state.curobject)) {
const rem = path.substring(state.curobject.length + 1)
r.push(`${rem}: ${leafVal(leaf)}`)
} else {
r.push('{}')
state.curobject = null
r.push(`${path}: ${leafVal(leaf)}`)
}
}
}
function is_obj_path_1(leaf) {
for (let i = 0; i < leaf.path.length; i++) {
if (isPathElemArray(leaf.path[i])) return false
}
return true
}
/* problem/
* When outputting an object we can use either the format:
* a.b: val1
* a.c: val2
* Or the equivalent format:
* {a}
* b: val1
* c: val2
* We need to figure out which one to use.
*
* way/
* We will use the option (2) when the object contains multiple
* values (say 3+) and option (1) when it contains a few. That will
* make it look nice. We set option (2) by outputing the
* `{object.path}` ArchieML line and setting it as the 'current
* object' in our state.
*/
function resolve_object_path_1(leafy, ndx, state, r) {
const p1 = leafyObjPath(leafy[ndx])
if (!p1) return
if (p1 == state.curobject) return
const p2 = leafyObjPath(leafy[ndx + 1])
if (!p2) return
const p3 = leafyObjPath(leafy[ndx + 2])
if (!p3) return
if (p2.startsWith(p1) && p3.startsWith(p1)) {
r.push(`{${p1}}`)
state.curobject = p1
}
}
/* problem/
* For a leaf path we would like to know in which object it is
* contained.
*
* examples/
*
* { path: "a.b.c.d", value: "val1" }
* ===> object path {a.b.c}
*
* { path: "a.b.[c].d", value: "val1" }
* ===> object path {a.b}
*
* way/
* We find the path to the first array element or the path upto the
* last element and return it.
*/
function leafyObjPath(leaf) {
if (!leaf || !leaf.path || !leaf.path.length) return
const p = []
for (let i = 0; i < leaf.path.length - 1; i++) {
if (isPathElemArray(leaf.path[i])) break
p.push(leaf.path[i])
}
return p.join('.')
}
/* outcome/
* When the existing current arrays don't line up with the current
* leaf path, exit them by removing them from the array and adding
* a corresponding "[]" (exit array) line in ArchieML.
* If we exit the last array, leave a line to make it look better.
*/
function exit_arrays_needed_1(leaf, state, r) {
const arrs = leafArraySplit(leaf.path)
let i
for (i = 0; i < state.curarrays.length; i++) {
if (arrs[i] != state.curarrays[i]) break
}
for (; i < state.curarrays.length; i++) {
r.push('[]')
state.curarrays.pop()
if (state.curarrays.length == 0) {
r.push('')
}
}
}
/* outcome/
* Given a path, we split into array components that are useful for
* ArchieML
*
* examples/
* [ "a", "b", "c" ] ==> []
* [ "a", "b", "[c]" ] ==> [ "a.b.c" ]
* [ "a", "[b]", "c" ] ==> [ "a.b" ]
* [ "a", "[b]", "c", "[d]" ] ==> [ "a.b", "c.d" ]
*/
function leafArraySplit(path) {
const arrs = []
let start = 0
for (let i = 0; i < path.length; i++) {
if (isPathElemArray(path[i])) {
arrs.push(as_array_path_1(path.slice(start, i + 1)))
start = i + 1
}
}
return arrs
}
function isPathElemArray(e) {
return rxArray.test(e)
}
function as_array_path_1(p) {
return p
.map((e) => {
const m = e.match(rxArray)
if (m) return m[1]
else return e
})
.join('.')
}
function as_obj_path_1(p) {
return p.join('.')
}
}