-
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.
Merge pull request #31 from dzencot/develop
refactoring
- Loading branch information
Showing
10 changed files
with
161 additions
and
120 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
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
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
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,34 +1,44 @@ | ||
// @flow | ||
/* eslint no-param-reassign: ["error", { "props": false }] */ | ||
/* eslint arrow-body-style: ["error", "always"] */ | ||
|
||
const getData = (data) => { | ||
import lodash from 'lodash'; | ||
|
||
const getDataString = (data, countTab) => { | ||
if (typeof data !== 'object') { | ||
return data; | ||
return ` ${data}`; | ||
} | ||
return Object.keys(data).reduce((acc, key) => { | ||
acc[` ${key}`] = data[key]; | ||
return acc; | ||
}, {}); | ||
const newCountTab = countTab + 1; | ||
const tab = lodash.repeat(' ', newCountTab); | ||
const result = Object.keys(data).reduce((acc, key) => { | ||
return `${acc}${tab} ${key}:${getDataString(data[key], newCountTab + 1)}\n`; | ||
}, ' {\n'); | ||
return `${result}${tab}}`; | ||
}; | ||
const toString = (obj) => { | ||
const result = Object.keys(obj).reduce((acc, key) => { | ||
const currentObj = obj[key]; | ||
|
||
const getObjString = (obj, countTab) => { | ||
const newCountTab = countTab + 1; | ||
const tab = lodash.repeat(' ', newCountTab); | ||
const result = lodash.flatten(obj).reduce((acc, currentObj) => { | ||
if (currentObj.type === 'unchanged') { | ||
return `${acc}${tab} ${currentObj.name}:${getDataString(currentObj.data, newCountTab)}\n`; | ||
} | ||
if (currentObj.type === 'added') { | ||
return `${acc}${tab}+ ${currentObj.name}:${getDataString(currentObj.data, newCountTab)}\n`; | ||
} | ||
if (currentObj.type === 'removed') { | ||
return `${acc}${tab}- ${currentObj.name}:${getDataString(currentObj.data, newCountTab)}\n`; | ||
} | ||
if (currentObj.type === 'object') { | ||
return `${acc}${tab} ${currentObj.name}: ${getObjString(currentObj.data, newCountTab + 1)}\n`; | ||
} | ||
if (currentObj.type === 'updated') { | ||
acc[`+ ${key}`] = currentObj.data; | ||
acc[`- ${key}`] = currentObj.previous; | ||
} else if (currentObj.type === 'added') { | ||
acc[`+ ${key}`] = getData(currentObj.data); | ||
} else if (currentObj.type === 'object') { | ||
acc[` ${key}`] = toString(currentObj.data); | ||
} else if (currentObj.type === 'removed') { | ||
acc[`- ${key}`] = getData(currentObj.data); | ||
} else if (currentObj.type === 'unchanged') { | ||
acc[` ${key}`] = currentObj.data; | ||
return `${acc}${tab}+ ${currentObj.name}:${getDataString(currentObj.data, newCountTab)} | ||
${tab}- ${currentObj.name}:${getDataString(currentObj.previous, newCountTab)}\n`; | ||
} | ||
return acc; | ||
}, {}); | ||
return result; | ||
}, '{\n'); | ||
return `${result}${lodash.repeat(' ', countTab)}}`; | ||
}; | ||
|
||
export default obj => JSON.stringify(toString(obj), null, 2).replace(/,/g, ''); | ||
export default (obj) => { return getObjString(obj, 0); }; | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// @flow | ||
/* eslint no-use-before-define: ["error", { "functions": false }] */ | ||
|
||
import lodash from 'lodash'; | ||
|
||
const getComparedData = (data1, data2) => { | ||
if (data1 === data2) { | ||
return { | ||
type: 'unchanged', | ||
data: data1, | ||
}; | ||
} | ||
if (!data1) { | ||
return { | ||
type: 'added', | ||
data: data2, | ||
}; | ||
} | ||
if (!data2) { | ||
return { | ||
type: 'removed', | ||
data: data1, | ||
}; | ||
} | ||
if (typeof data1 === 'object') { | ||
return { | ||
type: 'object', | ||
data: getDiff(data1, data2), | ||
}; | ||
} | ||
return { | ||
type: 'updated', | ||
data: data2, | ||
previous: data1, | ||
}; | ||
}; | ||
|
||
function getDiff(firstObject, secondObject) { | ||
const diffUnion = lodash.union(lodash.keys(firstObject), lodash.keys(secondObject)); | ||
const diff = diffUnion.reduce((acc, key) => { | ||
const res = getComparedData(firstObject[key], secondObject[key]); | ||
res.name = key; | ||
acc.push(res); | ||
return acc; | ||
}, []); | ||
return diff; | ||
} | ||
export default getDiff; | ||
|
Oops, something went wrong.