strip-comments
Advanced tools
Comparing version 0.3.4 to 0.4.0
122
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 @@ ] | ||
} | ||
} | ||
} |
# 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._ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
130
1
8643
1
4
95
1
1
+ Addedextract-comments@^0.8.5
+ Addedcr@0.1.0(transitive)
+ Addedexport-files@2.1.1(transitive)
+ Addedextend-shallow@2.0.1(transitive)
+ Addedextract-comments@0.8.7(transitive)
+ Addedis-extendable@0.1.1(transitive)
+ Addedlazy-cache@1.0.4(transitive)
+ Addednoncharacters@1.1.0(transitive)
+ Addedparse-code-context@0.2.2(transitive)
+ Addedquoted-string-regex@0.1.1(transitive)
+ Addedstrip-bom-string@0.1.2(transitive)