npm’s 2019 JavaScript ecosystem survey analysis is now available!Get your copy here »

strip-comments

0.4.4 • Public • Published

strip-comments NPM version Build Status

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'));
//=> 'Hey !';

API

strip

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 '

.block

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);
// => '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 '

.line

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);
// => 'foo; \n /* me too *\/'
var output = strip.line('foo; // this is a comment\n /* me too *\/');
console.log(output);
// => 'foo; \n /* me too *\/'

.first

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; '

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.

Install

npm i strip-comments

DownloadsWeekly Downloads

1,509,214

Version

0.4.4

License

MIT

Last publish

Collaborators

  • avatar