strip-any
v1.4.0
Published
Strip or extract comments, strings, or both from various source codes using custom parsing rules.
Readme
Description
Strip or extract comments, strings or both from various source codes (Js, HTML, SQL, Ts, C++, C, Json, Sass, CSS, Shell, etc.) using parsing options.
Install
npm install strip-anyUsage
const {
stripHtmlComments,
stripSqlComments,
stripJsComments,
stripTsComments,
stripCssComments,
stripSassComments,
stripCssComments,
stripSassComments,
stripShellComments,
stripCmdComments,
stripJson5Comments,
stripCComments,
// Custom
parseString,
stripStrings,
stripComments,
clearStrings,
setParsingOptions,
resetParsingOptions
} = require("strip-any");or
import {
stripHtmlComments,
stripSqlComments,
stripJsComments,
stripTsComments,
stripCssComments,
stripSassComments,
stripCssComments,
stripSassComments,
stripShellComments,
stripCmdComments,
stripJson5Comments,
stripCComments,
// Custom
parseString,
stripStrings,
stripComments,
clearStrings,
setParsingOptions,
resetParsingOptions
} from "strip-any"Examples
Strip comments and strings (JavaScript and related)
// Default language: JavaScript
const {stripComments, stripStrings} = require("strip-any");
str = stripComments(str);
str = stripStrings(str)
// ...It is best to always start by removing comments then strings.
Strip comments from HTML, Js, Ts, SQL, css, sass, etc.
// Strip HTML comments
const {
stripHtmlComments,
stripSQLComments,
stripJsComments,
stripTsComments,
stripCssComments,
stripSassComments,
stripJson5Comments,
stripCComments,
} = require("strip-any");
// HTML
const html = stripHtmlComments(htmlCode);
// SQL
const sql = stripSqlComments(sqlCode);
// JavaScript or TypeScript
const js = stripJsComments(jsCode);
// css
const css = stripCssComments(cssCode);
// sass
const sass = stripSassComments(sassCode);
// Json5
const json5 = stripJson5Comments(jsonString);
// C, C++
const cPlus = stripCComments(cCode);
// ...Strip comments and strings from any type of source code
// Example: Strip SQL comments
const {stripComments, COMMENT_TYPE} = require("strip-any");
// StripComments will now use the new defined parsing method
let sqlQuery = stripComments(strStatement, {parsingOptions: [
{name: COMMENT_TYPE.COMMENT_BLOCK, char: /\/\*!/, charEnd: "*/", skip: true}, // Preserved comments
{name: "quote", char: "'"}, // Text between quotes '' will be identified as Strings
{name: "literal", char: "`"}, // Text between backticks `` will be identified as Strings
{name: "doubleQuote", char: "\""}, // Text between double quotes "" will be identified as Strings
{name: COMMENT_TYPE.COMMENT_BLOCK, char: "/*", charEnd: "*/"}, // Text between /* */ will be identified as comment block
{name: COMMENT_TYPE.COMMENT_LINE, char: "//"}, // Text starting with // will be identified as comment line
{name: REGEX_TYPE, char: "/"}, // For Js and Ts code
]});
str = stripComments(str);
str = stripStrings(str)
// ...Note: This example is to show how to tweak the parser. For manipulating .sql file, you can use stripSqlComments()
Make parsing global
const {stripComments, setParsingOptions, resetParsingOptions, COMMENT_TYPE} = require("strip-any");
// Set parsing method
setParsingOptions([
{name: "quote", char: "'"},
{name: "doubleQuote", char: "\""},
{name: COMMENT_TYPE.COMMENT_LINE, char: "#"},
]);
// StripComments and other methods (stripComments, etc.) will now use the new defined parsing method as default
let str = stripComments(strStatement);
str = stripStrings(str)
// Reset parsing method so following calls to stripComments and stripStrings use the default parser
resetParsingOptions();
// ...Clearing strings
📝 running-code.js ↴
let str = fs.readFileSync("./example.js", "utf-8");
let result = clearStrings(str);
console.log(result)Before ↴ (example.js)
let c1 = "Ho you \" How are \" the you?";
let c2 = "dfgdfd dfgdfgd \" '*";
let c3 = "Okay okay";
let c4 = `sfdgfdf d d dd
\` ${c2}
le = "abd pdfdp \" ;lfgfdlgkd"`;
};After ↴
let c1 = "";
let c2 = "";
let c3 = "";
let c4 = ``;
};Stripping strings
📝 running-code.js ↴
let str = fs.readFileSync("./example.js", "utf-8");
let result = stripStrings(str);
console.log(result)Before ↴ (example.js)
let c1 = "Ho you \" How are \" the you?";
let c2 = "dfgdfd dfgdfgd \" '*";
let c3 = "Okay okay";
let c4 = `sfdgfdf d d dd
\` ${c2}
le = "abd pdfdp \" ;lfgfdlgkd"`;
};After ↴
let c1 = ;
let c2 = ;
let c3 = ;
let c4 = ;
};Stripping comments
📝 running-code.js ↴
let str = fs.readFileSync("./example.js", "utf-8");
let result = stripComments(str);
console.log(result)Before (example.js) ↴
//abcdefghij
const bb = `
// Hi
/** Copy **/
const chalk004 = require("chalk-cjs");
const chalk005 = require("chalk-cjs");
`
/**
* @class SomeClass
*/After ↴
const bb = `
// Hi
/** Copy **/
const chalk004 = require("chalk-cjs");
const chalk005 = require("chalk-cjs");
`Replacing comments from source via callback
📝 running-code.js ↴
let str = fs.readFileSync(path.join(__dirname, "./example.js"), "utf-8");
const stripMyComments = (str) =>
{
let counter = 0
str = stripComments(str, function (info)
{
return `(${counter++})`
});
return str
}
console.log(str)Before ↴
/** // **/
/** 1 **/
//aaaa
/** 2 **/
//bbbbb
//ccccc
///////////
/** 3 **/
/** 4 **/
/** 5 **/
""
aa
"attenti\"on"
bb
ccc
'warni\\'
'ng\''
ddd
eeee`info`
eeee
fffff
"silence"
fffff
gggggg
'noise'
gggggg
hhhhhhh`visibility`
hhhhhhh
"aaa"
"aaa"
"aaa"After ↴
(9)
(8)
(7)(6)
(5)(4)(3)(2)
(1)
(0)
""
aa
"attenti\"on"
bb
ccc
'warni\\'
'ng\''
ddd
eeee`info`
eeee
fffff
"silence"
fffff
gggggg
'noise'
gggggg
hhhhhhh`visibility`
hhhhhhh
"aaa"
"aaa"
"aaa"Replacing strings from source via callbacks
let str = fs.readFileSync("./example.js", "utf-8");
str = stripStrings(str, function (info)
{
return "!!"
}, {includeDelimiter: true});
Before (example.js) ↴
"What"
'Time'
`is it?`!!
!!
!!Parsing source
const found = parseString(str);
console.log( found );{
text: '',
strings: [
{ content: '', index: 107, indexEnd: 107, item: [Object] },
{
content: 'attenti\\"on',
index: 115,
indexEnd: 126,
item: [Object]
},
{ content: 'warni\\\\', index: 139, indexEnd: 146, item: [Object] },
{ content: "ng\\'", index: 150, indexEnd: 154, item: [Object] },
{ content: 'info', index: 167, indexEnd: 171, item: [Object] },
{ content: 'silence', index: 188, indexEnd: 195, item: [Object] },
{ content: 'noise', index: 214, indexEnd: 219, item: [Object] },
{
content: 'visibility',
index: 238,
indexEnd: 248,
item: [Object]
},
{ content: 'aaa', index: 261, indexEnd: 264, item: [Object] },
{ content: 'aaa', index: 268, indexEnd: 271, item: [Object] },
{ content: 'aaa', index: 275, indexEnd: 278, item: [Object] }
],
comments: [
{
type: 'commentBlock',
content: '/** // **/',
index: 0,
indexEnd: 10
},
{
type: 'commentBlock',
content: '/** 1 **/',
index: 12,
indexEnd: 21
},
{
type: 'commentLine',
content: '//aaaa\r\n',
index: 23,
indexEnd: 31
},
{
type: 'commentBlock',
content: '/** 2 **/',
index: 31,
indexEnd: 40
},
{
type: 'commentLine',
content: '//bbbbb\r\n',
index: 42,
indexEnd: 51
},
{
type: 'commentLine',
content: '//ccccc\r\n',
index: 51,
indexEnd: 60
},
{
type: 'commentLine',
content: '///////////\r\n',
index: 60,
indexEnd: 73
},
{
type: 'commentBlock',
content: '/** 3 **/',
index: 73,
indexEnd: 82
},
{
type: 'commentBlock',
content: '/** 4 **/',
index: 84,
indexEnd: 93
},
{
type: 'commentBlock',
content: '/** 5 **/',
index: 95,
indexEnd: 104
}
]
}
