テンプレート リテラルで複数行の段落を書こうとして、インデントが保持され、最終的に 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...
ちょっと待って、インデントを削除する必要がありますか?
いや。美しくフォーマットされたコードを手放すことはできません。
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()
それを受け取ります。 ?
このため、複数行のテキストは私にとって常に頭の痛い問題です。
しかし今は、もう自分自身と交渉する必要はありません。デデントを使用するだけです。
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()
私がやったのは、テンプレートリテラルの前にデデントを追加することでした。信じられませんか?
$ 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...
不要なインデントがすべて削除され、期待どおりのものが作成されます。
もっと複雑なものを試してみませんか?
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.
すっごくスムーズ!?
以上が[デイリーパッケージ] デデントの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。