Rewriting using Map.

This commit is contained in:
John Livingston 2021-05-04 16:43:38 +02:00
parent ad2d7742e9
commit 5792b98376

View File

@ -2,7 +2,7 @@ import type { ProsodyFilePaths } from './paths'
type ConfigEntryValue = boolean | number | string | ConfigEntryValue[] type ConfigEntryValue = boolean | number | string | ConfigEntryValue[]
interface ConfigEntries {[keys: string]: ConfigEntryValue} type ConfigEntries = Map<string, ConfigEntryValue>
function writeValue (value: ConfigEntryValue): string { function writeValue (value: ConfigEntryValue): string {
if (typeof value === 'boolean') { if (typeof value === 'boolean') {
@ -31,30 +31,30 @@ abstract class ProsodyConfigBlock {
constructor (prefix: string) { constructor (prefix: string) {
this.prefix = prefix this.prefix = prefix
this.entries = {} this.entries = new Map()
} }
set (name: string, value: ConfigEntryValue): void { set (name: string, value: ConfigEntryValue): void {
this.entries[name] = value this.entries.set(name, value)
} }
add (name: string, value: ConfigEntryValue): void { add (name: string, value: ConfigEntryValue): void {
if (!(name in this.entries)) { if (!this.entries.has(name)) {
this.entries[name] = [] this.entries.set(name, [])
} }
let entry = this.entries[name] let entry = this.entries.get(name) as ConfigEntryValue
if (!Array.isArray(entry)) { if (!Array.isArray(entry)) {
entry = [entry] entry = [entry]
} }
entry.push(value) entry.push(value)
this.entries[name] = entry this.entries.set(name, entry)
} }
write (): string { write (): string {
let content = '' let content = ''
// Using Reflect.ownKeys to keep order // Map keeps order :)
Reflect.ownKeys(this.entries).forEach(key => { this.entries.forEach((value, key) => {
content += this.prefix + (key as string) + ' = ' + writeValue(this.entries[key as string]) content += this.prefix + key + ' = ' + writeValue(value)
}) })
return content return content
} }