Skip to content

Commit

Permalink
Add diffString
Browse files Browse the repository at this point in the history
  • Loading branch information
andreyvit committed Nov 23, 2012
1 parent 1de4e92 commit 03ba019
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 2 deletions.
20 changes: 20 additions & 0 deletions example/result-colored.jsdiff
@@ -0,0 +1,20 @@
{
boz: [
+ 0
1
- 2
- 3
4
5
6
+ 7
]
fubar: {
kaboom: {
afoo: {
- aboz: "zoba"
+ aboz: "zozoba"
}
}
}
}
20 changes: 20 additions & 0 deletions example/result.jsdiff
@@ -0,0 +1,20 @@
{
boz: [
+ 0
1
- 2
- 3
4
5
6
+ 7
]
fubar: {
kaboom: {
afoo: {
- aboz: "zoba"
+ aboz: "zozoba"
}
}
}
}
8 changes: 7 additions & 1 deletion lib/index.iced
@@ -1,5 +1,6 @@
{ SequenceMatcher } = require 'difflib' { SequenceMatcher } = require 'difflib'
{ extendedTypeOf } = require './util' { extendedTypeOf } = require './util'
{ colorize } = require './colorize'


isScalar = (obj) -> (typeof obj isnt 'object') isScalar = (obj) -> (typeof obj isnt 'object')


Expand Down Expand Up @@ -159,4 +160,9 @@ diffScore = (obj1, obj2) ->
[score, change] = diffWithScore(obj1, obj2) [score, change] = diffWithScore(obj1, obj2)
return score return score


module.exports = { diff } diffString = (obj1, obj2, options) ->
return colorize(diff(obj1, obj2), options)



module.exports = { diff, diffString }
17 changes: 16 additions & 1 deletion test/diff_test.coffee
@@ -1,6 +1,8 @@
fs = require 'fs'
Path = require 'path'
assert = require 'assert' assert = require 'assert'


{ diff } = require "../#{process.env.JSLIB or 'lib'}/index" { diff, diffString } = require "../#{process.env.JSLIB or 'lib'}/index"


describe 'diff', -> describe 'diff', ->


Expand Down Expand Up @@ -62,3 +64,16 @@ describe 'diff', ->


it "should return [..., ['~', <diff>], ...] for two arrays when an item has been modified (note: involves a crazy heuristic)", -> it "should return [..., ['~', <diff>], ...] for two arrays when an item has been modified (note: involves a crazy heuristic)", ->
assert.deepEqual [[' '], ['~', { foo: { __old: 20, __new: 21 } }], [' ']], diff([{ foo: 10, bar: { bbbar: 10, bbboz: 11 } }, { foo: 20, bar: { bbbar: 50, bbboz: 25 } }, { foo: 30, bar: { bbbar: 92, bbboz: 34 } }], [{ foo: 10, bar: { bbbar: 10, bbboz: 11 } }, { foo: 21, bar: { bbbar: 50, bbboz: 25 } }, { foo: 30, bar: { bbbar: 92, bbboz: 34 } }]) assert.deepEqual [[' '], ['~', { foo: { __old: 20, __new: 21 } }], [' ']], diff([{ foo: 10, bar: { bbbar: 10, bbboz: 11 } }, { foo: 20, bar: { bbbar: 50, bbboz: 25 } }, { foo: 30, bar: { bbbar: 92, bbboz: 34 } }], [{ foo: 10, bar: { bbbar: 10, bbboz: 11 } }, { foo: 21, bar: { bbbar: 50, bbboz: 25 } }, { foo: 30, bar: { bbbar: 92, bbboz: 34 } }])


describe 'diffString', ->

readExampleFile = (file) -> fs.readFileSync(Path.join(__dirname, '../example', file), 'utf8')
a = JSON.parse(readExampleFile('a.json'))
b = JSON.parse(readExampleFile('b.json'))

it "should produce the expected result for the example JSON files", ->
assert.equal diffString(a, b, color: no), readExampleFile('result.jsdiff')

it "should produce the expected colored result for the example JSON files", ->
assert.equal diffString(a, b), readExampleFile('result-colored.jsdiff')

0 comments on commit 03ba019

Please sign in to comment.