Skip to content

Commit

Permalink
feat: adds createRoute function
Browse files Browse the repository at this point in the history
  • Loading branch information
rudionrails committed Oct 5, 2020
1 parent 8943bd6 commit eb40239
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
21 changes: 14 additions & 7 deletions index.js
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 };
46 changes: 29 additions & 17 deletions index.test.js
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);
});

0 comments on commit eb40239

Please # to comment.