-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjevkoToPrettyString.js
More file actions
37 lines (28 loc) · 1.02 KB
/
jevkoToPrettyString.js
File metadata and controls
37 lines (28 loc) · 1.02 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
import {escape, defaultDelimiters} from './deps.js'
const {opener, closer, escaper, quoter} = defaultDelimiters
const strToHeredoc = (str, tag) => `${escaper}${quoter}${tag}${quoter}${str}${quoter}${tag}${quoter}`
export const jevkoToPrettyString = (jevko) => {
const {subjevkos, suffix, tag} = jevko
if (tag !== undefined) return strToHeredoc(suffix, tag)
let ret = ''
for (const {prefix, jevko} of subjevkos) {
ret += `${escapePrefix(prefix)}${recur(jevko, ' ', '')}\n`
}
return ret + escape(suffix)
}
const escapePrefix = (prefix) => prefix === ''? '': escape(prefix) + ' '
const recur = (jevko, indent, prevIndent) => {
const {subjevkos, suffix, tag} = jevko
if (tag !== undefined) return strToHeredoc(suffix, tag)
let ret = ''
if (subjevkos.length > 0) {
ret += '\n'
for (const {prefix, jevko} of subjevkos) {
ret += `${indent}${
escapePrefix(prefix)
}${recur(jevko, indent + ' ', indent)}\n`
}
ret += prevIndent
}
return opener + ret + escape(suffix) + closer
}