Socket
Socket
Sign inDemoInstall

strip-comments

Package Overview
Dependencies
Maintainers
1
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

strip-comments - npm Package Compare versions

Comparing version 0.4.3 to 0.4.4

208

index.js

@@ -0,69 +1,131 @@

/*!
* strip-comments <https://github.com/jonschlinkert/strip-comments>
*
* Copyright (c) 2014-2016, Jon Schlinkert.
* Released under the MIT license.
*/
'use strict';
var extend = require('extend-shallow');
var extract = require('extract-comments');
/**
* Strip comments from the given `string`.
* Strip all code comments from the given `input`,
* including these that are ignored.
* Pass `opts.safe: true` to keep them.
*
* **Example**
*
* ```js
* //example.strip
* console.log(strip("foo // this is a comment\n/* me too *\/"));
* //=> 'foo'
* var str = strip('foo; // this is a comment\n /* me too *\/');
* console.log(str);
* // => 'foo; \n '
* ```
* @name strip
* @param {String} `string`
* @param {Object} `options` Pass `safe: true` to keep comments with `!`
* @return {String}
*
* @name strip
* @param {String} `<input>` string from which to strip comments
* @param {Object} `opts` optional options, passed to [extract-comments][extract-comments]
* @option {Boolean} [opts] `line` if `false` strip only block comments, default `true`
* @option {Boolean} [opts] `block` if `false` strip only line comments, default `true`
* @option {Boolean} [opts] `safe` pass `true` to keep ignored comments (e.g. `/*!` and `//!`)
* @option {Boolean} [opts] `preserveNewlines` if `true` preserve newlines after comments are stripped
* @return {String} modified input
* @api public
*/
function strip(str, options) {
options = options || {};
if (options.line) {
return line(str, options);
}
if (options.block) {
return block(str, options);
}
if (options.first) {
return first(str, options);
}
str = block(str, options);
return line(str, options);
}
exports = module.exports = function stripAllComments(input, opts) {
opts = extend({block: true, line: true}, opts);
return stripComments(input, opts);
};
/**
* Strip block comments from the given `string`.
* Strip only block comments.
*
* @name .block
* @param {String} `string`
* @param {Object} `options` Pass `safe: true` to keep comments with `!`
* @return {String}
* **Example**
*
* ```js
* var output = strip('foo; // this is a comment\n /* me too *\/', { line: false });
* console.log(output);
* // => 'foo; // this is a comment\n '
* ```
*
* **Example**
*
* ```js
* var output = strip.block('foo; // this is a comment\n /* me too *\/');
* console.log(output);
* // => 'foo; // this is a comment\n '
* ```
*
* @name .block
* @param {String} `<input>` string from which to strip comments
* @param {Object} `[opts]` pass `opts.safe: true` to keep ignored comments (e.g. `/*!`)
* @return {String} modified string
* @api public
*/
function block(str, options) {
return stripEach(str, extract.block(str, options), options);
}
exports.block = function stripBlockComments(input, opts) {
opts = extend({block: true}, opts);
return stripComments(input, opts);
};
/**
* Strip line comments from the given `string`.
* Strip only line comments.
*
* @name .line
* @param {String} `string`
* @param {Object} `options` Pass `safe: true` to keep comments with `!`
* @return {String}
* **Example**
*
* ```js
* var output = strip('foo; // this is a comment\n /* me too *\/', { block: false });
* console.log(output);
* // => 'foo; \n /* me too *\/'
* ```
*
* **Example**
*
* ```js
* var output = strip.line('foo; // this is a comment\n /* me too *\/');
* console.log(output);
* // => 'foo; \n /* me too *\/'
* ```
*
* @name .line
* @param {String} `<input>` string from which to strip comments
* @param {Object} `[opts]` pass `opts.safe: true` to keep ignored comments (e.g. `//!`)
* @return {String} modified string
* @api public
*/
function line(str, options) {
return stripEach(str, extract.line(str, options), options);
}
exports.line = function stripLineComments(input, opts) {
opts = extend({line: true}, opts);
return stripComments(input, opts);
};
/**
* Strip the first comment from the given `string`.
* Strip the first comment from the given `input`.
* If `opts.safe: true` is passed, will strip the first that is not ignored.
*
* **Example**
*
* ```js
* var str = '//! first comment\nfoo; // this is a comment';
* var output = strip(str, {
* first: true
* });
* console.log(output);
* // => '\nfoo; // this is a comment'
* ```
*
* **Example**
*
* ```js
* var str = '//! first comment\nfoo; // this is a comment';
* var output = strip.first(str, { safe: true });
* console.log(output);
* // => '//! first comment\nfoo; '
* ```
*
* @name .first
* @param {String} `string`
* @param {Object} `options` Pass `safe: true` to keep comments with `!`
* @param {String} `<input>`
* @param {Object} `[opts]` pass `opts.safe: true` to keep comments with `!`
* @return {String}

@@ -73,5 +135,6 @@ * @api public

function first(str, options) {
return stripEach(str, extract.first(str), options);
}
exports.first = function stripFirstComment(input, opts) {
opts = extend({block: true, line: true, first: true}, opts);
return stripComments(input, opts);
};

@@ -81,12 +144,28 @@ /**

*
* @param {String} `string`
* @param {Object} `options` Pass `safe: true` to keep comments with `!`
* @param {String} `<input>`
* @param {Object} `[opts]`
* @return {String}
* @api private
*/
function stripComments(input, opts) {
// strip all by default, including `ingored` comments.
opts = extend({
block: false,
line: false,
safe: false,
first: false
}, opts);
function stripEach(str, comments, options) {
comments.forEach(function(comment) {
str = discard(str, comment, options);
});
return str;
// compatibility with `extract-comments`
opts.keepProtected = opts.safe;
var comments = extract(input, opts);
var len = comments.length;
var i = 0;
while (i < len) {
var comment = comments[i++];
input = discard(input, comment, opts);
}
return input;
}

@@ -97,4 +176,5 @@

*
* @param {String} `string`
* @param {Object} `options` Pass `safe: true` to keep comments with `!`
* @param {String} `str`
* @param {Object} `comment`
* @param {Object} `opts`
* @return {String}

@@ -112,17 +192,9 @@ */

}
return str.replace(comment.raw, nl);
if (comment.type === 'line') {
str = str.replace('//' + comment.raw, nl);
}
if (comment.type === 'block') {
str = str.replace('/*' + comment.raw + '*/', nl);
}
return str;
}
/**
* Expose `strip`
*/
module.exports = strip;
/**
* Expose methods
*/
module.exports.block = block;
module.exports.first = first;
module.exports.line = line;
{
"name": "strip-comments",
"description": "Strip comments from code. Removes line comments, block comments, the first comment only, or all comments. Optionally leave protected comments unharmed.",
"version": "0.4.3",
"version": "0.4.4",
"homepage": "https://github.com/jonschlinkert/strip-comments",

@@ -22,7 +22,8 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)",

"dependencies": {
"extract-comments": "^0.8.7"
"extend-shallow": "^2.0.1",
"extract-comments": "^0.10.1"
},
"devDependencies": {
"mocha": "*",
"should": "*"
"gulp-format-md": "^0.1.5",
"mocha": "*"
},

@@ -44,2 +45,9 @@ "keywords": [

"verb": {
"toc": true,
"tasks": [
"readme"
],
"plugins": [
"gulp-format-md"
],
"related": {

@@ -55,7 +63,4 @@ "list": [

]
},
"plugins": [
"gulp-format-md"
]
}
}
}

@@ -5,5 +5,18 @@ # strip-comments [![NPM version](https://img.shields.io/npm/v/strip-comments.svg)](https://www.npmjs.com/package/strip-comments) [![Build Status](https://img.shields.io/travis/jonschlinkert/strip-comments.svg)](https://travis-ci.org/jonschlinkert/strip-comments)

## Table Of Contents
- [Install](#install)
- [Usage](#usage)
- [API](#api)
- [Related projects](#related-projects)
- [Running tests](#running-tests)
- [Contributing](#contributing)
- [Author](#author)
- [License](#license)
_(TOC generated by [verb](https://github.com/verbose/verb) using [markdown-toc](https://github.com/jonschlinkert/markdown-toc))_
## Install
Install with [npm](https://www.npmjs.com/)
Install with [npm](https://www.npmjs.com/):

@@ -16,2 +29,4 @@ ```sh

For more use-cases see the [tests](./test/test.js)
```js

@@ -25,78 +40,104 @@ var strip = require('strip-comments');

### [strip](index.js#L20)
### [strip](index.js#L37)
Strip comments from the given `string`.
Strip all code comments from the given `input`, including these that are ignored. Pass `opts.safe: true` to keep them.
**Params**
* `string` **{String}**
* `options` **{Object}**: Pass `safe: true` to keep comments with `!`
* `returns` **{String}**
*
`<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**
```js
//example.strip
console.log(strip("foo // this is a comment\n/* me too *\/"));
//=> 'foo'
var str = strip('foo; // this is a comment\n /* me too *\/');
console.log(str);
// => 'foo; \n '
```
### [.block](index.js#L45)
### [.block](index.js#L68)
Strip block comments from the given `string`.
Strip only block comments.
**Params**
* `string` **{String}**
* `options` **{Object}**: Pass `safe: true` to keep comments with `!`
* `returns` **{String}**
* `<input>` **{String}**: string from which to strip comments
* `[opts]` **{Object}**: pass `opts.safe: true` to keep ignored comments (e.g. `/*!`)
* `returns` **{String}**: modified string
**Example**
**Examples**
```js
console.log(strip.block("foo // this is a comment\n/* me too */"));
//=> 'foo // this is a comment\n'
var output = strip('foo; // this is a comment\n /* me too *\/', { line: false });
console.log(output);
// => 'foo; // this is a comment\n '
```
### [.line](index.js#L59)
```js
var output = strip.block('foo; // this is a comment\n /* me too *\/');
console.log(output);
// => 'foo; // this is a comment\n '
```
Strip line comments from the given `string`.
### [.line](index.js#L99)
Strip only line comments.
**Params**
* `string` **{String}**
* `options` **{Object}**: Pass `safe: true` to keep comments with `!`
* `returns` **{String}**
* `<input>` **{String}**: string from which to strip comments
* `[opts]` **{Object}**: pass `opts.safe: true` to keep ignored comments (e.g. `//!`)
* `returns` **{String}**: modified string
**Example**
**Examples**
```js
console.log(strip.line("foo /* me too */"));
//=> 'foo'
var output = strip('foo; // this is a comment\n /* me too *\/', { block: false });
console.log(output);
// => 'foo; \n /* me too *\/'
```
### [.first](index.js#L73)
```js
var output = strip.line('foo; // this is a comment\n /* me too *\/');
console.log(output);
// => 'foo; \n /* me too *\/'
```
Strip the first comment from the given `string`.
### [.first](index.js#L135)
Strip the first comment from the given `input`. If `opts.safe: true` is passed, will strip the first that is not ignored.
**Params**
* `string` **{String}**
* `options` **{Object}**: Pass `safe: true` to keep comments with `!`
* `<input>` **{String}**
* `[opts]` **{Object}**: pass `opts.safe: true` to keep comments with `!`
* `returns` **{String}**
## Options
**Examples**
### options.preserveNewlines
```js
var str = '//! first comment\nfoo; // this is a comment';
var output = strip(str, {
first: true
});
console.log(output);
// => '\nfoo; // this is a comment'
```
Preserve newlines after comments are stripped.
**Type**: `boolean`
**Default**: `undefined`
**Example**
```js
strip(commentString, { preserveNewlines: true });
var str = '//! first comment\nfoo; // this is a comment';
var output = strip.first(str, { safe: true });
console.log(output);
// => '//! first comment\nfoo; '
```

@@ -108,3 +149,3 @@

* [esprima-extract-comments](https://www.npmjs.com/package/esprima-extract-comments): Extract code comments from string or from a glob of files using esprima. | [homepage](https://github.com/jonschlinkert/esprima-extract-comments)
* [extract-comments](https://www.npmjs.com/package/extract-comments): Extract code comments from string or from a glob of files. | [homepage](https://github.com/jonschlinkert/extract-comments)
* [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://www.npmjs.com/package/extract-comments) | [homepage](https://github.com/jonschlinkert/extract-comments)
* [js-comments](https://www.npmjs.com/package/js-comments): Parse JavaScript code comments and generate API documentation. | [homepage](https://github.com/jonschlinkert/js-comments)

@@ -136,7 +177,7 @@ * [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://www.npmjs.com/package/parse-code-context) | [homepage](https://github.com/jonschlinkert/parse-code-context)

Copyright © 2015 [Jon Schlinkert](https://github.com/jonschlinkert)
Released under the MIT license.
Copyright © 2014-2016 [Jon Schlinkert](https://github.com/jonschlinkert)
Released under the [MIT license](https://github.com/jonschlinkert/strip-comments/blob/master/LICENSE).
***
_This file was generated by [verb](https://github.com/verbose/verb) on December 25, 2015._
_This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on February 14, 2016._
SocketSocket SOC 2 Logo

Product

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc