Strip comments from code. Removes line comments, block comments, the first comment only, or all comments. Optionally leave protected comments unharmed.
Table Of Contents
(TOC generated by verb using markdown-toc)
Install
Install with npm:
$ npm i strip-comments --save
Usage
For more use-cases see the tests
var strip = require('strip-comments');
console.log(strip('Hey! // foo'));
API
Strip all code comments from the given input
, including these that are ignored. Pass opts.safe: true
to keep them.
Params
<input>
{String}: string from which to strip comments
opts
{Object}: optional options, passed to [extract-comments][extract-comments]
line
{Boolean}: if false
strip only block comments, default true
block
{Boolean}: if false
strip only line comments, default true
safe
{Boolean}: pass true
to keep ignored comments (e.g. /*!
and //!
)preserveNewlines
{Boolean}: if true
preserve newlines after comments are stripped
returns
{String}: modified input
Example
var str = strip('foo; // this is a comment\n /* me too *\/');
console.log(str);
Strip only block comments.
Params
<input>
{String}: string from which to strip comments[opts]
{Object}: pass opts.safe: true
to keep ignored comments (e.g. /*!
)returns
{String}: modified string
Examples
var output = strip('foo; // this is a comment\n /* me too *\/', { line: false });
console.log(output);
var output = strip.block('foo; // this is a comment\n /* me too *\/');
console.log(output);
Strip only line comments.
Params
<input>
{String}: string from which to strip comments[opts]
{Object}: pass opts.safe: true
to keep ignored comments (e.g. //!
)returns
{String}: modified string
Examples
var output = strip('foo; // this is a comment\n /* me too *\/', { block: false });
console.log(output);
var output = strip.line('foo; // this is a comment\n /* me too *\/');
console.log(output);
Strip the first comment from the given input
. If opts.safe: true
is passed, will strip the first that is not ignored.
Params
<input>
{String}[opts]
{Object}: pass opts.safe: true
to keep comments with !
returns
{String}
Examples
var str = '//! first comment\nfoo; // this is a comment';
var output = strip(str, {
first: true
});
console.log(output);
var str = '//! first comment\nfoo; // this is a comment';
var output = strip.first(str, { safe: true });
console.log(output);
Related projects
Running tests
Install dev dependencies:
$ npm i -d && npm test
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Author
Jon Schlinkert
License
Copyright © 2014-2016 Jon Schlinkert
Released under the MIT license.
This file was generated by verb, v0.9.0, on February 14, 2016.