UNPKG

@wordpress/components

Version:
1 lines • 1.88 kB
{"version":3,"file":"router.mjs","names":[],"sources":["../../../src/navigator/utils/router.ts"],"sourcesContent":["import { match } from 'path-to-regexp';\nimport type { Screen, MatchParams } from '../types';\n\nfunction matchPath( path: string, pattern: string ) {\n\tconst matchingFunction = match< MatchParams >( pattern, {\n\t\tdecode: decodeURIComponent,\n\t} );\n\treturn matchingFunction( path );\n}\n\nexport function patternMatch( path: string, screens: Screen[] ) {\n\tfor ( const screen of screens ) {\n\t\tconst matched = matchPath( path, screen.path );\n\t\tif ( matched ) {\n\t\t\treturn { params: matched.params, id: screen.id };\n\t\t}\n\t}\n\n\treturn undefined;\n}\n\nexport function findParent( path: string, screens: Screen[] ) {\n\tif ( ! path.startsWith( '/' ) ) {\n\t\treturn undefined;\n\t}\n\tconst pathParts = path.split( '/' );\n\tlet parentPath;\n\twhile ( pathParts.length > 1 && parentPath === undefined ) {\n\t\tpathParts.pop();\n\t\tconst potentialParentPath =\n\t\t\tpathParts.join( '/' ) === '' ? '/' : pathParts.join( '/' );\n\t\tif (\n\t\t\tscreens.find( ( screen ) => {\n\t\t\t\treturn matchPath( potentialParentPath, screen.path ) !== false;\n\t\t\t} )\n\t\t) {\n\t\t\tparentPath = potentialParentPath;\n\t\t}\n\t}\n\n\treturn parentPath;\n}\n"],"mappings":";;AAGA,SAAS,UAAW,MAAc,SAAkB;CAInD,OAHyB,MAAsB,SAAS,EACvD,QAAQ,mBACT,CACsB,CAAC,CAAE,IAAK;AAC/B;AAEA,SAAgB,aAAc,MAAc,SAAoB;CAC/D,KAAM,MAAM,UAAU,SAAU;EAC/B,MAAM,UAAU,UAAW,MAAM,OAAO,IAAK;EAC7C,IAAK,SACJ,OAAO;GAAE,QAAQ,QAAQ;GAAQ,IAAI,OAAO;EAAG;CAEjD;AAGD;AAEA,SAAgB,WAAY,MAAc,SAAoB;CAC7D,IAAK,CAAE,KAAK,WAAY,GAAI,GAC3B;CAED,MAAM,YAAY,KAAK,MAAO,GAAI;CAClC,IAAI;CACJ,OAAQ,UAAU,SAAS,KAAK,eAAe,KAAA,GAAY;EAC1D,UAAU,IAAI;EACd,MAAM,sBACL,UAAU,KAAM,GAAI,MAAM,KAAK,MAAM,UAAU,KAAM,GAAI;EAC1D,IACC,QAAQ,MAAQ,WAAY;GAC3B,OAAO,UAAW,qBAAqB,OAAO,IAAK,MAAM;EAC1D,CAAE,GAEF,aAAa;CAEf;CAEA,OAAO;AACR"}