Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ Usage is simple - create an instance and call .strip(string) to get back the de-
<pre>var cs = new CommentStripper();
console.log( cs.strip(someString) );</pre>

It's also possible to preserve all newlines within multiline comments (e.g. to support sourcemaps)...

<pre>cs.setPreserveNewlines(true);</pre>

It's *intended* to be 100% effective - catching any valid comment, including:

<pre>var a = 10; // comment
Expand Down
20 changes: 18 additions & 2 deletions strip-comments.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

var CommentStripper = (function (window) {

var SLASH = '/';
Expand All @@ -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);
Expand Down Expand Up @@ -115,6 +122,11 @@ var CommentStripper = (function (window) {
return;
}
}
if (this.preserveNewlines) {
if (this.getCurrentCharacter() == NEW_LINE || this.getCurrentCharacter() == CARRIAGE_RETURN) {
this.add();
}
}
}
}
}
Expand Down Expand Up @@ -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);
})(typeof window === 'undefined' ? global : window);