diff options
author | Andre Meyering <info@andremeyering.de> | 2018-07-07 14:57:56 +0200 |
---|---|---|
committer | Andre Meyering <info@andremeyering.de> | 2018-11-28 14:29:10 +0100 |
commit | 061dcd35c70f3f24eaed181c4b517675948eef6d (patch) | |
tree | 984fe2e71977cc2f21bb36e29300557d2a69c481 /lib/utils.js | |
parent | d384986b3abcf2c8c21ae4f0b6749c067ba4f5ce (diff) | |
download | compiler-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.js | 15 |
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; |