variable-diff
Advanced tools
Comparing version 1.1.0 to 2.0.0
74
index.js
'use strict'; | ||
var chalk = require('chalk'); | ||
var objectAssign = require('object-assign'); | ||
@@ -12,16 +11,11 @@ var typeColors = { | ||
var options = { | ||
indent: 2, | ||
indentChar: ' ', | ||
newLineChar: '\n', | ||
var defaultOptions = { | ||
indent: ' ', | ||
newLine: '\n', | ||
wrap: function wrap(type, text) { | ||
return chalk[typeColors[type]](text); | ||
} | ||
}, | ||
color: true | ||
}; | ||
var indent = ''; | ||
for (var i = 0; i < options.indent; i++) { | ||
indent += options.indentChar; | ||
} | ||
function isObject(obj) { | ||
@@ -41,24 +35,24 @@ return typeof obj === 'object' && obj && !Array.isArray(obj); | ||
function indentSubItem(text) { | ||
return text.split(options.newLineChar).map(function onMap(line, index) { | ||
function indentSubItem(text, options) { | ||
return text.split(options.newLine).map(function onMap(line, index) { | ||
if (index === 0) { | ||
return line; | ||
} | ||
return indent + line; | ||
}).join(options.newLineChar); | ||
return options.indent + line; | ||
}).join(options.newLine); | ||
} | ||
function keyChanged(key, text) { | ||
return indent + key + ': ' + indentSubItem(text) + options.newLineChar | ||
function keyChanged(key, text, options) { | ||
return options.indent + key + ': ' + indentSubItem(text, options) + options.newLine | ||
} | ||
function keyRemoved(key, variable) { | ||
return options.wrap('removed', '- ' + key + ': ' + printVar(variable)) + options.newLineChar; | ||
function keyRemoved(key, variable, options) { | ||
return options.wrap('removed', '- ' + key + ': ' + printVar(variable)) + options.newLine; | ||
} | ||
function keyAdded(key, variable) { | ||
return options.wrap('added', '+ ' + key + ': ' + printVar(variable)) + options.newLineChar; | ||
function keyAdded(key, variable, options) { | ||
return options.wrap('added', '+ ' + key + ': ' + printVar(variable)) + options.newLine; | ||
} | ||
function diff(left, right) { | ||
function diffInternal(left, right, options) { | ||
var text = ''; | ||
@@ -73,9 +67,9 @@ var changed = false; | ||
if (i < right.length) { | ||
itemDiff = diff(left[i], right[i]); | ||
itemDiff = diffInternal(left[i], right[i], options); | ||
if (itemDiff.changed) { | ||
subOutput += keyChanged(i, itemDiff.text); | ||
subOutput += keyChanged(i, itemDiff.text, options); | ||
changed = true; | ||
} | ||
} else { | ||
subOutput += keyRemoved(i, left[i]); | ||
subOutput += keyRemoved(i, left[i], options); | ||
changed = true; | ||
@@ -86,3 +80,3 @@ } | ||
for (; i < right.length; i++) { | ||
subOutput += keyAdded(i, right[i]); | ||
subOutput += keyAdded(i, right[i], options); | ||
} | ||
@@ -92,7 +86,7 @@ changed = true; | ||
if (changed) { | ||
text = '[' + options.newLineChar + subOutput + ']'; | ||
text = '[' + options.newLine + subOutput + ']'; | ||
} | ||
} else if (isObject(left) && isObject(right)) { | ||
keys = Object.keys(left); | ||
var rightObj = objectAssign({}, right); | ||
var rightObj = Object.assign({}, right); | ||
var key; | ||
@@ -103,5 +97,5 @@ keys.sort(); | ||
if (right.hasOwnProperty(key)) { | ||
itemDiff = diff(left[key], right[key]); | ||
itemDiff = diffInternal(left[key], right[key], options); | ||
if (itemDiff.changed) { | ||
subOutput += keyChanged(key, itemDiff.text); | ||
subOutput += keyChanged(key, itemDiff.text, options); | ||
changed = true; | ||
@@ -111,3 +105,3 @@ } | ||
} else { | ||
subOutput += keyRemoved(key, left[key]); | ||
subOutput += keyRemoved(key, left[key], options); | ||
changed = true; | ||
@@ -119,3 +113,3 @@ } | ||
for (var i = 0; i < addedKeys.length; i++) { | ||
subOutput += keyAdded(addedKeys[i], right[addedKeys[i]]); | ||
subOutput += keyAdded(addedKeys[i], right[addedKeys[i]], options); | ||
changed = true; | ||
@@ -125,3 +119,3 @@ } | ||
if (changed) { | ||
text = '{' + options.newLineChar + subOutput + '}'; | ||
text = '{' + options.newLine + subOutput + '}'; | ||
} | ||
@@ -140,3 +134,17 @@ | ||
function diff(left, right, options) { | ||
options = options || {}; | ||
if (!options.color && options.wrap) { | ||
throw new Error('Can\'t specify wrap and color options together.') | ||
} | ||
var combinedOptions = Object.assign({}, defaultOptions, options); | ||
if (!combinedOptions.color) { | ||
combinedOptions.wrap = function(type, text) { return text } | ||
} | ||
return diffInternal(left, right, combinedOptions) | ||
} | ||
module.exports = diff; |
{ | ||
"name": "variable-diff", | ||
"version": "1.1.0", | ||
"version": "2.0.0", | ||
"description": "Visual diff between javascript variables", | ||
@@ -11,3 +11,3 @@ "main": "index.js", | ||
"devDependencies": { | ||
"tap": "^5.4.2" | ||
"tap": "^14.6.5" | ||
}, | ||
@@ -14,0 +14,0 @@ "scripts": { |
@@ -14,7 +14,15 @@ # variable-diff | ||
var result = diff({ a: 1, b: 2, d: 'hello' }, { a: 8, b: 2, c: 4}); | ||
var defaultOptions = { | ||
indent: ' ', | ||
newLine: '\n', | ||
wrap: function wrap(type, text) { | ||
return chalk[typeColors[type]](text); | ||
}, | ||
color: true | ||
}; | ||
var result = diff({ a: 1, b: 2, d: 'hello' }, { a: 8, b: 2, c: 4}, defaultOptions); | ||
console.log(result.text); | ||
``` | ||
![output](http://i.imgur.com/3cOu6Wr.png?1) | ||
@@ -21,0 +29,0 @@ ### Test |
58
test.js
@@ -6,3 +6,3 @@ var tap = require('tap'); | ||
test('null no diff', function(t) { | ||
t.same(diff(null, null), { | ||
t.same(diff(null, null, {color: false}), { | ||
changed: false, | ||
@@ -15,3 +15,3 @@ text: '' | ||
test('null and object', function(t) { | ||
t.same(diff(null, { a: 1 }), { | ||
t.same(diff(null, { a: 1 }, {color: false}), { | ||
changed: true, | ||
@@ -24,3 +24,3 @@ text: 'null => {"a":1}' | ||
test('null and string', function(t) { | ||
t.same(diff(null, 'b'), { | ||
t.same(diff(null, 'b', {color: false}), { | ||
changed: true, | ||
@@ -33,3 +33,3 @@ text: 'null => "b"' | ||
test('undefined no diff', function(t) { | ||
t.same(diff(undefined, undefined), { | ||
t.same(diff(undefined, undefined, {color: false}), { | ||
changed: false, | ||
@@ -42,3 +42,3 @@ text: '' | ||
test('undefined and number', function(t) { | ||
t.same(diff(undefined, 21), { | ||
t.same(diff(undefined, 21, {color: false}), { | ||
changed: true, | ||
@@ -51,3 +51,3 @@ text: 'undefined => 21' | ||
test('string no diff', function(t) { | ||
t.same(diff('hello', 'hello'), { | ||
t.same(diff('hello', 'hello', {color: false}), { | ||
changed: false, | ||
@@ -60,3 +60,3 @@ text: '' | ||
test('string changed', function(t) { | ||
t.same(diff('hello', 'hellos'), { | ||
t.same(diff('hello', 'hellos', {color: false}), { | ||
changed: true, | ||
@@ -69,3 +69,3 @@ text: '"hello" => "hellos"' | ||
test('string to number', function(t) { | ||
t.same(diff('fun', 9), { | ||
t.same(diff('fun', 9, {color: false}), { | ||
changed: true, | ||
@@ -78,3 +78,3 @@ text: '"fun" => 9' | ||
test('string to function', function(t) { | ||
t.same(diff('fun', function hello() { console.log('test') }), { | ||
t.same(diff('fun', function hello() { console.log('test') }, {color: false}), { | ||
changed: true, | ||
@@ -87,3 +87,3 @@ text: '"fun" => function hello() {}' | ||
test('string to regex', function(t) { | ||
t.same(diff('fun', /search/), { | ||
t.same(diff('fun', /search/, {color: false}), { | ||
changed: true, | ||
@@ -96,3 +96,3 @@ text: '"fun" => /search/' | ||
test('number not changed', function(t) { | ||
t.same(diff(987, 987), { | ||
t.same(diff(987, 987, {color: false}), { | ||
changed: false, | ||
@@ -104,3 +104,3 @@ text: '' | ||
test('number and string', function(t) { | ||
t.same(diff(1, 'hellos'), { | ||
t.same(diff(1, 'hellos', {color: false}), { | ||
changed: true, | ||
@@ -113,3 +113,3 @@ text: '1 => "hellos"' | ||
test('number and object', function(t) { | ||
t.same(diff(9, { test: 1 }), { | ||
t.same(diff(9, { test: 1 }, {color: false}), { | ||
changed: true, | ||
@@ -123,3 +123,3 @@ text: '9 => {"test":1}' | ||
function myFunc() {} | ||
t.same(diff(myFunc, myFunc), { | ||
t.same(diff(myFunc, myFunc, {color: false}), { | ||
changed: false, | ||
@@ -132,3 +132,3 @@ text: '' | ||
test('function same code different function', function(t) { | ||
t.same(diff(function test() { console.log('1'); } , function test() { console.log('1'); }), { | ||
t.same(diff(function test() { console.log('1'); } , function test() { console.log('1'); }, {color: false}), { | ||
changed: true, | ||
@@ -141,3 +141,3 @@ text: 'function test() {} => function test() {}' | ||
test('function and number', function(t) { | ||
t.same(diff(function () { console.log('1'); } , 2), { | ||
t.same(diff(function () { console.log('1'); } , 2, {color: false}), { | ||
changed: true, | ||
@@ -150,3 +150,3 @@ text: 'function () {} => 2' | ||
test('array switch index', function(t) { | ||
t.same(diff([9,8,1], [8,9,1]), { | ||
t.same(diff([9,8,1], [8,9,1], {color: false}), { | ||
changed: true, | ||
@@ -159,3 +159,3 @@ text: '[\n 0: 9 => 8\n 1: 8 => 9\n]' | ||
test('array add 2 items', function(t) { | ||
t.same(diff([8,9,1], [8,9,1,4,3]), { | ||
t.same(diff([8,9,1], [8,9,1,4,3], {color: false}), { | ||
changed: true, | ||
@@ -168,3 +168,3 @@ text: '[\n+ 3: 4\n+ 4: 3\n]' | ||
test('array remove 2 items', function(t) { | ||
t.same(diff([9,8,7], [9]), { | ||
t.same(diff([9,8,7], [9], {color: false}), { | ||
changed: true, | ||
@@ -177,3 +177,3 @@ text: '[\n- 1: 8\n- 2: 7\n]' | ||
test('array empty', function(t) { | ||
t.same(diff([], []), { | ||
t.same(diff([], [], {color: false}), { | ||
changed: false, | ||
@@ -186,3 +186,3 @@ text: '' | ||
test('array and object', function(t) { | ||
t.same(diff([1,2], { key: 1 }), { | ||
t.same(diff([1,2], { key: 1 }, {color: false}), { | ||
changed: true, | ||
@@ -195,3 +195,3 @@ text: '[1,2] => {"key":1}' | ||
test('array and null', function(t) { | ||
t.same(diff([2], null), { | ||
t.same(diff([2], null, {color: false}), { | ||
changed: true, | ||
@@ -204,3 +204,3 @@ text: '[2] => null' | ||
test('object same structure', function(t) { | ||
t.same(diff({ test: 1, and: 'fun'}, { test: 1, and: 'fun'}), { | ||
t.same(diff({ test: 1, and: 'fun'}, { test: 1, and: 'fun'}, {color: false}), { | ||
changed: false, | ||
@@ -213,3 +213,3 @@ text: '' | ||
test('object nested object', function(t) { | ||
t.same(diff({ test: { a: { b: 3, c: 5}}, and: 'fun'}, { test: { a: { b: 3, c: 5}}, and: 'fun'}), { | ||
t.same(diff({ test: { a: { b: 3, c: 5}}, and: 'fun'}, { test: { a: { b: 3, c: 5}}, and: 'fun'}, {color: false}), { | ||
changed: false, | ||
@@ -222,3 +222,3 @@ text: '' | ||
test('object nested object different', function(t) { | ||
t.same(diff({ test: { a: { b: 3, c: 5}}, and: 'fun'}, { test: { a: { b: 3, c: 'test'}}, and: 'fun'}), { | ||
t.same(diff({ test: { a: { b: 3, c: 5}}, and: 'fun'}, { test: { a: { b: 3, c: 'test'}}, and: 'fun'}, {color: false}), { | ||
changed: true, | ||
@@ -231,3 +231,3 @@ text: '{\n test: {\n a: {\n c: 5 => "test"\n }\n }\n}' | ||
test('object keys removed', function(t) { | ||
t.same(diff({ a: 1, b: 2, c: 3}, { a: 1 }), { | ||
t.same(diff({ a: 1, b: 2, c: 3}, { a: 1 }, {color: false}), { | ||
changed: true, | ||
@@ -240,3 +240,3 @@ text: '{\n- b: 2\n- c: 3\n}' | ||
test('object keys added', function(t) { | ||
t.same(diff({ a: 1 }, { a: 1, b: 9, c: 2 }), { | ||
t.same(diff({ a: 1 }, { a: 1, b: 9, c: 2 }, {color: false}), { | ||
changed: true, | ||
@@ -249,3 +249,3 @@ text: '{\n+ b: 9\n+ c: 2\n}' | ||
test('object with nested array', function(t) { | ||
t.same(diff({ test: { a: [5,6,7]}, and: 'fun'}, { test: { a: [9,8,2,4]}, and: 'fun'}), { | ||
t.same(diff({ test: { a: [5,6,7]}, and: 'fun'}, { test: { a: [9,8,2,4]}, and: 'fun'}, {color: false}), { | ||
changed: true, | ||
@@ -258,3 +258,3 @@ text: '{\n test: {\n a: [\n 0: 5 => 9\n 1: 6 => 8\n 2: 7 => 2\n + 3: 4\n ]\n }\n}' | ||
test('complex nested object difference', function(t) { | ||
t.same(diff({ test: { a: [5,6,7]}, b: { c: 1 }, and: 'fun'}, { test: { a: [9,8,2,4]}, b: {d: 2 }, and: [1,2]}), { | ||
t.same(diff({ test: { a: [5,6,7]}, b: { c: 1 }, and: 'fun'}, { test: { a: [9,8,2,4]}, b: {d: 2 }, and: [1,2]}, {color: false}), { | ||
changed: true, | ||
@@ -261,0 +261,0 @@ text: '{\n and: "fun" => [1,2]\n b: {\n - c: 1\n + d: 2\n }\n test: {\n a: [\n 0: 5 => 9\n 1: 6 => 8\n 2: 7 => 2\n + 3: 4\n ]\n }\n}' |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
327
32
23006
8