aboutsummaryrefslogtreecommitdiff
path: root/lib/utils.js
diff options
context:
space:
mode:
authorAndre Meyering <info@andremeyering.de>2018-07-07 14:57:56 +0200
committerAndre Meyering <info@andremeyering.de>2018-11-28 14:29:10 +0100
commit061dcd35c70f3f24eaed181c4b517675948eef6d (patch)
tree984fe2e71977cc2f21bb36e29300557d2a69c481 /lib/utils.js
parentd384986b3abcf2c8c21ae4f0b6749c067ba4f5ce (diff)
downloadcompiler-explorer-061dcd35c70f3f24eaed181c4b517675948eef6d.tar.gz
compiler-explorer-061dcd35c70f3f24eaed181c4b517675948eef6d.zip
[Utils] Add trimLine() function
- trimLine() trims left, right and int-between spaces
Diffstat (limited to 'lib/utils.js')
-rw-r--r--lib/utils.js15
1 files changed, 15 insertions, 0 deletions
diff --git a/lib/utils.js b/lib/utils.js
index ce5fa5f21..24d8e39d2 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -167,3 +167,18 @@ exports.glGetMainContents = function glGetMainContents(content) {
});
return contents;
};
+
+function trimLine(line) {
+ if (!line.trim().length) {
+ return "";
+ }
+ const splat = line.split(/\s+/);
+ if (splat[0] === "") {
+ // An indented line: preserve a two-space indent (max)
+ const intent = line[1] !== " " ? " " : " ";
+ return intent + splat.slice(1).join(" ");
+ }
+ return splat.join(" ");
+}
+
+exports.trimLine = trimLine;