Comparing version 0.0.5 to 0.1.0
30
index.js
'use strict'; | ||
var os = require('os'); | ||
var EOL = require('os').EOL; // OS-dependent End-of-Line; | ||
@@ -11,13 +11,13 @@ function decomment(text) { | ||
if (!text.length) { | ||
return text; | ||
} | ||
var idx = 0, // current index; | ||
s = '', // resulting text; | ||
len = text.length, // text length; | ||
regExIdx = -1, // first possible regEx index in the current line; | ||
emptyLine = true, // set while no symbols encountered on the current line; | ||
emptyLetters = '', // empty letters on a new line; | ||
regExIdx = -1; // first possible regEx index in the current line; | ||
emptyLetters = ''; // empty letters on a new line; | ||
if (!len) { | ||
return text; | ||
} | ||
do { | ||
@@ -27,3 +27,3 @@ if (text[idx] === '/' && idx < len - 1 && (!idx || text[idx - 1] !== '\\')) { | ||
regExIdx = -1; | ||
var lb = text.indexOf(os.EOL, idx + 1); | ||
var lb = text.indexOf(EOL, idx + 1); | ||
if (lb < 0) { | ||
@@ -34,3 +34,3 @@ break; | ||
emptyLetters = ''; | ||
idx = lb + os.EOL.length - 1; // last symbol of the line break; | ||
idx = lb + EOL.length - 1; // last symbol of the line break; | ||
} else { | ||
@@ -50,5 +50,5 @@ idx = lb - 1; // just before the line break; | ||
emptyLetters = ''; | ||
var lb = text.indexOf(os.EOL, idx + 1); | ||
var lb = text.indexOf(EOL, idx + 1); | ||
if (lb > idx) { | ||
idx = lb + os.EOL.length - 1; // last symbol of the line break; | ||
idx = lb + EOL.length - 1; // last symbol of the line break; | ||
} | ||
@@ -64,3 +64,3 @@ } | ||
if (symbol === '\r' || symbol === '\n') { | ||
if (text.indexOf(os.EOL, idx) === idx) { | ||
if (text.indexOf(EOL, idx) === idx) { | ||
emptyLine = true; | ||
@@ -101,9 +101,9 @@ regExIdx = -1; | ||
if (regExIdx >= 0) { | ||
var lb = text.indexOf(os.EOL, regExIdx + 1); | ||
var lb = text.indexOf(EOL, regExIdx + 1); | ||
var line = text.substr(regExIdx, lb < 0 ? (len - regExIdx) : (lb - regExIdx)); | ||
var startIdx = idx - regExIdx + 1; // local index that follows the symbol; | ||
var startIdx = idx - regExIdx + 1; | ||
var l = line.length, nextIdx = startIdx; | ||
do { | ||
if (line[nextIdx] === '/' && (nextIdx === l - 1 || line[nextIdx + 1] !== '/') && (nextIdx === startIdx || line[nextIdx - 1] !== '\\')) { | ||
return true; // regEx closure found; | ||
return true; | ||
} | ||
@@ -110,0 +110,0 @@ } while (++nextIdx < l); |
{ | ||
"name": "decomment", | ||
"version": "0.0.5", | ||
"version": "0.1.0", | ||
"description": "Removes comments from JSON or JavaScript.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -38,10 +38,10 @@ decomment | ||
* Removes both single-line and multi-line comments | ||
* 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 | ||
* Compliant with ECMAScript 6 | ||
In terms of the performance, this library is as fast as it gets, | ||
in part because it makes no use of regular expressions. | ||
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 the [source of AngularJS 1.5 Core](https://code.angularjs.org/1.5.0-rc.0/angular.js) (1.1MB of JavaScript) in under 40ms. | ||
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. |
@@ -23,2 +23,8 @@ 'use strict'; | ||
describe("mixed comments", function () { | ||
it("must be removed", function () { | ||
expect(decomment("//one" + LB + "/*two*/" + LB + "//three")).toBe(""); | ||
}); | ||
}); | ||
}); |
@@ -116,6 +116,6 @@ 'use strict'; | ||
describe("unfinished comment", function () { | ||
it("must assume the comment till the end", function () { | ||
expect(decomment("/*")).toBe(""); // Cutting invalid JavaScript; | ||
it("must cut it all off", function () { | ||
expect(decomment("/*")).toBe(""); | ||
}); | ||
}); | ||
}); |
17917
416