Comparing version 0.7.1 to 0.7.2
@@ -21,4 +21,5 @@ 'use strict'; | ||
emptyLetters = '', // empty letters on a new line; | ||
optSafe = options && options.safe, // 'safe' option; | ||
optSpace = options && options.space, // 'space' option; | ||
optTrim = options && options.trim, // 'trim' option; | ||
optSafe = options && options.safe, // 'safe' option; | ||
checkRegEx = false, // indicates whether regEx is to be used; | ||
@@ -55,4 +56,8 @@ isHtml, // set when the input is recognized as HTML; | ||
emptyLetters = ''; | ||
idx = lb + EOL.length - 1; // last symbol of the line break; | ||
trim(); | ||
if (optSpace) { | ||
idx = lb - 1; // just before the line break; | ||
} else { | ||
idx = lb + EOL.length - 1; // last symbol of the line break; | ||
trim(); | ||
} | ||
} else { | ||
@@ -81,10 +86,22 @@ idx = lb - 1; // just before the line break; | ||
} | ||
var l = end - idx + 2; // entire comment length; | ||
idx = end + 1; | ||
if (emptyLine) { | ||
emptyLetters = ''; | ||
if (!keep) { | ||
var lb = code.indexOf(EOL, idx + 1); | ||
if (lb > idx) { | ||
idx = lb + EOL.length - 1; // last symbol of the line break; | ||
trim(); | ||
} | ||
if (!keep) { | ||
var lb = code.indexOf(EOL, idx + 1); | ||
if (lb > idx) { | ||
var gapIdx = lb - 1; | ||
while ((code[gapIdx] === ' ' || code[gapIdx] === '\t') && --gapIdx > idx); | ||
if (gapIdx === idx) { | ||
if (emptyLine && !optSpace) { | ||
idx = lb + EOL.length - 1; // last symbol of the line break; | ||
trim(); | ||
} | ||
} else { | ||
if (optSpace) { | ||
s += utils.getSpaces(l); | ||
} | ||
} | ||
@@ -102,9 +119,20 @@ } | ||
} | ||
var l = end - idx + 3; // entire comment length; | ||
idx = end + 2; | ||
if (emptyLine) { | ||
emptyLetters = ''; | ||
var lb = code.indexOf(EOL, idx + 1); | ||
if (lb > idx) { | ||
idx = lb + EOL.length - 1; // last symbol of the line break; | ||
trim(); | ||
} | ||
var lb = code.indexOf(EOL, idx + 1); | ||
if (lb > idx) { | ||
var gapIdx = lb - 1; | ||
while ((code[gapIdx] === ' ' || code[gapIdx] === '\t') && --gapIdx > idx); | ||
if (gapIdx === idx) { | ||
if (emptyLine && !optSpace) { | ||
idx = lb + EOL.length - 1; // last symbol of the line break; | ||
trim(); | ||
} | ||
} else { | ||
if (optSpace) { | ||
s += utils.getSpaces(l); | ||
} | ||
} | ||
@@ -111,0 +139,0 @@ } |
@@ -58,3 +58,15 @@ 'use strict'; | ||
// istanbul ignore next | ||
// we are not generating test coverage for this | ||
// function, since it is environment-dependent. | ||
function getSpaces(n) { | ||
if (typeof String.prototype.repeat === 'function') { | ||
return ' '.repeat(n); | ||
} else { | ||
return new Array(n + 1).join(' '); | ||
} | ||
} | ||
module.exports = { | ||
getSpaces: getSpaces, | ||
parseRegEx: parseRegEx, | ||
@@ -61,0 +73,0 @@ indexInRegEx: indexInRegEx, |
{ | ||
"name": "decomment", | ||
"version": "0.7.1", | ||
"version": "0.7.2", | ||
"description": "Removes comments from JSON, JavaScript, CSS, HTML, etc.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -68,2 +68,33 @@ decomment | ||
##### options.safe ⇒ Boolean | ||
* `false (default)` - remove all multi-line comments | ||
* `true` - keep multi-line comments that start with `/*!` | ||
Example: | ||
```js | ||
var decomment = require('decomment'); | ||
var code = "/*! special */ var a; /* normal */"; | ||
decomment(code); //=> var a; | ||
decomment(code, {safe: true}); //=> /*! special */ var a; | ||
``` | ||
NOTE: This option has no effect when processing HTML. | ||
##### options.space ⇒ Boolean | ||
* `false (default)` - remove comment blocks entirely | ||
* `true` - replace comment blocks with white spaces where needed, in order to preserve | ||
the original line + column position of every code element. | ||
Example: | ||
```js | ||
var decomment = require('decomment'); | ||
var code = "var a/*text*/, b"; | ||
decomment(code); //=> var a, b | ||
decomment(code, {space: true}); //=> var a , b | ||
``` | ||
NOTE: When this option is enabled, option `trim` is ignored. | ||
##### options.trim ⇒ Boolean | ||
@@ -82,17 +113,4 @@ * `false (default)` - do not trim comments | ||
##### options.safe ⇒ Boolean | ||
* `false (default)` - remove all multi-line comments | ||
* `true` - keep multi-line comments that start with `/*!` | ||
NOTE: This option has no effect when option `space` is enabled. | ||
Example: | ||
```js | ||
var decomment = require('decomment'); | ||
var code = "/*! special */ var a; /* normal */"; | ||
decomment(code); //=> var a; | ||
decomment(code, {safe: true}); //=> /*! special */ var a; | ||
``` | ||
This option has no effect when processing HTML. | ||
### decomment.text(text, [options]) ⇒ String | ||
@@ -99,0 +117,0 @@ |
@@ -54,2 +54,20 @@ 'use strict'; | ||
describe("with space=true", function () { | ||
it("must return the preceding text", function () { | ||
expect(decomment.html("Text<!---->", {space: true})).toBe("Text"); | ||
expect(decomment.html(LB + "Text<!---->", {space: true})).toBe(LB + "Text"); | ||
expect(decomment.html("Text" + LB + "<!---->", {space: true})).toBe("Text" + LB); | ||
expect(decomment.html("Text<!---->" + LB + "Here", {space: true})).toBe("Text" + LB + "Here"); | ||
}); | ||
}); | ||
describe("with preceding text", function () { | ||
it("must return the preceding text", function () { | ||
expect(decomment.html("Text<!---->")).toBe("Text"); | ||
expect(decomment.html(LB + "Text<!---->")).toBe(LB + "Text"); | ||
expect(decomment.html("Text" + LB + "<!---->")).toBe("Text" + LB); | ||
expect(decomment.html("Text<!---->" + LB + "Here")).toBe("Text" + LB + "Here"); | ||
}); | ||
}); | ||
describe("Explicit HTML call", function () { | ||
@@ -60,2 +78,24 @@ it("must process it as HTML always", function () { | ||
}); | ||
describe("starting comments suffixed by spaces", function () { | ||
it("must remove comment and spaces", function () { | ||
expect(decomment("<!--hello--> " + LB + "next")).toBe("next"); | ||
expect(decomment(" <!--hello--> " + LB + "next")).toBe("next"); | ||
expect(decomment("<!--hello--> \t " + LB + "next")).toBe("next"); | ||
expect(decomment(" \t <!--hello--> \t " + LB + "next")).toBe("next"); | ||
}); | ||
}); | ||
describe("starting comments suffixed by text, space=false", function () { | ||
it("must remove comment and preserve the suffix", function () { | ||
expect(decomment("<!--hello-->text" + LB + "next")).toBe("text" + LB + "next"); | ||
}); | ||
}); | ||
describe("starting comments suffixed by text, space=true", function () { | ||
it("must replace comment with white spaces and preserve the suffix", function () { | ||
expect(decomment("<!--hello-->text" + LB + "next", {space: true})).toBe(" text" + LB + "next"); | ||
}); | ||
}); | ||
}); |
@@ -11,3 +11,3 @@ 'use strict'; | ||
describe("empty comment", function () { | ||
describe("empty comment, space=false", function () { | ||
it("must return an empty string", function () { | ||
@@ -19,12 +19,37 @@ expect(decomment("/**/")).toBe(""); | ||
describe("multiple empty comments", function () { | ||
describe("empty comment, space=true", function () { | ||
it("must return spaces where needed", function () { | ||
expect(decomment("/**/", {space: true})).toBe(""); | ||
expect(decomment("\/**\/")).toBe(""); | ||
}); | ||
}); | ||
describe("multiple empty comments, space=false", function () { | ||
it("must return an empty string", function () { | ||
expect(decomment("/**/" + LB + "/**/")).toBe(""); | ||
expect(decomment("/**/" + LB + "/**/" + LB)).toBe(""); | ||
expect(decomment("/**/" + LB + "/**/")).toBe(""); | ||
}); | ||
}); | ||
describe("multiple empty comments, space=true", function () { | ||
it("must return only line breaks", function () { | ||
expect(decomment("/**/" + LB, {space: true})).toBe(LB); | ||
expect(decomment("/**/" + LB + "/**/", {space: true})).toBe(LB); | ||
expect(decomment("/**/" + LB + "/**/" + LB, {space: true})).toBe(LB + LB); | ||
}); | ||
}); | ||
describe("multiple comments, space=true", function () { | ||
it("must return correct spaces", function () { | ||
expect(decomment("/**/ " + LB, {space: true})).toBe(LB); | ||
expect(decomment("/**/ " + LB, {space: true})).toBe(LB); | ||
expect(decomment("/**/ /**/" + LB, {space: true})).toBe(" " + LB); | ||
expect(decomment("/**/a/**/b/**/c" + LB, {space: true})).toBe(" a b c" + LB); | ||
expect(decomment("/**/ a/**/ b/**/ c " + LB, {space: true})).toBe(" a b c " + LB); | ||
}); | ||
}); | ||
describe("non-empty comment", function () { | ||
it("must return an empty string", function () { | ||
expect(decomment("/* text*/")).toBe(""); | ||
expect(decomment("/*text*/")).toBe(""); | ||
}); | ||
@@ -58,15 +83,20 @@ }); | ||
describe("with preceding text", function () { | ||
var out1 = decomment("Text/**/"); | ||
var out2 = decomment(LB + "Text/**/"); | ||
var out3 = decomment("Text" + LB + "/**/"); | ||
var out4 = decomment("Text/**/" + LB + "Here"); | ||
describe("with preceding text, space=false", function () { | ||
it("must return the preceding text", function () { | ||
expect(out1).toBe("Text"); | ||
expect(out2).toBe(LB + "Text"); | ||
expect(out3).toBe("Text" + LB); | ||
expect(out4).toBe("Text" + LB + "Here"); | ||
expect(decomment("Text/**/")).toBe("Text"); | ||
expect(decomment(LB + "Text/**/")).toBe(LB + "Text"); | ||
expect(decomment("Text" + LB + "/**/")).toBe("Text" + LB); | ||
expect(decomment("Text/**/" + LB + "Here")).toBe("Text" + LB + "Here"); | ||
}); | ||
}); | ||
describe("with preceding text, space=true", function () { | ||
it("must return the preceding text", function () { | ||
expect(decomment("Text/**/", {space: true})).toBe("Text"); | ||
expect(decomment(LB + "Text/**/", {space: true})).toBe(LB + "Text"); | ||
expect(decomment("Text" + LB + "/**/", {space: true})).toBe("Text" + LB); | ||
expect(decomment("Text/**/" + LB + "Here", {space: true})).toBe("Text" + LB + "Here"); | ||
}); | ||
}); | ||
describe("with empty text prefix", function () { | ||
@@ -95,8 +125,28 @@ var out1 = decomment("''/**/"); | ||
describe("comments inside text", function () { | ||
var out = decomment("'/**/Text'"); | ||
it("must leave only the comment", function () { | ||
expect(out).toBe("'/**/Text'"); | ||
expect(decomment("'/**/text'")).toBe("'/**/text'"); | ||
}); | ||
}); | ||
describe("starting comments suffixed by text, space=false", function () { | ||
it("must remove comment and preserve the suffix", function () { | ||
expect(decomment("/*hello*/text" + LB + "next")).toBe("text" + LB + "next"); | ||
}); | ||
}); | ||
describe("starting comments suffixed by text, space=true", function () { | ||
it("must replace comment with white spaces and preserve the suffix", function () { | ||
expect(decomment("/*hello*/text" + LB + "next", {space: true})).toBe(" text" + LB + "next"); | ||
}); | ||
}); | ||
describe("starting comments suffixed by spaces", function () { | ||
it("must remove comment and spaces", function () { | ||
expect(decomment("/*hello*/ " + LB + "next")).toBe("next"); | ||
expect(decomment(" /*hello*/ " + LB + "next")).toBe("next"); | ||
expect(decomment("/*hello*/ \t " + LB + "next")).toBe("next"); | ||
expect(decomment(" \t /*hello*/ \t " + LB + "next")).toBe("next"); | ||
}); | ||
}); | ||
describe("spaces", function () { | ||
@@ -103,0 +153,0 @@ describe("complex case", function () { |
@@ -14,15 +14,20 @@ 'use strict'; | ||
expect(decomment("//")).toBe(""); | ||
expect(decomment("/\/text")).toBe(""); // '/\/' = '//' | ||
expect(decomment("/\/text")).toBe(""); | ||
}); | ||
}); | ||
describe("multiple empty comments", function () { | ||
var out1 = decomment("//" + LB + "//" + LB); | ||
var out2 = decomment("//" + LB + "//"); | ||
describe("multiple empty comments, space=false", function () { | ||
it("must return an empty string", function () { | ||
expect(out1).toBe(""); | ||
expect(out2).toBe(""); | ||
expect(decomment("//" + LB + "//")).toBe(""); | ||
expect(decomment("//" + LB + "//" + LB)).toBe(""); | ||
}); | ||
}); | ||
describe("multiple empty comments, space=true", function () { | ||
it("must return line breaks", function () { | ||
expect(decomment("//" + LB + "//", {space: true})).toBe(LB); | ||
expect(decomment("//" + LB + "//" + LB, {space: true})).toBe(LB + LB); | ||
}); | ||
}); | ||
describe("non-empty comment", function () { | ||
@@ -29,0 +34,0 @@ var out = decomment("// text"); |
39180
815
146