refactor: use `Array.prototype.at()` to look at the end (#3703)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ryu · committed by GitHub · parent 76b7109 · Browse files · 5 files·+13 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+13 added−17 removed
- other:+13 added−17 removed
Provenance
- Commit
190e122- Tree
1ef724f- Author
ryu <114303361+ryuapp@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
76b7109
5 files changed+13 added−17 removed
modified:src/client/client.ts+7 added-7 removed
125 unmodified lines126 createProxy(function proxyCallback(opts) {127 const parts = [...opts.path]128 129 // allow calling .toString() and .valueOf() on the proxy130- if (parts[parts.length - 1] === 'toString') {131- if (parts[parts.length - 2] === 'name') {130+ if (parts.at(-1) === 'toString') {131+ if (parts.at(-2) === 'name') {132 // e.g. hc().somePath.name.toString() -> "somePath"133- return parts[parts.length - 3] || ''133+ return parts.at(-3) || ''134 }135 // e.g. hc().somePath.toString()136 return proxyCallback.toString()137 }138 139- if (parts[parts.length - 1] === 'valueOf') {140- if (parts[parts.length - 2] === 'name') {139+ if (parts.at(-1) === 'valueOf') {140+ if (parts.at(-2) === 'name') {141 // e.g. hc().somePath.name.valueOf() -> "somePath"142- return parts[parts.length - 3] || ''142+ return parts.at(-3) || ''143 }144 // e.g. hc().somePath.valueOf()145 return proxyCallback146 }147 148 let method = ''149- if (/^\$/.test(parts[parts.length - 1])) {149+ if (/^\$/.test(parts.at(-1) as string)) {150 const last = parts.pop()151 if (last) {152 method = last.replace(/^\$/, '')153 }51 unmodified linesmodified:src/helper/html/index.ts+1 added-1 removed
45 unmodified lines464748495050515253543 unmodified lines45 unmodified lines escapeToBuffer(child.toString(), buffer) } } } buffer[0] += strings[strings.length - 1] buffer[0] += strings.at(-1) as string
return buffer.length === 1 ? 'callbacks' in buffer ? raw(resolveCallbackSync(raw(buffer[0], buffer.callbacks)))3 unmodified linesmodified:src/middleware/trailing-slash/index.ts+2 added-6 removed
26 unmodified lines2728293031313233343520 unmodified lines56575859606162636460616263643 unmodified lines26 unmodified lines if ( 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)
20 unmodified linesexport 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 += '/'
c.res = c.redirect(url.toString(), 301)3 unmodified linesmodified:src/router/pattern-router/router.ts+1 added-1 removed
6 unmodified lines7891011111213141541 unmodified lines6 unmodified lines name: string = 'PatternRouter' #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) } if (path.at(-1) === '?') {41 unmodified linesmodified:src/utils/url.ts+2 added-2 removed
128 unmodified lines129 export const getPathNoStrict = (request: Request): string => {130 const result = getPath(request)131 132 // if strict routing is false => `/hello/hey/` and `/hello/hey` are treated the same133- return result.length > 1 && result[result.length - 1] === '/' ? result.slice(0, -1) : result133+ return result.length > 1 && result.at(-1) === '/' ? result.slice(0, -1) : result134 }135 136 export const mergePath = (...paths: string[]): string => {137 let p: string = ''138 let endsWithSlash = false139 140 for (let path of paths) {141 /* ['/hey/','/say'] => ['/hey', '/say'] */142- if (p[p.length - 1] === '/') {142+ if (p.at(-1) === '/') {143 p = p.slice(0, -1)144 endsWithSlash = true145 }146 167 unmodified lines