Skip to content

Commit

Permalink
Add naïve escaping to stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
chee committed May 17, 2019
1 parent 7ddb761 commit f2144d5
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions code/stringify.js
@@ -1,19 +1,25 @@
module.exports = function stringify (object) {
function escape (string) {
return string
.replace(/\\/g, '\\\\')
.replace(/"/g, '\\"')
}

module.exports = function stringify (thing) {
let result = ''
if (Array.isArray(object)) {
if (Array.isArray(thing)) {
result += '( '
result += object.map(stringify).join(', ')
result += thing.map(stringify).join(', ')
result += ' )'
} else if (object.constructor === Object) {
} else if (thing.constructor === Object) {
result += '{ '
for (const key in object) {
for (const key in thing) {
result += `"${key}" = `
result += stringify(object[key])
result += stringify(thing[key])
result += '; '
}
result += '}'
} else {
result += `"${object.toString()}"`
result += `"${escape(thing.toString())}"`
}
return result
}

0 comments on commit f2144d5

Please sign in to comment.