import * as dedentOriginal from 'dedent'; export { dedentOriginal as dedent }; export function parseProperties(content: string): T { const buff = Buffer.alloc(content.length * 2); let offset = 0; let withinBackTick = false; for (let i = 0; i < content.length; ++i) { if (!withinBackTick && content[i] === '`') { withinBackTick = true; buff[i + offset] = content.charCodeAt(i); continue; } if (withinBackTick) { if (content[i] === '\n') { buff[i + offset] = 92; // \ ++offset; buff[i + offset] = 110; // n continue; } if (content[i] === '`' && content[i-1] !== '\\') { buff[i + offset] = content.charCodeAt(i); withinBackTick = false; continue; } } buff[i + offset] = content.charCodeAt(i); } content = buff.slice(0, content.length + offset).toString('ascii'); const flatMap = content // Split by line breaks. .split('\n') // Remove commented lines: .filter((line) => /(\#|\!)/.test(line.replace(/\s/g, '').slice(0, 1)) ? false : line ) .reduce((obj, line) => { // Replace only '=' that are not escaped with '\' to handle separator inside key const colonifiedLine = line.replace(/(? trimQuote(a.trim())); const baseKey = lit2[0]; const childKey = lit2[1]; if (!result[baseKey]) { result[baseKey] = {}; } if (value.indexOf(',') >= 0) { result[baseKey][childKey] = value.split(',').map(a => trimQuote(a.trim())).filter(a => a); } else { result[baseKey][childKey] = trimQuote(value); } } else { if (value.indexOf(',') >= 0) { result[propName] = value.split(',').map(a => trimQuote(a.trim())).filter(a => a); } else { result[propName] = trimQuote(value); } } } for (const prop of Object.keys(result)) { if (!isNaN(+result[prop])) { result[prop] = +result[prop]; } } return result; } function trimQuote(str: string) { while (str.startsWith("'") && str.endsWith("'")) { str = str.slice(1, -1).trim(); } while (str.startsWith('"') && str.endsWith('"')) { str = str.slice(1, -1).trim(); } return str; }