Comparing version 0.1.1 to 0.2.0
38
index.js
@@ -16,3 +16,4 @@ 'use strict'; | ||
emptyLine = true, // set while no symbols encountered on the current line; | ||
emptyLetters = ''; // empty letters on a new line; | ||
emptyLetters = '', // empty letters on a new line; | ||
isHtml = false; // set when the input is recognized as HTML. | ||
@@ -23,7 +24,17 @@ if (!len) { | ||
var tag; | ||
do { | ||
if (text[idx] === '/' && idx < len - 1 && (!idx || text[idx - 1] !== '\\')) { | ||
tag = text[idx]; | ||
if (tag !== ' ' && tag !== '\t' && tag !== '\r' && tag !== '\n') { | ||
isHtml = tag === '<'; | ||
break; | ||
} | ||
} while (++idx < len); | ||
idx = 0; | ||
do { | ||
if (!isHtml && text[idx] === '/' && idx < len - 1 && (!idx || text[idx - 1] !== '\\')) { | ||
if (text[idx + 1] === '/') { | ||
regExIdx = -1; | ||
var lb = text.indexOf(EOL, idx + 1); | ||
var lb = text.indexOf(EOL, idx + 2); | ||
if (lb < 0) { | ||
@@ -42,3 +53,3 @@ break; | ||
regExIdx = -1; | ||
var end = text.indexOf('*/', idx + 1); | ||
var end = text.indexOf('*/', idx + 2); | ||
if (end < 0) { | ||
@@ -60,2 +71,19 @@ break; | ||
if (isHtml && text[idx] === '<' && idx < len - 3 && text.substr(idx + 1, 3) === '!--') { | ||
regExIdx = -1; | ||
var end = text.indexOf('-->', idx + 4); | ||
if (end < 0) { | ||
break; | ||
} | ||
idx = end + 2; | ||
if (emptyLine) { | ||
emptyLetters = ''; | ||
var lb = text.indexOf(EOL, idx + 1); | ||
if (lb > idx) { | ||
idx = lb + EOL.length - 1; // last symbol of the line break; | ||
} | ||
} | ||
continue; | ||
} | ||
var symbol = text[idx]; | ||
@@ -81,3 +109,3 @@ var isSpace = symbol === ' ' || symbol === '\t'; | ||
if (symbol === '\'' || symbol === '"' || symbol === '`') { | ||
if (!isHtml && (symbol === '\'' || symbol === '"' || symbol === '`')) { | ||
var closeIdx = idx; | ||
@@ -84,0 +112,0 @@ do { |
{ | ||
"name": "decomment", | ||
"version": "0.1.1", | ||
"description": "Removes comments from JSON or JavaScript.", | ||
"version": "0.2.0", | ||
"description": "Removes comments from JSON, JavaScript, CSS and HTML.", | ||
"main": "index.js", | ||
@@ -27,3 +27,5 @@ "scripts": { | ||
"JavaScript", | ||
"JSON" | ||
"JSON", | ||
"CSS", | ||
"HTML" | ||
], | ||
@@ -30,0 +32,0 @@ "author": { |
decomment | ||
=========== | ||
Removes comments from JSON or JavaScript. | ||
Removes comments from JSON, JavaScript, CSS and HTML. | ||
@@ -31,3 +31,3 @@ [![Build Status](https://travis-ci.org/vitaly-t/decomment.svg?branch=master)](https://travis-ci.org/vitaly-t/decomment) | ||
var code = "var t; // comments"; // any valid JSON or JavaScript | ||
var code = "var t; // comments"; | ||
@@ -39,10 +39,16 @@ console.log(decomment(code)); //=> var t; | ||
* Removes both single and multi-line comments | ||
* Removes unnecessary gaps left by comment blocks | ||
* Does not change the resulting layout / formatting | ||
* Can handle valid JSON or JavaScript of any size | ||
* Removes both single and multi-line comments from JSON, JavaScript and CSS | ||
* Automatically recognizes HTML and removes all `<!-- comments -->` from it | ||
* Removes unnecessary gaps on empty lines and the ones left by comment blocks | ||
* Does not change layout / formatting of the original document | ||
* Can handle input code of any size | ||
* Compliant with ECMAScript 6 | ||
The library does not support mixed content - HTML with JavaScript or CSS in it. | ||
Once the input code is recognized as HTML, only the HTML comments will be removed from it. | ||
### Performance | ||
In terms of the performance, this library is as fast as it gets, in part because it makes no use of regular expressions. | ||
For example, it churns through [AngularJS 1.5 Core](https://code.angularjs.org/1.5.0-rc.0/angular.js) (1.1MB ~ 30,000 lines of JavaScript) in under 40ms. |
@@ -43,6 +43,7 @@ 'use strict'; | ||
describe("with line-break prefix", function () { | ||
var out = decomment(LB + "//"); | ||
it("must return the break", function () { | ||
expect(out).toBe(LB); | ||
describe("with a prefix", function () { | ||
it("must return the prefix", function () { | ||
expect(decomment(LB + "//")).toBe(LB); | ||
// spaces and tabs are removed from empty lines: | ||
expect(decomment(" \t \t")).toBe(""); | ||
}); | ||
@@ -49,0 +50,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
20250
9
469
53