match-requires
Match require statements in a string. Returns an array of matching require statements. Each match is an object with line number, variable name, and module name. Statements in code comments are ignored.
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your ❤️ and support.
Install
Install with npm:
$ npm install --save match-requires
Usage
const matches = ;console;
Returns:
string: 'require(\'a-b-c\');' variable: '' name: 'a-b-c' string: 'var fooBar = require(\'foo-bar\');' variable: 'fooBar' name: 'foo-bar'
Ignore requires in code comments
To ignore require()
statements found inside code comments, pass true
as the second argument to strip comments before matching. Alternatively, you may pass a function as the second argument to use your own approach to stripping comments.
Without comments stripped
console; // [ { string: 'require(\'a-b-c\');', variable: '', name: 'a-b-c' },// { string: 'var fooBar = require(\'foo-bar\');',// variable: 'fooBar',// name: 'foo-bar' } ]
With comments stripped
console;// [ { string: 'var fooBar = require(\'foo-bar\');',// variable: 'fooBar',// name: 'foo-bar' } ]
With custom function
const str = '/* require(\'a-b-c\');*/\nvar fooBar = require(\'foo-bar\');';const fn = ;console;// [ { string: 'var fooBar = require(\'foo-bar\');',// variable: 'fooBar',// name: 'foo-bar' } ]
About
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Running Tests
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
Building docs
(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
Related projects
You might also be interested in these projects:
- requires-regex: Regular expression for matching javascript require statements. | homepage
- to-regex-range: Pass two numbers, get a regex-compatible source string for matching ranges. Validated against more than… more | homepage
- year-range-regex: Generates a regular expression for validating a range of years. | homepage
Author
Jon Schlinkert
License
Copyright © 2018, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on April 15, 2018.