هل سبق لك أن حاولت كتابة فقرة متعددة الخطوط في القالب الحرفي ، لكنها أدركت أنها تحافظ على المسافة البادئة ، والتي انتهى بها الأمر باستخدام إضافة سلسلة مع \ 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.
سو smoooth!؟
تنصل: جميع الموارد المقدمة هي جزئيًا من الإنترنت. إذا كان هناك أي انتهاك لحقوق الطبع والنشر الخاصة بك أو الحقوق والمصالح الأخرى، فيرجى توضيح الأسباب التفصيلية وتقديم دليل على حقوق الطبع والنشر أو الحقوق والمصالح ثم إرسالها إلى البريد الإلكتروني: [email protected]. سوف نتعامل مع الأمر لك في أقرب وقت ممكن.
Copyright© 2022 湘ICP备2022001581号-3