Socket
Socket
Sign inDemoInstall

strip-comments

Package Overview
Dependencies
12
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    strip-comments

Strip comments from code. Removes line comments, block comments, the first comment only, or all comments. Optionally leave protected comments unharmed.


Version published
Maintainers
1
Install size
361 kB
Created

Package description

What is strip-comments?

The strip-comments npm package is designed to remove comments from code. It supports stripping both line comments and block comments from source code strings, making it useful for cleaning up code or preparing it for minification.

What are strip-comments's main functionalities?

Remove line comments

This feature allows you to remove single line comments from your code. It's particularly useful for cleaning up code by removing unnecessary or explanatory comments that are not needed in production.

"use strict";\n// This is a line comment\nconst x = 1;\n// Another comment\nconsole.log(x);

Remove block comments

This feature enables the removal of block comments, which can span multiple lines. It's useful for eliminating large sections of commented-out code or detailed explanations that are not required in the final codebase.

"use strict";\n/* This is a block comment\n that spans multiple lines */\nconst x = 1;\nconsole.log(x);

Preserve important comments

Strip-comments can be configured to preserve comments that are marked as important, usually with a specific syntax like `/*!`. This is useful for keeping license information or other critical comments while removing the rest.

"use strict";\n/*! Preserve this comment */\n// This is a line comment\nconst x = 1;\nconsole.log(x);

Other packages similar to strip-comments

Changelog

Source

[0.4.4] - 2016-02-14

  • refactor to use babylon

Readme

Source

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

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.

Keywords

FAQs

Last updated on 14 Feb 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc