# strip-comments [![NPM version](https://img.shields.io/npm/v/strip-comments.svg)](https://www.npmjs.com/package/strip-comments) [![Build Status](https://img.shields.io/travis/jonschlinkert/strip-comments.svg)](https://travis-ci.org/jonschlinkert/strip-comments) > Strip comments from code. Removes line comments, block comments, the first comment only, or all comments. Optionally leave protected comments unharmed. ## Table Of Contents - [Install](#install) - [Usage](#usage) - [API](#api) - [Related projects](#related-projects) - [Running tests](#running-tests) - [Contributing](#contributing) - [Author](#author) - [License](#license) _(TOC generated by [verb](https://github.com/verbose/verb) using [markdown-toc](https://github.com/jonschlinkert/markdown-toc))_ ## Install Install with [npm](https://www.npmjs.com/): ```sh $ npm i strip-comments --save ``` ## Usage For more use-cases see the [tests](./test/test.js) ```js var strip = require('strip-comments'); console.log(strip('Hey! // foo')); //=> 'Hey !'; ``` ## API ### [strip](index.js#L37) Strip all code comments from the given `input`, including these that are ignored. Pass `opts.safe: true` to keep them. **Params** * `` **{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** ```js var str = strip('foo; // this is a comment\n /* me too *\/'); console.log(str); // => 'foo; \n ' ``` ### [.block](index.js#L68) Strip only block comments. **Params** * `` **{String}**: string from which to strip comments * `[opts]` **{Object}**: pass `opts.safe: true` to keep ignored comments (e.g. `/*!`) * `returns` **{String}**: modified string **Examples** ```js var output = strip('foo; // this is a comment\n /* me too *\/', { line: false }); console.log(output); // => 'foo; // this is a comment\n ' ``` ```js var output = strip.block('foo; // this is a comment\n /* me too *\/'); console.log(output); // => 'foo; // this is a comment\n ' ``` ### [.line](index.js#L99) Strip only line comments. **Params** * `` **{String}**: string from which to strip comments * `[opts]` **{Object}**: pass `opts.safe: true` to keep ignored comments (e.g. `//!`) * `returns` **{String}**: modified string **Examples** ```js var output = strip('foo; // this is a comment\n /* me too *\/', { block: false }); console.log(output); // => 'foo; \n /* me too *\/' ``` ```js var output = strip.line('foo; // this is a comment\n /* me too *\/'); console.log(output); // => 'foo; \n /* me too *\/' ``` ### [.first](index.js#L135) Strip the first comment from the given `input`. If `opts.safe: true` is passed, will strip the first that is not ignored. **Params** * `` **{String}** * `[opts]` **{Object}**: pass `opts.safe: true` to keep comments with `!` * `returns` **{String}** **Examples** ```js var str = '//! first comment\nfoo; // this is a comment'; var output = strip(str, { first: true }); console.log(output); // => '\nfoo; // this is a comment' ``` ```js var str = '//! first comment\nfoo; // this is a comment'; var output = strip.first(str, { safe: true }); console.log(output); // => '//! first comment\nfoo; ' ``` ## Related projects * [code-context](https://www.npmjs.com/package/code-context): Parse a string of javascript to determine the context for functions, variables and comments based… [more](https://www.npmjs.com/package/code-context) | [homepage](https://github.com/jonschlinkert/code-context) * [esprima-extract-comments](https://www.npmjs.com/package/esprima-extract-comments): Extract code comments from string or from a glob of files using esprima. | [homepage](https://github.com/jonschlinkert/esprima-extract-comments) * [extract-comments](https://www.npmjs.com/package/extract-comments): Uses esprima to extract line and block comments from a string of JavaScript. Also optionally… [more](https://www.npmjs.com/package/extract-comments) | [homepage](https://github.com/jonschlinkert/extract-comments) * [js-comments](https://www.npmjs.com/package/js-comments): Parse JavaScript code comments and generate API documentation. | [homepage](https://github.com/jonschlinkert/js-comments) * [parse-code-context](https://www.npmjs.com/package/parse-code-context): Parse code context in a single line of javascript, for functions, variable declarations, methods, prototype… [more](https://www.npmjs.com/package/parse-code-context) | [homepage](https://github.com/jonschlinkert/parse-code-context) * [parse-comments](https://www.npmjs.com/package/parse-comments): Parse code comments from JavaScript or any language that uses the same format. | [homepage](https://github.com/jonschlinkert/parse-comments) * [snapdragon](https://www.npmjs.com/package/snapdragon): snapdragon is an extremely pluggable, powerful and easy-to-use parser-renderer factory. | [homepage](https://github.com/jonschlinkert/snapdragon) ## Running tests Install dev dependencies: ```sh $ npm i -d && npm test ``` ## Contributing Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/strip-comments/issues/new). ## Author **Jon Schlinkert** * [github/jonschlinkert](https://github.com/jonschlinkert) * [twitter/jonschlinkert](http://twitter.com/jonschlinkert) ## License Copyright © 2014-2016 [Jon Schlinkert](https://github.com/jonschlinkert) Released under the [MIT license](https://github.com/jonschlinkert/strip-comments/blob/master/LICENSE). *** _This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on February 14, 2016._