import startCase from 'lodash/startCase' ; import { ControlElement , JsonSchema , LabelDescription } from '../models' ; const deriveLabel = ( controlElement , schemaElement ? ) : string => { if ( schemaElement && typeof schemaElement . title === 'string' ) { return schemaElement . title ; } if ( typeof controlElement . scope === 'string' ) { const ref = controlElement . scope ; const label = ref . substr ( ref . lastIndexOf ( '/' ) + 1 ) ; return startCase ( label ) ; } return '' ; } ; export const createCleanLabel = ( label ) : string => { return startCase ( label . replace ( '_' , ' ' ) ) ; } ; param { ControlElement } withLabel param { JsonSchema } schema returns { LabelDescription } export const createLabelDescriptionFrom = ( withLabel , schema ? ) : LabelDescription => { const labelProperty = withLabel . label ; if ( typeof labelProperty === 'boolean' ) { return labelDescription ( deriveLabel ( withLabel , schema ) , labelProperty ) ; } if ( typeof labelProperty === 'string' ) { return labelDescription ( labelProperty , true ) ; } if ( typeof labelProperty === 'object' ) { const label = typeof labelProperty . text === 'string' ? labelProperty . text : deriveLabel ( withLabel , schema ) ; const show = typeof labelProperty . show === 'boolean' ? labelProperty . show : true ; return labelDescription ( label , show ) ; } return labelDescription ( deriveLabel ( withLabel , schema ) , true ) ; } ; const labelDescription = ( text , show ) : LabelDescription => ( { text : text , show : show } ) ;