首页 > web前端 > js教程 > 正文

[每日套餐] dedent

PHPz
发布: 2024-08-30 21:01:02
原创
989 人浏览过

[Daily Package] dedent

认识 dedent 之前的生活

您是否曾经尝试过在模板文字中编写多行段落,但意识到它保留了缩进,最终使用了带有 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()
登录后复制

我会接受的。 ?

因此,多行文本总是让我头疼。

现在你知道了

但是现在,你不用再和自己谈判了。只需使用 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()
登录后复制

我所做的是在模板文字之前添加缩进。你不相信吗?

$ 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.
登录后复制

太流畅了!?

以上是[每日套餐] dedent的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板