| 1 | # strip-comments [](https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=W8YFZ425KND68) [](https://www.npmjs.com/package/strip-comments) [](https://npmjs.org/package/strip-comments) [](https://npmjs.org/package/strip-comments) [](https://travis-ci.org/jonschlinkert/strip-comments)
|
|---|
| 2 |
|
|---|
| 3 | > Strip line and/or block comments from a string. Blazing fast, and works with JavaScript, Sass, CSS, Less.js, and a number of other languages.
|
|---|
| 4 |
|
|---|
| 5 | Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
|
|---|
| 6 |
|
|---|
| 7 | - [Install](#install)
|
|---|
| 8 | - [What does this do?](#what-does-this-do)
|
|---|
| 9 | - [Usage](#usage)
|
|---|
| 10 | - [API](#api)
|
|---|
| 11 | - [About](#about)
|
|---|
| 12 |
|
|---|
| 13 | _(TOC generated by [verb](https://github.com/verbose/verb) using [markdown-toc](https://github.com/jonschlinkert/markdown-toc))_
|
|---|
| 14 |
|
|---|
| 15 | ## Install
|
|---|
| 16 |
|
|---|
| 17 | Install with [npm](https://www.npmjs.com/) (requires [Node.js](https://nodejs.org/en/) >=10):
|
|---|
| 18 |
|
|---|
| 19 | ```sh
|
|---|
| 20 | $ npm install --save strip-comments
|
|---|
| 21 | ```
|
|---|
| 22 |
|
|---|
| 23 | ## What does this do?
|
|---|
| 24 |
|
|---|
| 25 | Takes a string and returns a new string with comments removed. Works with line comments and/or block comments. Optionally removes the first comment only or ignores protected comments.
|
|---|
| 26 |
|
|---|
| 27 | Works with:
|
|---|
| 28 |
|
|---|
| 29 | * ada
|
|---|
| 30 | * apl
|
|---|
| 31 | * applescript
|
|---|
| 32 | * c
|
|---|
| 33 | * csharp
|
|---|
| 34 | * css
|
|---|
| 35 | * hashbang
|
|---|
| 36 | * haskell
|
|---|
| 37 | * html
|
|---|
| 38 | * java
|
|---|
| 39 | * javascript
|
|---|
| 40 | * less
|
|---|
| 41 | * lua
|
|---|
| 42 | * matlab
|
|---|
| 43 | * ocaml
|
|---|
| 44 | * pascal
|
|---|
| 45 | * perl
|
|---|
| 46 | * php
|
|---|
| 47 | * python
|
|---|
| 48 | * ruby
|
|---|
| 49 | * sass
|
|---|
| 50 | * shebang
|
|---|
| 51 | * sql
|
|---|
| 52 | * swift
|
|---|
| 53 | * typscript
|
|---|
| 54 | * xml
|
|---|
| 55 |
|
|---|
| 56 | ## Usage
|
|---|
| 57 |
|
|---|
| 58 | By default all comments are stripped.
|
|---|
| 59 |
|
|---|
| 60 | ```js
|
|---|
| 61 | const strip = require('strip-comments');
|
|---|
| 62 | const str = strip('const foo = "bar";// this is a comment\n /* me too *\/');
|
|---|
| 63 | console.log(str);
|
|---|
| 64 | // => 'const foo = "bar";\n'
|
|---|
| 65 | ```
|
|---|
| 66 |
|
|---|
| 67 | For more use-cases see the [tests](./test/test.js)
|
|---|
| 68 |
|
|---|
| 69 | ## API
|
|---|
| 70 |
|
|---|
| 71 | ### [strip](index.js#L33)
|
|---|
| 72 |
|
|---|
| 73 | Strip all code comments from the given `input`, including protected comments that start with `!`, unless disabled by setting `options.keepProtected` to true.
|
|---|
| 74 |
|
|---|
| 75 | **Params**
|
|---|
| 76 |
|
|---|
| 77 | * `input` **{String}**: string from which to strip comments
|
|---|
| 78 | * `options` **{Object}**: optional options, passed to [extract-comments](https://github.com/jonschlinkert/extract-comments)
|
|---|
| 79 |
|
|---|
| 80 | - `line` **{Boolean}**: if `false` strip only block comments, default `true`
|
|---|
| 81 | - `block` **{Boolean}**: if `false` strip only line comments, default `true`
|
|---|
| 82 | - `keepProtected` **{Boolean}**: Keep ignored comments (e.g. `/*!` and `//!`)
|
|---|
| 83 | - `preserveNewlines` **{Boolean}**: Preserve newlines after comments are stripped
|
|---|
| 84 | * `returns` **{String}**: modified input
|
|---|
| 85 |
|
|---|
| 86 | **Example**
|
|---|
| 87 |
|
|---|
| 88 | ```js
|
|---|
| 89 | const str = strip('const foo = "bar";// this is a comment\n /* me too */');
|
|---|
| 90 | console.log(str);
|
|---|
| 91 | // => 'const foo = "bar";'
|
|---|
| 92 | ```
|
|---|
| 93 |
|
|---|
| 94 | ### [.block](index.js#L54)
|
|---|
| 95 |
|
|---|
| 96 | Strip only block comments.
|
|---|
| 97 |
|
|---|
| 98 | **Params**
|
|---|
| 99 |
|
|---|
| 100 | * `input` **{String}**: string from which to strip comments
|
|---|
| 101 | * `options` **{Object}**: pass `opts.keepProtected: true` to keep ignored comments (e.g. `/*!`)
|
|---|
| 102 | * `returns` **{String}**: modified string
|
|---|
| 103 |
|
|---|
| 104 | **Example**
|
|---|
| 105 |
|
|---|
| 106 | ```js
|
|---|
| 107 | const strip = require('..');
|
|---|
| 108 | const str = strip.block('const foo = "bar";// this is a comment\n /* me too */');
|
|---|
| 109 | console.log(str);
|
|---|
| 110 | // => 'const foo = "bar";// this is a comment'
|
|---|
| 111 | ```
|
|---|
| 112 |
|
|---|
| 113 | ### [.line](index.js#L74)
|
|---|
| 114 |
|
|---|
| 115 | Strip only line comments.
|
|---|
| 116 |
|
|---|
| 117 | **Params**
|
|---|
| 118 |
|
|---|
| 119 | * `input` **{String}**: string from which to strip comments
|
|---|
| 120 | * `options` **{Object}**: pass `opts.keepProtected: true` to keep ignored comments (e.g. `//!`)
|
|---|
| 121 | * `returns` **{String}**: modified string
|
|---|
| 122 |
|
|---|
| 123 | **Example**
|
|---|
| 124 |
|
|---|
| 125 | ```js
|
|---|
| 126 | const str = strip.line('const foo = "bar";// this is a comment\n /* me too */');
|
|---|
| 127 | console.log(str);
|
|---|
| 128 | // => 'const foo = "bar";\n/* me too */'
|
|---|
| 129 | ```
|
|---|
| 130 |
|
|---|
| 131 | ### [.first](index.js#L95)
|
|---|
| 132 |
|
|---|
| 133 | Strip the first comment from the given `input`. Or, if `opts.keepProtected` is true, the first non-protected comment will be stripped.
|
|---|
| 134 |
|
|---|
| 135 | **Params**
|
|---|
| 136 |
|
|---|
| 137 | * `input` **{String}**
|
|---|
| 138 | * `options` **{Object}**: pass `opts.keepProtected: true` to keep comments with `!`
|
|---|
| 139 | * `returns` **{String}**
|
|---|
| 140 |
|
|---|
| 141 | **Example**
|
|---|
| 142 |
|
|---|
| 143 | ```js
|
|---|
| 144 | const output = strip.first(input, { keepProtected: true });
|
|---|
| 145 | console.log(output);
|
|---|
| 146 | // => '//! first comment\nfoo; '
|
|---|
| 147 | ```
|
|---|
| 148 |
|
|---|
| 149 | ### [.block](index.js#L116)
|
|---|
| 150 |
|
|---|
| 151 | Parses a string and returns a basic CST (Concrete Syntax Tree).
|
|---|
| 152 |
|
|---|
| 153 | **Params**
|
|---|
| 154 |
|
|---|
| 155 | * `input` **{String}**: string from which to strip comments
|
|---|
| 156 | * `options` **{Object}**: pass `opts.keepProtected: true` to keep ignored comments (e.g. `/*!`)
|
|---|
| 157 | * `returns` **{String}**: modified string
|
|---|
| 158 |
|
|---|
| 159 | **Example**
|
|---|
| 160 |
|
|---|
| 161 | ```js
|
|---|
| 162 | const strip = require('..');
|
|---|
| 163 | const str = strip.block('const foo = "bar";// this is a comment\n /* me too */');
|
|---|
| 164 | console.log(str);
|
|---|
| 165 | // => 'const foo = "bar";// this is a comment'
|
|---|
| 166 | ```
|
|---|
| 167 |
|
|---|
| 168 | ## About
|
|---|
| 169 |
|
|---|
| 170 | <details>
|
|---|
| 171 | <summary><strong>Contributing</strong></summary>
|
|---|
| 172 |
|
|---|
| 173 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
|
|---|
| 174 |
|
|---|
| 175 | </details>
|
|---|
| 176 |
|
|---|
| 177 | <details>
|
|---|
| 178 | <summary><strong>Running Tests</strong></summary>
|
|---|
| 179 |
|
|---|
| 180 | Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
|
|---|
| 181 |
|
|---|
| 182 | ```sh
|
|---|
| 183 | $ npm install && npm test
|
|---|
| 184 | ```
|
|---|
| 185 |
|
|---|
| 186 | </details>
|
|---|
| 187 |
|
|---|
| 188 | <details>
|
|---|
| 189 | <summary><strong>Building docs</strong></summary>
|
|---|
| 190 |
|
|---|
| 191 | _(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_
|
|---|
| 192 |
|
|---|
| 193 | To generate the readme, run the following command:
|
|---|
| 194 |
|
|---|
| 195 | ```sh
|
|---|
| 196 | $ npm install -g verbose/verb#dev verb-generate-readme && verb
|
|---|
| 197 | ```
|
|---|
| 198 |
|
|---|
| 199 | </details>
|
|---|
| 200 |
|
|---|
| 201 | ### Related projects
|
|---|
| 202 |
|
|---|
| 203 | You might also be interested in these projects:
|
|---|
| 204 |
|
|---|
| 205 | * [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://github.com/jonschlinkert/code-context) | [homepage](https://github.com/jonschlinkert/code-context "Parse a string of javascript to determine the context for functions, variables and comments based on the code that follows.")
|
|---|
| 206 | * [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://github.com/jonschlinkert/extract-comments) | [homepage](https://github.com/jonschlinkert/extract-comments "Uses esprima to extract line and block comments from a string of JavaScript. Also optionally parses code context (the next line of code after a comment).")
|
|---|
| 207 | * [parse-code-context](https://www.npmjs.com/package/parse-code-context): Fast and simple way to parse code context for use with documentation from code comments… [more](https://github.com/jonschlinkert/parse-code-context) | [homepage](https://github.com/jonschlinkert/parse-code-context "Fast and simple way to parse code context for use with documentation from code comments. Parses context from a single line of JavaScript, for functions, variable declarations, methods, prototype properties, prototype methods etc.")
|
|---|
| 208 | * [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 "Parse code comments from JavaScript or any language that uses the same format.")
|
|---|
| 209 |
|
|---|
| 210 | ### Contributors
|
|---|
| 211 |
|
|---|
| 212 | | **Commits** | **Contributor** |
|
|---|
| 213 | | --- | --- |
|
|---|
| 214 | | 82 | [jonschlinkert](https://github.com/jonschlinkert) |
|
|---|
| 215 | | 4 | [tunnckoCore](https://github.com/tunnckoCore) |
|
|---|
| 216 | | 2 | [mk-pmb](https://github.com/mk-pmb) |
|
|---|
| 217 | | 1 | [kgryte](https://github.com/kgryte) |
|
|---|
| 218 | | 1 | [briandipalma](https://github.com/briandipalma) |
|
|---|
| 219 | | 1 | [epicoxymoron](https://github.com/epicoxymoron) |
|
|---|
| 220 | | 1 | [XuluWarrior](https://github.com/XuluWarrior) |
|
|---|
| 221 |
|
|---|
| 222 | ### Author
|
|---|
| 223 |
|
|---|
| 224 | **Jon Schlinkert**
|
|---|
| 225 |
|
|---|
| 226 | * [GitHub Profile](https://github.com/jonschlinkert)
|
|---|
| 227 | * [Twitter Profile](https://twitter.com/jonschlinkert)
|
|---|
| 228 | * [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)
|
|---|
| 229 |
|
|---|
| 230 | ### License
|
|---|
| 231 |
|
|---|
| 232 | Copyright © 2019, [Jon Schlinkert](https://github.com/jonschlinkert).
|
|---|
| 233 | Released under the [MIT License](LICENSE).
|
|---|
| 234 |
|
|---|
| 235 | ***
|
|---|
| 236 |
|
|---|
| 237 | _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.8.0, on November 13, 2019._ |
|---|