Skip to content
Myr

refactor(utils/url): splitRoutingPath (#2058)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* refactor: url util splitRoutingPath * refactor: splitRoutingPath

authored by Aris Kemper · committed by GitHub · parent 0802633 · Browse files · 2 files+42 added−40 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+42 added−40 removed

  • other:+42 added−40 removed

2 files+42 added−40 removed

modified:deno_dist/utils/url.ts+21 added-20 removed
6 unmodified lines
7 }8 return paths9}1011export const splitRoutingPath = (path: string): string[] => {12 const groups: [string, string][] = [] // [mark, original string]13 for (let i = 0; ; ) {14 let replaced = false15 path = path.replace(/\{[^}]+\}/g, (m) => {16 const mark = `@\\${i}`17 groups[i] = [mark, m]18 i++19 replaced = true20 return mark21 })22 if (!replaced) {23 break24 }25 }11export const splitRoutingPath = (routePath: string): string[] => {12 const { groups, path } = extractGroupsFromPath(routePath)1327 const paths = path.split('/')28 if (paths[0] === '') {29 paths.shift()30 }14 const paths = splitPath(path)15 return replaceGroupMarks(paths, groups)16}1718const extractGroupsFromPath = (path: string): { groups: [string, string][]; path: string } => {19 const groups: [string, string][] = []2021 path = path.replace(/\{[^}]+\}/g, (match, index) => {22 const mark = `@${index}`23 groups.push([mark, match])24 return mark25 })2627 return { groups, path }28}2930const replaceGroupMarks = (paths: string[], groups: [string, string][]): string[] => {31 for (let i = groups.length - 1; i >= 0; i--) {32 const [mark] = groups[i]3334 for (let j = paths.length - 1; j >= 0; j--) {34 if (paths[j].indexOf(mark) !== -1) {35 if (paths[j].includes(mark)) {36 paths[j] = paths[j].replace(mark, groups[i][1])37 break38 }39 }
226 unmodified lines
modified:src/utils/url.ts+21 added-20 removed
6 unmodified lines
7 }8 return paths9}1011export const splitRoutingPath = (path: string): string[] => {12 const groups: [string, string][] = [] // [mark, original string]13 for (let i = 0; ; ) {14 let replaced = false15 path = path.replace(/\{[^}]+\}/g, (m) => {16 const mark = `@\\${i}`17 groups[i] = [mark, m]18 i++19 replaced = true20 return mark21 })22 if (!replaced) {23 break24 }25 }11export const splitRoutingPath = (routePath: string): string[] => {12 const { groups, path } = extractGroupsFromPath(routePath)1327 const paths = path.split('/')28 if (paths[0] === '') {29 paths.shift()30 }14 const paths = splitPath(path)15 return replaceGroupMarks(paths, groups)16}1718const extractGroupsFromPath = (path: string): { groups: [string, string][]; path: string } => {19 const groups: [string, string][] = []2021 path = path.replace(/\{[^}]+\}/g, (match, index) => {22 const mark = `@${index}`23 groups.push([mark, match])24 return mark25 })2627 return { groups, path }28}2930const replaceGroupMarks = (paths: string[], groups: [string, string][]): string[] => {31 for (let i = groups.length - 1; i >= 0; i--) {32 const [mark] = groups[i]3334 for (let j = paths.length - 1; j >= 0; j--) {34 if (paths[j].indexOf(mark) !== -1) {35 if (paths[j].includes(mark)) {36 paths[j] = paths[j].replace(mark, groups[i][1])37 break38 }39 }
226 unmodified lines