
Research
Security News
Lazarus Strikes npm Again with New Wave of Malicious Packages
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.
extract-comments
Advanced tools
Extract code comments from string or from a glob of files.
Heads up! As of v0.7.0 this no longer has a .fromFile()
method to read from the file system. See [extracting from files].
Can be used with code-context to match comments up with related code.
npm i extract-comments --save
var extract = require('extract-comments');
// pass a string of javascript, CSS, LESS etc
extract(string);
Example
var str = '/**\n * this is\n *\n * a comment\n*/\nvar foo = "bar";\n';
extract(str);
Results in:
// key is the starting line number
{ '1':
{ begin: 1,
end: 5,
// line number of the code after the comment
codeStart: 7 } }
content: 'this is\n\na comment\n',
// sames as content, but split into blocks at double newlines
blocks: [
'this is',
'a comment\n'
],
// first line of code after the comment
after: 'var foo = "bar";',
(The reason the key is the starting line number is that it's easy to use this format with templates)
Customize output
// use code-context to parse the first line of code following
// the comment
var context = require('code-context');
// pass a function to modify the returned object
// and avoid looping more than once
var comments = extract(str, function(comment) {
comment.context = context(comment.after);
return comment;
});
Results in:
{ begin: 1,
content: 'this is\n\na comment\n',
after: 'var foo = "bar";',
end: 5,
codeStart: 7,
blocks: [ 'this is', 'a comment\n' ],
context:
[ { begin: 1,
type: 'declaration',
name: 'foo',
value: '"bar"',
string: 'foo',
original: 'var foo = "bar";' } ] }
Prior to v0.7.0, there was a method to extract code comments from files. Here is the equivalent code to accomplish the same thing:
var fs = require('fs');
var extract = require('extract-comments');
var mapFiles = require('map-files');
function extractComments(patterns, opts) {
opts = opts || {};
opts.name = opts.rename || function(fp) {
return fp;
};
opts.read = opts.read || function(fp, options) {
var code = fs.readFileSync(fp, 'utf8');
return extract(code, options);
};
return mapFiles(patterns, opts);
}
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue
Install dev dependencies.
npm i -d && npm test
Jon Schlinkert
Copyright (c) 2014-2015 Jon Schlinkert
Released under the MIT license
This file was generated by verb-cli on March 12, 2015.
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.
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.
Security News
Opengrep continues building momentum with the alpha release of its Playground tool, demonstrating the project's rapid evolution just two months after its initial launch.