Skip to content

Commit

Permalink
Escape all single line values with single quotes to avoid parsing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott McWhirter committed Feb 21, 2012
1 parent 25bec9d commit 0bc27aa
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions yamlish.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ function encode (obj, indent) {
obj = obj.trim()
if (obj.indexOf("\n") !== -1) {
return "|\n" + indent + obj.split(/\r?\n/).join("\n"+indent)
} else if (obj.indexOf(":") !== -1) {
return JSON.stringify(obj)
} else {
return obj
return "'" + obj + "'"
}

case "number":
Expand All @@ -45,15 +43,15 @@ function encode (obj, indent) {

if (obj instanceof Date ||
Object.prototype.toString.call(obj) === "[object Date]") {
return "[Date " + obj.toISOString() + "]"
return encode("[Date " + obj.toISOString() + "]", indent)
}

if (obj instanceof RegExp ||
Object.prototype.toString.call(obj) === "[object RegExp]" ||
obj instanceof Boolean ||
Object.prototype.toString.call(obj) === "[object Boolean]"
) {
return obj.toString()
return encode(obj.toString(), indent)
}

if (seen.indexOf(obj) !== -1) {
Expand All @@ -63,7 +61,7 @@ function encode (obj, indent) {

if (typeof Buffer === "function" &&
typeof Buffer.isBuffer === "function" &&
Buffer.isBuffer(obj)) return obj.inspect()
Buffer.isBuffer(obj)) return encode(obj.inspect(), indent)

if (obj instanceof Error) {
var o = { name: obj.name
Expand Down

0 comments on commit 0bc27aa

Please sign in to comment.