From 336642efa89738f5b89350d0f85016c6fdaa5640 Mon Sep 17 00:00:00 2001 From: charlierudolph Date: Wed, 6 May 2015 15:19:13 -0700 Subject: [PATCH 1/3] minified cson --- lib/stringify.js | 79 ++++++++++++++++++++++++------------------- src/stringify.coffee | 61 ++++++++++++++++++--------------- test/stringify.coffee | 33 +++++++++++++++--- 3 files changed, 107 insertions(+), 66 deletions(-) diff --git a/lib/stringify.js b/lib/stringify.js index 1cc554b..169df1d 100644 --- a/lib/stringify.js +++ b/lib/stringify.js @@ -49,7 +49,7 @@ isObject = function(obj) { }; module.exports = function(data, visitor, indent) { - var indentLine, indentLines, n, normalized, out, visitArray, visitNode, visitObject, visitString, _ref; + var indentLine, indentLines, n, normalized, visitArray, visitNode, visitObject, visitString, _ref; if ((_ref = typeof data) === 'undefined' || _ref === 'function') { return void 0; } @@ -67,9 +67,6 @@ module.exports = function(data, visitor, indent) { return 0; } })(); - if (!indent) { - return JSON.stringify(data, visitor, indent); - } indentLine = function(line) { return indent + line; }; @@ -82,7 +79,7 @@ module.exports = function(data, visitor, indent) { normalized = JSON.parse(JSON.stringify(data, visitor)); visitString = function(str) { var string; - if (str.indexOf('\n') === -1) { + if (str.indexOf('\n') === -1 || !indent) { return JSON.stringify(str); } else { string = str.replace(/\\/g, '\\\\').replace(tripleQuotesRE, "\\'''"); @@ -90,21 +87,18 @@ module.exports = function(data, visitor, indent) { } }; visitArray = function(arr) { - var array, items; + var items, serializedItems; items = arr.map(function(value) { - var serializedValue; - serializedValue = visitNode(value); - if (isObject(value)) { - return "{" + (newlineWrap(indentLines(serializedValue))) + "}"; - } else { - return serializedValue; - } + return visitNode(value, { + bracesRequired: true + }); }); - array = items.join('\n'); - return "[" + (newlineWrap(indentLines(array))) + "]"; + serializedItems = indent ? newlineWrap(indentLines(items.join('\n'))) : items.join(','); + return "[" + serializedItems + "]"; }; - visitObject = function(obj) { - var key, keypairs, serializedValue, value; + visitObject = function(obj, _arg) { + var bracesRequired, key, keypairs, serializedKeyPairs, serializedValue, value; + bracesRequired = _arg.bracesRequired; keypairs = (function() { var _results; _results = []; @@ -113,22 +107,42 @@ module.exports = function(data, visitor, indent) { if (!key.match(jsIdentifierRE)) { key = JSON.stringify(key); } - serializedValue = visitNode(value); - if (isObject(value)) { - if (serializedValue === '') { - _results.push(key + ": {}"); + serializedValue = visitNode(value, { + bracesRequired: !indent + }); + if (indent) { + if (isObject(value)) { + serializedValue = "\n" + (indentLines(serializedValue)); } else { - _results.push(key + ":\n" + (indentLines(serializedValue))); + serializedValue = " " + serializedValue; } - } else { - _results.push(key + ": " + serializedValue); } + _results.push(key + ":" + serializedValue); } return _results; })(); - return keypairs.join('\n'); + if (keypairs.length === 0) { + return '{}'; + } else if (indent) { + serializedKeyPairs = keypairs.join('\n'); + if (bracesRequired) { + return "{" + (newlineWrap(indentLines(serializedKeyPairs))) + "}"; + } else { + return serializedKeyPairs; + } + } else { + serializedKeyPairs = keypairs.join(','); + if (bracesRequired) { + return "{" + serializedKeyPairs + "}"; + } else { + return serializedKeyPairs; + } + } }; - visitNode = function(node) { + visitNode = function(node, options) { + if (options == null) { + options = {}; + } switch (typeof node) { case 'boolean': return "" + node; @@ -140,21 +154,16 @@ module.exports = function(data, visitor, indent) { } break; case 'string': - return visitString(node); + return visitString(node, options); case 'object': if (node === null) { return 'null'; } else if (Array.isArray(node)) { - return visitArray(node); + return visitArray(node, options); } else { - return visitObject(node); + return visitObject(node, options); } } }; - out = visitNode(normalized); - if (out === '') { - return '{}'; - } else { - return out; - } + return visitNode(normalized); }; diff --git a/src/stringify.coffee b/src/stringify.coffee index 19af285..08d5f13 100644 --- a/src/stringify.coffee +++ b/src/stringify.coffee @@ -60,8 +60,6 @@ module.exports = (data, visitor, indent) -> else 0 - return JSON.stringify data, visitor, indent unless indent - indentLine = (line) -> indent + line indentLines = (str) -> @@ -72,7 +70,7 @@ module.exports = (data, visitor, indent) -> normalized = JSON.parse JSON.stringify data, visitor visitString = (str) -> - if str.indexOf('\n') == -1 + if str.indexOf('\n') == -1 or !indent JSON.stringify str else string = str @@ -81,31 +79,42 @@ module.exports = (data, visitor, indent) -> "'''#{ newlineWrap indentLines string }'''" visitArray = (arr) -> - items = arr.map (value) -> - serializedValue = visitNode value - if isObject value - "{#{ newlineWrap indentLines serializedValue }}" - else - serializedValue + items = arr.map (value) -> visitNode value, bracesRequired: true + + serializedItems = if indent + newlineWrap indentLines items.join '\n' + else + items.join ',' - array = items.join '\n' - "[#{ newlineWrap indentLines array }]" + "[#{serializedItems}]" - visitObject = (obj) -> + visitObject = (obj, {bracesRequired}) -> keypairs = for key, value of obj key = JSON.stringify key unless key.match jsIdentifierRE - serializedValue = visitNode value - if isObject value - if serializedValue == '' - "#{ key }: {}" + serializedValue = visitNode value, bracesRequired: !indent + if indent + if isObject value + serializedValue = "\n#{ indentLines serializedValue }" else - "#{ key }:\n#{ indentLines serializedValue }" + serializedValue = " #{ serializedValue }" + "#{ key }:#{ serializedValue }" + + if keypairs.length is 0 + '{}' + else if indent + serializedKeyPairs = keypairs.join '\n' + if bracesRequired + "{#{ newlineWrap indentLines serializedKeyPairs }}" else - "#{ key }: #{ serializedValue }" - - keypairs.join '\n' + serializedKeyPairs + else + serializedKeyPairs = keypairs.join ',' + if bracesRequired + "{#{ serializedKeyPairs }}" + else + serializedKeyPairs - visitNode = (node) -> + visitNode = (node, options = {}) -> switch typeof node when 'boolean' then "#{node}" @@ -113,13 +122,11 @@ module.exports = (data, visitor, indent) -> if isFinite node then "#{node}" else 'null' # NaN, Infinity and -Infinity - when 'string' then visitString node + when 'string' then visitString node, options when 'object' if node == null then 'null' - else if Array.isArray(node) then visitArray(node) - else visitObject(node) + else if Array.isArray(node) then visitArray node, options + else visitObject node, options - out = visitNode normalized - if out == '' then '{}' # the only thing that serializes to '' is an empty object - else out + visitNode normalized diff --git a/test/stringify.coffee b/test/stringify.coffee index 341217b..7a49afd 100644 --- a/test/stringify.coffee +++ b/test/stringify.coffee @@ -37,6 +37,14 @@ describe 'CSON.stringify', -> and I have a sneaky ''' in here, too """ + it 'handles multi-line strings (with 0 indentation)', -> + equal """ + "I am your average multi-line string,\\nand I have a sneaky ''' in here, too" + """, cson """ + I am your average multi-line string, + and I have a sneaky ''' in here, too + """, null, 0 + it 'handles multi-line strings w/ backslash', -> test = '\\\n\\' expected = "'''\n \\\\\n \\\\\n'''" @@ -58,6 +66,11 @@ describe 'CSON.stringify', -> ] ''', cson [ [1], null, [], a: 'str', {} ] + it 'handles arrays (with 0 indentation)', -> + equal ''' + [[1],null,[],{a:"str"},{}] + ''', cson [ [1], null, [], a: 'str', {} ], null, 0 + it 'handles objects', -> equal ''' "": "empty" @@ -82,6 +95,22 @@ describe 'CSON.stringify', -> ] } + it 'handles objects (with 0 indentation)', -> + equal ''' + "":"empty","non\\nidentifier":true,default:false,nested:{string:"too"},array:[{},[]] + ''', cson { + '': 'empty' + "non\nidentifier": true + default: false + nested: { + string: 'too' + } + array: [ + {} + [] + ] + }, null, 0 + it 'handles NaN and +/-Infinity like JSON.stringify does', -> equal 'null', cson NaN equal 'null', cson +Infinity @@ -93,10 +122,6 @@ describe 'CSON.stringify', -> it 'handles functions like JSON.stringify does', -> equal undefined, cson -> - it 'works just like JSON.stringify when asking for no indentation', -> - equal '{"zeroed":0}', cson zeroed: 0, null, 0 - equal '{"empty":""}', cson empty: '', null, '' - it 'accepts no more than ten indentation steps, just like JSON.stringify', -> equal ''' x: From fe0aac513bb3ef2daee450a18d1bff25ec251946 Mon Sep 17 00:00:00 2001 From: charlierudolph Date: Wed, 6 May 2015 15:28:16 -0700 Subject: [PATCH 2/3] cleanup --- lib/stringify.js | 6 +----- src/stringify.coffee | 6 +++--- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/lib/stringify.js b/lib/stringify.js index 169df1d..426e5db 100644 --- a/lib/stringify.js +++ b/lib/stringify.js @@ -111,11 +111,7 @@ module.exports = function(data, visitor, indent) { bracesRequired: !indent }); if (indent) { - if (isObject(value)) { - serializedValue = "\n" + (indentLines(serializedValue)); - } else { - serializedValue = " " + serializedValue; - } + serializedValue = isObject(value) ? "\n" + (indentLines(serializedValue)) : " " + serializedValue; } _results.push(key + ":" + serializedValue); } diff --git a/src/stringify.coffee b/src/stringify.coffee index 08d5f13..2e26b52 100644 --- a/src/stringify.coffee +++ b/src/stringify.coffee @@ -93,10 +93,10 @@ module.exports = (data, visitor, indent) -> key = JSON.stringify key unless key.match jsIdentifierRE serializedValue = visitNode value, bracesRequired: !indent if indent - if isObject value - serializedValue = "\n#{ indentLines serializedValue }" + serializedValue = if isObject value + "\n#{ indentLines serializedValue }" else - serializedValue = " #{ serializedValue }" + " #{ serializedValue }" "#{ key }:#{ serializedValue }" if keypairs.length is 0 From 1f1900d478f0ff9b11ca12f33a24d7e2ff948cc3 Mon Sep 17 00:00:00 2001 From: charlierudolph Date: Wed, 6 May 2015 17:03:49 -0700 Subject: [PATCH 3/3] align if and else --- src/stringify.coffee | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/stringify.coffee b/src/stringify.coffee index 2e26b52..4a4b52c 100644 --- a/src/stringify.coffee +++ b/src/stringify.coffee @@ -81,22 +81,24 @@ module.exports = (data, visitor, indent) -> visitArray = (arr) -> items = arr.map (value) -> visitNode value, bracesRequired: true - serializedItems = if indent - newlineWrap indentLines items.join '\n' - else - items.join ',' + serializedItems = + if indent + newlineWrap indentLines items.join '\n' + else + items.join ',' - "[#{serializedItems}]" + "[#{ serializedItems }]" visitObject = (obj, {bracesRequired}) -> keypairs = for key, value of obj key = JSON.stringify key unless key.match jsIdentifierRE serializedValue = visitNode value, bracesRequired: !indent if indent - serializedValue = if isObject value - "\n#{ indentLines serializedValue }" - else - " #{ serializedValue }" + serializedValue = + if isObject value + "\n#{ indentLines serializedValue }" + else + " #{ serializedValue }" "#{ key }:#{ serializedValue }" if keypairs.length is 0