防止 ES6 模板字符串中的换行符
Prevent line breaks in ES6 template string
ESLint:第 403 行超过最大行长度 120 (max-len)
我有一个很长的字符串,它是我使用 ES6 模板字符串构建的,但我希望它没有换行符:
var string = `Let me be the 'throws Exception’ to your 'public static void
main (String[] args)’. I will accept whatever you give me my ${love}.`
console.log(string);
结果:
Let me be the 'throws Exception’ to your 'public static void
main (String[] args)’. I will accept whatever you give me xxx.
我的期望:
Let me be the 'throws Exception’ to your 'public static void main (String[] args)’. I will accept whatever you give me xxx.
要求:
我无法禁用 eslint 规则,因为强制执行是必要的。
我不能将数据放在单独的文件中,因为数据是动态的。
我无法连接多个较短的字符串,因为那太麻烦了。
这是预期的行为。模板文字解决的重要问题之一是 multiline strings:
Any new line characters inserted in the source are part of the template literal.
如果字符串需要进一步处理,这可以通过其他 JS 功能来完成,比如正则表达式:
var string = `Let me be the 'throws Exception’ to your 'public static void
main (String[] args)’. I will accept whatever you give me.`
.replace(/[\n\r]+ */g, ' ');
String.raw
is built-in function to transform template literals. It's possible to use tag function to provide custom behaviour to template literals. It should be noticed that String.raw
differs from default template transformer in terms of how it processes special characters. If they are used in a string, they should be additionally processed with unescape-js
或类似的辅助函数。
function singleLine(strsObj, ...values) {
const strs = strsObj.raw
.map(str => str.replace(/[\n\r]+ */g, ' '))
.map(unescapeSpecialChars);
return String.raw(
{raw: strs },
...values
);
}
var string = singleLine`Let me be the 'throws Exception’ to your 'public static void
main (String[] args)’. I will accept whatever you give me.`;
如果您的问题只是 EsLint 错误,您可以使用此功能忽略此特定行的错误:/* eslint-disable max-len */
老实说,这是最好的方法,因为您没有提供额外的复杂性。
如果您开始使用正则表达式或串联,那么您正在通过不使用串联来改变模板字符串的用途...
也使用字符串连接:
var string=`abc`+`def`;
console.log(string);
产量:
abcdef
达到目的的一个好方法是加入一个字符串数组:
var string = [
`Let me be the 'throws Exception’ to your 'public static void`,
`main (String[] args)’. I will accept whatever you give me my ${love}.`
].join(' ');
ESLint:第 403 行超过最大行长度 120 (max-len)
我有一个很长的字符串,它是我使用 ES6 模板字符串构建的,但我希望它没有换行符:
var string = `Let me be the 'throws Exception’ to your 'public static void
main (String[] args)’. I will accept whatever you give me my ${love}.`
console.log(string);
结果:
Let me be the 'throws Exception’ to your 'public static void
main (String[] args)’. I will accept whatever you give me xxx.
我的期望:
Let me be the 'throws Exception’ to your 'public static void main (String[] args)’. I will accept whatever you give me xxx.
要求:
我无法禁用 eslint 规则,因为强制执行是必要的。
我不能将数据放在单独的文件中,因为数据是动态的。
我无法连接多个较短的字符串,因为那太麻烦了。
这是预期的行为。模板文字解决的重要问题之一是 multiline strings:
Any new line characters inserted in the source are part of the template literal.
如果字符串需要进一步处理,这可以通过其他 JS 功能来完成,比如正则表达式:
var string = `Let me be the 'throws Exception’ to your 'public static void
main (String[] args)’. I will accept whatever you give me.`
.replace(/[\n\r]+ */g, ' ');
String.raw
is built-in function to transform template literals. It's possible to use tag function to provide custom behaviour to template literals. It should be noticed that String.raw
differs from default template transformer in terms of how it processes special characters. If they are used in a string, they should be additionally processed with unescape-js
或类似的辅助函数。
function singleLine(strsObj, ...values) {
const strs = strsObj.raw
.map(str => str.replace(/[\n\r]+ */g, ' '))
.map(unescapeSpecialChars);
return String.raw(
{raw: strs },
...values
);
}
var string = singleLine`Let me be the 'throws Exception’ to your 'public static void
main (String[] args)’. I will accept whatever you give me.`;
如果您的问题只是 EsLint 错误,您可以使用此功能忽略此特定行的错误:/* eslint-disable max-len */
老实说,这是最好的方法,因为您没有提供额外的复杂性。
如果您开始使用正则表达式或串联,那么您正在通过不使用串联来改变模板字符串的用途...
也使用字符串连接:
var string=`abc`+`def`;
console.log(string);
产量:
abcdef
达到目的的一个好方法是加入一个字符串数组:
var string = [
`Let me be the 'throws Exception’ to your 'public static void`,
`main (String[] args)’. I will accept whatever you give me my ${love}.`
].join(' ');