From 882c72a84b9596a1ba14d803309fcef85778b110 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20H=C3=BCtter?= Date: Sun, 6 Mar 2016 19:15:47 +0100 Subject: [PATCH 1/6] make it usable outside of a browser (e.g. Node.js) --- strip-comments.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/strip-comments.js b/strip-comments.js index 0724c1b..75b9663 100644 --- a/strip-comments.js +++ b/strip-comments.js @@ -173,4 +173,4 @@ var CommentStripper = (function (window) { return window.CommentStripper = CommentStripper; -})(window); \ No newline at end of file +})(typeof window == "undefined" ? global : window); From 17bd1bc4d138b4e2b91ad0e4b23bd964e7d8c265 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20H=C3=BCtter?= Date: Sun, 6 Mar 2016 19:36:33 +0100 Subject: [PATCH 2/6] strict mode --- strip-comments.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/strip-comments.js b/strip-comments.js index 75b9663..9fe726c 100644 --- a/strip-comments.js +++ b/strip-comments.js @@ -1,3 +1,5 @@ +"use strict"; + var CommentStripper = (function (window) { var SLASH = '/'; From 80e1f6c104f2827cacc492f800f2657181e29f21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20H=C3=BCtter?= Date: Sun, 6 Mar 2016 20:00:05 +0100 Subject: [PATCH 3/6] add the opportunity to preserve newlines (optional) --- README.md | 5 +++++ strip-comments.js | 12 ++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a3d0fd5..3d3feb9 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,11 @@ Usage is simple - create an instance and call .strip(string) to get back the de-
var cs = new CommentStripper();
 console.log( cs.strip(someString) );
+Or specify an option to preserve all newlines within multiline comments (e.g. to support sourcemaps)... + +
var cs = new CommentStripper({'preserveNewlines': true});
+console.log( cs.strip(someString) );
+ It's *intended* to be 100% effective - catching any valid comment, including:
var a = 10; // comment
diff --git a/strip-comments.js b/strip-comments.js
index 9fe726c..0cbda9e 100644
--- a/strip-comments.js
+++ b/strip-comments.js
@@ -10,8 +10,10 @@ var CommentStripper = (function (window) {
     var NEW_LINE = '\n';
     var CARRIAGE_RETURN = '\r';
 
-    var CommentStripper = function () {
-
+    var CommentStripper = function (options) {
+        if (options !== undefined) {
+            this.preserveNewlines = typeof options.preserveNewlines !== 'undefined' ? options.preserveNewlines : false;
+        }
     };
 
     CommentStripper.prototype = {
@@ -20,6 +22,7 @@ var CommentStripper = (function (window) {
         length: 0,
         position: 0,
         output: null,
+        preserveNewlines: false,
 
         getCurrentCharacter: function () {
             return this.string.charAt(this.position);
@@ -117,6 +120,11 @@ var CommentStripper = (function (window) {
                                 return;
                             }
                         }
+                        if (this.preserveNewlines) {
+                            if (this.getCurrentCharacter() == NEW_LINE || this.getCurrentCharacter() == CARRIAGE_RETURN) {
+                                this.add();
+                            }
+                        }
                     }
                 }
             }

From eafa38ab2b9b072b6795f97779d0eb217ed0508b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andreas=20H=C3=BCtter?= 
Date: Tue, 8 Mar 2016 20:32:28 +0100
Subject: [PATCH 4/6] removed the options-object (as requested by you), and
 replaced it with a setter function

---
 strip-comments.js | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/strip-comments.js b/strip-comments.js
index 0cbda9e..06ebced 100644
--- a/strip-comments.js
+++ b/strip-comments.js
@@ -10,10 +10,8 @@ var CommentStripper = (function (window) {
     var NEW_LINE = '\n';
     var CARRIAGE_RETURN = '\r';
 
-    var CommentStripper = function (options) {
-        if (options !== undefined) {
-            this.preserveNewlines = typeof options.preserveNewlines !== 'undefined' ? options.preserveNewlines : false;
-        }
+    var CommentStripper = function () {
+
     };
 
     CommentStripper.prototype = {
@@ -24,6 +22,10 @@ var CommentStripper = (function (window) {
         output: null,
         preserveNewlines: false,
 
+        setPreserveNewlines: function (preserveNewlines) {
+            this.preserveNewlines = preserveNewlines;
+        },
+
         getCurrentCharacter: function () {
             return this.string.charAt(this.position);
         },

From 6fdb54384db1f8e973984f1254df637e757baf78 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andreas=20H=C3=BCtter?= 
Date: Tue, 8 Mar 2016 20:38:13 +0100
Subject: [PATCH 5/6] updated README.md

---
 README.md | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/README.md b/README.md
index 3d3feb9..c96d7a2 100644
--- a/README.md
+++ b/README.md
@@ -14,10 +14,9 @@ Usage is simple - create an instance and call .strip(string) to get back the de-
 
var cs = new CommentStripper();
 console.log( cs.strip(someString) );
-Or specify an option to preserve all newlines within multiline comments (e.g. to support sourcemaps)... +It's also possible to preserve all newlines within multiline comments (e.g. to support sourcemaps)... -
var cs = new CommentStripper({'preserveNewlines': true});
-console.log( cs.strip(someString) );
+
cs.setPreserveNewlines(true);
It's *intended* to be 100% effective - catching any valid comment, including: From 403418b20eeace2c41dcef38d11677a03ca9533a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20H=C3=BCtter?= Date: Tue, 8 Mar 2016 21:15:55 +0100 Subject: [PATCH 6/6] added 'module.exports' - works now both in browser and in via 'require' in node.js Would you adopt those changes into your repository? And could you also make this available via 'npm install'? You would also have to create a package.json file. Of course I can do this as well, but because it's originally your code and it's maintained in your repo, I would think it's more appropriate if the npm package is also connected to you... --- strip-comments.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/strip-comments.js b/strip-comments.js index 06ebced..7d93025 100644 --- a/strip-comments.js +++ b/strip-comments.js @@ -183,6 +183,10 @@ var CommentStripper = (function (window) { }); } - return window.CommentStripper = CommentStripper; + if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') { + module.exports = CommentStripper; + } else { + return window.CommentStripper = CommentStripper; + } -})(typeof window == "undefined" ? global : window); +})(typeof window === 'undefined' ? global : window);