Comparing version 0.3.0 to 0.4.0
24
index.js
@@ -21,3 +21,5 @@ 'use strict'; | ||
emptyLetters = '', // empty letters on a new line; | ||
isHtml = false; // set when the input is recognized as HTML. | ||
isHtml = false, // set when the input is recognized as HTML; | ||
optTrim = options && options.trim, | ||
optSafe = options && options.safe; | ||
@@ -58,2 +60,10 @@ if (!len) { | ||
var end = text.indexOf('*/', idx + 2); | ||
var keep = optSafe && idx < len - 2 && text[idx + 2] === '!'; | ||
if (keep) { | ||
if (end < 0) { | ||
s += text.substr(idx, len - idx); | ||
} else { | ||
s += text.substr(idx, end - idx + 2); | ||
} | ||
} | ||
if (end < 0) { | ||
@@ -65,6 +75,8 @@ break; | ||
emptyLetters = ''; | ||
var lb = text.indexOf(EOL, idx + 1); | ||
if (lb > idx) { | ||
idx = lb + EOL.length - 1; // last symbol of the line break; | ||
trim(); | ||
if (!keep) { | ||
var lb = text.indexOf(EOL, idx + 1); | ||
if (lb > idx) { | ||
idx = lb + EOL.length - 1; // last symbol of the line break; | ||
trim(); | ||
} | ||
} | ||
@@ -145,3 +157,3 @@ } | ||
function trim() { | ||
if (options && options.trim) { | ||
if (optTrim) { | ||
var startIdx, endIdx, i; | ||
@@ -148,0 +160,0 @@ do { |
{ | ||
"name": "decomment", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "Removes comments from JSON, JavaScript, CSS and HTML.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -36,17 +36,2 @@ decomment | ||
#### API | ||
**decomment(text, [options]) ⇒ String** | ||
Option `trim` is the only one currently supported: | ||
* `false (default)` - use no trimming for comment blocks | ||
* `true` - remove empty lines that follow full-line comments | ||
Example: | ||
```js | ||
var text = "/* comment */\r\n\r\nvar test = 123;"; | ||
decomment(text, {trim: true}); //=> var test = 123; | ||
``` | ||
## Features | ||
@@ -69,2 +54,32 @@ | ||
## API | ||
#### decomment(text, [options]) ⇒ String | ||
##### options ⇒ trim | ||
* `false (default)` - do not trim comments | ||
* `true` - remove empty lines that follow removed full-line comments | ||
Examples: | ||
```js | ||
var text = "/* comment */\r\n\r\n var test = 123"; | ||
decomment(text); //=> \r\n var test = 123 | ||
decomment(text, {trim: true}); //=> var test = 123 | ||
``` | ||
##### options ⇒ safe | ||
* `false (default)` - treat all multi-line comments the same | ||
* `true` - preserve multi-line comments that start with `/*!` | ||
Examples: | ||
```js | ||
var text = "/*! special */ js code /* normal */"; | ||
decomment(text); //=> js code | ||
decomment(text, {safe: true}); //=> /*! special */ js code | ||
``` | ||
This option has no effect when processing HTML. | ||
## License | ||
@@ -71,0 +86,0 @@ |
@@ -127,2 +127,23 @@ 'use strict'; | ||
describe("with safe options", function () { | ||
it("must become empty when safe=false", function () { | ||
expect(decomment("/*!*/")).toBe(""); | ||
expect(decomment("/*!")).toBe(""); | ||
}); | ||
it("must keep comments when safe=true", function () { | ||
expect(decomment("/*!*/", {safe: true})).toBe("/*!*/"); | ||
expect(decomment("/*!", {safe: true})).toBe("/*!"); | ||
}); | ||
}); | ||
describe("combination of options", function () { | ||
it("must process correctly", function () { | ||
expect(decomment("/*!special*/" + LB + LB + "code" + LB + "/*normal*/" + LB + LB + "hello", { | ||
trim: true, | ||
safe: true | ||
})).toBe("/*!special*/" + LB + LB + "code" + LB + "hello"); | ||
}); | ||
}); | ||
}); |
24892
559
87