Skip to content

Commit

Permalink
backup almost working version
Browse files Browse the repository at this point in the history
  • Loading branch information
igshipilov committed Oct 20, 2023
1 parent e9dff93 commit f022303
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
2 changes: 1 addition & 1 deletion gendiff.js → bin/gendiff.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env node

import { program } from 'commander';
import genDiff from './src/index.js';
import genDiff from '../src/index.js';

program
.name('gendiff')
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"test": "npx jest"
},
"bin": {
"gendiff": "gendiff.js"
"gendiff": "bin/gendiff.js"
},
"keywords": [
"study",
Expand Down
29 changes: 24 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@ import getParsedFile from './parsers.js';
const getStylish = (data) => {
const iter = (node, depth) => {
// if (!Array.isArray(node)) {

Check failure on line 6 in src/index.js

View workflow job for this annotation

GitHub Actions / build

Trailing spaces not allowed

if (!_.isPlainObject(node) && !Array.isArray(node)) {
return node;
}

// if (_.isPlainObject(node)) {
// return iter([node], depth + 1);
// }


Check failure on line 16 in src/index.js

View workflow job for this annotation

GitHub Actions / build

More than 1 blank line not allowed

const spacer = ' ';
const spacerCount = 4;
const indentCount = (spacerCount * depth) - 2;
Expand All @@ -33,6 +34,9 @@ const getStylish = (data) => {
const [currentStat, key] = Object.keys(obj);

Check failure on line 34 in src/index.js

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 6 spaces but found 8
const value = obj[key];
const stat = obj[currentStat];
if (!_.has(obj, 'stat')) {
return obj;
}

if (stat === 'added') {
return `${currentIndent}${statSign(stat)}${key}: ${iter(value, depth + 1)}`;
Expand All @@ -44,6 +48,15 @@ const getStylish = (data) => {
const [value1, value2] = value;
const [deleted, added] = statSign(stat);

const processValue = (val) => {
if (_.isPlainObject(val)) {
const entries = Object.entries(val);
const result = entries.map(([key, value]) => `${key}: ${processValue(value)}`);
return `{\n${result.join(',\n')}\n}`;
}
return val;
}

// FIXME -- возвращает неправильный результат:
// `nest: [object Object]` вместо `nest: { key: value }`

Expand All @@ -52,9 +65,13 @@ const getStylish = (data) => {

// но если так сделать, то вылетает ошибка:
// TypeError: node.map is not a function
// потому что я пытаюсь замэпить `{ key: value }`,
// когда итеративно проваливаюсь в value1
return `${currentIndent}${deleted}${key}: ${value1}${br}${currentIndent}${added}${key}: ${value2}`;
// потому что я пытаюсь замэпить объект `{ key: value }`,
// когда итеративно проваливаюсь в value1 и value2

// return `${currentIndent}${deleted}${key}: ${value1}${br}${currentIndent}${added}${key}: ${value2}`;
return `${currentIndent}${deleted}${key}: ${processValue(value1)}${br}${currentIndent}${added}${key}: ${processValue(value2)}`;
// return `${currentIndent}${deleted}${key}: ${iter(value1, depth + 1)}${br}${currentIndent}${added}${key}: ${iter(value2, depth + 1)}`;
// return `${currentIndent}${deleted}${key}: ${processValue(value1)}${br}${currentIndent}${added}${key}: ${processValue(value2)}`;

} if (stat === 'unchanged') {
return `${currentIndent}${statSign(stat)}${key}: ${iter(value, depth + 1)}`;
Expand All @@ -64,7 +81,8 @@ const getStylish = (data) => {
}
});

return ['{', ...arr, `${bracketIndent}}`].join('\n');
return ['{', ...arr, `${bracketIndent}}`]
.join('\n');
};

return iter(data, 1);
Expand Down Expand Up @@ -171,6 +189,7 @@ const genDiff = (file1, file2, formatter = 'stylish') => {

console.log('>> genDiff:');
// const testResult = JSON.stringify(genDiff('__fixtures__/gdFile1.json', '__fixtures__/gdFile2.json'), null, ' ');
// const testResult = JSON.stringify(genDiff('__fixtures__/file1.json', '__fixtures__/file2.json'), null, 2);
// console.log(testResult);

// console.log(genDiff('__fixtures__/gdFile1.json', '__fixtures__/gdFile2.json'));
Expand Down

0 comments on commit f022303

Please sign in to comment.