import { assert } from "../../fixed_assert" ; import { isSingleQuoted } from "../../util/strings" ; import { IResourceInfo } from "./getResourcesInfo" ; /**
 * Determines if two top-level resources are in a parent/child relationship
 */ export function areDecoupledChildAndParent ( child , parent ) { const childTypeSegments = child . typeSegmentExpressions ; const parentTypeSegments = parent . typeSegmentExpressions ; if ( ( child . parent && ! child . isDecoupledChild ) || ( parent . parent && ! parent . isDecoupledChild ) ) { return false ; } if ( childTypeSegments . length === 0 || parentTypeSegments . length === 0 ) { return false ; } if ( ! ( childTypeSegments . length === parentTypeSegments . length + 1 ) ) { return false ; } const childNameSegments = child . nameSegmentExpressions ; const parentNameSegments = parent . nameSegmentExpressions ; if ( childTypeSegments . length === 0 || parentTypeSegments . length === 0 ) { return false ; } if ( ! ( childNameSegments . length === parentNameSegments . length + 1 ) ) { return false ; } if ( childTypeSegments . length !== childNameSegments . length ) { return false ; } if ( ! childMatchesParentSegments ( childNameSegments , parentNameSegments ) ) { return false ; } if ( ! childMatchesParentSegments ( childTypeSegments , parentTypeSegments ) ) { return false ; } return true ; function childMatchesParentSegments ( childSegments , parentSegments ) { assert ( childSegments . length === parentSegments . length + 1 ) ; for ( let i = 0 ; i < parentSegments . length ; ++ i ) { if ( ! expressionsAreSame ( childSegments [ i ] , parentSegments [ i ] ) ) { return false ; } } return true ; } } function expressionsAreSame ( expr1 , expr2 ) { const expr1LC = expr1 . toLowerCase ( ) ; const expr2LC = expr2 . toLowerCase ( ) ; if ( expr1LC === expr2LC ) { return true ; } if ( ! isSingleQuoted ( expr1 ) && ! isSingleQuoted ( expr2 ) ) { const expr1NoWhitespace = expr1 . replace ( /\s+/g , '' ) ; const expr2NoWhitespace = expr2 . replace ( /\s+/g , '' ) ; return expr1NoWhitespace === expr2NoWhitespace ; } return false ; }