import * as yaml from 'js-yaml' ; import { Integration , CamelElement , } from "../model/CamelModel" ; import { CamelApi } from "./CamelApi" ; export class CamelUtil { static cloneIntegration = ( integration ) : Integration => { const clone = JSON . parse ( JSON . stringify ( integration ) ) ; const int = new Integration ( { ... clone } ) ; const flows = int . spec . flows . map ( f => CamelApi . createFrom ( f ) ) ; int . spec . flows = flows ; return int ; } ; static cloneStep = ( step ) : CamelElement => { const clone = JSON . parse ( JSON . stringify ( step ) ) ; return CamelApi . createStep ( step . dslName , clone , true ) ; } ; static replacer = ( key , value ) : any => { if ( typeof value == 'object' && ( value . hasOwnProperty ( 'dslName' ) || value . hasOwnProperty ( 'dsl-name' ) ) ) { const dslNameField = value . hasOwnProperty ( 'dslName' ) ? 'dslName' : 'dsl-name' ; const dslName = value [ dslNameField ] ; const x = JSON . parse ( JSON . stringify ( value ) ) ; delete x [ dslNameField ] ; if ( [ 'when' , 'otherwise' , 'expression' ] . includes ( value [ dslNameField ] ) ) { return x ; } else { const newValue = { } ; newValue [ dslName ] = x ; return newValue ; } } else { return value ; } } ; static capitalizeName = ( name ) => { try { return name [ 0 ] . toUpperCase ( ) + name . substring ( 1 ) ; } catch ( e ) { return name ; } } ; static camelizeName = ( name , separator , firstSmall ) => { const res = name . split ( separator ) . map ( ( value ) => CamelUtil . capitalizeName ( value ) ) . join ( "" ) ; return firstSmall ? res [ 0 ] . toLowerCase ( ) + res . substring ( 1 ) : res ; } ; static camelizeBody = ( name , body , clone ) : any => { if ( body ) { const oldKey = Object . keys ( body ) [ 0 ] ; const key = CamelUtil . camelizeName ( oldKey , '-' , true ) ; return ! clone && key === name ? { [ key ] : body [ oldKey ] } : body ; } else { return { } ; } } ; }