From 190e122180dda4b51e46cbe21a301ccbec1b1f3d Mon Sep 17 00:00:00 2001 From: ryu <114303361+ryuapp@users.noreply.github.com> Date: Tue, 26 Nov 2024 22:29:22 +0900 Subject: [PATCH] refactor: use `Array.prototype.at()` to look at the end (#3703) --- src/client/client.ts | 14 +++++++------- src/helper/html/index.ts | 2 +- src/middleware/trailing-slash/index.ts | 8 ++------ src/router/pattern-router/router.ts | 2 +- src/utils/url.ts | 4 ++-- 5 files changed, 13 insertions(+), 17 deletions(-) diff --git a/src/client/client.ts b/src/client/client.ts index ec8eb24fe..95f3a6fd0 100644 --- a/src/client/client.ts +++ b/src/client/client.ts @@ -127,26 +127,26 @@ export const hc = >( const parts = [...opts.path] // allow calling .toString() and .valueOf() on the proxy - if (parts[parts.length - 1] === 'toString') { - if (parts[parts.length - 2] === 'name') { + if (parts.at(-1) === 'toString') { + if (parts.at(-2) === 'name') { // e.g. hc().somePath.name.toString() -> "somePath" - return parts[parts.length - 3] || '' + return parts.at(-3) || '' } // e.g. hc().somePath.toString() return proxyCallback.toString() } - if (parts[parts.length - 1] === 'valueOf') { - if (parts[parts.length - 2] === 'name') { + if (parts.at(-1) === 'valueOf') { + if (parts.at(-2) === 'name') { // e.g. hc().somePath.name.valueOf() -> "somePath" - return parts[parts.length - 3] || '' + return parts.at(-3) || '' } // e.g. hc().somePath.valueOf() return proxyCallback } let method = '' - if (/^\$/.test(parts[parts.length - 1])) { + if (/^\$/.test(parts.at(-1) as string)) { const last = parts.pop() if (last) { method = last.replace(/^\$/, '') diff --git a/src/helper/html/index.ts b/src/helper/html/index.ts index 7db7da1c8..f71fb78ca 100644 --- a/src/helper/html/index.ts +++ b/src/helper/html/index.ts @@ -47,7 +47,7 @@ export const html = ( } } } - buffer[0] += strings[strings.length - 1] + buffer[0] += strings.at(-1) as string return buffer.length === 1 ? 'callbacks' in buffer diff --git a/src/middleware/trailing-slash/index.ts b/src/middleware/trailing-slash/index.ts index 4b500a00e..973a6d9a1 100644 --- a/src/middleware/trailing-slash/index.ts +++ b/src/middleware/trailing-slash/index.ts @@ -28,7 +28,7 @@ export const trimTrailingSlash = (): MiddlewareHandler => { c.res.status === 404 && c.req.method === 'GET' && c.req.path !== '/' && - c.req.path[c.req.path.length - 1] === '/' + c.req.path.at(-1) === '/' ) { const url = new URL(c.req.url) url.pathname = url.pathname.substring(0, url.pathname.length - 1) @@ -57,11 +57,7 @@ export const appendTrailingSlash = (): MiddlewareHandler => { return async function appendTrailingSlash(c, next) { await next() - if ( - c.res.status === 404 && - c.req.method === 'GET' && - c.req.path[c.req.path.length - 1] !== '/' - ) { + if (c.res.status === 404 && c.req.method === 'GET' && c.req.path.at(-1) !== '/') { const url = new URL(c.req.url) url.pathname += '/' diff --git a/src/router/pattern-router/router.ts b/src/router/pattern-router/router.ts index fb819b217..d15610409 100644 --- a/src/router/pattern-router/router.ts +++ b/src/router/pattern-router/router.ts @@ -8,7 +8,7 @@ export class PatternRouter implements Router { #routes: Route[] = [] add(method: string, path: string, handler: T) { - const endsWithWildcard = path[path.length - 1] === '*' + const endsWithWildcard = path.at(-1) === '*' if (endsWithWildcard) { path = path.slice(0, -2) } diff --git a/src/utils/url.ts b/src/utils/url.ts index f9f822812..1c7c168b5 100644 --- a/src/utils/url.ts +++ b/src/utils/url.ts @@ -130,7 +130,7 @@ export const getPathNoStrict = (request: Request): string => { const result = getPath(request) // if strict routing is false => `/hello/hey/` and `/hello/hey` are treated the same - return result.length > 1 && result[result.length - 1] === '/' ? result.slice(0, -1) : result + return result.length > 1 && result.at(-1) === '/' ? result.slice(0, -1) : result } export const mergePath = (...paths: string[]): string => { @@ -139,7 +139,7 @@ export const mergePath = (...paths: string[]): string => { for (let path of paths) { /* ['/hey/','/say'] => ['/hey', '/say'] */ - if (p[p.length - 1] === '/') { + if (p.at(-1) === '/') { p = p.slice(0, -1) endsWithSlash = true }