You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 7.2 KiB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # strip-comments [![NPM version](https://img.shields.io/npm/v/strip-comments.svg?style=flat)](https://www.npmjs.com/package/strip-comments) [![NPM monthly downloads](https://img.shields.io/npm/dm/strip-comments.svg?style=flat)](https://npmjs.org/package/strip-comments) [![NPM total downloads](https://img.shields.io/npm/dt/strip-comments.svg?style=flat)](https://npmjs.org/package/strip-comments) [![Linux Build Status](https://img.shields.io/travis/jonschlinkert/strip-comments.svg?style=flat&label=Travis)](https://travis-ci.org/jonschlinkert/strip-comments)
  2. > Strip comments from code. Removes line comments, block comments, the first comment only, or all comments. Optionally leave protected comments unharmed.
  3. Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support.
  4. - [Install](#install)
  5. - [Usage](#usage)
  6. - [API](#api)
  7. - [About](#about)
  8. _(TOC generated by [verb](https://github.com/verbose/verb) using [markdown-toc](https://github.com/jonschlinkert/markdown-toc))_
  9. ## Install
  10. Install with [npm](https://www.npmjs.com/):
  11. ```sh
  12. $ npm install --save strip-comments
  13. ```
  14. ## Usage
  15. By default all comments are stripped.
  16. ```js
  17. const strip = require('strip-comments');
  18. const str = strip('const foo = "bar";// this is a comment\n /* me too *\/');
  19. console.log(str);
  20. // => 'const foo = "bar";\n'
  21. ```
  22. For more use-cases see the [tests](./test/test.js)
  23. ## API
  24. ### [strip](index.js#L34)
  25. Strip all code comments from the given `input`, including protected comments that start with `!`, unless disabled by setting `options.keepProtected` to true.
  26. **Params**
  27. * `input` **{String}**: string from which to strip comments
  28. * `options` **{Object}**: optional options, passed to [extract-comments](https://github.com/jonschlinkert/extract-comments)
  29. * `returns` **{String}**: modified input
  30. **Options**
  31. - `line` **{Boolean}**: if `false` strip only block comments, default `true`
  32. - `block` **{Boolean}**: if `false` strip only line comments, default `true`
  33. - `keepProtected` **{Boolean}**: Keep ignored comments (e.g. `/*!`, `/**!` and `//!`)
  34. - `preserveNewlines` **{Boolean}**: Preserve newlines after comments are stripped
  35. **Example**
  36. ```js
  37. const str = strip('const foo = "bar";// this is a comment\n /* me too */');
  38. console.log(str);
  39. // => 'const foo = "bar";'
  40. ```
  41. ### [.block](index.js#L54)
  42. Strip only block comments.
  43. **Params**
  44. * `input` **{String}**: string from which to strip comments
  45. * `options` **{Object}**: pass `opts.keepProtected: true` to keep ignored comments (e.g. `/*!`)
  46. * `returns` **{String}**: modified string
  47. **Example**
  48. ```js
  49. const strip = require('..');
  50. const str = strip.block('const foo = "bar";// this is a comment\n /* me too */');
  51. console.log(str);
  52. // => 'const foo = "bar";// this is a comment'
  53. ```
  54. ### [.line](index.js#L73)
  55. Strip only line comments.
  56. **Params**
  57. * `input` **{String}**: string from which to strip comments
  58. * `options` **{Object}**: pass `opts.keepProtected: true` to keep ignored comments (e.g. `//!`)
  59. * `returns` **{String}**: modified string
  60. **Example**
  61. ```js
  62. const str = strip.line('const foo = "bar";// this is a comment\n /* me too */');
  63. console.log(str);
  64. // => 'const foo = "bar";\n/* me too */'
  65. ```
  66. ### [.first](index.js#L93)
  67. Strip the first comment from the given `input`. Or, if `opts.keepProtected` is true, the first non-protected comment will be stripped.
  68. **Params**
  69. * `input` **{String}**
  70. * `options` **{Object}**: pass `opts.keepProtected: true` to keep comments with `!`
  71. * `returns` **{String}**
  72. **Example**
  73. ```js
  74. const output = strip.first(input, { keepProtected: true });
  75. console.log(output);
  76. // => '//! first comment\nfoo; '
  77. ```
  78. ## About
  79. <details>
  80. <summary><strong>Contributing</strong></summary>
  81. Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new).
  82. </details>
  83. <details>
  84. <summary><strong>Running Tests</strong></summary>
  85. 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:
  86. ```sh
  87. $ npm install && npm test
  88. ```
  89. </details>
  90. <details>
  91. <summary><strong>Building docs</strong></summary>
  92. _(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.)_
  93. To generate the readme, run the following command:
  94. ```sh
  95. $ npm install -g verbose/verb#dev verb-generate-readme && verb
  96. ```
  97. </details>
  98. ### Related projects
  99. You might also be interested in these projects:
  100. * [babel-extract-comments](https://www.npmjs.com/package/babel-extract-comments): Uses babel (babylon) to extract JavaScript code comments from a JavaScript string or file. | [homepage](https://github.com/jonschlinkert/babel-extract-comments "Uses babel (babylon) to extract JavaScript code comments from a JavaScript string or file.")
  101. * [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.")
  102. * [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).")
  103. * [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://github.com/jonschlinkert/parse-code-context) | [homepage](https://github.com/jonschlinkert/parse-code-context "Parse code context in a single line of javascript, for functions, variable declarations, methods, prototype properties, prototype methods etc.")
  104. * [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.")
  105. ### Contributors
  106. | **Commits** | **Contributor** |
  107. | --- | --- |
  108. | 59 | [jonschlinkert](https://github.com/jonschlinkert) |
  109. | 4 | [charlike-old](https://github.com/charlike-old) |
  110. | 2 | [mk-pmb](https://github.com/mk-pmb) |
  111. | 1 | [kgryte](https://github.com/kgryte) |
  112. | 1 | [epicoxymoron](https://github.com/epicoxymoron) |
  113. ### Author
  114. **Jon Schlinkert**
  115. * [LinkedIn Profile](https://linkedin.com/in/jonschlinkert)
  116. * [GitHub Profile](https://github.com/jonschlinkert)
  117. * [Twitter Profile](https://twitter.com/jonschlinkert)
  118. ### License
  119. Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert).
  120. Released under the [MIT License](LICENSE).
  121. ***
  122. _This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on March 24, 2018._