Skip to content

Commit

Permalink
refactor: use Array.prototype.at() to look at the end (#3703)
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuapp authored Nov 26, 2024
1 parent 76b7109 commit 190e122
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 17 deletions.
14 changes: 7 additions & 7 deletions src/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,26 +127,26 @@ export const hc = <T extends Hono<any, any, any>>(
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(/^\$/, '')
Expand Down
2 changes: 1 addition & 1 deletion src/helper/html/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions src/middleware/trailing-slash/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 += '/'

Expand Down
2 changes: 1 addition & 1 deletion src/router/pattern-router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class PatternRouter<T> implements Router<T> {
#routes: Route<T>[] = []

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)
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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
}
Expand Down

0 comments on commit 190e122

Please sign in to comment.