function parseNumber(str: string): number | undefined { if (!/^(\d|\.)+$/g.test(str)) { return undefined } const float = parseFloat(str) const int = parseInt(str, 10) if (isNaN(int)) { return undefined } if (int !== float) { return float } return int } function decodeTime(text: string): number { let timeInSec = 0 for (const it of text.split(' ')) { const lastChar = it.charAt(it.length - 1) const time = parseInt(it.slice(0, it.length - 1), 10) switch (lastChar) { case 'm': timeInSec += time * 60 break; case 's': timeInSec += time break; case 'h': timeInSec += time * 60 * 60 break; case 'd': timeInSec += time * 60 * 60 * 24 break; default: throw new Error(`error parsing time ${it} (${time})`) } } return timeInSec } export function getParams(data: string) { const lines = data.split('\n').filter((it) => it.startsWith(';') && it.includes('=')) const obj: Record = {} for (const line of lines) { const [key, value] = line.split('=', 2).map((it) => it.slice(1).trim()) let realKey = key.replace(/ /g, '_').replace(/\[|\]|\(|\)/g, '') const realValue = parseNumber(value) ?? value let offset = 0 while (obj[`${realKey}${offset > 0 ? `_${offset}` : ''}`] && obj[`${realKey}${offset > 0 ? `_${offset}` : ''}`] !== realValue) { offset++ } if (offset > 0) { realKey = `${realKey}_${offset}` } if (obj[realKey] && obj[realKey] !== realValue) { throw new Error(`Key collision ${key}=${realValue} ${realKey}=${obj[realKey]}`) } obj[realKey] = parseNumber(value) ?? value if (realKey === 'estimated_printing_time_normal_mode') { obj['estimated_printing_time_seconds'] = decodeTime(value) } } return obj }