Skip to content

Commit 8a1ed6d

Browse files
penalosacyyynthia
andauthored
feat: simplify nested tables (#46)
Co-authored-by: Cynthia <[email protected]>
1 parent 699fb61 commit 8a1ed6d

2 files changed

Lines changed: 96 additions & 8 deletions

File tree

src/stringify.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,14 @@ function stringifyArrayTable (array: any[], key: string, depth: number, numberAs
130130
let res = ''
131131
for (let i = 0; i < array.length; i++) {
132132
res += `[[${key}]]\n`
133-
res += stringifyTable(array[i], key, depth, numberAsFloat)
134-
res += '\n\n'
133+
res += stringifyTable(0, array[i], key, depth, numberAsFloat)
134+
res += '\n'
135135
}
136136

137137
return res
138138
}
139139

140-
function stringifyTable (obj: any, prefix: string, depth: number, numberAsFloat: boolean) {
140+
function stringifyTable (tableKey: string | 0, obj: any, prefix: string, depth: number, numberAsFloat: boolean) {
141141
if (depth === 0) {
142142
throw new Error('Could not stringify the object: maximum object depth exceeded')
143143
}
@@ -160,9 +160,8 @@ function stringifyTable (obj: any, prefix: string, depth: number, numberAsFloat:
160160
tables += stringifyArrayTable(obj[k], prefix ? `${prefix}.${key}` : key, depth - 1, numberAsFloat)
161161
} else if (type === 'object') {
162162
let tblKey = prefix ? `${prefix}.${key}` : key
163-
tables += `[${tblKey}]\n`
164-
tables += stringifyTable(obj[k], tblKey, depth - 1, numberAsFloat)
165-
tables += '\n\n'
163+
let table = stringifyTable(tblKey, obj[k], tblKey, depth - 1, numberAsFloat)
164+
if (table) tables += table + '\n'
166165
} else {
167166
preamble += key
168167
preamble += ' = '
@@ -172,7 +171,12 @@ function stringifyTable (obj: any, prefix: string, depth: number, numberAsFloat:
172171
}
173172
}
174173

175-
return `${preamble}\n${tables}`.trim()
174+
if (tableKey && (preamble || !tables)) // Create table only if necessary
175+
preamble = preamble ? `[${tableKey}]\n${preamble}` : `[${tableKey}]`
176+
177+
return preamble && tables
178+
? `${preamble}\n${tables}`
179+
: preamble || tables
176180
}
177181

178182
export function stringify (
@@ -183,5 +187,5 @@ export function stringify (
183187
throw new TypeError('stringify can only be called with an object')
184188
}
185189

186-
return stringifyTable(obj, '', maxDepth, numbersAsFloat)
190+
return stringifyTable(0, obj, '', maxDepth, numbersAsFloat)
187191
}

test/stringify.test.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,37 @@ c = 2
154154
expect(stringify({ a: { b: 1, c: 2 }, d: 3 }).trim()).toBe(expected)
155155
})
156156

157+
it('stringifies nested tables and handles top-level keys', () => {
158+
const expected = `
159+
d = 3
160+
161+
[a.b]
162+
b = 1
163+
c = 2
164+
`.trim()
165+
166+
expect(stringify({ a: {b: { b: 1, c: 2 }, }, d: 3}).trim()).toBe(expected)
167+
})
168+
169+
it("stringifies empty tables with no gaps", () => {
170+
const expected = `
171+
[animal]
172+
[fruit.apple]
173+
[fruit.orange]
174+
`.trim();
175+
176+
expect(
177+
stringify({
178+
animal: {},
179+
fruit: {
180+
apple: {},
181+
orange: {},
182+
},
183+
}).trim()
184+
).toBe(expected);
185+
});
186+
187+
157188
it('stringifies tables contained in arrays', () => {
158189
const expected = `
159190
a = [ 1, { b = 2, c = 3 }, 4 ]
@@ -176,6 +207,59 @@ c = 4
176207
expect(stringify({ a: [ { b: 1, c: 2 }, { b: 3, c: 4 } ] }).trim()).toBe(expected)
177208
})
178209

210+
it("stringifies nested arrays of tables", () => {
211+
const expected = `
212+
[[a.b]]
213+
b = 1
214+
c = 2
215+
216+
[[a.b]]
217+
b = 3
218+
c = 4
219+
`.trim();
220+
221+
expect(
222+
stringify({
223+
a: {
224+
b: [
225+
{ b: 1, c: 2 },
226+
{ b: 3, c: 4 },
227+
],
228+
},
229+
}).trim()
230+
).toBe(expected);
231+
});
232+
233+
it("stringifies nested arrays of tables with top level keys", () => {
234+
const expected = `
235+
key = "hello"
236+
237+
[a]
238+
key = "hello"
239+
240+
[[a.b]]
241+
b = 1
242+
c = 2
243+
244+
[[a.b]]
245+
b = 3
246+
c = 4
247+
`.trim();
248+
249+
expect(
250+
stringify({
251+
key: "hello",
252+
a: {
253+
key: "hello",
254+
b: [
255+
{ b: 1, c: 2 },
256+
{ b: 3, c: 4 },
257+
],
258+
},
259+
}).trim()
260+
).toBe(expected);
261+
});
262+
179263
it('does not produce invalid keys', () => {
180264
const expected = `
181265
test-key123_ = 1

0 commit comments

Comments
 (0)