Skip to content

Commit

Permalink
feat: Implemented stringifyIni support
Browse files Browse the repository at this point in the history
chore: prettier
  • Loading branch information
thegostisdead committed May 11, 2023
1 parent 4f3b966 commit 0ac1a54
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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'
25 changes: 24 additions & 1 deletion test/parser.test.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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`)
})
})

0 comments on commit 0ac1a54

Please sign in to comment.