From 6d3ca776ac9330b4988129c372b37a52770ce320 Mon Sep 17 00:00:00 2001 From: Nicolas Date: Wed, 1 Apr 2015 16:26:56 +0200 Subject: [PATCH] Merging upstream & inPlus and InLess & cli-tables --- bin/jsobjectdiff | 35 +++++++++++++++++++++++++++-------- index.js | 46 +++++++++++++++++++++++++++++++++++++++++++--- package.json | 1 + tests/test.json | 19 ++++++++----------- 4 files changed, 79 insertions(+), 22 deletions(-) diff --git a/bin/jsobjectdiff b/bin/jsobjectdiff index cadc245..aff8d97 100755 --- a/bin/jsobjectdiff +++ b/bin/jsobjectdiff @@ -7,8 +7,7 @@ var _ = require('lodash'); var fs = require('fs'); var path = require('path'); var when = require('when'); - -var program = require('commander'); +var Table = require('cli-table'); var opts = require('nomnom') .option('config', { @@ -22,8 +21,8 @@ var opts = require('nomnom') var FULL_CONFIG_PATH = path.resolve(process.cwd(), opts.config); when - .promise(function (resolve, reject) { - fs.readFile(FULL_CONFIG_PATH, 'utf8', function (err, data) { + .promise(function(resolve, reject) { + fs.readFile(FULL_CONFIG_PATH, 'utf8', function(err, data) { if (err) { reject(err); return; @@ -35,9 +34,10 @@ when .then(function onSuccess(data) { when.try(JSON.parse, data).then(function onSuccess(json) { - var configuration = json.files.map(function (file) { + var configuration = json.files.map(function(file) { var absolutePath = path.resolve(path.dirname(FULL_CONFIG_PATH), file.filePath); return { + filePath: file.filePath, objectPath: file.objectPath, content: fs.readFileSync(absolutePath).toString('utf8') }; @@ -48,8 +48,27 @@ when if (diff.length === 0) { process.exit(0); } - - console.info('Diff:', JSON.stringify(diff, null, 2)); + var table = new Table(); + diff.map(function(objectFiles) { + table.push(['filePath', 'objectPath']); + table.push([objectFiles.filePath, objectFiles.objectPath]); + objectFiles.comparedTo.map(function(comparaison) { + table.push(['Compared with : ' + comparaison.filePath + ' ' + comparaison.objectPath]); + table.push(['comparaison', 'key', 'index']); + comparaison.inPlus.map(function(keyAndIndex) { + table.push({ + 'inPlus': [keyAndIndex.key, keyAndIndex.index] + }) + }); + comparaison.inLess.map(function(keyAndIndex) { + table.push({ + 'inLess': [keyAndIndex.key, keyAndIndex.index] + }) + }); + }) + table.push() + }) + console.log(table.toString()); process.exit(diff.length); }, function onError(err) { @@ -59,4 +78,4 @@ when }, function onError(err) { console.error('Could not open %s', FULL_CONFIG_PATH); console.error(err); - }); + }); \ No newline at end of file diff --git a/index.js b/index.js index cb0aa05..558a265 100644 --- a/index.js +++ b/index.js @@ -15,10 +15,50 @@ module.exports = { return objectPath.get(eval(fileContent), file.objectPath); }); - var filesKeys = filesObjects.map(function (obj) { - return Object.keys(obj); + var filesKeys = filesObjects.map(function(obj, index) { + return { + filePath: files[index].filePath, + objectPath: files[index].objectPath, + fileKeys: Object.keys(obj) + } }); - return _.union(_.difference.apply(_, filesKeys), _.difference.apply(_, filesKeys.reverse())); + return filesKeys.map(function(obj) { + return { + filePath: obj.filePath, + objectPath: obj.objectPath, + comparedTo: _.flatten(filesKeys.map(function(otherObj){ + if(obj.filePath == otherObj.filePath && obj.objectPath == otherObj.objectPath){ + return [] + } + return { + filePath: otherObj.filePath, + objectPath: otherObj.objectPath, + inPlus: _.flatten(_.difference(obj.fileKeys, otherObj.fileKeys).map(function(differenceKeys){ + return _.flatten(obj.fileKeys.map(function(objKey, index){ + if(objKey == differenceKeys){ + return { + key: objKey, + index: index + } + } + return [] + })) + })), + inLess: _.flatten(_.difference(otherObj.fileKeys, obj.fileKeys).map(function(differenceKeys){ + return _.flatten(otherObj.fileKeys.map(function(objKey, index){ + if(objKey == differenceKeys){ + return { + key: objKey, + index: index + } + } + return [] + })) + })) + } + })) + } + }) } }; diff --git a/package.json b/package.json index e0114f8..02533cd 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,7 @@ "author": "Francois-Guillaume Ribreau (http://fgribreau.com/)", "license": "MIT", "dependencies": { + "cli-table": "^0.3.1", "lodash": "^3.5.0", "nomnom": "^1.8.1", "object-path": "^0.9.0", diff --git a/tests/test.json b/tests/test.json index f2fc8d1..3cf14b0 100644 --- a/tests/test.json +++ b/tests/test.json @@ -1,14 +1,11 @@ { - "files": [ - [ - "./fixtures/en_US.js", - "i18n.en_US" - ], - [ - "./fixtures/fr_FR.js", - "i18n.fr_FR" - ] - ], "pre": "(function (Bringr) {", - "post": "return Bringr;})({});" + "post": "return Bringr;})({});", + "files": [{ + "filePath": "./fixtures/en_US.js", + "objectPath": "i18n.en_US" + }, { + "filePath": "./fixtures/fr_FR.js", + "objectPath": "i18n.fr_FR" + }] } \ No newline at end of file