import deepMerge from './deepMerge' ; import transformName from '../utilities/transformName' ; import { Settings } from '@typings/settings' ; import { OriginalFormatTokenInterface } from '@typings/originalFormatProperties' ; import { StandardTokenInterface } from '@typings/standardToken' ; const nestedObjectFromArray = ( array , value ) => { const reducer = ( val , key ) => ( { [ key ] : val } ) ; return array . reduceRight ( reducer , value ) ; } ; export const groupByKeyAndName = ( tokenArray , userSettings ) => { const removeName = true ; if ( tokenArray . length <= 0 ) return [ ] ; const groupedTokens = tokenArray . map ( token => { const groupsFromName = token . name . split ( '/' ) . map ( group => transformName ( group , userSettings . nameConversion ) ) ; if ( removeName === true ) { delete token . name ; } return nestedObjectFromArray ( groupsFromName , token ) ; } ) ; return groupedTokens . reduce ( ( accumulator = { } , currentValue ) => deepMerge ( accumulator , currentValue ) ) ; } ;