import { endsWith , isEmpty , maxBy } from "lodash" ; import { join } from "path" ; import { IAsset } from "../../entities/Asset" ; import appendDotHtml from "./appendDotHtml" ; import appendIndexDotHtml from "./appendIndexDotHtml" ; export default function findMatchingAsset ( requestedPath , assets , fallbackAsset ) { const perfectlyMatching = assets . filter ( ( { path } ) => endsWith ( requestedPath , path ) || endsWith ( requestedPath , join ( path , "/" ) ) ) ; if ( ! isEmpty ( perfectlyMatching ) ) { return maxBy ( perfectlyMatching , "path.length" ) ! ; } const matchingWithDotHtml = assets . filter ( ( { path } ) => endsWith ( appendDotHtml ( requestedPath ) , path ) ) ; if ( ! isEmpty ( matchingWithDotHtml ) ) { return maxBy ( matchingWithDotHtml , "path.length" ) ! ; } const matchingWithIndexDotHtml = assets . filter ( ( { path } ) => path === "/index.html" ? requestedPath === "/" || requestedPath === "/index.html" : endsWith ( appendIndexDotHtml ( requestedPath ) , path ) ) ; if ( ! isEmpty ( matchingWithIndexDotHtml ) ) { return maxBy ( matchingWithIndexDotHtml , "path.length" ) ! ; } return fallbackAsset ; }