diff --git a/README.md b/README.md index a3d0fd5..c96d7a2 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,10 @@ Usage is simple - create an instance and call .strip(string) to get back the de-
var cs = new CommentStripper(); console.log( cs.strip(someString) );+It's also possible to preserve all newlines within multiline comments (e.g. to support sourcemaps)... + +
cs.setPreserveNewlines(true);+ 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 0724c1b..7d93025 100644
--- a/strip-comments.js
+++ b/strip-comments.js
@@ -1,3 +1,5 @@
+"use strict";
+
var CommentStripper = (function (window) {
var SLASH = '/';
@@ -18,6 +20,11 @@ var CommentStripper = (function (window) {
length: 0,
position: 0,
output: null,
+ preserveNewlines: false,
+
+ setPreserveNewlines: function (preserveNewlines) {
+ this.preserveNewlines = preserveNewlines;
+ },
getCurrentCharacter: function () {
return this.string.charAt(this.position);
@@ -115,6 +122,11 @@ var CommentStripper = (function (window) {
return;
}
}
+ if (this.preserveNewlines) {
+ if (this.getCurrentCharacter() == NEW_LINE || this.getCurrentCharacter() == CARRIAGE_RETURN) {
+ this.add();
+ }
+ }
}
}
}
@@ -171,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;
+ }
-})(window);
\ No newline at end of file
+})(typeof window === 'undefined' ? global : window);