import { Column } from '../Models/Column' ; import { Field } from '../types' ; import { getField , getFieldParts , getLastField , getLastFieldParents } from './ColumnUtils' ; export const getParentValue = ( rowData , fieldParents ) => { const parentValue = fieldParents . reduce ( ( previousValue , currentValue ) => { const result = ( previousValue && previousValue [ currentValue ] ) ; return result !== undefined ? result : undefined ; } , rowData ) ; return parentValue ? { ... parentValue } : undefined ; } ; export const createObjByFields = ( fieldParents , field , value ) => { const parentValue = { } ; if ( fieldParents . length ) { fieldParents . reduce ( ( previousValue , currentItem , currentIndex ) => { const lastObj = { } ; previousValue [ currentItem ] = lastObj ; if ( currentIndex === ( fieldParents . length - 1 ) ) { lastObj [ field ] = value ; } return lastObj ; } , parentValue ) ; } else { parentValue [ field ] = value ; } return { ... parentValue } ; } ; export const getValueByColumn = ( rowData , column ) => { return getValueByField ( rowData , getField ( column ) ) ; } ; export const getValueByField = ( rowData , field ) => { let o = { ... rowData } ; const names = getFieldParts ( field ) ; for ( let i = 0 , n = names . length ; i < n ; ++ i ) { const k = names [ i ] ; if ( k in o ) { o = o [ k ] ; } else { return ; } } return o ; } ; const replaceValueForField = ( rowData , field , newValue , fieldParents ? ) : void => { let result = { ... rowData } ; if ( fieldParents && fieldParents . length ) { const parentValue = getParentValue ( result , fieldParents ) || { } ; parentValue [ field ] = newValue ; const parentsOfParent = [ ... fieldParents ] ; const parentFieldName = parentsOfParent . pop ( ) as string ; result = replaceValueForField ( result , parentFieldName , parentValue , parentsOfParent ) ; } else { result [ field ] = newValue ; } return result ; } ; export const replaceValue = ( rowData , column , newValue ) => { const field = getField ( column ) ; return replaceValueForField ( rowData , getLastField ( field ) , newValue , getLastFieldParents ( field ) ) ; } ; export const reorderData = ( data , getKey , keyValue , targetKeyValue ) => { const moved = data . find ( d => getKey ( d ) === keyValue ) ; const newData = data . filter ( d => getKey ( d ) !== keyValue ) ; const targetIndex = data . findIndex ( d => getKey ( d ) === targetKeyValue ) ; newData . splice ( targetIndex , 0 , moved ) ; return newData ; } ;