11import _ from 'lodash' ;
22
3- // Для интереса и челленджа решил попробовать сделать также обработку массивов,
4- // когда они являются значением ключа. Почти получилось, остаётся подшаманить
5- // правильные отсупы у объектов, вложенных в массив.
3+ // Для интереса и челленджа решил также добавить развернутый вывод массивов,
4+ // когда они являются значением ключа. Что-то получилось, но поймал затык
5+ // по отступам и скобкам объектов, вложенных в массив.
66const replacer = ' ' ;
77const spacesCount = 4 ;
8+ const indentShift = 2 ;
9+
810const spacesCountForArray = 3 ;
911const indentShiftForArray = 2 ;
1012
1113const getCurrentAndBracketIndents = ( depth ) => {
12- const currentIndent = replacer . repeat ( depth * spacesCount - 2 ) ;
14+ const currentIndent = replacer . repeat ( depth * spacesCount - indentShift ) ;
1315 const closingBracketIndent = replacer . repeat ( depth * spacesCount - spacesCount ) ;
1416
1517 return [ currentIndent , closingBracketIndent ] ;
@@ -22,13 +24,15 @@ const getCurrentAndBracketIndentsArr = (depth) => {
2224 return [ currentIndent , closingBracketIndent ] ;
2325} ;
2426
27+ // Для обработки только объектов, которые имеют ключи type, т.е.
28+ // которые и надо обрабатывать в проекте
2529const stringify = ( node , depth ) => {
2630 if ( ! _ . isPlainObject ( node ) ) {
2731 return `${ node } ` ;
2832 }
2933
3034 const [ currentIndent , closingBracketIndent ] = getCurrentAndBracketIndents ( depth + 1 ) ;
31- const extraIndent = ' ' ;
35+ const extraIndent = replacer . repeat ( indentShift ) ;
3236
3337 const properties = Object
3438 . entries ( node )
@@ -41,8 +45,9 @@ const stringify = (node, depth) => {
4145 ] . join ( '\n' ) ;
4246} ;
4347
48+ // Для обработки массивов, объектов внутри массивов, назовем это доп.задачей
4449const stringifyArr = ( node , depth ) => {
45- const [ currentIndent , closingBracketIndent ] = getCurrentAndBracketIndentsArr ( depth + 1 ) ;
50+ const [ currentIndent ] = getCurrentAndBracketIndentsArr ( depth + 1 ) ;
4651
4752 if ( ! _ . isObject ( node ) ) {
4853 return `${ currentIndent } ${ node } ` ;
@@ -57,12 +62,13 @@ const stringifyArr = (node, depth) => {
5762 . map ( ( item ) => stringifyArr ( item , depth + 1 ) ) ;
5863
5964 const lines = items . join ( ',\n' ) ;
60- return `${ currentIndent } [\n${ lines } \n ${ closingBracketIndent } ]` ;
65+
66+ return `${ currentIndent } [\n${ lines } \n${ currentIndent } ]` ;
6167 }
6268
6369 const properties = Object
6470 . entries ( node )
65- . map ( ( [ key , value ] ) => `${ currentIndent } ${ key } : ${ stringify ( value , depth + 1 ) } ` ) ;
71+ . map ( ( [ key , value ] ) => `${ currentIndent } ${ key } : ${ stringifyArr ( value , depth + 1 ) } ` ) ;
6672
6773 if ( properties . length === 0 ) {
6874 return `${ currentIndent } {}` ;
@@ -81,14 +87,17 @@ const stylish = (tree) => {
8187
8288 if ( Array . isArray ( node ) && ! node [ 0 ] ?. type ) {
8389 if ( node . length === 0 ) {
84- return '[] ' ;
90+ return 'both files are empty ' ;
8591 }
8692
8793 const items = node
8894 . map ( ( item ) => stringifyArr ( item , depth + 1 ) ) ;
8995
9096 const lines = items . join ( ',\n' ) ;
91- return `[\n${ lines } \n${ replacer . repeat ( spacesCount ) } ${ closingBracketIndent } ]` ;
97+
98+ const extraIndent = replacer . repeat ( spacesCount ) ;
99+
100+ return `[\n${ lines } \n${ extraIndent } ${ closingBracketIndent } ]` ;
92101 }
93102
94103 if ( Array . isArray ( node ) ) {
0 commit comments