0

I am running a js file through a minifier which is leaving line breaks at unexplained positions.

For example currently I have the following code:

var example = "var name = 'john';
               function sayName () {
                 alert('hi');
               }
               var person = {
                 name: 'peter',
                 sayName: function() {
                    alert('this is my name ' + this.name);
                 }
               }"

After it is run through the minifier I get:

var name='john';function sayName(){alert('hi');}
var person={name:'peter',sayName:function(){alert('this is my name '+this.name);}}

I have tried many regex and trim() but cannot figure out how to fix this. Does anyone have an idea of how to remove the white lines at the end of a line?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 2
    Where does the `var example` and that syntax-error string literal come from? – Bergi Sep 13 '16 at 13:34
  • 2
    Still invalid after the edit - you can't have a multiline string. – VLAZ Sep 13 '16 at 13:35
  • apologies, that was a small off sight on my part. I shortened the example for the purpose of the question and forgot the closing `"` –  Sep 13 '16 at 13:35
  • @vlaz and berg, weird! its in a script.js file and when i check `typeof` I get string. I am running it through an npm module which is reading the file –  Sep 13 '16 at 13:37
  • Let me clarify - you _can_ have a string with newlines in it, you cannot have a string that starts on one line (`"` open on line 1) and ends in another (closing `"` on another). You can have a string on one line, then end it on the same line and use `+` to join it to the string on the next line. So, you get `"hello\n" + "world"` if you want a string with two lines in it. – VLAZ Sep 13 '16 at 13:39
  • 1
    At any rate, `trim()` _on a string_ will only remove starting and ending whitespace, e.g. `" hello \n" + " world "` will be turned into `"hello \n" + " world"` but notice that whitespace in the middle is _not_ affected. Unless you are calling a different `trim()` or something, it won't trim every line. – VLAZ Sep 13 '16 at 13:42
  • 1
    @phantom: Are you saying that the string is actually coming from reading a file, but you decided to represent it as `var example = "..."`? If so, please don't do that. Describe *exactly* what you're doing in the question. These shortcuts just waste people's time. –  Sep 13 '16 at 13:43
  • APOLOGIES ALLl! I will be in trouble here. I had to update the question as there was one step i had 'mis-debugged'. this is in direct reply to @squint also! sorry again –  Sep 13 '16 at 13:43
  • 2
    Which minifier are you using that produces this output, and what exactly is unexpected? – Bergi Sep 13 '16 at 13:44
  • @Bergi should there be a new line? I thought if i minified the code it should not contain these new lines like if you run it through https://javascript-minifier.com/ it is coming from `jsmin` –  Sep 13 '16 at 13:46
  • If you're using jsmin, keep your expectations low or just find a better minifier. –  Sep 13 '16 at 13:47
  • @squint ok, its not considered good? –  Sep 13 '16 at 13:49
  • Try `.replace(/^\s+|\s+$|[\r\n]+/gm, '')` or explain what output you need. – Wiktor Stribiżew Sep 13 '16 at 13:50
  • There's very little that comes from Crockford that I consider good. You shouldn't need to post-process the output from a minifier in order to improve its result. There are many others that'll do a better job. –  Sep 13 '16 at 13:51

1 Answers1

0

Try something like this:

var withoutWhitespaceAtLineEnd = example.split(/\n/).map(function(line){
  return line.replace(/\s+$/,'');
}).join("\n");
Philipp Dahse
  • 749
  • 3
  • 9
  • man, apologies, I updated the question. I will look at the solution but just so you know its worth seeing the update –  Sep 13 '16 at 13:43