Big News: Socket Selected for OpenAI's Cybersecurity Grant Program.Details
Socket
Book a DemoSign in
Socket

strip-comments-strings

Package Overview
Dependencies
Maintainers
1
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

strip-comments-strings - npm Package Compare versions

Comparing version
1.0.4
to
1.0.5
+36
examples/demo.cjs
const path = require("path");
const fs = require("fs");
const {parseString, stripStrings, stripComments} = require("../index.cjs");
const stripMyStrings = (str) =>
{
let result = stripStrings(str, function (info)
{
// return "!!"
return "!!!"
}, {includeDelimiter: false});
return result;
}
const stripMyComments = (str) =>
{
let counter = 0
str = stripComments(str, "");
return str
}
for (let i = 2; i < 9; ++i)
{
console.log("=============================================================================")
console.log(`./example-${i}.js`)
console.log("-----------------------------------------------------------------------------")
let str = fs.readFileSync(path.join(__dirname, `./example-${i}.js`), "utf-8");
str = stripMyComments(str)
str = stripMyStrings(str)
console.log(str)
}
//abcdefghij
const bb = `
const chalk004 = require("chalk-cjs");
const chalk005 = require("chalk-cjs");
`
/**
* @class SomeClass
*/
+6
-6

@@ -22,5 +22,5 @@ **

{
let c1 = **;
let c1 = "**";
const extension = path.extname(filepath);
if (** === extension)
if ("**" === extension)
{

@@ -30,10 +30,10 @@ return false;

let c2 = **;
let c3 = **;
let c2 = "**";
let c3 = "**";
let c4 = `s**
let c4 = `s
let content = "r**
content = stripComments(content);
content = stripComments(content); `
};
+37
-37

@@ -77,6 +77,4 @@ const COMMENT_TYPE = {

let megaIndex = 0;
const movePointerIndex = (str, index) =>
{
megaIndex += index;
str = str.substring(index);

@@ -88,3 +86,4 @@ return str;

{
megaIndex = 0;
const originalString = str;
const originalStringLength = originalString.length;

@@ -102,20 +101,25 @@ const detectedString = [];

const enter = {
item
};
if (item.name === COMMENT_TYPE.COMMENT_BLOCK)
{
const startIndex = item.index;
item.index = str.indexOf("*/");
if (item.index === -1)
enter.type = COMMENT_TYPE.COMMENT_BLOCK;
str = movePointerIndex(str, item.index);
enter.index = originalStringLength - str.length;
const nextIndex = str.indexOf("*/");
if (nextIndex === -1)
{
throw new Error(`Comment Block opened at position ${megaIndex} not enclosed`);
throw new Error("Comment Block opened at position ... not enclosed");
}
const content = str.substring(startIndex, item.index + 2);
detectedComments.push({
type : COMMENT_TYPE.COMMENT_BLOCK,
content,
index : megaIndex + startIndex,
indexEnd: megaIndex + startIndex + content.length,
});
str = movePointerIndex(str, item.index + 3);
str = movePointerIndex(str, nextIndex + 2);
enter.indexEnd = originalStringLength - str.length;
enter.content = originalString.substring(enter.index, enter.indexEnd);
detectedComments.push(enter);
continue;

@@ -125,26 +129,28 @@ }

{
enter.type = COMMENT_TYPE.COMMENT_LINE;
// Beginning of line comment //
str = movePointerIndex(str, item.index);
enter.index = originalStringLength - str.length;
let newLinePos = str.indexOf("\n");
if (newLinePos === -1)
{
newLinePos = str.length;
enter.indexEnd = originalStringLength;
enter.content = originalString.substring(enter.index, enter.indexEnd - 1);
detectedComments.push(enter);
break;
}
let nextIndex = newLinePos + 1;
const content = str.substring(0, nextIndex);
detectedComments.push({
type : COMMENT_TYPE.COMMENT_LINE,
content : content,
index : megaIndex,
indexEnd: megaIndex + content.length
});
str = movePointerIndex(str, newLinePos + 1);
enter.indexEnd = originalStringLength - str.length - 1;
str = movePointerIndex(str, content.length);
enter.content = originalString.substring(enter.index, enter.indexEnd);
detectedComments.push(enter);
continue;
}
megaIndex += item.index;
str = str.substring(item.index + 1);
enter.index = originalStringLength - str.length;

@@ -156,16 +162,10 @@ const nextItem = getNextClosingQuote(str, item.char);

{
throw new Error(`String opened at position ${megaIndex} with a ${item.name} not enclosed`);
throw new Error(`String opened at position ... with a ${item.name} not enclosed`);
}
const content = str.substring(0, nextItem.index);
let newLineNb = content.split("\n").length;
megaIndex += newLineNb;
detectedString.push({
content,
index : megaIndex,
indexEnd: megaIndex + content.length,
item
});
str = movePointerIndex(str, nextItem.index + 1);
enter.indexEnd = originalStringLength - str.length - 1;
enter.content = originalString.substring(enter.index, enter.indexEnd - 1);
str = movePointerIndex(str, nextItem.index + 1);
detectedString.push(enter);
}

@@ -172,0 +172,0 @@ while (true);

+38
-38

@@ -6,3 +6,3 @@ /**

*
**/
**/
const COMMENT_TYPE = {

@@ -84,6 +84,4 @@ COMMENT_BLOCK: "commentBlock",

let megaIndex = 0;
const movePointerIndex = (str, index) =>
{
megaIndex += index;
str = str.substring(index);

@@ -95,3 +93,4 @@ return str;

{
megaIndex = 0;
const originalString = str;
const originalStringLength = originalString.length;

@@ -109,20 +108,25 @@ const detectedString = [];

const enter = {
item
};
if (item.name === COMMENT_TYPE.COMMENT_BLOCK)
{
const startIndex = item.index;
item.index = str.indexOf("*/");
if (item.index === -1)
enter.type = COMMENT_TYPE.COMMENT_BLOCK;
str = movePointerIndex(str, item.index);
enter.index = originalStringLength - str.length;
const nextIndex = str.indexOf("*/");
if (nextIndex === -1)
{
throw new Error(`Comment Block opened at position ${megaIndex} not enclosed`);
throw new Error("Comment Block opened at position ... not enclosed");
}
const content = str.substring(startIndex, item.index + 2);
detectedComments.push({
type : COMMENT_TYPE.COMMENT_BLOCK,
content,
index : megaIndex + startIndex,
indexEnd: megaIndex + startIndex + content.length,
});
str = movePointerIndex(str, item.index + 3);
str = movePointerIndex(str, nextIndex + 2);
enter.indexEnd = originalStringLength - str.length;
enter.content = originalString.substring(enter.index, enter.indexEnd);
detectedComments.push(enter);
continue;

@@ -132,26 +136,28 @@ }

{
enter.type = COMMENT_TYPE.COMMENT_LINE;
// Beginning of line comment //
str = movePointerIndex(str, item.index);
enter.index = originalStringLength - str.length;
let newLinePos = str.indexOf("\n");
if (newLinePos === -1)
{
newLinePos = str.length;
enter.indexEnd = originalStringLength;
enter.content = originalString.substring(enter.index, enter.indexEnd - 1);
detectedComments.push(enter);
break;
}
let nextIndex = newLinePos + 1;
const content = str.substring(0, nextIndex);
detectedComments.push({
type : COMMENT_TYPE.COMMENT_LINE,
content : content,
index : megaIndex,
indexEnd: megaIndex + content.length
});
str = movePointerIndex(str, newLinePos + 1);
enter.indexEnd = originalStringLength - str.length - 1;
str = movePointerIndex(str, content.length);
enter.content = originalString.substring(enter.index, enter.indexEnd);
detectedComments.push(enter);
continue;
}
megaIndex += item.index;
str = str.substring(item.index + 1);
enter.index = originalStringLength - str.length;

@@ -163,16 +169,10 @@ const nextItem = getNextClosingQuote(str, item.char);

{
throw new Error(`String opened at position ${megaIndex} with a ${item.name} not enclosed`);
throw new Error(`String opened at position ... with a ${item.name} not enclosed`);
}
const content = str.substring(0, nextItem.index);
let newLineNb = content.split("\n").length;
megaIndex += newLineNb;
detectedString.push({
content,
index : megaIndex,
indexEnd: megaIndex + content.length,
item
});
str = movePointerIndex(str, nextItem.index + 1);
enter.indexEnd = originalStringLength - str.length - 1;
enter.content = originalString.substring(enter.index, enter.indexEnd - 1);
str = movePointerIndex(str, nextItem.index + 1);
detectedString.push(enter);
}

@@ -179,0 +179,0 @@ while (true);

{
"name": "strip-comments-strings",
"version": "1.0.4",
"version": "1.0.5",
"description": "Strip or extract comments, strings or both from source code.",
"main": "index.cjs",
"module": "index.mjs",
"main": "./index.cjs",
"module": "./index.mjs",
"type": "module",

@@ -25,4 +25,5 @@ "exports": {

"babel-eslint": "^10.1.0",
"eslint": "^8.9.0"
"eslint": "^8.9.0",
"to-esm": "^2.2.0"
}
}
const path = require("path");
const fs = require("fs");
const {parseString, stripStrings, stripComments} = require("../index.cjs");
let str = fs.readFileSync(path.join(__dirname, "./example.js"), "utf-8");
// let str = fs.readFileSync(path.join(__dirname, "./example-6.js"), "utf-8");
let index
const stripMyStrings = (str) =>
{
let result = stripStrings(str, function (info)
{
return "!!"
}, {includeDelimiter: true});
return result;
}
const stripMyComments = (str) =>
{
let counter = 0
str = stripComments(str, function (info)
{
return `(${counter++})`
});
return str
}
// console.log(parseString(str))
// str = stripMyComments(str)
str = stripMyStrings(str)
console.log(str)