This JavaScript package provides an enhanced substitution for String.prototype.trim()
, which functions well in cases with multiple lines.
When no argument is passed and the string it's called on is a single-liner, it behaves just like the native String.prototype.trim()
.
' hello\t'.trim()
=> 'hello'
When called on a string with multiple lines (separated with '\n'
), it will automatically detect the indent pattern of the source string and trim the redundant indents off.
If there are any preceding or trailing lines full of whitespace characters, they'll be trimmed as well.
` hello, world\t
What is this weird indent?
(empty line below)
`.trim()
=> `hello, world
What is this weird indent?
(empty line below)`
This is super handy when dealing with blog/code formatting in an HTML page.
<pre>
#include <stdio.h>
int main() {
printf("hello, world");
return 0;
}
</pre>
<script>
// Automatically trim redundant code indents in <pre>
for($pre of document.getElementsByTagName('pre')) {
$pre => $pre.innerHTML = $pre.innerHTML.trim();
}
</script>
You can also pass an object as an argument to configure its behavior. The configurable settings are listed below.
trimWhiteLines
: whether to remove the preceding and trailing lines that are full of whitespace characters, default set totrue
.tabWidth
: no need to explain, pfft.forceConvert
: forcefully convert all indents to spaces or tabs, default set tofalse
(which means not to convert), you can set it to' '
or'\t'
. Note that the conversion result is affected bytabWidth
.strict
: when set totrue
, lines full of whitespace characters will not be ignored or trimmed, default set tofalse
.trimTrailing
: whether to remove the trailing whitespace characters in the lines, default set totrue
.