-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
154 additions
and
155 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
[{"name":"common","value":[{"name":"follow","stage":"added","value":false},{"name":"setting1","stage":"unchanged","value":"Value 1"},{"name":"setting2","stage":"removed","value":200},{"name":"setting3","stage":"updated","value":{"removed":true,"added":null}},{"name":"setting4","stage":"added","value":"blah blah"},{"name":"setting5","stage":"added","value":{"key5":"value5"}},{"name":"setting6","value":[{"name":"doge","value":[{"name":"wow","stage":"updated","value":{"removed":"","added":"so much"}}],"stage":"nested"},{"name":"key","stage":"unchanged","value":"value"},{"name":"ops","stage":"added","value":"vops"}],"stage":"nested"}],"stage":"nested"},{"name":"group1","value":[{"name":"baz","stage":"updated","value":{"removed":"bas","added":"bars"}},{"name":"foo","stage":"unchanged","value":"bar"},{"name":"nest","stage":"updated","value":{"removed":{"key":"value"},"added":"str"}}],"stage":"nested"},{"name":"group2","stage":"removed","value":{"abc":12345,"deep":{"id":45}}},{"name":"group3","stage":"added","value":{"deep":{"id":{"number":45}},"fee":100500}}] | ||
[{"name":"common","children":[{"name":"follow","stage":"added","value":false},{"name":"setting1","stage":"unchanged","value":"Value 1"},{"name":"setting2","stage":"removed","value":200},{"name":"setting3","stage":"updated","value":{"removed":true,"added":null}},{"name":"setting4","stage":"added","value":"blah blah"},{"name":"setting5","stage":"added","value":{"key5":"value5"}},{"name":"setting6","children":[{"name":"doge","children":[{"name":"wow","stage":"updated","value":{"removed":"","added":"so much"}}],"stage":"nested"},{"name":"key","stage":"unchanged","value":"value"},{"name":"ops","stage":"added","value":"vops"}],"stage":"nested"}],"stage":"nested"},{"name":"group1","children":[{"name":"baz","stage":"updated","value":{"removed":"bas","added":"bars"}},{"name":"foo","stage":"unchanged","value":"bar"},{"name":"nest","stage":"updated","value":{"removed":{"key":"value"},"added":"str"}}],"stage":"nested"},{"name":"group2","stage":"removed","value":{"abc":12345,"deep":{"id":45}}},{"name":"group3","stage":"added","value":{"deep":{"id":{"number":45}},"fee":100500}}] |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
File renamed without changes.
File renamed without changes.
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,48 @@ | ||
import _ from 'lodash'; | ||
|
||
const normalizeValue = (item) => { | ||
if (_.isObject(item)) { | ||
return '[complex value]'; | ||
} | ||
if (typeof item === 'string') { | ||
return `'${item}'`; | ||
} | ||
return item; | ||
}; | ||
|
||
const plain = (tree) => { | ||
const iter = (item, ancestry) => { | ||
const lines = item | ||
.filter((node) => node.stage !== 'unchanged') | ||
.flatMap((node) => { | ||
const { stage } = node; | ||
const { name } = node; | ||
const newAncestry = `${ancestry}.${name}`; | ||
if (stage === 'nested') { | ||
return iter(node.children, newAncestry); | ||
} | ||
const getTail = (status) => { | ||
const value = normalizeValue(node.value); | ||
switch (status) { | ||
case 'added': | ||
return `added with value: ${value}`; | ||
case 'removed': | ||
return 'removed'; | ||
case 'updated': { | ||
const removed = `${normalizeValue(node.value.removed)}`; | ||
const added = `${normalizeValue(node.value.added)}`; | ||
return `updated. From ${removed} to ${added}`; | ||
} | ||
default: | ||
return `Unknown stage '${status}'`; | ||
} | ||
}; | ||
|
||
const tail = getTail(stage); | ||
return `Property '${newAncestry.slice(1)}' was ${tail}`; | ||
}); | ||
return lines.join('\n'); | ||
}; | ||
return iter(tree, ''); | ||
}; | ||
export default plain; |
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,56 @@ | ||
import _ from 'lodash'; | ||
|
||
const stylish = (tree, replacer = ' ', spacesCount = 4) => { | ||
const stringify = (item, depth) => { | ||
if (!_.isObject(item) || item === null) { | ||
return String(item); | ||
} | ||
|
||
const indentSize = ((depth * spacesCount) - 2); | ||
const currentIndent = replacer.repeat(indentSize); | ||
const bracketIndent = replacer.repeat(indentSize - 2); | ||
|
||
const currentVal = _.isPlainObject(item) ? Object.entries(item) : item; | ||
const lines = currentVal.flatMap((node) => { | ||
const { stage } = node; | ||
if (!stage) { | ||
const [key, value] = node; | ||
return `${currentIndent} ${key}: ${stringify(value, depth + 1)}`; | ||
} | ||
const getSign = (status) => { | ||
switch (status) { | ||
case 'added': | ||
return '+'; | ||
case 'removed': | ||
return '-'; | ||
default: | ||
return ' '; | ||
} | ||
}; | ||
|
||
const sign = getSign(stage); | ||
const key = `${sign} ${node.name}`; | ||
const value = stringify(node.value, depth + 1); | ||
|
||
if (stage === 'nested') { | ||
return `${currentIndent}${key}: ${stringify(node.children, depth + 1)}`; | ||
} | ||
if (stage === 'updated') { | ||
const removed = stringify(node.value.removed, depth + 1); | ||
const added = stringify(node.value.added, depth + 1); | ||
return `${currentIndent}- ${node.name}: ${removed}\n${currentIndent}+ ${node.name}: ${added}`; | ||
} | ||
|
||
return `${currentIndent}${key}: ${value}`; | ||
}); | ||
|
||
return [ | ||
'{', | ||
...lines, | ||
`${bracketIndent}}`, | ||
].join('\n'); | ||
}; | ||
|
||
return stringify(tree, 1); | ||
}; | ||
export default stylish; |
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 |
---|---|---|
@@ -1,14 +1,15 @@ | ||
import yaml from 'js-yaml'; | ||
|
||
const parse = (file, extname) => { | ||
let parsed; | ||
if (extname === '.json') { | ||
parsed = JSON.parse(file); | ||
const parse = (data, format) => { | ||
switch (format) { | ||
case 'json': | ||
return JSON.parse(data); | ||
case 'yaml': | ||
case 'yml': | ||
return yaml.load(data); | ||
default: | ||
throw new Error('Unknown format'); | ||
} | ||
if (extname === '.yaml' || extname === '.yml') { | ||
parsed = yaml.load(file); | ||
} | ||
return parsed; | ||
}; | ||
|
||
export default parse; |