import { PropertyModel , WidgetModel } from "./models/models" ; export function extractForLoopParams ( value ) { const statements = value . split ( ',' ) ; const indexName = statements . length > 1 ? statements [ 0 ] . trim ( ) : '' ; const segments = ( statements . length > 1 ? statements [ 1 ] : statements [ 0 ] ) . trim ( ) . split ( ' of ' ) ; const { itemName , typeName } = extractVarAndType ( segments [ 0 ] ) ; const listName = segments [ 1 ] ; return { listName , indexName , itemName , typeName } ; } export function extractVarAndType ( value ) { const statements = value . split ( ' ' ) ; if ( statements . length === 2 ) { return { itemName : statements [ 1 ] , typeName : statements [ 0 ] } ; } else { return { itemName : statements [ 0 ] , typeName : '' } ; } } export function spaceAfter ( text ) { return text ? text + ' ' : '' ; } export function makeTabs ( tabsLevel ) { return [ ... new Array ( tabsLevel + 2 ) ] . map ( _ => '  ' ) . join ( '' ) ; } export function makeVariableName ( name , value ) { const res = value . replace ( /[^a-zA-Z0-9_]/ig , '_' ) . replace ( /__+/g , '_' ) . replace ( /_([a-z])/g , g => g [ 1 ] . toUpperCase ( ) ) . replace ( /_/g , '' ) ; return res [ 0 ] . toLowerCase ( ) + res . substring ( 1 ) ; } export function makePipeUniqueName ( data ) { const names = ( data . pipes as any [ ] ) . map ( a => a . name ) . join ( '_' ) ; const res = data . value . replace ( /[^a-zA-Z0-9_]/ig , '_' ) ; return names + res ; } export function sortProperties ( a , b ) { if ( ! a . name && b . name ) { return - 1 ; } else if ( a . name && ! b . name ) { return 1 ; } const aIsWidget = [ 'widget' , 'widgetList' , 'function' , 'propertyElement' ] . filter ( t => t === a . dataType ) . length === 1 ; const bIsWidget = [ 'widget' , 'widgetList' , 'function' , 'propertyElement' ] . filter ( t => t === b . dataType ) . length === 1 ; if ( ! aIsWidget && bIsWidget ) { return - 1 ; } else if ( aIsWidget && ! bIsWidget ) { return 1 ; } if ( a . name > b . name ) { return 1 ; } else if ( a . name < b . name ) { return - 1 ; } return 0 ; } export function getUniqueBy ( array , key ) { return [ ... new Map ( array . map ( x => [ key ( x ) , x ] ) ) . values ( ) ] ; } export function findWidgetByName ( name , widget ) { if ( name === widget . type ) { return widget ; } return findWidgetByName ( name , widget . wrappedWidgets [ 0 ] ) ; }