Wrap objects inside arrays in {}

This commit is contained in:
Kevin Sawicki
2013-03-05 08:35:09 -08:00
parent 887e285cce
commit 5ddea28d80
2 changed files with 7 additions and 2 deletions

View File

@@ -62,6 +62,10 @@ describe "CSON", ->
it "formats the undefined value as null", ->
expect(CSON.stringify(['a', undefined, 'b'])).toBe "[\n 'a'\n null\n 'b'\n]"
describe "when the array contains an object", ->
it "wraps the object in {}", ->
expect(CSON.stringify([{a:'b', a1: 'b1'}, {c: 'd'}])).toBe "[\n {\n 'a': 'b'\n 'a1': 'b1'\n }\n {\n 'c': 'd'\n }\n]"
describe "when formatting an object", ->
describe "when the object is empty", ->
it "returns the empty string", ->

View File

@@ -21,7 +21,8 @@ module.exports =
cson = '[\n'
for value in array
cson += @stringifyIndent(indentLevel + 2)
indent = @stringifyIndent(indentLevel + 2)
cson += indent
if _.isString(value)
cson += @stringifyString(value)
else if _.isBoolean(value)
@@ -33,7 +34,7 @@ module.exports =
else if _.isArray(value)
cson += @stringifyArray(value, indentLevel + 2)
else if _.isObject(value)
cson += @stringifyObject(value, indentLevel + 2)
cson += "{\n#{@stringifyObject(value, indentLevel + 4)}\n#{indent}}"
else
throw new Error("Unrecognized type for array value: #{value}")
cson += '\n'