diff --git a/src/parse.js b/src/parse.js index 52a3596..efc0dbe 100644 --- a/src/parse.js +++ b/src/parse.js @@ -26,13 +26,14 @@ const TOKENS = { 'UnquotedString', 'EmptyString', 'QuotedString', + 'SingleQuotedString' ], }, UnquotedString: [{ type: 'SafeChar', flatten: true }, { type: 'UnquotedStringChars', optional: true, flatten: true }], // any char except newline and ", " is reserved for future quoted strings. - SafeChar: [/[^\n"]/], + SafeChar: [/[^\n"']/], UnquotedStringChars: [/[^\\\n]/, { type: 'UnquotedStringChars', optional: true, flatten: true }], // TODO: This type should not exist, but QuotedString[1].optional does not work. this is a hotfix. @@ -45,7 +46,13 @@ const TOKENS = { 'EscapedChars', ], }, + SingleQuotedString: ["'", { type: 'SingleQuotedStringChars', optional: true }, "'"], + SingleQuotedStringChars: [{ type: 'SingleQuotedStringChar', flatten: true }, { type: 'SingleQuotedStringChars', optional: true, flatten: true }], + SingleQuotedStringChar: { + $or: ['UnescapedSingleQuotedStringChar'] + }, UnescapedQuotedStringChar: [/[^\\"]/], + UnescapedSingleQuotedStringChar: [/[^']/], EscapedChars: { replacer(val) { switch (val[0]) { diff --git a/src/serialize.js b/src/serialize.js index 7e07c7e..60cc267 100644 --- a/src/serialize.js +++ b/src/serialize.js @@ -17,10 +17,17 @@ export default function serialize(obj) { } } - serializedEntry += `${key}=${JSON.stringify(value)}`; + serializedEntry += `${key}=${serializeString(value)}`; serializedDoc.push(serializedEntry); } - return serializedDoc.join('\n\n'); + return serializedDoc.join('\n'); +} + +function serializeString(val) { + if (val.includes('\n') && !val.includes("'")) { + return `'${val}'`; + } + return JSON.stringify(val) }