import isEmpty from 'lodash/isEmpty' ; import range from 'lodash/range' ; import { Scopable } from '../models' ; export const compose = ( path1 , path2 ) => { let p1 = path1 ; if ( ! isEmpty ( path1 ) && ! isEmpty ( path2 ) && ! path2 . startsWith ( '[' ) ) { p1 = path1 + '.' ; } if ( isEmpty ( p1 ) ) { return path2 ; } else if ( isEmpty ( path2 ) ) { return p1 ; } else { return `${ p1 }${ path2 }` ; } } ; export { compose as composePaths } ; param { string } schemaPath returns { string [ ] } export const toDataPathSegments = ( schemaPath ) : string [ ] => { const s = schemaPath . replace ( /anyOf\/[\d]\//g , '' ) . replace ( /allOf\/[\d]\//g , '' ) . replace ( /oneOf\/[\d]\//g , '' ) ; const segments = s . split ( '/' ) ; const startFromRoot = segments [ 0 ] === '#' || segments [ 0 ] === '' ; const startIndex = startFromRoot ? 2 : 1 ; return range ( startIndex , segments . length , 2 ) . map ( idx => segments [ idx ] ) ; } ; example param { string } schemaPath returns { string } export const toDataPath = ( schemaPath ) : string => { return toDataPathSegments ( schemaPath ) . join ( '.' ) ; } ; export const composeWithUi = ( scopableUi , path ) : string => { const segments = toDataPathSegments ( scopableUi . scope ) ; if ( isEmpty ( segments ) && path === undefined ) { return '' ; } return isEmpty ( segments ) ? path : compose ( path , segments . join ( '.' ) ) ; } ;