Kill trailing newline and inline empty arrays

This commit is contained in:
Kevin Sawicki
2013-02-01 16:20:25 -08:00
parent 83b6a93684
commit 69097bee50
2 changed files with 21 additions and 21 deletions

View File

@@ -1,6 +1,6 @@
CSON = require 'cson'
fdescribe "CSON", ->
describe "CSON", ->
describe "@stringify(object)", ->
describe "when the object is undefined", ->
it "throws an exception", ->
@@ -16,7 +16,7 @@ fdescribe "CSON", ->
describe "when formatting an undefined key", ->
it "does not include the key in the formatted CSON", ->
expect(CSON.stringify(b: 1, c: undefined)).toBe "'b': 1\n"
expect(CSON.stringify(b: 1, c: undefined)).toBe "'b': 1"
describe "when formatting an empty object", ->
it "returns the empty string", ->
@@ -24,33 +24,37 @@ fdescribe "CSON", ->
describe "when formatting a string", ->
it "returns formatted CSON", ->
expect(CSON.stringify(a: 'b')).toBe "'a': 'b'\n"
expect(CSON.stringify(a: 'b')).toBe "'a': 'b'"
it "escapes single quotes", ->
expect(CSON.stringify(a: "'b'")).toBe "'a': '\\\'b\\\''\n"
expect(CSON.stringify(a: "'b'")).toBe "'a': '\\\'b\\\''"
it "doesn't escape double quotes", ->
expect(CSON.stringify(a: '"b"')).toBe "'a': '\"b\"'\n"
expect(CSON.stringify(a: '"b"')).toBe "'a': '\"b\"'"
describe "when formatting a boolean", ->
it "returns formatted CSON", ->
expect(CSON.stringify(a: true)).toBe "'a': true\n"
expect(CSON.stringify(a: false)).toBe "'a': false\n"
expect(CSON.stringify(a: true)).toBe "'a': true"
expect(CSON.stringify(a: false)).toBe "'a': false"
describe "when formatting a number", ->
it "returns formatted CSON", ->
expect(CSON.stringify(a: 14)).toBe "'a': 14\n"
expect(CSON.stringify(a: 1.23)).toBe "'a': 1.23\n"
expect(CSON.stringify(a: 14)).toBe "'a': 14"
expect(CSON.stringify(a: 1.23)).toBe "'a': 1.23"
describe "when formatting null", ->
it "returns formatted CSON", ->
expect(CSON.stringify(a: null)).toBe "'a': null\n"
expect(CSON.stringify(a: null)).toBe "'a': null"
describe "when formatting an array", ->
describe "when the array is empty", ->
it "puts the array on a single line", ->
expect(CSON.stringify([])).toBe "[]"
it "returns formatted CSON", ->
expect(CSON.stringify(a: ['b'])).toBe "'a': [\n 'b'\n]\n"
expect(CSON.stringify(a: ['b', 4])).toBe "'a': [\n 'b'\n 4\n]\n"
expect(CSON.stringify(a: ['b'])).toBe "'a': [\n 'b'\n]"
expect(CSON.stringify(a: ['b', 4])).toBe "'a': [\n 'b'\n 4\n]"
describe "when formatting an object", ->
it "returns formatted CSON", ->
expect(CSON.stringify(a: {b: 'c'})).toBe "'a':\n 'b': 'c'\n"
expect(CSON.stringify(a: {b: 'c'})).toBe "'a':\n 'b': 'c'"

View File

@@ -17,6 +17,8 @@ module.exports =
stringifyNull: -> 'null'
stringifyArray: (array, indentLevel=0) ->
return '[]' if array.length is 0
cson = '[\n'
for value in array
cson += @stringifyIndent(indentLevel + 2)
@@ -72,13 +74,7 @@ module.exports =
return @stringifyBoolean(object) if _.isBoolean(object)
return @stringifyNumber(object) if _.isNumber(object)
return @stringifyNull(object) if _.isNull(object)
if _.isArray(object)
cson = @stringifyArray(object)
cson = "#{cson}\n" if cson
return cson
if _.isObject(object)
cson = @stringifyObject(object)
cson = "#{cson}\n" if cson
return cson
return @stringifyArray(object) if _.isArray(object)
return @stringifyObject(object) if _.isObject(object)
throw new Error("Unrecognized type to stringify: #{object}")