Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

decomment

Package Overview
Dependencies
Maintainers
1
Versions
44
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

decomment - npm Package Compare versions

Comparing version 0.8.8 to 0.9.0

4

lib/index.js
'use strict';
var parser = require('./parser');
var utils = require('./utils');
const parser = require('./parser');
const utils = require('./utils');

@@ -6,0 +6,0 @@ function main(code, options) {

'use strict';
var utils = require('./utils');
const utils = require('./utils');

@@ -15,11 +15,12 @@ function parser(code, options, config) {

var idx = 0, // current index;
const len = code.length, // code length;
optSafe = options && options.safe, // 'safe' option;
optSpace = options && options.space, // 'space' option;
optTrim = options && options.trim, // 'trim' option;
EOL = utils.getEOL(code); // get EOL from the code;
let idx = 0, // current index;
s = '', // resulting code;
len = code.length, // code length;
emptyLine = true, // set while no symbols encountered on the current line;
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;
EOL = utils.getEOL(code), // get EOL from the code;
isHtml, // set when the input is recognized as HTML;

@@ -42,3 +43,3 @@ regEx = []; // regular expression details;

if (options && options.ignore) {
var ignore = options.ignore;
let ignore = options.ignore;
if (ignore instanceof RegExp) {

@@ -48,5 +49,3 @@ ignore = [ignore];

if (ignore instanceof Array) {
ignore = ignore.filter(function (f) {
return f instanceof RegExp;
});
ignore = ignore.filter(f => f instanceof RegExp);
if (!ignore.length) {

@@ -60,4 +59,5 @@ ignore = null;

if (ignore) {
for (var i = 0; i < ignore.length; i++) {
var match, reg = ignore[i];
for (let i = 0; i < ignore.length; i++) {
const reg = ignore[i];
let match;
do {

@@ -73,5 +73,3 @@ match = reg.exec(code);

}
regEx = regEx.sort(function (a, b) {
return a.start - b.start;
});
regEx = regEx.sort((a, b) => a.start - b.start);
}

@@ -91,3 +89,3 @@ }

}
var lb1 = code.indexOf(EOL, idx + 2);
const lb1 = code.indexOf(EOL, idx + 2);
if (lb1 < 0) {

@@ -118,4 +116,4 @@ break;

}
var end1 = code.indexOf('*/', idx + 2);
var keep1 = optSafe && idx < len - 2 && code[idx + 2] === '!';
const end1 = code.indexOf('*/', idx + 2),
keep1 = optSafe && idx < len - 2 && code[idx + 2] === '!';
if (keep1) {

@@ -131,3 +129,3 @@ if (end1 >= 0) {

}
var comment1 = code.substr(idx, end1 - idx + 2);
const comment1 = code.substr(idx, end1 - idx + 2);
idx = end1 + 1;

@@ -138,11 +136,11 @@ if (emptyLine) {

if (!keep1) {
var parts1 = comment1.split(EOL);
const parts1 = comment1.split(EOL);
if (optSpace) {
for (var k1 = 0; k1 < parts1.length - 1; k1++) {
for (let k1 = 0; k1 < parts1.length - 1; k1++) {
s += EOL;
}
}
var lb2 = code.indexOf(EOL, idx + 1);
const lb2 = code.indexOf(EOL, idx + 1);
if (lb2 > idx) {
var gapIdx1 = lb2 - 1;
let gapIdx1 = lb2 - 1;
while ((code[gapIdx1] === ' ' || code[gapIdx1] === '\t') && --gapIdx1 > idx) ;

@@ -161,3 +159,3 @@ if (gapIdx1 === idx) {

if (optSpace) {
var gapIdx2 = idx + 1;
let gapIdx2 = idx + 1;
while ((code[gapIdx2] === ' ' || code[gapIdx2] === '\t') && ++gapIdx2 < len) ;

@@ -183,4 +181,4 @@ if (gapIdx2 < len) {

}
var end2 = code.indexOf('-->', idx + 4);
var keep2 = optSafe && code.substr(idx + 4, 3) === '[if';
const end2 = code.indexOf('-->', idx + 4),
keep2 = optSafe && code.substr(idx + 4, 3) === '[if';
if (keep2) {

@@ -196,3 +194,3 @@ if (end2 >= 0) {

}
var comment2 = code.substr(idx, end2 - idx + 3);
const comment2 = code.substr(idx, end2 - idx + 3);
idx = end2 + 2;

@@ -203,11 +201,11 @@ if (emptyLine) {

if (!keep2) {
var parts2 = comment2.split(EOL);
const parts2 = comment2.split(EOL);
if (optSpace) {
for (var k2 = 0; k2 < parts2.length - 1; k2++) {
for (let k2 = 0; k2 < parts2.length - 1; k2++) {
s += EOL;
}
}
var lb3 = code.indexOf(EOL, idx + 1);
const lb3 = code.indexOf(EOL, idx + 1);
if (lb3 > idx) {
var gapIdx3 = lb3 - 1;
let gapIdx3 = lb3 - 1;
while ((code[gapIdx3] === ' ' || code[gapIdx3] === '\t') && --gapIdx3 > idx) ;

@@ -226,3 +224,3 @@ if (gapIdx3 === idx) {

if (optSpace) {
var gapIdx4 = idx + 1;
let gapIdx4 = idx + 1;
while ((code[gapIdx4] === ' ' || code[gapIdx4] === '\t') && ++gapIdx4 < len) ;

@@ -238,4 +236,4 @@ if (gapIdx4 < len) {

var symbol = code[idx];
var isSpace = symbol === ' ' || symbol === '\t';
const symbol = code[idx],
isSpace = symbol === ' ' || symbol === '\t';
if (symbol === '\r' || symbol === '\n') {

@@ -262,7 +260,7 @@ if (code.indexOf(EOL, idx) === idx) {

}
var closeIdx = idx;
let closeIdx = idx;
do {
closeIdx = code.indexOf(symbol, closeIdx + 1);
if (closeIdx > 0) {
var shIdx = closeIdx;
let shIdx = closeIdx;
while (code[--shIdx] === '\\') ;

@@ -291,3 +289,3 @@ if ((closeIdx - shIdx) % 2) {

if (optTrim) {
var startIdx, endIdx, i;
let startIdx, endIdx, i;
do {

@@ -294,0 +292,0 @@ startIdx = idx + 1;

'use strict';
var esprima = require('esprima');
var os = require('os');
const esprima = require('esprima');
const os = require('os');

@@ -10,3 +10,3 @@ ////////////////////////////////////////////////////

function getEOL(text) {
var idx = 0, unix = 0, windows = 0;
let idx = 0, unix = 0, windows = 0;
while (idx < text.length) {

@@ -35,6 +35,6 @@ idx = text.indexOf('\n', idx);

function parseRegEx(code) {
var result = [];
const result = [];
// NOTE: Even though we do not need the location details,
// using option `loc` makes `tokenize` perform 40% faster.
esprima.tokenize(code, {loc: true, range: true}, function (node) {
esprima.tokenize(code, {loc: true, range: true}, node => {
if (node.type === 'RegularExpression') {

@@ -55,6 +55,6 @@ result.push({

// regExData - the output provided by parseRegEx;
var mid, low = 0, high = regExData.length - 1;
let mid, low = 0, high = regExData.length - 1;
while (low <= high) {
mid = Math.round((low + high) / 2);
var a = regExData[mid];
const a = regExData[mid];
if (idx >= a.start) {

@@ -76,3 +76,3 @@ if (idx <= a.end) {

function isHtml(text) {
var s, idx = 0;
let s, idx = 0;
do {

@@ -89,10 +89,4 @@ s = text[idx];

//
// 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);
}
return new Array(n + 1).join(' ');
return ' '.repeat(n);
}

@@ -99,0 +93,0 @@

{
"name": "decomment",
"version": "0.8.8",
"version": "0.9.0",
"description": "Removes comments from JSON/JavaScript, CSS/HTML, CPP/H, etc.",

@@ -9,3 +9,3 @@ "main": "lib/index.js",

"coverage": "istanbul cover ./node_modules/jasmine-node/bin/jasmine-node test",
"travis": "istanbul cover ./node_modules/jasmine-node/bin/jasmine-node test --captureExceptions && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage",
"travis": "npm run lint && istanbul cover ./node_modules/jasmine-node/bin/jasmine-node test --captureExceptions && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage",
"lint": "./node_modules/.bin/eslint ./lib ./test"

@@ -42,4 +42,4 @@ },

"engines": {
"node": ">=0.10",
"npm": ">=1.4"
"node": ">=4.0",
"npm": ">=2.15"
},

@@ -46,0 +46,0 @@ "dependencies": {

@@ -5,3 +5,3 @@ 'use strict';

var decomment = require('../lib');
const decomment = require('../lib');

@@ -8,0 +8,0 @@ describe('Entry:', function () {

@@ -5,5 +5,5 @@ 'use strict';

var decomment = require('../lib');
var os = require('os');
var LB = os.EOL;
const decomment = require('../lib');
const os = require('os');
const LB = os.EOL;

@@ -10,0 +10,0 @@ describe('HTML:', function () {

'use strict';
/* eslint-disable */
// Tests for 'ignore' option;
var decomment = require('../lib');
var os = require('os');
var LB = os.EOL;
const decomment = require('../lib');
const os = require('os');
const LB = os.EOL;
describe("Ignore:", function () {
describe('Ignore:', function () {
describe("invalid values", function () {
expect(decomment("text", {ignore: true})).toBe("text");
expect(decomment("text", {ignore: []})).toBe("text");
expect(decomment("text", {ignore: [1, 2]})).toBe("text");
describe('invalid values', function () {
expect(decomment('text', {ignore: true})).toBe('text');
expect(decomment('text', {ignore: []})).toBe('text');
expect(decomment('text', {ignore: [1, 2]})).toBe('text');
});
describe("single-line", function () {
describe("one occurrence", function () {
expect(decomment("remove(//text)", {ignore: /remove\(/})).toBe("remove(");
expect(decomment("skip(//text)", {ignore: [/skip\([/\w]*\)/]})).toBe("skip(//text)");
describe('single-line', function () {
describe('one occurrence', function () {
expect(decomment('remove(//text)', {ignore: /remove\(/})).toBe('remove(');
expect(decomment('skip(//text)', {ignore: [/skip\([/\w]*\)/]})).toBe('skip(//text)');
});
describe("multi-occurrence", function () {
expect(decomment("remove(//some), remove(//long), remove(//text)", {ignore: /remove\(/g})).toBe("remove(");
expect(decomment("skip(//some), skip(//long), skip(//text)", {ignore: /skip\([/\w]*\)/g})).toBe("skip(//some), skip(//long), skip(//text)");
describe('multi-occurrence', function () {
expect(decomment('remove(//some), remove(//long), remove(//text)', {ignore: /remove\(/g})).toBe('remove(');
expect(decomment('skip(//some), skip(//long), skip(//text)', {ignore: /skip\([/\w]*\)/g})).toBe('skip(//some), skip(//long), skip(//text)');
});
});
describe("CSS url", function () {
describe('CSS url', function () {
var input =

@@ -43,6 +41,6 @@ '@font-face {' + LB +

expect(decomment.text(input, {ignore: /url\([\w\s:\/=\-\+;,]*\)/g})).toBe(output);
expect(decomment.text(input, {ignore: /url\([\w\s:/=\-+;,]*\)/g})).toBe(output);
});
describe("HTML", function () {
describe('HTML', function () {
var input =

@@ -59,6 +57,6 @@ '<!--keep' + LB +

expect(decomment.html(input, {ignore: /<!--keep[\w\s]*-->/g})).toBe(output);
expect(decomment.html(" <!--keep-->", {ignore: /<!--keep[\w\s]*-->/g})).toBe(" <!--keep-->");
expect(decomment.html(' <!--keep-->', {ignore: /<!--keep[\w\s]*-->/g})).toBe(' <!--keep-->');
});
describe("jsDoc", function () {
describe('jsDoc', function () {
var input =

@@ -75,13 +73,13 @@ '/* remove this */' + LB +

var regEx = /\/\*\*\s*\n([^\*]*(\*[^\/])?)*\*\//g;
var regEx = /\/\*\*\s*\n([^*]*(\*[^/])?)*\*\//g;
expect(decomment(input, {ignore: regEx})).toBe(output);
expect(decomment(" /**" + LB + "text */", {ignore: regEx})).toBe(" /**" + LB + "text */");
expect(decomment(' /**' + LB + 'text */', {ignore: regEx})).toBe(' /**' + LB + 'text */');
});
describe("one-line", function () {
expect(decomment("//comment", {ignore: /\/\/\w*/})).toBe("//comment");
expect(decomment(" //comment", {ignore: /\/\/\w*/})).toBe(" //comment");
describe('one-line', function () {
expect(decomment('//comment', {ignore: /\/\/\w*/})).toBe('//comment');
expect(decomment(' //comment', {ignore: /\/\/\w*/})).toBe(' //comment');
});
});
'use strict';
/* eslint-disable */
// Tests for combinations of single + multi-line comments;
var decomment = require('../lib');
var os = require('os');
var LB = os.EOL;
const decomment = require('../lib');
const os = require('os');
const LB = os.EOL;
describe("Mixed:", function () {
describe('Mixed:', function () {
describe("special slash cases", function () {
it("must be ignored", function () {
expect(decomment("'\f'")).toBe("'\f'");
describe('special slash cases', function () {
it('must be ignored', function () {
expect(decomment('\'\f\'')).toBe('\'\f\'');
expect(decomment("'\\''")).toBe("'\\''");
expect(decomment('\'\\\'\'')).toBe('\'\\\'\'');
expect(decomment('"\\""')).toBe('"\\""');
expect(decomment('`\\``')).toBe('`\\``');
expect(decomment("'\\'\\''")).toBe("'\\'\\''");
expect(decomment('\'\\\'\\\'\'')).toBe('\'\\\'\\\'\'');
expect(decomment('"\\"\\""')).toBe('"\\"\\""');
expect(decomment('`\\`\\``')).toBe('`\\`\\``');
expect(decomment("'\\\\'")).toBe("'\\\\'");
expect(decomment('\'\\\\\'')).toBe('\'\\\\\'');
expect(decomment('"\\\\"')).toBe('"\\\\"');
expect(decomment('`\\\\`')).toBe('`\\\\`');
expect(decomment("'\\\\\\''")).toBe("'\\\\\\''");
expect(decomment("'\\\\\\'';")).toBe("'\\\\\\'';");
expect(decomment('\'\\\\\\\'\'')).toBe('\'\\\\\\\'\'');
expect(decomment('\'\\\\\\\'\';')).toBe('\'\\\\\\\'\';');

@@ -41,33 +39,33 @@ expect(decomment('"\\\\\\""')).toBe('"\\\\\\""');

describe("single-line gaps", function () {
it("must be removed", function () {
expect(decomment("//one" + LB + "//two" + LB + "//three")).toBe("");
describe('single-line gaps', function () {
it('must be removed', function () {
expect(decomment('//one' + LB + '//two' + LB + '//three')).toBe('');
});
});
describe("multi-line gaps", function () {
it("must be removed", function () {
expect(decomment("/*one*/" + LB + "/*two*/" + LB + "/*three*/")).toBe("");
describe('multi-line gaps', function () {
it('must be removed', function () {
expect(decomment('/*one*/' + LB + '/*two*/' + LB + '/*three*/')).toBe('');
});
});
describe("mixed comments", function () {
it("must be removed", function () {
expect(decomment("//one" + LB + "/*two*/" + LB + "//three")).toBe("");
describe('mixed comments', function () {
it('must be removed', function () {
expect(decomment('//one' + LB + '/*two*/' + LB + '//three')).toBe('');
});
});
describe("Automatic EOL", function () {
describe('Automatic EOL', function () {
it("must always ignore solo \\r symbols", function () {
expect(decomment("//comment\r\rtext", {trim: true})).toBe("");
expect(decomment("/*comment*/\r\rtext", {trim: true})).toBe("\r\rtext");
it('must always ignore solo \\r symbols', function () {
expect(decomment('//comment\r\rtext', {trim: true})).toBe('');
expect(decomment('/*comment*/\r\rtext', {trim: true})).toBe('\r\rtext');
});
it("must determine Unix and break on \\n", function () {
expect(decomment("//comment\n\ntext", {trim: true})).toBe("text");
expect(decomment("/*comment*/\n\ntext", {trim: true})).toBe("text");
it('must determine Unix and break on \\n', function () {
expect(decomment('//comment\n\ntext', {trim: true})).toBe('text');
expect(decomment('/*comment*/\n\ntext', {trim: true})).toBe('text');
});
it("must determine Windows and break on \\r\\n", function () {
expect(decomment("//comment\r\n\r\ntext", {trim: true})).toBe("text");
expect(decomment("/*comment*/\r\n\r\ntext", {trim: true})).toBe("text");
it('must determine Windows and break on \\r\\n', function () {
expect(decomment('//comment\r\n\r\ntext', {trim: true})).toBe('text');
expect(decomment('/*comment*/\r\n\r\ntext', {trim: true})).toBe('text');
});

@@ -74,0 +72,0 @@

'use strict';
/* eslint-disable */
// Tests for multi-line comments;
var decomment = require('../lib');
var os = require('os');
var LB = os.EOL;
const decomment = require('../lib');
const os = require('os');
const LB = os.EOL;
describe("Multi:", function () {
describe('Multi:', function () {
describe("empty comment, space=false", function () {
it("must return an empty string", function () {
expect(decomment("/**/")).toBe("");
expect(decomment("\/**\/")).toBe("");
describe('empty comment, space=false', function () {
it('must return an empty string', function () {
expect(decomment('/**/')).toBe('');
expect(decomment('/**/')).toBe('');
});
});
describe("empty comment, space=true", function () {
it("must return spaces where needed", function () {
expect(decomment("/**/", {space: true})).toBe("");
expect(decomment("\/**\/")).toBe("");
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("");
describe('multiple empty comments, space=false', function () {
it('must return an empty string', function () {
expect(decomment('/**/' + LB + '/**/')).toBe('');
expect(decomment('/**/' + LB + '/**/' + 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 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('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("");
describe('non-empty comment', function () {
it('must return an empty string', function () {
expect(decomment('/*text*/')).toBe('');
});
});
describe("non-empty multiple comments", function () {
it("must return an empty string", function () {
expect(decomment("/* text1 */" + LB + "/*text2*/")).toBe("");
expect(decomment("/* text1 */" + LB + "/*text2*/" + LB)).toBe("");
describe('non-empty multiple comments', function () {
it('must return an empty string', function () {
expect(decomment('/* text1 */' + LB + '/*text2*/')).toBe('');
expect(decomment('/* text1 */' + LB + '/*text2*/' + LB)).toBe('');
});
});
describe("with line-break prefix", function () {
it("must return the break", function () {
expect(decomment(LB + "/**/")).toBe(LB);
describe('with line-break prefix', function () {
it('must return the break', function () {
expect(decomment(LB + '/**/')).toBe(LB);
});
});
describe("with line-break suffix", function () {
it("must return an empty string", function () {
expect(decomment("/**/" + LB)).toBe("");
describe('with line-break suffix', function () {
it('must return an empty string', function () {
expect(decomment('/**/' + LB)).toBe('');
});
});
describe("with multiple line-break suffixes", function () {
it("must return a single line break", function () {
expect(decomment("/**/" + LB + LB)).toBe(LB);
describe('with multiple line-break suffixes', function () {
it('must return a single line break', function () {
expect(decomment('/**/' + LB + LB)).toBe(LB);
});
});
describe("with preceding text, space=false", function () {
it("must return the preceding text", function () {
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=false', function () {
it('must return the preceding text', function () {
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 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 () {
var out1 = decomment("''/**/");
var out2 = decomment("\"\"/**/");
var out3 = decomment("``/**/");
it("must leave only the comment", function () {
expect(out1).toBe("''");
expect(out2).toBe("\"\"");
expect(out3).toBe("``");
describe('with empty text prefix', function () {
var out1 = decomment('\'\'/**/');
var out2 = decomment('""/**/');
var out3 = decomment('``/**/');
it('must leave only the comment', function () {
expect(out1).toBe('\'\'');
expect(out2).toBe('""');
expect(out3).toBe('``');
});
});
describe("with empty text suffix", function () {
var out1 = decomment("/**/" + LB + "''");
var out2 = decomment("/**/" + LB + "\"\"");
var out3 = decomment("/**/" + LB + "``");
it("must leave only the comment", function () {
expect(out1).toBe("''");
expect(out2).toBe("\"\"");
expect(out3).toBe("``");
describe('with empty text suffix', function () {
var out1 = decomment('/**/' + LB + '\'\'');
var out2 = decomment('/**/' + LB + '""');
var out3 = decomment('/**/' + LB + '``');
it('must leave only the comment', function () {
expect(out1).toBe('\'\'');
expect(out2).toBe('""');
expect(out3).toBe('``');
});
});
describe("comments inside text", function () {
it("must leave only the comment", function () {
expect(decomment("'/**/text'")).toBe("'/**/text'");
describe('comments inside text', function () {
it('must leave only the comment', function () {
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=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 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('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 () {
describe("complex case", function () {
var out = decomment("a /* comment*/" + LB + "\tb /* comment*/" + LB + "c/*end*/");
it("must keep spaces correctly", function () {
expect(out).toBe("a " + LB + "\tb " + LB + "c");
describe('spaces', function () {
describe('complex case', function () {
var out = decomment('a /* comment*/' + LB + '\tb /* comment*/' + LB + 'c/*end*/');
it('must keep spaces correctly', function () {
expect(out).toBe('a ' + LB + '\tb ' + LB + 'c');
});

@@ -159,50 +157,50 @@ });

describe("multiple line breaks that follow", function () {
it("must be removed", function () {
expect(decomment("/*text*/" + LB + LB + "end", {trim: true})).toBe("end");
describe('multiple line breaks that follow', function () {
it('must be removed', function () {
expect(decomment('/*text*/' + LB + LB + 'end', {trim: true})).toBe('end');
});
});
describe("with safe options", function () {
describe('with safe options', function () {
it("must become empty when safe=false", function () {
expect(decomment("/*!*/")).toBe("");
it('must become empty when safe=false', function () {
expect(decomment('/*!*/')).toBe('');
});
it("must keep comments when safe=true", function () {
expect(decomment("/*!*/", {safe: true})).toBe("/*!*/");
it('must keep comments when safe=true', function () {
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" + LB, {
describe('combination of options', function () {
it('must process correctly', function () {
expect(decomment('/*!special*/' + LB + LB + 'code' + LB + '/*normal*/' + LB + LB + 'hello' + LB, {
trim: true,
safe: true
})).toBe("/*!special*/" + LB + LB + "code" + LB + "hello" + LB);
})).toBe('/*!special*/' + LB + LB + 'code' + LB + 'hello' + LB);
});
});
describe("inside regEx", function () {
it("must be ignored", function () {
expect(decomment("/[a-b/*]text/")).toBe("/[a-b/*]text/");
describe('inside regEx', function () {
it('must be ignored', function () {
expect(decomment('/[a-b/*]text/')).toBe('/[a-b/*]text/');
});
});
describe("across lines, with space=false", function () {
it("must delete all lines", function () {
expect(decomment("/*start" + LB + "middle" + LB + "end*/")).toBe("");
expect(decomment("/*start" + LB + "middle" + LB + "end*/text")).toBe("text");
expect(decomment("prefix-/*start" + LB + "middle" + LB + "end*/suffix")).toBe("prefix-suffix");
describe('across lines, with space=false', function () {
it('must delete all lines', function () {
expect(decomment('/*start' + LB + 'middle' + LB + 'end*/')).toBe('');
expect(decomment('/*start' + LB + 'middle' + LB + 'end*/text')).toBe('text');
expect(decomment('prefix-/*start' + LB + 'middle' + LB + 'end*/suffix')).toBe('prefix-suffix');
});
});
describe("across lines, with space=true", function () {
it("must replace deleted lines with line break", function () {
expect(decomment("prefix/*comment*/suffix", {space: true})).toBe("prefix suffix");
expect(decomment("/*start" + LB + "middle" + LB + "end*/text" + LB, {space: true})).toBe(LB + LB + " text" + LB);
expect(decomment("/*start" + LB + "middle" + LB + "end*/\ttext", {space: true})).toBe(LB + LB + " \ttext");
expect(decomment("/*start" + LB + "middle" + LB + "end*/", {space: true})).toBe(LB + LB);
expect(decomment("/*start" + LB + "middle" + LB + "end*/text", {space: true})).toBe(LB + LB + " text");
expect(decomment("prefix/*start" + LB + "middle" + LB + "end*/suffix", {space: true})).toBe("prefix" + LB + LB + " suffix");
describe('across lines, with space=true', function () {
it('must replace deleted lines with line break', function () {
expect(decomment('prefix/*comment*/suffix', {space: true})).toBe('prefix suffix');
expect(decomment('/*start' + LB + 'middle' + LB + 'end*/text' + LB, {space: true})).toBe(LB + LB + ' text' + LB);
expect(decomment('/*start' + LB + 'middle' + LB + 'end*/\ttext', {space: true})).toBe(LB + LB + ' \ttext');
expect(decomment('/*start' + LB + 'middle' + LB + 'end*/', {space: true})).toBe(LB + LB);
expect(decomment('/*start' + LB + 'middle' + LB + 'end*/text', {space: true})).toBe(LB + LB + ' text');
expect(decomment('prefix/*start' + LB + 'middle' + LB + 'end*/suffix', {space: true})).toBe('prefix' + LB + LB + ' suffix');
});

@@ -209,0 +207,0 @@ });

'use strict';
/* eslint-disable */
// Tests for skipping regular expressions;
var decomment = require('../lib');
var os = require('os');
var LB = os.EOL;
const decomment = require('../lib');
const os = require('os');
const LB = os.EOL;
describe("RegEx:", function () {
describe('RegEx:', function () {
describe("with apostrophe", function () {
it("must ignore the apostrophe", function () {
expect(decomment("/'/")).toBe("/'/");
expect(decomment("/'/" + LB)).toBe("/'/" + LB);
expect(decomment("/\'/")).toBe("/'/");
expect(decomment("/'\/text")).toBe("/'\/text");
describe('with apostrophe', function () {
it('must ignore the apostrophe', function () {
expect(decomment('/\'/')).toBe('/\'/');
expect(decomment('/\'/' + LB)).toBe('/\'/' + LB);
expect(decomment('/\'/')).toBe('/\'/');
expect(decomment('/\'/text')).toBe('/\'/text');
});
});
describe("with a quote", function () {
it("must ignore the quote", function () {
describe('with a quote', function () {
it('must ignore the quote', function () {
expect(decomment('/"/')).toBe('/"/');
expect(decomment('/"/' + LB)).toBe('/"/' + LB);
expect(decomment('/\"/')).toBe('/"/');
expect(decomment('/"\/text')).toBe('/"\/text');
expect(decomment('/"/')).toBe('/"/');
expect(decomment('/"/text')).toBe('/"/text');
});
});
describe("with an es6 apostrophe", function () {
it("must ignore the apostrophe", function () {
describe('with an es6 apostrophe', function () {
it('must ignore the apostrophe', function () {
expect(decomment('/`/')).toBe('/`/');
expect(decomment('/`/' + LB)).toBe('/`/' + LB);
expect(decomment('/\`/')).toBe('/`/');
expect(decomment('/`/')).toBe('/`/');
});
});
describe("with a back-slash", function () {
it("must ignore the back-slash", function () {
describe('with a back-slash', function () {
it('must ignore the back-slash', function () {
expect(decomment('/\\//')).toBe('/\\//');
expect(decomment('/\\\//')).toBe('/\\//');
expect(decomment('/\\//')).toBe('/\\//');
});
});
describe("with a multi-line", function () {
it("must ignore the multi-line", function () {
describe('with a multi-line', function () {
it('must ignore the multi-line', function () {
expect(decomment('/\\/*/')).toBe('/\\/*/');
expect(decomment('/\\/\*/')).toBe('/\\/*/');
expect(decomment('/\\/*/')).toBe('/\\/*/');
});
});
describe("with a multi-line opener", function () {
it("must ignore the multi-line", function () {
describe('with a multi-line opener', function () {
it('must ignore the multi-line', function () {
expect(decomment('/\\*/')).toBe('/\\*/');
expect(decomment('/\\\*/')).toBe('/\\*/');
expect(decomment('/\\*/')).toBe('/\\*/');
});
});
describe("valid regular expressions", function () {
it("must repent any content", function () {
expect(decomment("t=/'/")).toBe("t=/'/");
expect(decomment(LB + "t=/'/")).toBe(LB + "t=/'/");
expect(decomment("/[']/")).toBe("/[']/");
describe('valid regular expressions', function () {
it('must repent any content', function () {
expect(decomment('t=/\'/')).toBe('t=/\'/');
expect(decomment(LB + 't=/\'/')).toBe(LB + 't=/\'/');
expect(decomment('/[\']/')).toBe('/[\']/');
});
});
describe("multiple regEx in one line", function () {
it("must be detected correctly", function () {
expect(decomment("/[']/, /[\"]/")).toBe("/[']/, /[\"]/");
expect(decomment("/[']/, /[\"]/, /[`]/")).toBe("/[']/, /[\"]/, /[`]/");
describe('multiple regEx in one line', function () {
it('must be detected correctly', function () {
expect(decomment('/[\']/, /[\']/')).toBe('/[\']/, /[\']/');
expect(decomment('/[\']/, /[\']/, /[`]/')).toBe('/[\']/, /[\']/, /[`]/');
});
});
describe("with comments on the outside", function () {
it("must remove the comments", function () {
expect(decomment("/*comment*/ /text/")).toBe(" /text/");
expect(decomment("/text/ //comment")).toBe("/text/ ");
describe('with comments on the outside', function () {
it('must remove the comments', function () {
expect(decomment('/*comment*/ /text/')).toBe(' /text/');
expect(decomment('/text/ //comment')).toBe('/text/ ');
});
});
});
'use strict';
/* eslint-disable */
// Tests for single-line comments;
var decomment = require('../lib');
var os = require('os');
var LB = os.EOL;
const decomment = require('../lib');
const os = require('os');
const LB = os.EOL;
describe("Single:", function () {
describe('Single:', function () {
describe("empty comment", function () {
it("must return an empty string", function () {
expect(decomment("//")).toBe("");
expect(decomment("/\/text")).toBe("");
describe('empty comment', function () {
it('must return an empty string', function () {
expect(decomment('//')).toBe('');
expect(decomment('//text')).toBe('');
});
});
describe("multiple empty comments, space=false", function () {
it("must return an empty string", function () {
expect(decomment("//" + LB + "//")).toBe("");
expect(decomment("//" + LB + "//" + LB)).toBe("");
describe('multiple empty comments, space=false', function () {
it('must return an empty string', function () {
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('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 () {
var out = decomment("// text");
it("must return an empty string", function () {
expect(out).toBe("");
describe('non-empty comment', function () {
var out = decomment('// text');
it('must return an empty string', function () {
expect(out).toBe('');
});
});
describe("non-empty multiple comments", function () {
var out1 = decomment("// text1" + LB + "// text2");
var out2 = decomment("// text1" + LB + "// text2" + LB);
it("must return an empty string", function () {
expect(out1).toBe("");
expect(out2).toBe("");
describe('non-empty multiple comments', function () {
var out1 = decomment('// text1' + LB + '// text2');
var out2 = decomment('// text1' + LB + '// text2' + LB);
it('must return an empty string', function () {
expect(out1).toBe('');
expect(out2).toBe('');
});
});
describe("with a prefix", function () {
it("must return the prefix", function () {
expect(decomment(LB + "//")).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("");
expect(decomment("text//comment" + LB)).toBe("text" + LB);
expect(decomment(' \t \t')).toBe('');
expect(decomment('text//comment' + LB)).toBe('text' + LB);
});
});
describe("starting comments suffixed by spaces", function () {
it("must remove comment and spaces", function () {
expect(decomment(" //hello " + LB + "next")).toBe("next");
expect(decomment(" \t //hello \t " + LB + "next")).toBe("next");
describe('starting comments suffixed by spaces', function () {
it('must remove comment and spaces', function () {
expect(decomment(' //hello ' + LB + 'next')).toBe('next');
expect(decomment(' \t //hello \t ' + LB + 'next')).toBe('next');
});
});
describe("with line-break suffix", function () {
var out = decomment("//" + LB);
it("must return an empty string", function () {
expect(out).toBe("");
describe('with line-break suffix', function () {
var out = decomment('//' + LB);
it('must return an empty string', function () {
expect(out).toBe('');
});
});
describe("with multiple line-break suffixes", function () {
var out = decomment("//" + LB + LB);
it("must return a single line break", function () {
describe('with multiple line-break suffixes', function () {
var out = decomment('//' + LB + LB);
it('must return a single line break', function () {
expect(out).toBe(LB);

@@ -80,61 +78,61 @@ });

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");
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");
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');
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');
});
});
describe("with empty text prefix", function () {
var out1 = decomment("''//");
var out2 = decomment("\"\"//");
var out3 = decomment("``//");
it("must leave only the comment", function () {
expect(out1).toBe("''");
expect(out2).toBe("\"\"");
expect(out3).toBe("``");
describe('with empty text prefix', function () {
var out1 = decomment('\'\'//');
var out2 = decomment('\'\'//');
var out3 = decomment('``//');
it('must leave only the comment', function () {
expect(out1).toBe('\'\'');
expect(out2).toBe('\'\'');
expect(out3).toBe('``');
});
});
describe("with empty text suffix", function () {
var out1 = decomment("//" + LB + "''");
var out2 = decomment("//" + LB + "\"\"");
var out3 = decomment("//" + LB + "``");
it("must leave only the comment", function () {
expect(out1).toBe("''");
expect(out2).toBe("\"\"");
expect(out3).toBe("``");
describe('with empty text suffix', function () {
var out1 = decomment('//' + LB + '\'\'');
var out2 = decomment('//' + LB + '\'\'');
var out3 = decomment('//' + LB + '``');
it('must leave only the comment', function () {
expect(out1).toBe('\'\'');
expect(out2).toBe('\'\'');
expect(out3).toBe('``');
});
});
describe("comments inside text", function () {
var out = decomment("'//Text'");
it("must leave only the comment", function () {
expect(out).toBe("'//Text'");
describe('comments inside text', function () {
var out = decomment('\'//Text\'');
it('must leave only the comment', function () {
expect(out).toBe('\'//Text\'');
});
});
describe("spaces", function () {
describe("before text", function () {
var out = decomment("\t \tText");
it("must preserve the spaces", function () {
expect(out).toBe("\t \tText");
describe('spaces', function () {
describe('before text', function () {
var out = decomment('\t \tText');
it('must preserve the spaces', function () {
expect(out).toBe('\t \tText');
});
});
describe("after text", function () {
var out = decomment("Text\t \t");
it("must preserve the spaces", function () {
expect(out).toBe("Text\t \t");
describe('after text', function () {
var out = decomment('Text\t \t');
it('must preserve the spaces', function () {
expect(out).toBe('Text\t \t');
});
});
describe("complex case", function () {
var out = decomment("a // comment" + LB + "\tb // comment" + LB + "c//end");
it("must keep spaces correctly", function () {
expect(out).toBe("a " + LB + "\tb " + LB + "c");
describe('complex case', function () {
var out = decomment('a // comment' + LB + '\tb // comment' + LB + 'c//end');
it('must keep spaces correctly', function () {
expect(out).toBe('a ' + LB + '\tb ' + LB + 'c');
});

@@ -144,14 +142,14 @@ });

describe("multiple line breaks that follow", function () {
it("must be removed", function () {
expect(decomment("//text" + LB + LB + "end", {trim: true})).toBe("end");
expect(decomment("//text" + LB + "\t" + LB + "end", {trim: true})).toBe("end");
describe('multiple line breaks that follow', function () {
it('must be removed', function () {
expect(decomment('//text' + LB + LB + 'end', {trim: true})).toBe('end');
expect(decomment('//text' + LB + '\t' + LB + 'end', {trim: true})).toBe('end');
});
});
describe("inside regEx", function () {
it("must be ignored", function () {
expect(decomment("/[a-b//]text/")).toBe("/[a-b//]text/");
describe('inside regEx', function () {
it('must be ignored', function () {
expect(decomment('/[a-b//]text/')).toBe('/[a-b//]text/');
});
});
});

@@ -5,5 +5,5 @@ 'use strict';

var decomment = require('../lib').text;
var os = require('os');
var LB = os.EOL;
const decomment = require('../lib').text;
const os = require('os');
const LB = os.EOL;

@@ -10,0 +10,0 @@ describe('Text:', function () {

@@ -5,5 +5,5 @@ 'use strict';

var decomment = require('../lib');
var os = require('os');
var LB = os.EOL;
const decomment = require('../lib');
const os = require('os');
const LB = os.EOL;

@@ -10,0 +10,0 @@ describe('Utils/Positive:', function () {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc