Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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]) {
Expand Down
11 changes: 9 additions & 2 deletions src/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}