-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(diff): detailed comparison via verbose output flag (#61)
- Loading branch information
1 parent
9dca832
commit 0becf21
Showing
6 changed files
with
173 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
function flattenRulesDiff(diff) { | ||
if (Array.isArray(diff)) { | ||
return flattenRulesDiffArray(diff) | ||
} else if (typeof diff === 'object') { | ||
return flattenRulesDiffObject(diff) | ||
} | ||
|
||
return [] | ||
} | ||
|
||
function flattenRulesDiffObject(diffObject) { | ||
var flattened = [] | ||
|
||
Object.keys(diffObject).forEach(function flattenEachRuleDiff(ruleName) { | ||
var ruleRow = [ruleName] | ||
var diff = diffObject[ruleName] | ||
|
||
Object.keys(diff).forEach(function flattenEachChildProp(configName) { | ||
ruleRow.push(diff[configName]) | ||
}) | ||
|
||
flattened.push.apply(flattened, ruleRow) | ||
}) | ||
|
||
return flattened | ||
} | ||
|
||
function flattenRulesDiffArray(diffArray) { | ||
var flattened = [] | ||
|
||
diffArray.forEach(function flattenEachDiff(diff) { | ||
flattened.push.apply(flattened, flattenRulesDiff(diff)) | ||
}) | ||
|
||
return flattened | ||
} | ||
|
||
module.exports = flattenRulesDiff |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
function stringifyRuleConfig(rule) { | ||
if (typeof rule === 'string') { | ||
return rule | ||
} else if (typeof rule === 'undefined') { | ||
return '-' | ||
} | ||
|
||
return JSON.stringify(rule) | ||
} | ||
|
||
module.exports = stringifyRuleConfig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
var assert = require('assert') | ||
var flattenRulesDiff = require('../../src/lib/flatten-rules-diff') | ||
|
||
describe('flatten rules diff', function() { | ||
it('should return flat array from diff-object with single rule', function() { | ||
assert.deepEqual( | ||
flattenRulesDiff({'foo-rule': {config1: [2, 'foo'], config2: [2, 'bar']}}), | ||
['foo-rule', [2, 'foo'], [2, 'bar']] | ||
) | ||
}) | ||
|
||
it('should return flat array from diff-object with multiple rules', function() { | ||
assert.deepEqual( | ||
flattenRulesDiff({ | ||
'foo-rule': {config1: [2, 'foo'], config2: [2, 'bar']}, | ||
'bar-rule': {config1: undefined, config2: [1, 'bar']}, | ||
}), | ||
['foo-rule', [2, 'foo'], [2, 'bar'], 'bar-rule', undefined, [1, 'bar']] | ||
) | ||
}) | ||
|
||
it('should return flat array from an array of diff-objects', function() { | ||
assert.deepEqual( | ||
flattenRulesDiff([ | ||
{'foo-rule': {config1: [2, 'foo'], config2: [2, 'bar']}}, | ||
{'bar-rule': {config1: undefined, config2: [1, 'bar']}}, | ||
]), | ||
['foo-rule', [2, 'foo'], [2, 'bar'], 'bar-rule', undefined, [1, 'bar']] | ||
) | ||
}) | ||
|
||
it('should return empty array on anything else', function() { | ||
assert.deepEqual( | ||
flattenRulesDiff(undefined), | ||
[] | ||
) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
var assert = require('assert') | ||
var stringifyRuleConfig = require('../../src/lib/stringify-rule-config') | ||
|
||
describe('stringify rule config', function() { | ||
it('should return a string', function() { | ||
assert.equal( | ||
stringifyRuleConfig('A simple string'), | ||
'A simple string' | ||
) | ||
}) | ||
|
||
it('should return \'-\' for "undefined"', function() { | ||
assert.equal( | ||
stringifyRuleConfig(undefined), | ||
'-' | ||
) | ||
}) | ||
|
||
it('should return a JSON.stringify\'ed result for any object', function() { | ||
assert.deepEqual( | ||
stringifyRuleConfig([2, 'foo', {bar: true}]), | ||
JSON.stringify([2, 'foo', {bar: true}]) | ||
) | ||
}) | ||
}) |