GithubHelp home page GithubHelp logo

travisperson / strip-comments Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jonschlinkert/strip-comments

0.0 1.0 0.0 74 KB

Strip block comments or line comments from code. The tests remove comments from JavaScript, but this will work with any language that uses the same syntax for comments.

strip-comments's Introduction

strip-comments NPM version

Strip comments from code. Remove both line comments and block comments.

Should work with any language that uses the same syntax, e.g. JavaScript, LESS, SASS/SCSS, CSS.

Install

 npm i strip-comments --save
 npm install strip-comments --save

API

Table of contents

strip(str[, opts])

Strip all comments

  • str {String} file content or string to strip to
  • opts {Object} options are passed to .block, and .line
  • return {String}

Example:

/*!
 * this multiline
 * block comment ('top banner')
 */

'use strict';

/**!
 * and this multiline
 * block comment
 */
var foo = function(/* and these single-line block comments */) {};

/**
 * and this multiline
 * block comment
 */
var bar = function(/* and these single-line block comments */) {};

// this single-line line comment
var baz = function () {
  // this multiline
  // line comment
  var some = true;
  //this
  var fafa = true; //and this
  // var also = 'that';
  var but = 'not'; //! that comment
};

// also this multiline
// line comment
var fun = false;

Source:

var strip = module.exports = function(str, opts) {
  return str ? strip.block(strip.line(str, opts), opts) : '';
};

strip.block(str[, opts])

Strip only block comments

  • str {String} file content or string to strip to
  • opts {Object} if safe:true, strip only that not starts with /*! or /**!
  • return {String}

Example:

/**
 * this multiline
 * block comment
 */
var bar = function(/* and these single-line block comment */) {
  /**
   * also that comment
   */
  var str = 'something'
};

Source:

strip.block = function(str, opts) {
  opts = opts || {};
  var re = new RegExp(reBlock + reBlockEnd, 'gm');
  if(opts.safe) {
    re = new RegExp(reBlockIgnore + reBlockEnd, 'gm');
  }
  return str ? str.replace(re, '') : '';
};

strip.line(str[, opts])

Strip only line comments

  • str {String} file content or string to strip to
  • opts {Object} if safe:true, strip all that not starts with //!
  • return {String}

Example:

// this single-line line comment
var baz = function () {
  // this multiline
  // line comment
  var some = true;
  //this
  var fafa = true; //and this
  // var also = 'that';
  var but = 'not'; //! that comment
};

Source:

strip.line = function(str, opts) {
  opts = opts || {};
  var re = new RegExp(reLine, 'gm');
  if(opts.safe) {
    re = new RegExp(reLineIgnore, 'gm');
  }
  return str ? str.replace(re, '') : '';
};

Tests

mocha -R spec

Author

Jon Schlinkert

License

Copyright (c) 2014 Jon Schlinkert, contributors. Released under the MIT license

strip-comments's People

Contributors

jonschlinkert avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.