Created
May 30, 2019 04:16
-
-
Save islishude/8a2f081ed78d035296b8de2114fa9d17 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { deepEqual } = require("assert"); | |
/** | |
* router path resolver | |
* @param {string} path | |
* @param {{[key: string]: string}} data | |
*/ | |
const router = (path, data) => { | |
for (const [key, value] of Object.entries(data)) { | |
path = path.replace(new RegExp(":" + key, "g"), value); | |
} | |
return path; | |
}; | |
const testData = [ | |
{ | |
param: { | |
path: "/app/:name/:age", | |
data: { | |
name: "test", | |
age: "10" | |
} | |
}, | |
want: "/app/test/10" | |
}, | |
{ | |
param: { | |
path: "/app/:name/:age/:name", | |
data: { | |
name: "test", | |
age: "10" | |
} | |
}, | |
want: "/app/test/10/test" | |
} | |
]; | |
for (const { param, want } of testData) { | |
const { path, data } = param; | |
const got = router(path, data); | |
deepEqual(got, want, `want: ${want} but got ${got}`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment