diff --git a/src/index.ts b/src/index.ts index 142ee4f..14735c5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,5 +1,7 @@ import type { Block, ParserOptions, Section } from './types' +// TODO check if comments are added near to the key-value pair + export function parseIni(text: string, options: ParserOptions): Section[] { const sections = [] as Section[] let currentSection: Section | undefined @@ -55,7 +57,19 @@ export function stringifyIni( options: ParserOptions ): string { console.log(sections, options) - throw new Error('Not implemented') // TODO implement + + const lines = [] as string[] + for (const section of sections) { + lines.push(`[${section.section}]`) + for (const block of section.blocks) { + if (block.type === 'comment') { + lines.push(`${block.text}`) + } else { + lines.push(`${block.key}=${block.value}`) + } + } + } + return lines.join('\n') } export type { Block, ParserOptions, Section } from './types' diff --git a/test/parser.test.ts b/test/parser.test.ts index 6dfe050..8af9628 100644 --- a/test/parser.test.ts +++ b/test/parser.test.ts @@ -1,6 +1,6 @@ import { it, describe, expect } from 'vitest' -import { parseIni } from '../src' +import { parseIni, Section, stringifyIni } from '../src' const sampleIni = ` ; last modified 1 April 2001 by John Doe @@ -143,4 +143,27 @@ describe('ini parser library', () => { }) expect(obj[0].section).to.equal('other') }) + + it('It should generate text from parsed object', () => { + const parsedObj = [] as Section[] + + parsedObj.push({ + section: 'package', + blocks: [ + { + type: 'data', + key: 'name', + value: 'ini-parser', + }, + { + type: 'data', + key: 'version', + value: '1.0.0', + }, + ], + } as Section) + + const res = stringifyIni(parsedObj, {}) + expect(res).to.equal(`[package]\nname=ini-parser\nversion=1.0.0`) + }) })