import { compact , isObject } from 'lodash' ; import { Maybe } from '../../typings/common' ; export interface KeyValuePair { key : string ; value : unknown ; } export const flattenObject = ( item , parentKey ? ) : KeyValuePair [ ] => { if ( item ) { const isArrayWithSingleValue = Array . isArray ( item ) && item . length === 1 ; return Object . keys ( item ) . sort ( ) . reduce ( ( acc , key ) => { const childKey = isArrayWithSingleValue ? '' : key ; const currentKey = compact ( [ parentKey , childKey ] ) . join ( '.' ) ; if ( isObject ( item [ key ] ) ) { return acc . concat ( flattenObject ( item [ key ] , currentKey ) ) ; } else { acc . push ( { key : currentKey , value : item [ key ] } ) ; return acc ; } } , [ ] ) ; } return [ ] ; } ;