Big News: Socket Selected for OpenAI's Cybersecurity Grant Program.Details
Socket
Book a DemoSign in
Socket

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.3.4
to
0.4.0
+82
-40
index.js

@@ -1,65 +0,99 @@

/*!
* strip-comments <https://github.com/jonschlinkert/strip-comments>
'use strict';
var extract = require('extract-comments');
/**
* Strip comments from the given `string`.
*
* Copyright (c) 2014-2015 Jon Schlinkert.
* Licensed under the MIT license.
* @param {String} `string`
* @param {Object} `options` Pass `safe: true` to keep comments with `!`
* @return {String}
* @api public
*/
'use strict';
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);
}
var reBlock = /\/\*(?!\/)(.|[\r\n]|\n)+?\*\/\n?\n?/gm;
var reBlockIgnore = /\/\*(?!(\*?\/|\*?\!))(.|[\r\n]|\n)+?\*\/\n?\n?/gm;
var reLine = /(^|[^\S\n])(?:\/\/)([\s\S]+?)$/gm;
var reLineIgnore = /(^|[^\S\n])(?:\/\/[^!])([\s\S]+?)$/gm;
/**
* Strip block comments from the given `string`.
*
* @param {String} `string`
* @param {Object} `options` Pass `safe: true` to keep comments with `!`
* @return {String}
* @api public
*/
function block(str, options) {
return stripEach(str, extract.block(str, options), options);
}
/**
* Strip all comments
* Strip line comments from the given `string`.
*
* @param {String} `str` file contents or string to strip.
* @param {Object} `opts` options are passed to `.block`, and `.line`
* @return {String} String without comments.
* @param {String} `string`
* @param {Object} `options` Pass `safe: true` to keep comments with `!`
* @return {String}
* @api public
*/
function strip(str, opts) {
return str ? strip.block(strip.line(str, opts), opts) : '';
function line(str, options) {
return stripEach(str, extract.line(str, options), options);
}
/**
* Strip only block comments, optionally leaving protected comments
* (e.g. `/*!`) intact.
* Strip the first comment from the given `string`.
*
* @param {String} `str` file content or string to strip to
* @param {Object} `opts` if `safe:true`, strip only comments that do not start with `/*!` or `/**!`
* @return {String} String without block comments.
* @param {String} `string`
* @param {Object} `options` Pass `safe: true` to keep comments with `!`
* @return {String}
* @api public
*/
strip.block = function(str, opts) {
opts = opts || {};
var re = reBlock; //new RegExp(reBlock + reBlockEnd, 'gm');
if(opts.safe) {
re = reBlockIgnore; //new RegExp(reBlockIgnore + reBlockEnd, 'gm');
}
return str ? str.replace(re, '') : '';
};
function first(str, options) {
return stripEach(str, extract.first(str), options);
}
/**
* Private function for stripping comments.
*
* @param {String} `string`
* @param {Object} `options` Pass `safe: true` to keep comments with `!`
* @return {String}
*/
function stripEach(str, comments, options) {
comments.forEach(function(comment) {
str = discard(str, comment, options);
});
return str;
}
/**
* Strip only line comments
* Remove a comment from the given string.
*
* @param {String} `str` file content or string to strip to
* @param {Object} `opts` if `safe:true`, strip all that not starts with `//!`
* @return {String} String without line comments.
* @param {String} `string`
* @param {Object} `options` Pass `safe: true` to keep comments with `!`
* @return {String}
* @api public
*/
strip.line = function(str, opts) {
opts = opts || {};
var re = reLine;
if(opts.safe) {
re = reLineIgnore;
function discard(str, comment, opts) {
var ch = comment.value.charAt(0);
if (opts && opts.safe === true && ch === '!') {
return str;
}
return str ? str.replace(re, '') : '';
};
return str.split(comment.raw).join('');
}

@@ -70,2 +104,10 @@ /**

module.exports = 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 both line comments and/or block comments, with options to leave protected comments unharmed.",
"version": "0.3.4",
"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.0",
"homepage": "https://github.com/jonschlinkert/strip-comments",
"author": "Jon Schlinkert (https://github.com/jonschlinkert)",
"repository": "jonschlinkert/strip-comments",
"bugs": {
"url": "https://github.com/jonschlinkert/strip-comments/issues"
"bugs": "https://github.com/jonschlinkert/strip-comments/issues",
"license": "MIT",
"files": [
"index.js"
],
"engines": {
"node": ">=0.10.0"
},
"license": "MIT",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"engines": {
"node": ">=0.10.0"
"dependencies": {
"extract-comments": "^0.8.5"
},

@@ -40,2 +43,3 @@ "devDependencies": {

"list": [
"snapdragon",
"esprima-extract-comments",

@@ -45,2 +49,3 @@ "extract-comments",

"parse-comments",
"code-context",
"parse-code-context"

@@ -50,2 +55,2 @@ ]

}
}
}
+42
-21
# strip-comments [![NPM version](https://badge.fury.io/js/strip-comments.svg)](http://badge.fury.io/js/strip-comments) [![Build Status](https://travis-ci.org/jonschlinkert/strip-comments.svg)](https://travis-ci.org/jonschlinkert/strip-comments)
> Strip comments from code. Removes both line comments and/or block comments, with options to leave protected comments unharmed.
> Strip comments from code. Removes line comments, block comments, the first comment only, or all comments. Optionally leave protected comments unharmed.

@@ -23,11 +23,11 @@ ## Install

### [strip](index.js#L24)
### [strip](index.js#L14)
Strip all comments
Strip comments from the given `string`.
**Params**
* `str` **{String}**: file contents or string to strip.
* `opts` **{Object}**: options are passed to `.block`, and `.line`
* `returns` **{String}**: String without comments.
* `string` **{String}**
* `options` **{Object}**: Pass `safe: true` to keep comments with `!`
* `returns` **{String}**

@@ -41,12 +41,11 @@ **Example**

### [.block](index.js#L38)
### [block](index.js#L38)
Strip only block comments, optionally leaving protected comments
(e.g. `/*!`) intact.
Strip block comments from the given `string`.
**Params**
* `str` **{String}**: file content or string to strip to
* `opts` **{Object}**: if `safe:true`, strip only comments that do not start with `/*!` or `/**!`
* `returns` **{String}**: String without block comments.
* `string` **{String}**
* `options` **{Object}**: Pass `safe: true` to keep comments with `!`
* `returns` **{String}**

@@ -56,15 +55,15 @@ **Example**

```js
console.log(strip("foo // this is a comment\n/* me too */"));
console.log(strip.block("foo // this is a comment\n/* me too */"));
//=> 'foo // this is a comment\n'
```
### [.line](index.js#L57)
### [line](index.js#L51)
Strip only line comments
Strip line comments from the given `string`.
**Params**
* `str` **{String}**: file content or string to strip to
* `opts` **{Object}**: if `safe:true`, strip all that not starts with `//!`
* `returns` **{String}**: String without line comments.
* `string` **{String}**
* `options` **{Object}**: Pass `safe: true` to keep comments with `!`
* `returns` **{String}**

@@ -74,8 +73,29 @@ **Example**

```js
console.log(strip("foo /* me too */"));
console.log(strip.line("foo /* me too */"));
//=> 'foo'
```
### [first](index.js#L64)
Strip the first comment from the given `string`.
**Params**
* `string` **{String}**
* `options` **{Object}**: Pass `safe: true` to keep comments with `!`
* `returns` **{String}**
### [discard](index.js#L92)
Remove a comment from the given string.
**Params**
* `string` **{String}**
* `options` **{Object}**: Pass `safe: true` to keep comments with `!`
* `returns` **{String}**
## Related projects
* [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://www.npmjs.com/package/code-context) | [homepage](https://github.com/jonschlinkert/code-context)
* [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)

@@ -86,2 +106,3 @@ * [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)

* [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)
* [snapdragon](https://www.npmjs.com/package/snapdragon): snapdragon is an extremely pluggable, powerful and easy-to-use parser-renderer factory. | [homepage](https://github.com/jonschlinkert/snapdragon)

@@ -98,3 +119,3 @@ ## Running tests

Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/strip-comments/issues/new).
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](/new).

@@ -115,2 +136,2 @@ ## Author

_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on October 22, 2015._
_This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on November 04, 2015._
# Enforce Unix newlines
*.* text eol=lf
*.css text eol=lf
*.html text eol=lf
*.js text eol=lf
*.json text eol=lf
*.less text eol=lf
*.md text eol=lf
*.yml text eol=lf
*.jpg binary
*.gif binary
*.png binary
*.jpeg binary
{
"esnext": true,
"boss": true,
"curly": true,
"eqeqeq": true,
"eqnull": true,
"immed": true,
"latedef": true,
"newcap": true,
"noarg": true,
"node": true,
"sub": true,
"undef": true,
"unused": true,
"globals": {
"define": true,
"before": true,
"after": true,
"describe": true,
"it": true
}
}

Sorry, the diff of this file is not supported yet

language: node_js
node_js:
- 'stable'
- '0.12'
- '0.10'
# {%= name %} {%= badge("fury") %} {%= badge("travis") %}
> {%= description %}
## Install
{%= include("install-npm", {save: true}) %}
## Usage
```js
var strip = require('{%= name %}');
console.log(strip('Hey! // foo'));
//=> 'Hey !';
```
## API
{%= apidocs("index.js") %}
## Related projects
{%= related(verb.related.list) %}
## Running tests
{%= include("tests") %}
## Contributing
{%= include("contributing") %}
## Author
{%= include("author") %}
## License
{%= copyright() %}
{%= license() %}
***
{%= include("footer") %}
```js
//example.strip
console.log(strip("foo // this is a comment\n/* me too */"));
//=> 'foo'
```
```js
//example.block
console.log(strip("foo // this is a comment\n/* me too */"));
//=> 'foo // this is a comment\n'
```
```js
//example.line
console.log(strip("foo /* me too */"));
//=> 'foo'
```
{
"name": "strip-comments",
"description": "Strip comments from code. Removes both line comments and/or block comments, with options to leave protected comments unharmed.",
"repository": "jonschlinkert/strip-comments",
"license": "MIT",
"homepage": "https://github.com/jonschlinkert/strip-comments",
"authors": [
"Jon Schlinkert (https://github.com/jonschlinkert)"
],
"main": [
"index.js"
],
"devDependencies": {
"mocha": "*",
"should": "*"
},
"keywords": [
"block",
"comment",
"comments",
"expressions",
"line",
"protected",
"re",
"regex",
"regexp",
"regular",
"remove",
"strip"
]
}
'use strict';
var foo = function() {};
var bar = function() {};
var baz = function() {
var some = true;
var fafa = true;
var but = 'not';
};
var fun = false;
var path = '/path/to/*/something/that/not/be/stripped.js';
var globstar = '/path/to/globstar/not/be/stripped/**/*.js';
/*!
* strip this multiline
* block comment
*/
'use strict';
/**!
* and this multiline
* block comment
*/
var foo = function() {};
var bar = function() {};
// 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;
var path = '/path/to/*/something/that/not/be/stripped.js';
var globstar = '/path/to/globstar/not/be/stripped/**/*.js';
/**
* this block comment
* will not be striped
*/
'use strict';
//! and this multiline
//! block comment
var foo = function(/* and these single-line block comment */) {};
/**
* and this
* multiline block
* comment
*/
var bar = function(/* and that */) {};
var baz = function() {
var some = true;
var fafa = true;
var but = 'not'; //! that comment
};
var path = '/path/to/*/something/that/not/be/stripped.js';
var globstar = '/path/to/globstar/not/be/stripped/**/*.js';
'use strict';
process.stdout.write('string literals: ');
console.dir({
str0: '&apos;',
str1: "&quot;",
str2: ". // ' \\ . // ' \\ .",
});
process.stdout.write('RegExp literals: ');
console.dir({
regexp0: /I'm the easiest in Chomsky hierarchy!/,
});
/*!
* strip this multiline
* block comment
*/
'use strict';
/**!
* and this multiline
* block comment
*/
var foo = function(/* and these single-line block comment */) {};
/**
* and this
* multiline block
* comment
*/
var bar = function(/* and that */) {};
// 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;
var path = '/path/to/*/something/that/not/be/stripped.js';
var globstar = '/path/to/globstar/not/be/stripped/**/*.js';
/**
* this block comment
* will not be striped
*/
'use strict';
//! and this multiline
//! block comment
var foo = function(/* and these single-line block comment */) {};
/**
* and this
* multiline block
* comment
*/
var bar = function(/* and that */) {};
//will be removed
var baz = function() {
// this multiline
// line comment
var some = true;
// will be
var fafa = true;
// var removed = 'yes';
var but = 'not'; //! that comment
};
var path = '/path/to/*/something/that/not/be/stripped.js';
var globstar = '/path/to/globstar/not/be/stripped/**/*.js';
'use strict';
var fs = require('fs');
var strip = require('../index');
require('should');
require('mocha');
function read(src) {
var str = fs.readFileSync('./' + src,'utf-8');
return str;
}
function normalize(str) {
return str.replace(/[\n\r\s]+/g, '');
}
describe('strip:', function () {
it('should strip all comments.', function () {
var actual = strip("foo // this is a comment\n/* me too */")
var expected = 'foo';
normalize(actual).should.eql(normalize(expected));
});
it('should strip line comments.', function () {
var actual = strip.line("foo // this is a comment\n/* me too */")
var expected = 'foo\n/* me too */';
normalize(actual).should.eql(normalize(expected));
});
it('should strip block comments.', function () {
var actual = strip.block("foo // this is a comment\n/* me too */")
var expected = 'foo // this is a comment\n';
normalize(actual).should.eql(normalize(expected));
});
it('should strip all but not `/*/`', function() {
var actual = strip("/* I will be stripped */\nvar path = '/and/this/*/not/be/stripped';")
var expected = "\nvar path = '/and/this/*/not/be/stripped';"
normalize(actual).should.eql(normalize(expected));
})
it('should strip all but not globstars `/**/*` #1', function() {
var actual = strip("var path = './path/to/scripts/**/*.js';")
var expected = "var path = './path/to/scripts/**/*.js';"
normalize(actual).should.eql(normalize(expected));
})
it('should strip all but not globstars `/**/` #2 and `//!` line comments (safe:true)', function() {
var actual = strip("var partPath = './path/*/to/scripts/**/'; //! line comment", {safe:true})
var expected = "var partPath = './path/*/to/scripts/**/'; //! line comment"
normalize(actual).should.eql(normalize(expected));
})
it('should strip all but not `/*/*something` from anywhere', function() {
var actual = strip("var partPath = './path/*/*something/test.txt';")
var expected = "var partPath = './path/*/*something/test.txt';"
normalize(actual).should.eql(normalize(expected));
})
it('should strip all but not `/*/*something/*.js` from anywhere (globstar-like)', function() {
var actual = strip("var partPath = './path/*/*something/*.js';")
var expected = "var partPath = './path/*/*something/*.js';"
normalize(actual).should.eql(normalize(expected));
})
it('should leave alone code without any comments', function() {
var fixture = read('test/fixtures/no-comment.js');
var actual = strip(fixture);
var expected = fixture;
actual.should.eql(expected);
})
});
describe('strip all or empty:', function () {
it('should not throw an error at an empty string.', function () {
var actual = strip();
var expected = '';
normalize(actual).should.eql(normalize(expected));
});
it('should strip all multiline, singleline, block and line comments.', function () {
var fixture = read('test/fixtures/strip-all.js');
var actual = strip(fixture);
var expected = read('test/expected/strip-all.js');
normalize(actual).should.eql(normalize(expected));
});
it('should strip only all block comments that not starts with `/*!` or `/**!` (safe:true).', function () {
var fixture = read('test/fixtures/strip-all.js');
var actual = strip.block(fixture, {safe:true});
var expected = read('test/expected/strip-keep-block.js');
normalize(actual).should.eql(normalize(expected));
});
it('should strip only all line comments that not starts with `//!` (safe:true).', function () {
var fixture = read('test/fixtures/strip-keep-line.js');
var actual = strip.line(fixture, {safe:true});
var expected = read('test/expected/strip-keep-line.js');
normalize(actual).should.eql(normalize(expected));
});
});
describe('block comments:', function () {
it('should strip block comments from a function.', function () {
var actual = strip.block('var bar = function(/* this is a comment*/) {return;};');
var expected = 'var bar = function() {return;};';
normalize(actual).should.eql(normalize(expected));
});
it('should strip block comments before and from a function.', function () {
var actual = strip.block('/* this is a comment */\nvar bar = function(/*this is a comment*/) {return;};');
var expected = 'var bar = function() {return;};';
normalize(actual).should.eql(normalize(expected));
});
it('should strip block comments before, after and from a function.', function () {
var actual = strip.block('/* this is a comment */var bar = function(/*this is a comment*/) {return;};\n/* this is a comment*/');
var expected = 'var bar = function() {return;};\n';
normalize(actual).should.eql(normalize(expected));
});
});
describe('line comments:', function () {
it('should strip line comments.', function () {
var actual = strip.line('// this is a line comment\nvar bar = function(/*this is a comment*/) {return;};');
var expected = '\nvar bar = function(/*this is a comment*/) {return;};';
normalize(actual).should.eql(normalize(expected));
});
it('should strip line comments with leading whitespace.', function () {
var actual = strip.line(' // this should be stripped');
var expected = '';
normalize(actual).should.eql(normalize(expected));
});
it('should not strip line comments in variables.', function () {
var actual = strip.line('var foo = "//this is not a comment";');
var expected = 'var foo = "//this is not a comment";';
normalize(actual).should.eql(normalize(expected));
});
it('should not strip line comments in variables, but should after.', function () {
var actual = strip.line('var foo = "//this is not a comment"; //this should be stripped');
var expected = 'var foo = "//this is not a comment";';
normalize(actual).should.eql(normalize(expected));
});
it('should not be whitespace sensitive.', function () {
var actual = strip.line('var foo = "//this is not a comment"; // this should be stripped');
var expected = 'var foo = "//this is not a comment";';
normalize(actual).should.eql(normalize(expected));
});
it('should not strip URLs in a variable.', function () {
var actual = strip.line('var foo = "http://github.com"; // this should be stripped');
var expected = 'var foo = "http://github.com";';
normalize(actual).should.eql(normalize(expected));
});
it('should strip URLs in a line comment.', function () {
var actual = strip.line('// http://github.com"');
var expected = '';
normalize(actual).should.eql(normalize(expected));
});
it('should strip URLs in a block comment.', function () {
var actual = strip.block('/**\n* http://github.com\n *\n */');
var expected = '';
normalize(actual).should.eql(normalize(expected));
});
it('should strip line comments before a function, and not block comments.', function () {
var actual = strip.line('/* this is a comment */\n//this is a comment\nvar bar = function(/*this is a comment*/) {return;};');
var expected = '/* this is a comment */\n\nvar bar = function(/*this is a comment*/) {return;};';
normalize(actual).should.eql(normalize(expected));
});
it('should strip line comments before and after a function, and not block comments.', function () {
var actual = strip.line('/* this is a comment */\n//this is a comment\nvar bar = function(/*this is a comment*/) {return;};\n//this is a line comment');
var expected = '/* this is a comment */\n\nvar bar = function(/*this is a comment*/) {return;};\n';
normalize(actual).should.eql(normalize(expected));
});
});