Avez-vous déjà essayé d'écrire un paragraphe multiligne dans un modèle littéral, mais vous avez réalisé qu'il préservait l'indentation, ce qui a fini par utiliser l'addition de chaîne avec 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...
Attendez, dois-je supprimer les indentations ?
Non. Je ne peux pas abandonner mon code magnifiquement formaté.
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()
Je prends ça. ?
Pour cette raison, le texte multiligne est toujours un casse-tête pour moi.
Mais désormais, vous n'avez plus à négocier avec vous-même. Utilisez simplement 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()
Ce que j'ai fait, c'est ajouter un retrait avant le modèle littéral. Vous n'y croyez pas ?
$ 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...
Il supprime toutes les indentations inutiles et le rend comme prévu.
Pourquoi n'essayons-nous pas une solution plus complexe ?
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.
Tellement doux !?
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!