# extract-comments [![NPM version](https://img.shields.io/npm/v/extract-comments.svg)](https://www.npmjs.com/package/extract-comments) > 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). ## Install Install with [npm](https://www.npmjs.com/): ```sh $ npm i extract-comments --save ``` ## Usage ```js var extract = require('extract-comments'); // pass a string of javascript, CSS, LESS etc extract(string); ``` **Example** ```js 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', lines: [ 'this is', 'a comment' ], loc: { start: { line: 1, pos: 0 }, end: { line: 5, pos: 33 } }, code: { line: 7, loc: { start: { line: 7, pos: 36 }, end: { line: 7, pos: 52 } }, value: 'var foo = "bar";' } ``` ## API ### [extract](index.js#L25) Extract comments from the given `string`. **Params** * `string` **{String}** * `options` **{Object}**: Pass `first: true` to return after the first comment is found. * `returns` **{String}** **Example** ```js extract(str, options); ``` ### [.block](index.js#L48) 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** ```js extract.block(str, options); ``` ### [.line](index.js#L65) 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** ```js extract.line(str, options); ``` ### [.first](index.js#L79) 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}** ## Related * [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) * [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) ## Contributing Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/extract-comments/issues/new). ## Tests ### Run tests Install dev dependencies: ```sh $ npm i -d && npm test ``` ### Coverage As of December 30, 2015: ```sh Statements : 100% (133/133) Branches : 100% (32/32) Functions : 100% (19/19) Lines : 100% (132/132) ``` ## Release history **v0.10.0** * Parsing is now handled by esprima * Breaking change: since parsing is now done by esprima, on both the line and block comment objects, the `loc.start.pos` and `loc.end.pos` properties have been renamed to `loc.start.column` and `loc.end.column`. **v0.9.0** * Breaking change: `lines` property was removed from `Block` comments, since this can easily be done by splitting `value` ## Author **Jon Schlinkert** * [github/jonschlinkert](https://github.com/jonschlinkert) * [twitter/jonschlinkert](http://twitter.com/jonschlinkert) ## License Copyright © 2014-2015 [Jon Schlinkert](https://github.com/jonschlinkert) Released under the MIT license. *** _This file was generated by [verb](https://github.com/verbose/verb) on December 30, 2015._