Security News
Opengrep Emerges as Open Source Alternative Amid Semgrep Licensing Controversy
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Removes comments from JSON, JavaScript, CSS, HTML, etc.
$ npm install decomment
$ npm test
Testing with coverage:
$ npm run coverage
var decomment = require('decomment');
var code = "var t; // comments";
decomment(code); //=> var t;
<!-- comments -->
from itThe 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.
For JSON and JavaScript this library uses esprima to guarantee correct processing for regular expressions.
As an example, it can process AngularJS 1.5 Core in under 100ms, which is 1.1MB ~ 30,000 lines of JavaScript.
This method first parses code
to determine whether it is an HTML (starts with <
),
and if so, removes all <!-- comment -->
entries from it, according to options
.
When code
is not recognized as HTML, it is assumed to be either JSON or JavaScript.
In this case the code is parsed through esprima for ECMAScript 6 compliance, and
to extract details about regular expressions.
If esprima fails to validate the code, it will throw a parsing error. When successful,
this method will remove //
and /**/
comments according to the options
(see below).
false (default)
- do not trim commentstrue
- remove empty lines that follow removed full-line commentsExample:
var decomment = require('decomment');
var code = "/* comment */\r\n\r\n var test = 123";
decomment(code); //=> \r\n var test = 123
decomment(code, {trim: true}); //=> var test = 123
false (default)
- remove all multi-line commentstrue
- keep multi-line comments that start with /*!
Example:
var decomment = require('decomment');
var code = "/*! special */ var a; /* normal */";
decomment(code); //=> var a;
decomment(code, {safe: true}); //=> /*! special */ var a;
This option has no effect when processing HTML.
Unlike the default decomment, it instructs the library that text
is not
a JSON, JavaScript or HTML, rather a plain text that needs no parsing or validation,
only to remove //
and /**/
comments from it according to the options
.
CSS is the most frequent example of where this method is to be used.
Example:
var decomment = require('decomment');
var text = "cssClass{color:Red;}// comments";
decomment.text(text); //=> cssClass{color:Red;}
Please note that while comment blocks located inside ''
, ""
or `` are not removed,
the same as for JSON or JavaScript, you should not use this method for JSON or JavaScript,
as it can break your regular expressions.
Unlike the default decomment method, it instructs the library not to parse
or validate the input in any way, rather assume it to be HTML, and remove all
<!-- comment -->
entries from it according to the options
.
Copyright © 2016 Vitaly Tomilov; Released under the MIT license.
FAQs
Removes comments from JSON/JavaScript, CSS/HTML, CPP/H, etc.
The npm package decomment receives a total of 58,677 weekly downloads. As such, decomment popularity was classified as popular.
We found that decomment demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.