Comparing version 0.4.0 to 0.4.1
20
index.js
@@ -22,4 +22,4 @@ 'use strict'; | ||
isHtml = false, // set when the input is recognized as HTML; | ||
optTrim = options && options.trim, | ||
optSafe = options && options.safe; | ||
optTrim = options && options.trim, // 'trim' option; | ||
optSafe = options && options.safe; // 'safe' option. | ||
@@ -147,3 +147,17 @@ if (!len) { | ||
if (line[nextIdx] === '/' && (nextIdx === l - 1 || line[nextIdx + 1] !== '/') && (nextIdx === startIdx || line[nextIdx - 1] !== '\\')) { | ||
return true; | ||
if (nextIdx === startIdx || line[nextIdx - 1] === '\\') { | ||
var lineStartLB = text.lastIndexOf(EOL, regExIdx), searchIdx = regExIdx; | ||
lineStartLB = lineStartLB < 0 ? 0 : lineStartLB + EOL.length; | ||
while (--searchIdx >= lineStartLB && (text[searchIdx] === ' ' || text[searchIdx] === '\t')); | ||
if (searchIdx < lineStartLB) { | ||
return true; // nothing precedes '/' on the line, so it is a valid regEx; | ||
} | ||
var prs = ['=', ';', '(', '{', '[']; // symbols that can precede a valid regEx; | ||
for (var w = 0; w < prs.length; w++) { | ||
if (prs[w] === text[searchIdx]) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
@@ -150,0 +164,0 @@ } while (++nextIdx < l); |
{ | ||
"name": "decomment", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "Removes comments from JSON, JavaScript, CSS and HTML.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
decomment | ||
=========== | ||
========= | ||
@@ -57,3 +57,4 @@ Removes comments from JSON, JavaScript, CSS and HTML. | ||
##### options ⇒ trim | ||
##### options.trim ⇒ Boolean | ||
* `false (default)` - do not trim comments | ||
@@ -70,5 +71,5 @@ * `true` - remove empty lines that follow removed full-line comments | ||
##### options ⇒ safe | ||
* `false (default)` - treat all multi-line comments the same | ||
* `true` - preserve multi-line comments that start with `/*!` | ||
##### options.safe ⇒ Boolean | ||
* `false (default)` - remove all multi-line comments | ||
* `true` - keep multi-line comments that start with `/*!` | ||
@@ -87,3 +88,3 @@ Examples: | ||
Copyright © 2015 [Vitaly Tomilov](https://github.com/vitaly-t) | ||
Copyright © 2015 [Vitaly Tomilov](https://github.com/vitaly-t); | ||
Released under the MIT license. |
@@ -12,5 +12,5 @@ 'use strict'; | ||
describe("empty comment", function () { | ||
var out = decomment("/**/"); | ||
it("must return an empty string", function () { | ||
expect(out).toBe(""); | ||
expect(decomment("/**/")).toBe(""); | ||
expect(decomment("\/**\/")).toBe(""); | ||
}); | ||
@@ -143,8 +143,8 @@ }); | ||
it("must process correctly", function () { | ||
expect(decomment("/*!special*/" + LB + LB + "code" + LB + "/*normal*/" + LB + LB + "hello", { | ||
expect(decomment("/*!special*/" + LB + LB + "code" + LB + "/*normal*/" + LB + LB + "hello" + LB, { | ||
trim: true, | ||
safe: true | ||
})).toBe("/*!special*/" + LB + LB + "code" + LB + "hello"); | ||
})).toBe("/*!special*/" + LB + LB + "code" + LB + "hello" + LB); | ||
}); | ||
}); | ||
}); |
@@ -16,2 +16,3 @@ 'use strict'; | ||
expect(decomment("/\'/")).toBe("/'/"); | ||
expect(decomment("/'\/text")).toBe("/'\/text"); | ||
}); | ||
@@ -25,2 +26,4 @@ }); | ||
expect(decomment('/\"/')).toBe('/"/'); | ||
expect(decomment('/"\/text')).toBe('/"\/text'); | ||
expect(decomment('/"\/\/text')).toBe('/"'); | ||
}); | ||
@@ -64,2 +67,30 @@ }); | ||
}); | ||
describe("comments inside text, between dividers", function () { | ||
it("must ignore the comment", function () { | ||
expect(decomment("func(1 * 2, '//', 3 / 4)")).toBe("func(1 * 2, '//', 3 / 4)"); | ||
expect(decomment("func(1 / 2, '//', 3 * 4)")).toBe("func(1 / 2, '//', 3 * 4)"); | ||
expect(decomment("func(1 * 2, '/text/', 3 / 4)")).toBe("func(1 * 2, '/text/', 3 / 4)"); | ||
expect(decomment("func(1 * 2, '/some\/text/', 3 / 4)")).toBe("func(1 * 2, '/some/text/', 3 / 4)"); | ||
expect(decomment("func(1 / 2, '/text/', 3 / 4)")).toBe("func(1 / 2, '/text/', 3 / 4)"); | ||
expect(decomment("func(1 / 2, '/some\/text//', 3 / 4)")).toBe("func(1 / 2, '/some/text//', 3 / 4)"); | ||
expect(decomment("func(1 / 2, '/**/', 3 * 4)")).toBe("func(1 / 2, '/**/', 3 * 4)"); | ||
expect(decomment("func(1 / 2, '/**/', 3 / 4)")).toBe("func(1 / 2, '/**/', 3 / 4)"); | ||
expect(decomment("a/'/*text*/'")).toBe("a/'/*text*/'"); | ||
// Warning: below is a synthetic test, because it is not a valid JavaScript; | ||
expect(decomment("/ 2, '/\/*/'")).toBe("/ 2, '//*/'"); | ||
}); | ||
}); | ||
describe("valid regular expressions", function () { | ||
it("must repent any content", function () { | ||
expect(decomment("=/'/")).toBe("=/'/"); | ||
expect(decomment(LB + "=/'/")).toBe(LB + "=/'/"); | ||
}); | ||
}); | ||
}); |
@@ -12,5 +12,5 @@ 'use strict'; | ||
describe("empty comment", function () { | ||
var out = decomment("//"); | ||
it("must return an empty string", function () { | ||
expect(out).toBe(""); | ||
expect(decomment("//")).toBe(""); | ||
expect(decomment("/\/text")).toBe(""); // '/\/' = '//' | ||
}); | ||
@@ -17,0 +17,0 @@ }); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
27563
597
88