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
Provenance
- Commit
27521e6- Tree
f297cb7- Author
Aris Kemper <aris.github@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
0802633
2 files changed+42 added−40 removed
modified:deno_dist/utils/url.ts+21 added-20 removed
6 unmodified lines7 }8 return paths9 }10 11-export 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- }11+export const splitRoutingPath = (routePath: string): string[] => {12+ const { groups, path } = extractGroupsFromPath(routePath)13 27- const paths = path.split('/')28- if (paths[0] === '') {29- paths.shift()30- }14+ const paths = splitPath(path)15+ return replaceGroupMarks(paths, groups)16+}17+18+const extractGroupsFromPath = (path: string): { groups: [string, string][]; path: string } => {19+ const groups: [string, string][] = []20+21+ path = path.replace(/\{[^}]+\}/g, (match, index) => {22+ const mark = `@${index}`23+ groups.push([mark, match])24+ return mark25+ })26+27+ return { groups, path }28+}29+30+const replaceGroupMarks = (paths: string[], groups: [string, string][]): string[] => {31 for (let i = groups.length - 1; i >= 0; i--) {32 const [mark] = groups[i]33+34 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 linesmodified:src/utils/url.ts+21 added-20 removed
6 unmodified lines7 }8 return paths9 }10 11-export 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- }11+export const splitRoutingPath = (routePath: string): string[] => {12+ const { groups, path } = extractGroupsFromPath(routePath)13 27- const paths = path.split('/')28- if (paths[0] === '') {29- paths.shift()30- }14+ const paths = splitPath(path)15+ return replaceGroupMarks(paths, groups)16+}17+18+const extractGroupsFromPath = (path: string): { groups: [string, string][]; path: string } => {19+ const groups: [string, string][] = []20+21+ path = path.replace(/\{[^}]+\}/g, (match, index) => {22+ const mark = `@${index}`23+ groups.push([mark, match])24+ return mark25+ })26+27+ return { groups, path }28+}29+30+const replaceGroupMarks = (paths: string[], groups: [string, string][]): string[] => {31 for (let i = groups.length - 1; i >= 0; i--) {32 const [mark] = groups[i]33+34 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