Permalink
Browse files

Add diffString

  • Loading branch information...
1 parent 1de4e92 commit 03ba0195d881b48575b151c294f04485fc55e93e @andreyvit committed Nov 23, 2012
Showing with 63 additions and 2 deletions.
  1. +20 −0 example/result-colored.jsdiff
  2. +20 −0 example/result.jsdiff
  3. +7 −1 lib/index.iced
  4. +16 −1 test/diff_test.coffee
@@ -0,0 +1,20 @@
+ {
+ boz: [
++ 0
+ 1
+- 2
+- 3
+ 4
+ 5
+ 6
++ 7
+ ]
+ fubar: {
+ kaboom: {
+ afoo: {
+- aboz: "zoba"
++ aboz: "zozoba"
+ }
+ }
+ }
+ }
View
@@ -0,0 +1,20 @@
+ {
+ boz: [
++ 0
+ 1
+- 2
+- 3
+ 4
+ 5
+ 6
++ 7
+ ]
+ fubar: {
+ kaboom: {
+ afoo: {
+- aboz: "zoba"
++ aboz: "zozoba"
+ }
+ }
+ }
+ }
View
@@ -1,5 +1,6 @@
{ SequenceMatcher } = require 'difflib'
{ extendedTypeOf } = require './util'
+{ colorize } = require './colorize'
isScalar = (obj) -> (typeof obj isnt 'object')
@@ -159,4 +160,9 @@ diffScore = (obj1, obj2) ->
[score, change] = diffWithScore(obj1, obj2)
return score
-module.exports = { diff }
+diffString = (obj1, obj2, options) ->
+ return colorize(diff(obj1, obj2), options)
+
+
+
+module.exports = { diff, diffString }
View
@@ -1,6 +1,8 @@
+fs = require 'fs'
+Path = require 'path'
assert = require 'assert'
-{ diff } = require "../#{process.env.JSLIB or 'lib'}/index"
+{ diff, diffString } = require "../#{process.env.JSLIB or 'lib'}/index"
describe 'diff', ->
@@ -62,3 +64,16 @@ describe 'diff', ->
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 } }])
+
+
+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.