|
| 1 | +import { Schema } from './util' |
| 2 | + |
| 3 | +export type StringifyOptions = { |
| 4 | + indentDepth?: number |
| 5 | +} |
| 6 | + |
| 7 | + |
| 8 | +export function stringify (data: any, blockSchema: Schema.Block, opts: StringifyOptions): string { |
| 9 | + const parts: string[] = [] |
| 10 | + |
| 11 | + const depth = opts.indentDepth || 0 |
| 12 | + const i_1 = indentation(depth) |
| 13 | + |
| 14 | + const dataItems = blockSchema.type === 'hash' |
| 15 | + ? Object.keys(blockSchema.schema).map(configKey => [configKey, data[configKey]]) |
| 16 | + : data |
| 17 | + |
| 18 | + for (let [key, _value] of dataItems) { |
| 19 | + console.log("Serialize", key, _value) |
| 20 | + |
| 21 | + if ( _value === undefined ) continue; |
| 22 | + |
| 23 | + let t = blockSchema.schema[key] |
| 24 | + let [value, block] = ('block' in t && t.type !== 'list') |
| 25 | + ? _value |
| 26 | + : [_value, null] |
| 27 | + |
| 28 | + if ( |
| 29 | + t.type === 'num' || |
| 30 | + t.type === 'str' || |
| 31 | + t.type === 'bool' || |
| 32 | + t.type === 'enum' |
| 33 | + ) { |
| 34 | + parts.push(i_1 + `${key} ${value}`) |
| 35 | + } |
| 36 | + else if (t.type === 'list') { |
| 37 | + let i_2 = indentation(depth + 1) |
| 38 | + |
| 39 | + parts.push(i_1 + `${key} {`) |
| 40 | + |
| 41 | + if (Array.isArray(value)) { |
| 42 | + for (let item of value) { |
| 43 | + |
| 44 | + if (t.block) { |
| 45 | + let [itemValue, itemBlock] = item |
| 46 | + parts.push(i_2 + itemValue) |
| 47 | + if (itemBlock) { |
| 48 | + addBlockBracket(parts) |
| 49 | + parts.push(stringify(itemBlock, t.block, indent(depth+1, opts))) |
| 50 | + parts.push(i_2 + '}') |
| 51 | + } |
| 52 | + } |
| 53 | + else { |
| 54 | + parts.push(i_2 + item) |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + parts.push(i_1 + '}') |
| 59 | + } |
| 60 | + else if (t.type === 'kv') { |
| 61 | + let i_2 = indentation(depth + 1) |
| 62 | + parts.push(i_1 + `${key} {`) |
| 63 | + |
| 64 | + for (let key in value) { |
| 65 | + parts.push(i_2 + `${key} ${value[key]}`) |
| 66 | + } |
| 67 | + |
| 68 | + parts.push(i_1 + '}') |
| 69 | + } |
| 70 | + else if (t.type === 'tuple') { |
| 71 | + parts.push(i_1 + `${key} ${value.join(', ')}`) |
| 72 | + } |
| 73 | + else if (t.type === 'hash' || t.type === 'array') { |
| 74 | + parts.push(i_1 + `${key} {`) |
| 75 | + |
| 76 | + parts.push(stringify(value, t, indent(depth, opts))) |
| 77 | + |
| 78 | + parts.push(i_1 + '}') |
| 79 | + } |
| 80 | + |
| 81 | + if ('block' in t && t.block && block) { |
| 82 | + addBlockBracket(parts) |
| 83 | + |
| 84 | + parts.push(stringify(block, t.block, indent(depth, opts))) |
| 85 | + |
| 86 | + parts.push(i_1 + '}') |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return parts.join('\n') |
| 91 | +} |
| 92 | + |
| 93 | +function indentation (n: number) { |
| 94 | + let result = '' |
| 95 | + for (let i=0; i < n; i++) { |
| 96 | + result += ' ' |
| 97 | + } |
| 98 | + return result |
| 99 | +} |
| 100 | + |
| 101 | +function indent (depth: number, opts: StringifyOptions) { |
| 102 | + return { ...opts, indentDepth: depth+1 } |
| 103 | +} |
| 104 | + |
| 105 | +function addBlockBracket (parts: string[]) { |
| 106 | + parts[parts.length-1] = parts[parts.length-1] + ' {' |
| 107 | +} |
0 commit comments