Permalink
Browse files

Fix formatting of complex array diffs

  • Loading branch information...
1 parent bfdcb2a commit a582fd1c182245f8ea364876ce97fe49c3170d9d @andreyvit committed Nov 23, 2012
Showing with 9 additions and 2 deletions.
  1. +2 −2 lib/colorize.iced
  2. +7 −0 test/colorize_test.coffee
View
4 lib/colorize.iced
@@ -33,13 +33,13 @@ subcolorizeToCallback = (key, diff, output, color, indent) ->
looksLikeDiff = yes
for item in diff
- if (extendedTypeOf(item) isnt 'array') or (item.length != 2) or !(typeof(item[0]) is 'string') or item[0].length != 1 or !(item[0] in [' ', '-', '+', '~'])
+ if (extendedTypeOf(item) isnt 'array') or !((item.length is 2) or ((item.length is 1) and (item[0] is ' '))) or !(typeof(item[0]) is 'string') or item[0].length != 1 or !(item[0] in [' ', '-', '+', '~'])
looksLikeDiff = no
if looksLikeDiff
for [op, subvalue] in diff
if op is ' ' && !subvalue?
- subcolorizeToCallback('', '...', output, ' ', subindent)
+ output(' ', subindent + '...')
else
unless op in [' ', '~', '+', '-']
throw new Error("Unexpected op '#{op}' in #{JSON.stringify(diff, null, 2)}")
View
7 test/colorize_test.coffee
@@ -28,6 +28,13 @@ describe 'colorizeToArray', ->
it "should return '-<deleted item>' for an array diff", ->
assert.deepEqual [' [', ' 10', '- 20', ' 30', ' ]'], colorizeToArray([[' ', 10], ['-', 20], [' ', 30]])
+ it "should handle an array diff with subobject diff", ->
+ input = [ [" "], ["~", {"foo__added": 42}], [" "] ]
+ expected = [" [", " ...", " {", "+ foo: 42", " }", " ...", " ]"]
+ console.log "output:\n%s", colorizeToArray(input).join("\n")
+ assert.deepEqual colorizeToArray(input), expected
+
+
describe 'colorize', ->

0 comments on commit a582fd1

Please sign in to comment.