Rumah  >  Artikel  >  hujung hadapan web  >  [Pakej Harian] deden

[Pakej Harian] deden

PHPz
PHPzasal
2024-08-30 21:01:02946semak imbas

[Daily Package] dedent

Kehidupan sebelum mengenal deden

Pernahkah anda cuba menulis perenggan berbilang baris dalam literal templat, tetapi menyedari ia mengekalkan lekukan, yang akhirnya menggunakan penambahan rentetan dengan n?

function explain() {
  const description = `
    - 200 OK
      The request succeeded. The result meaning of "success" depends on the HTTP method:

      * GET: The resource has been fetched...
      * HEAD: The representation headers are...
      * PUT or POST: The resource describing...
      * TRACE: The message body contains the...
  `

  console.log(description)
}

explain()
$ bun index.ts

    - 200 OK
      The request succeeded. The result meaning of "success" depends on the HTTP method:

      * GET: The resource has been fetched...
      * HEAD: The representation headers are...
      * PUT or POST: The resource describing...
      * TRACE: The message body contains the...

Tunggu, adakah saya perlu mengalih keluar lekukan?
Nah. Saya tidak boleh melepaskan kod berformat indah saya.

function explain() {
  const description = '- 200 OK\n' +
    'The request succeeded. The result meaning of "success" depends on the HTTP method:\n\n' +
    '  * GET: The resource has been fetched...\n' +
    '  * HEAD: The representation headers are...\n' +
    '  * PUT or POST: The resource describing...\n' +
    '  * TRACE: The message body contains the...\n'

  console.log(description)
}

explain()

Saya akan terima itu. ?

Atas sebab ini, teks berbilang baris sentiasa menyusahkan saya.

Sekarang anda tahu dedent

Tetapi sekarang, anda tidak perlu berunding dengan diri sendiri lagi. Hanya gunakan dedent.

import dedent from 'dedent'

function explain() {
  const description = dedent`
    - 200 OK
      The request succeeded. The result meaning of "success" depends on the HTTP method:

      * GET: The resource has been fetched...
      * HEAD: The representation headers are...
      * PUT or POST: The resource describing...
      * TRACE: The message body contains the...
  `

  console.log(description)
}

explain()

Apa yang saya lakukan ialah menambah dedent sebelum literal templat. Anda tidak percaya?

$ bun index.ts
- 200 OK
  The request succeeded. The result meaning of "success" depends on the HTTP method:

  * GET: The resource has been fetched...
  * HEAD: The representation headers are...
  * PUT or POST: The resource describing...
  * TRACE: The message body contains the...

Ia membuang semua lekukan yang tidak perlu dan menjadikannya seperti yang kami jangkakan.

Kenapa kita tidak mencuba yang lebih kompleks?

import dedent from 'dedent'

const explainStatus = (status: string) => {
    switch(status) {
        case '2xx':
          return dedent`
            - 200 OK
              The request succeeded. The result meaning of "success" depends on the HTTP method:

              * GET: The resource has been fetched and transmitted in the message body.
              * HEAD: The representation headers are included in the response without any message body.
              * PUT or POST: The resource describing the result of the action is transmitted in the message body.
              * TRACE: The message body contains the request message as received by the server.

            - 201 Created
              The request succeeded, and a new resource was created as a result.
              This is typically the response sent after POST requests, or some PUT requests.
            `

        case '4xx':
          return dedent`
            - 400 Bad Request
              The server cannot or will not process the request due to something that is perceived to be a client error
              (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

            - 401 Unauthorized
              Although the HTTP standard specifies "unauthorized", semantically this response means "unauthenticated".
              That is, the client must authenticate itself to get the requested response.
          `

          default:
            return 'not yet!'
      }
}

console.log(explainStatus('2xx'))
$ bun index.ts
- 200 OK
  The request succeeded. The result meaning of "success" depends on the HTTP method:

  * GET: The resource has been fetched and transmitted in the message body.
  * HEAD: The representation headers are included in the response without any message body.
  * PUT or POST: The resource describing the result of the action is transmitted in the message body.
  * TRACE: The message body contains the request message as received by the server.

- 201 Created
  The request succeeded, and a new resource was created as a result.
  This is typically the response sent after POST requests, or some PUT requests.

Sangat lancar!?

Atas ialah kandungan terperinci [Pakej Harian] deden. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn