最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

typescript - Javascript URL with Template String - Stack Overflow

programmeradmin3浏览0评论

I would like to mount a URL string with Template String. I want to know if it is possible.

This is what I am doing:

let url = `/
                              MY_SERVICE/
                              MY_CONTROLLER/
                              ${MY_FIRST_PARAM}/
                              ${MY_SECOND_PARAM}`;

This code will return a string with break lines.

My question is: Is this possible to to this without break lines? The purpose is to have a more readable code.

My real URL is a little bit bigger than this.

I would like to mount a URL string with Template String. I want to know if it is possible.

This is what I am doing:

let url = `http://example./
                              MY_SERVICE/
                              MY_CONTROLLER/
                              ${MY_FIRST_PARAM}/
                              ${MY_SECOND_PARAM}`;

This code will return a string with break lines.

My question is: Is this possible to to this without break lines? The purpose is to have a more readable code.

My real URL is a little bit bigger than this.

Share Improve this question asked Jul 27, 2016 at 15:34 Luis Antonio PestanaLuis Antonio Pestana 1,0092 gold badges10 silver badges24 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9

You can call replace on any white space character after the template string:

let MY_FIRST_PARAM = 'foo';
let MY_SECOND_PARAM = 'bar';

let url = `http://example./
  MY_SERVICE/
  MY_CONTROLLER/
  ${MY_FIRST_PARAM}/
  ${MY_SECOND_PARAM}`
  .replace(/\s/g, '');

console.log(url);

In case this bees useful to somebody else. Here are notes from my own experience related to similar situation.

The question is originally about a long template string with new lines. But the example also shows up about a system where one could store URLs and plug in parameters dynamically.

This is for people from the future who happens to try doing the same, either in relation to URI Templates, and/or template literals, and/or stripping off white-space/new-lines. Like I did today.

The author most likely implemented like he asked the question.

The pattern of having URLs we can configure and insert variables has a specification and tools to help you not re-invent that wheel. :-)

Besides, if you concatenate many template literals you may get plaints from ESLint.

A better way of doing is to leverage JavaScript's URL object and RFC 6570 URI Template specification

It makes things much more manageable.

The implementation I used today was using bramstein/url-template. This library is awesome and it supports a lot of use-cases

Here is a full-circle example of a URI Template, and how to manipulate the URL without using RegExes.

// const urltemplate = require('url-template');
// Using example from question, slightly modified.
let url = `http://example./
                              MY_SERVICE/
                              MY_CONTROLLER
                              {/MY_FIRST_PARAM,MY_SECOND_PARAM}
                              `;
//                              (below) STRIP OFF new lines and spaces
//                               Credits to KevBot
const parsed = urltemplate.parse(url.replace(/[\s\n]/g,''));
const exampleFactory = a => new URL(parsed.expand(a))

// NOTICE we do not give any velue to MY_SECOND_PARAM
const firstExample = {
  MY_FIRST_PARAM: 'foo'
}
console.log((exampleFactory(firstExample)).pathname);
// > '/MY_SERVICE/MY_CONTROLLER/foo'

const secondExample = {
  MY_FIRST_PARAM: 'foo',
  MY_SECOND_PARAM: 'bar'
}
console.log((exampleFactory(secondExample)).pathname);
// > '/MY_SERVICE/MY_CONTROLLER/foo/bar'
<script src="https://cdn.rawgit./bramstein/url-template/c7119202/lib/url-template.js"></script>

NOTE Use of URI and URL is voluntarily mixed as we aren't talking about XML namespaces ("URI") that looks like a Web Address ("URL")

You get line breaks because an ENTER with the "``" notation in javascript will cause a newline, as explained here: https://developer.mozilla/en/docs/Web/JavaScript/Reference/Template_literals

Simply write everything in the same line.

发布评论

评论列表(0)

  1. 暂无评论