Repository: honojs/hono
File: src/hono.ts (TypeScript)

Code before:

  constructor(init: Partial<Pick<Hono, 'router' | 'strict'>> = {}) {
    super()

    // Implementation of app.get(...handlers[]) or app.get(path, ...handlers[])
    const allMethods = [...METHODS, METHOD_NAME_ALL_LOWERCASE]
    allMethods.map((method) => {
      this[method] = (args1: string | H, ...args: H[]) => {
        if (typeof args1 === 'string') {
          this.path = args1
        } else {
          this.addRoute(method, this.path, args1)
        }
        args.map((handler) => {
          if (typeof handler !== 'string') {
            this.addRoute(method, this.path, handler)
          }
        })
        return this
      }
    })

    // Implementation of app.on(method, path, ...handlers[])
    this.on = (method: string | string[], path: string, ...handlers: H[]) => {
      if (!method) return this
      let methods: string[] = []
      if (Array.isArray(method)) {
        methods = method
      } else {
        methods = [method]
      }
      this.path = path

      for (const m of methods) {
        handlers.map((handler) => {
          this.addRoute(m.toUpperCase(), this.path, handler)
        })
      }
      return this
    }

    // Implementation of app.use(...handlers[]) or app.get(path, ...handlers[])
    this.use = (arg1: string | MiddlewareHandler, ...handlers: MiddlewareHandler[]) => {
      if (typeof arg1 === 'string') {
        this.path = arg1
      } else {
        handlers.unshift(arg1)
      }
      handlers.map((handler) => {
        this.addRoute(METHOD_NAME_ALL, this.path, handler)
      })

Review comment (performance):
You can do `[method].flat()` to convert a string into a tuple containing it while keeping an array of strings untouched ([example](https://www.typescriptlang.org/play?target=8#code/MYewdgzgLgBAZiEMC8MBECRoNwChSSwBGAhgE4BCJAXijANpqlloA06p1aAurvuBBAAbAKYA6ISADmACnqZuYuEJJQZASnV4Cg0ROlzmVaouWqNW3EA)). I don't know which version is more performant but the performance won't be so different because the length of methods array is very small in this use case.

Code after:

  constructor(init: Partial<Pick<Hono, 'router' | 'strict'>> = {}) {
    super()

    // Implementation of app.get(...handlers[]) or app.get(path, ...handlers[])
    const allMethods = [...METHODS, METHOD_NAME_ALL_LOWERCASE]
    allMethods.map((method) => {
      this[method] = (args1: string | H, ...args: H[]) => {
        if (typeof args1 === 'string') {
          this.path = args1
        } else {
          this.addRoute(method, this.path, args1)
        }
        args.map((handler) => {
          if (typeof handler !== 'string') {
            this.addRoute(method, this.path, handler)
          }
        })
        return this
      }
    })

    // Implementation of app.on(method, path, ...handlers[])
    this.on = (method: string | string[], path: string, ...handlers: H[]) => {
      if (!method) return this
      this.path = path
      for (const m of [method].flat()) {
        handlers.map((handler) => {
          this.addRoute(m.toUpperCase(), this.path, handler)
        })
      }
      return this
    }

    // Implementation of app.use(...handlers[]) or app.get(path, ...handlers[])
    this.use = (arg1: string | MiddlewareHandler, ...handlers: MiddlewareHandler[]) => {
      if (typeof arg1 === 'string') {
        this.path = arg1
      } else {
        handlers.unshift(arg1)
      }
      handlers.map((handler) => {
        this.addRoute(METHOD_NAME_ALL, this.path, handler)
      })
      return this
    }

    Object.assign(this, init)
  }

  private notFoundHandler: NotFoundHandler = notFoundHandler