-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8943bd6
commit eb40239
Showing
2 changed files
with
43 additions
and
24 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,25 @@ | ||
function createRoute(route) { | ||
const routeFn = (params = {}) => | ||
Object.entries(params).reduce( | ||
(acc, [key, value]) => acc.replace(new RegExp(`:${key}`, 'g'), value), | ||
route, | ||
(acc, [key, value]) => acc.replace(new RegExp(`:${key}`, "g"), value), | ||
route | ||
); | ||
routeFn.getType = route; | ||
|
||
return routeFn; | ||
} | ||
|
||
const createRoutes = routes => | ||
Object.entries(routes).reduce( | ||
(acc, [key, value]) => ({...acc, [`${key}Path`]: createRoute(value)}), | ||
{}, | ||
function createRoutes(objectOrFn) { | ||
const routes = | ||
typeof objectOrFn === "function" ? objectOrFn(createRoute) : objectOrFn; | ||
|
||
return Object.entries(routes).reduce( | ||
(acc, [key, value]) => ({ | ||
...acc, | ||
[`${key}Path`]: typeof value === "function" ? value : createRoute(value) | ||
}), | ||
{} | ||
); | ||
} | ||
|
||
module.exports = {createRoutes}; | ||
module.exports = { createRoute, createRoutes }; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,44 @@ | ||
const test = require('ava'); | ||
const test = require("ava"); | ||
|
||
const {createRoutes} = require('./index'); | ||
const { createRoutes } = require("./index"); | ||
|
||
test('to be an object', t => { | ||
test("to be an object", t => { | ||
const routes = createRoutes({}); | ||
t.is('object', typeof routes); | ||
t.is("object", typeof routes); | ||
}); | ||
|
||
test('to set copmuted property (set getType)', t => { | ||
const routes = createRoutes({tasks: '/tasks/:id'}); | ||
test("to accept object and return routes", t => { | ||
const routes = createRoutes({ tasks: "/tasks", people: "/people" }); | ||
|
||
t.is('/tasks/:id', routes.tasksPath.getType); | ||
t.is("/tasks", routes.tasksPath()); | ||
t.is("/people", routes.peoplePath()); | ||
}); | ||
|
||
test('to return tasksRoute => /tasks', t => { | ||
const routes = createRoutes({tasks: '/tasks'}); | ||
t.is('/tasks', routes.tasksPath()); | ||
test("to accept function and return routes", t => { | ||
const routes = createRoutes(route => ({ | ||
tasks: route("/tasks"), | ||
people: route("/people") | ||
})); | ||
|
||
t.is("/tasks", routes.tasksPath()); | ||
t.is("/people", routes.peoplePath()); | ||
}); | ||
|
||
test('to return taskPath => /tasks/:id', t => { | ||
const routes = createRoutes({task: '/tasks/:id'}); | ||
t.is('/tasks/123', routes.taskPath({id: '123'})); | ||
test("to return route with params /tasks/:id", t => { | ||
const routes = createRoutes({ task: "/tasks/:id" }); | ||
t.is("/tasks/123", routes.taskPath({ id: "123" })); | ||
}); | ||
|
||
test('to return peopleTaskPath => people/:id/tasks/:taskId', t => { | ||
const routes = createRoutes({peopleTask: '/people/:personId/tasks/:id'}); | ||
test("to return nested route, e.g. people/:id/tasks/:taskId", t => { | ||
const routes = createRoutes({ peopleTask: "/people/:personId/tasks/:id" }); | ||
t.is( | ||
'/people/abc/tasks/123', | ||
routes.peopleTaskPath({id: '123', personId: 'abc'}), | ||
"/people/abc/tasks/123", | ||
routes.peopleTaskPath({ id: "123", personId: "abc" }) | ||
); | ||
}); | ||
|
||
test("to set computed property (set getType)", t => { | ||
const routes = createRoutes({ tasks: "/tasks/:id" }); | ||
|
||
t.is("/tasks/:id", routes.tasksPath.getType); | ||
}); |