模板字符串工具
缩进
根据一行所发现的缩入来条缩入. 会离开的 额外的新线和额外的缩进完整。 然而,它将消除领先和 尾随行断.
import { deindent } from "@gustavnikolaj/string-utils";
console.log(deindent`
foo bar
qux baz
`); // => "foo bar\nqux baz"
console.log(deindent`
foo bar
qux baz
`); // => "foo bar\n qux baz"
console.log(deindent`
foo bar
qux baz
`); // => "foo bar\n\n qux baz"
可用作常规函数以及模板字符串标签:
console.log(deindent`
foo bar
qux baz
`); // => "foo bar\nqux baz"
console.log(deindent(`
foo bar
qux baz
`)); // => "foo bar\nqux baz"
也可直接要求:
import deindent from "@gustavnikolaj/string-utils/deindent";
崩溃
在多行相包裹的一行中折叠出白空间.
import { collapse } from "@gustavnikolaj/string-utils";
console.log(collapse`
foo bar
qux baz
`); // => "foo bar qux baz"
console.log(collapse`
foo bar
qux baz
`); // => "foo bar qux baz"
console.log(collapse`
This is a very long string that I rather not have to put in a single line
because then it would make my code spill over and take more than the 80
characters per line that I prefer.
`);
// => "This is a very long string that I rather not have to put in a single " +
// "line because then it would make my code spill over and take more " +
// "than the 80 characters per line that I prefer."
也可直接要求:
import collapse from "@gustavnikolaj/string-utils/collapse";
重新流动
缩进为去缩出,但会坚持段落(以双相分隔) 将各段重新排出,使其符合 定义宽度.
import { reflow } from "@gustavnikolaj/string-utils";
console.log(reflow(8)`
foo bar qux baz foo bar
`); // => "foo bar\nqux baz\nfoo bar"
console.log(reflow(80)`
foo bar
qux baz
`); // => "foo bar\n\nqux baz"
console.log(reflow(30)`
This is a very long string that is for sure longer than 30 characters.
This is a very long string that is for sure longer than 30 characters.
`);
/* => This is a very long string
that is for sure longer than
30 characters.
This is a very long string
that is for sure longer than
30 characters. */
它还支持被调用为常规的非时空字符串函数:
reflow(
"This is a very long string that is for sure longer than 30 characters.",
30
);
也可直接要求:
import reflow from "@gustavnikolaj/string-utils/reflow";
单位
A 类 qw (引用单词) 助手 对于老, 不满的 perl 程序员 .
import { qw } from "@gustavnikolaj/string-utils";
const blah = "hey";
console.log(qw` foo bar
quux ${blah} baz
`); // => [ 'foo', 'bar', 'quux', 'hey', 'baz' ]
也可以直接导入:
import qw from "@gustavnikolaj/string-utils/qw";