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.

78 lines
1.9 KiB

4 years ago
  1. # strip-json-comments [![Build Status](https://travis-ci.com/sindresorhus/strip-json-comments.svg?branch=master)](https://travis-ci.com/github/sindresorhus/strip-json-comments)
  2. > Strip comments from JSON. Lets you use comments in your JSON files!
  3. This is now possible:
  4. ```js
  5. {
  6. // Rainbows
  7. "unicorn": /* ❤ */ "cake"
  8. }
  9. ```
  10. It will replace single-line comments `//` and multi-line comments `/**/` with whitespace. This allows JSON error positions to remain as close as possible to the original source.
  11. Also available as a [Gulp](https://github.com/sindresorhus/gulp-strip-json-comments)/[Grunt](https://github.com/sindresorhus/grunt-strip-json-comments)/[Broccoli](https://github.com/sindresorhus/broccoli-strip-json-comments) plugin.
  12. ## Install
  13. ```
  14. $ npm install strip-json-comments
  15. ```
  16. ## Usage
  17. ```js
  18. const json = `{
  19. // Rainbows
  20. "unicorn": /* ❤ */ "cake"
  21. }`;
  22. JSON.parse(stripJsonComments(json));
  23. //=> {unicorn: 'cake'}
  24. ```
  25. ## API
  26. ### stripJsonComments(jsonString, options?)
  27. #### jsonString
  28. Type: `string`
  29. Accepts a string with JSON and returns a string without comments.
  30. #### options
  31. Type: `object`
  32. ##### whitespace
  33. Type: `boolean`\
  34. Default: `true`
  35. Replace comments with whitespace instead of stripping them entirely.
  36. ## Benchmark
  37. ```
  38. $ npm run bench
  39. ```
  40. ## Related
  41. - [strip-json-comments-cli](https://github.com/sindresorhus/strip-json-comments-cli) - CLI for this module
  42. - [strip-css-comments](https://github.com/sindresorhus/strip-css-comments) - Strip comments from CSS
  43. ---
  44. <div align="center">
  45. <b>
  46. <a href="https://tidelift.com/subscription/pkg/npm-strip-json-comments?utm_source=npm-strip-json-comments&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
  47. </b>
  48. <br>
  49. <sub>
  50. Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
  51. </sub>
  52. </div>