UNPKG

file-router-react

Version:

File-based routing for React using wouter.

10 lines (9 loc) 451 B
// Utility to convert file names to route paths export function filenameToRoute(filename) { // Remove extension, convert [param] to :param, index to root let route = filename.replace(/\.[jt]sx?$/, ''); route = route.replace(/\[(\w+)\]/g, ':$1'); // [id] -> :id route = route.replace(/^index$/, '/'); // index.tsx -> / route = route.replace(/\/index$/, '/'); // /foo/index.tsx -> /foo/ return '/' + route.replace(/\\/g, '/'); }