
Security News
TypeScript is Porting Its Compiler to Go for 10x Faster Builds
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
extract-comments
Advanced tools
Uses esprima to extract line and block comments from a string of JavaScript. Also optionally parses code context (the next line of code after a comment).
Uses esprima to extract line and block comments from a string of JavaScript. Also optionally parses code context (the next line of code after a comment).
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your :heart: and support.
Install with npm:
$ npm install --save extract-comments
var extract = require('extract-comments');
// pass a string of JavaScript
extract(string);
Example
var str = '/**\n * this is\n *\n * a comment\n*/\n\n\nvar foo = "bar";\n';
var comments = extract(str);
console.log(comments);
[{
type: 'block',
raw: '/**\n * this is\n *\n * a comment\n*/',
value: 'this is\na comment',
loc: { start: { line: 1, column: 0 }, end: { line: 5, column: 33 } },
code:
{ line: 7,
loc: { start: { line: 7, column: 36 }, end: { line: 7, column: 52 } },
value: 'var foo = "bar";' }
By default, esprima is used for extracting comments. This can easily be changed by passing a function to options.extractor
.
The easy way
Use a published module, such as:
Example:
extract(str, {extractor: require('babel-extract-comments')});
If you create a compatible extractor, feel free to do pr or create an issue to add it to the readme!
Roll your own
extract(str, {
extractor: function(str) {
// must return an array of tokens with:
// - type: 'Block', 'CommentBlock', 'Line' or 'CommentLine'
// - value: the comment inner string
// - loc: with `start` and `end` line and column
// example:
return [
{
type: 'Block',
{start: { line: 1, column: 0 },
end: { line: 5, column: 33 }},
value: ' this is a comment string '
}
];
}
});
Extract comments from the given string
.
Params
string
{String}options
{Object}: Pass first: true
to return after the first comment is found.tranformFn
{Function}: (optional) Tranform function to modify each commentreturns
{Array}: Returns an array of comment objectsExample
const extract = require('extract-comments');
console.log(extract(string, options));
Extract block comments from the given string
.
Params
string
{String}options
{Object}: Pass first: true
to return after the first comment is found.returns
{String}Example
console.log(extract.block(string, options));
Extract line comments from the given string
.
Params
string
{String}options
{Object}: Pass first: true
to return after the first comment is found.returns
{String}Example
console.log(extract.line(string, options));
Extract the first comment from the given string
.
Params
string
{String}options
{Object}: Pass first: true
to return after the first comment is found.returns
{String}Example
console.log(extract.first(string, options));
v0.10.0
loc.start.pos
and loc.end.pos
properties have been renamed to loc.start.column
and loc.end.column
.v0.9.0
lines
property was removed from Block
comments, since this can easily be done by splitting value
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
$ npm install && npm test
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
$ npm install -g verbose/verb#dev verb-generate-readme && verb
You might also be interested in these projects:
Commits | Contributor |
---|---|
93 | jonschlinkert |
3 | cazzer |
1 | architectcodes |
Jon Schlinkert
Copyright © 2018, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on February 12, 2018.
FAQs
Uses esprima to extract line and block comments from a string of JavaScript. Also optionally parses code context (the next line of code after a comment).
The npm package extract-comments receives a total of 51,320 weekly downloads. As such, extract-comments popularity was classified as popular.
We found that extract-comments demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
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.
Security News
TypeScript is porting its compiler to Go, delivering 10x faster builds, lower memory usage, and improved editor performance for a smoother developer experience.
Research
Security News
The Socket Research Team has discovered six new malicious npm packages linked to North Korea’s Lazarus Group, designed to steal credentials and deploy backdoors.
Security News
Socket CEO Feross Aboukhadijeh discusses the open web, open source security, and how Socket tackles software supply chain attacks on The Pair Program podcast.