1- const transformValue = ( value ) => {
1+ const stringify = ( value ) => {
22 if ( typeof value === 'object' && value !== null ) {
33 return '[complex value]' ;
44 }
55
6- if ( typeof value === 'string' ) {
7- return `'${ value } '` ;
8- }
9-
10- return value ;
6+ return typeof value === 'string' ? `'${ value } '` : value ;
117} ;
128
139const getPath = ( keyPath ) => {
@@ -17,29 +13,37 @@ const getPath = (keyPath) => {
1713 return newPath . join ( '.' ) ;
1814} ;
1915
20- const plain = ( properties ) => {
21- const iter = ( data , path = '' ) => {
22- const result = data
23- . flatMap ( ( property ) => {
24- const newPath = getPath ( [ path , property . key ] ) ;
25- switch ( property . type ) {
16+ const plain = ( tree ) => {
17+ const iter = ( node , path = '' ) => {
18+ const properties = node
19+ . filter ( ( { type } ) => type !== 'unchanged' )
20+ . map ( ( {
21+ type,
22+ key,
23+ value,
24+ oldValue,
25+ newValue,
26+ children,
27+ } ) => {
28+ const newPath = getPath ( [ path , key ] ) ;
29+ switch ( type ) {
2630 case 'added' :
27- return `Property '${ newPath } ' was added with value: ${ transformValue ( property . value ) } ` ;
31+ return `Property '${ newPath } ' was added with value: ${ stringify ( value ) } ` ;
2832 case 'deleted' :
2933 return `Property '${ newPath } ' was removed` ;
3034 case 'changed' :
31- return `Property '${ newPath } ' was updated. From ${ transformValue ( property . oldValue ) } to ${ transformValue ( property . newValue ) } ` ;
35+ return `Property '${ newPath } ' was updated. From ${ stringify ( oldValue ) } to ${ stringify ( newValue ) } ` ;
3236 case 'nested' :
33- return iter ( property . children , newPath ) ;
37+ return iter ( children , newPath ) ;
3438 default :
35- return [ ] ;
39+ throw new Error ( `Such type - ${ type } doesn't exist` ) ;
3640 }
3741 } ) ;
3842
39- return result . join ( '\n' ) ;
43+ return properties . join ( '\n' ) ;
4044 } ;
4145
42- return iter ( properties ) ;
46+ return iter ( tree ) ;
4347} ;
4448
4549export default plain ;
0 commit comments