Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: move checkRoutes to navigate #15

Merged
merged 1 commit into from
Mar 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 67 additions & 67 deletions src/ts/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,105 +4,105 @@ const TICKTIME = 250;

// 라우터에서 사용되는 인터페이스 정의
interface Route {
testRegExp: RegExp;
callback: (params: Record<string, string>) => void;
params: string[];
testRegExp: RegExp;
callback: (params: Record<string, string>) => void;
params: string[];
}

// URL 파라미터 추출 함수 타입 정의
interface ExtractUrlParams {
(route: Route, pathname: string): Record<string, string>;
(route: Route, pathname: string): Record<string, string>;
}

// 라우터 객체의 인터페이스 정의
interface Router {
addRoute: (path: string, callback: (params: Record<string, string>) => void) => Router;
setNotFound: (cb: () => void) => Router;
navigate: (path: string) => void;
start: () => void;
addRoute: (path: string, callback: (params: Record<string, string>) => void) => Router;
setNotFound: (cb: () => void) => Router;
navigate: (path: string) => void;
start: () => void;
}

const extractUrlParams: ExtractUrlParams = (route, pathname) => {
const params: Record<string, string> = {};
const params: Record<string, string> = {};

if (route.params.length === 0) {
return params;
}
if (route.params.length === 0) {
return params;
}

const matches: string[] | null = pathname.match(route.testRegExp);
const matches: string[] | null = pathname.match(route.testRegExp);

matches?.shift();
matches?.shift();

matches?.forEach((paramValue, index) => {
const paramName = route.params[index];
params[paramName] = paramValue;
});
matches?.forEach((paramValue, index) => {
const paramName = route.params[index];
params[paramName] = paramValue;
});

return params;
return params;
};

export const createRouter = (): Router => {
const routes: Route[] = [];
let notFound = () => {};
let lastPathname = "";
const routes: Route[] = [];
let notFound = () => {};
let lastPathname = "";

const router = {} as Router;
const router = {} as Router;

const checkRoutes = () => {
const { pathname } = window.location;
if (lastPathname === pathname) {
return;
}
const checkRoutes = () => {
const { pathname } = window.location;
if (lastPathname === pathname) {
return;
}

lastPathname = pathname;
lastPathname = pathname;

const currentRoute = routes.find((route) => {
const { testRegExp } = route;
return testRegExp.test(pathname);
});
const currentRoute = routes.find((route) => {
const { testRegExp } = route;
return testRegExp.test(pathname);
});

if (!currentRoute) {
notFound();
return;
}
if (!currentRoute) {
notFound();
return;
}

const urlParams = extractUrlParams(currentRoute, pathname);
const urlParams = extractUrlParams(currentRoute, pathname);

currentRoute.callback(urlParams);
};
currentRoute.callback(urlParams);
};

router.addRoute = (path, callback) => {
const params: string[] = [];
router.addRoute = (path, callback) => {
const params: string[] = [];

const parsedPath = path
.replace(ROUTE_PARAMETER_REGEXP, (_, paramName) => {
params.push(paramName);
return URL_FRAGMENT_REGEXP;
})
.replace(/\//g, "\\/");
const parsedPath = path
.replace(ROUTE_PARAMETER_REGEXP, (_, paramName) => {
params.push(paramName);
return URL_FRAGMENT_REGEXP;
})
.replace(/\//g, "\\/");

routes.push({
testRegExp: new RegExp(`^${parsedPath}$`),
callback,
params,
});
routes.push({
testRegExp: new RegExp(`^${parsedPath}$`),
callback,
params,
});

return router;
};
return router;
};

router.setNotFound = (cb) => {
notFound = cb;
return router;
};
router.setNotFound = (cb) => {
notFound = cb;
return router;
};

router.navigate = (path) => {
window.history.pushState(null, "", path);
};
router.navigate = (path) => {
window.history.pushState(null, "", path);
checkRoutes();
};

router.start = () => {
checkRoutes();
window.setInterval(checkRoutes, TICKTIME);
};
router.start = () => {
checkRoutes();
};

return router;
return router;
};
Loading