-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
stringify cson with no indent #37
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,45 +79,54 @@ 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 | ||
serializedValue = if isObject value | ||
"\n#{ indentLines serializedValue }" | ||
else | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The indentation here doesn't seem to align There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thats the proper place for the else. Its matching if is only two lines above, not three There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it does – lines 96 and 98 go together, not 95 and 98. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, got it. Sorry. Could you maybe change this to something like this: serializedValue =
if isObject value
"\n#{ indentLines serializedValue }"
else
" #{ serializedValue }" That way it's a bit easier to scan the left side of the code and grok the structure. Partially a question of personal taste but I'm fairly certain that matches our (informal) code style. |
||
"#{ key }:\n#{ indentLines 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}" | ||
|
||
when 'number' | ||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here - I'd prefer if we keep
if
andelse
tokens on one height.