[Pakej Harian] deden

PHPz
Lepaskan: 2024-08-30 21:01:02
asal
729 orang telah melayarinya

[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()
Salin selepas log masuk
$ 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...
Salin selepas log masuk

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()
Salin selepas log masuk

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()
Salin selepas log masuk

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...
Salin selepas log masuk

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'))
Salin selepas log masuk
$ 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.
Salin selepas log masuk

Sangat lancar!?

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

sumber:dev.to
Kenyataan Laman Web ini
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
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!