|
|
3 лет назад | |
|---|---|---|
| .. | ||
| node_modules | 3 лет назад | |
| LICENSE | 3 лет назад | |
| README.md | 3 лет назад | |
| index.js | 3 лет назад | |
| package.json | 3 лет назад | |
Strip comments from code. Removes line comments, block comments, the first comment only, or all comments. Optionally leave protected comments unharmed.
(TOC generated by verb using markdown-toc)
Install with npm:
$ npm i strip-comments --save
For more use-cases see the tests
var strip = require('strip-comments');
console.log(strip('Hey! // foo'));
//=> 'Hey !';
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);
// => 'foo; \n '
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 stringExamples
var output = strip('foo; // this is a comment\n /* me too *\/', { line: false });
console.log(output);
// => 'foo; // this is a comment\n '
var output = strip.block('foo; // this is a comment\n /* me too *\/');
console.log(output);
// => 'foo; // this is a comment\n '
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 stringExamples
var output = strip('foo; // this is a comment\n /* me too *\/', { block: false });
console.log(output);
// => 'foo; \n /* me too *\/'
var output = strip.line('foo; // this is a comment\n /* me too *\/');
console.log(output);
// => 'foo; \n /* me too *\/'
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);
// => '\nfoo; // this is a comment'
var str = '//! first comment\nfoo; // this is a comment';
var output = strip.first(str, { safe: true });
console.log(output);
// => '//! first comment\nfoo; '
Install dev dependencies:
$ npm i -d && npm test
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Jon Schlinkert
Copyright © 2014-2016 Jon Schlinkert Released under the MIT license.
This file was generated by verb, v0.9.0, on February 14, 2016.