[Daily Package] dedent

PHPz
Release: 2024-08-30 21:01:02
Original
729 people have browsed it

[Daily Package] dedent

Life before knowing dedent

Have you ever tried to write multi-line paragraph in template literal, but realized it preserves indentation, which ended up using string addition with 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()
Copy after login
$ 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...
Copy after login

Wait, do I need to remove the indentations?
Nah. I can't give up my beautifully formatted code.

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()
Copy after login

I'll take that. ?

For this reason, multiline text is always a headache for me.

Now you know dedent

But now, you don't have to negotiate with yourself anymore. Just use 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()
Copy after login

What I did was add dedent before the template literal. You don't believe it?

$ 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...
Copy after login

It removes all the unnecessary indentation and makes it as we expected.

Why don't we try a more complex one?

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'))
Copy after login
$ 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.
Copy after login

Soo smoooth!?

The above is the detailed content of [Daily Package] dedent. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!