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.
Install with npm
npm i match-requires --save
Usage
var re = require('match-requires');
console.log(re('require(\'a-b-c\');\nvar fooBar = require(\'foo-bar\');'))
Returns:
[ { line: 1,
variable: '',
module: 'a-b-c',
original: 'require(\'a-b-c\');' },
{ line: 2,
variable: 'fooBar',
module: 'foo-bar',
original: 'var fooBar = require(\'foo-bar\');' } ]
To ignore require statements found in code comments, pass true as the second arg:
re('/* require(\'a-b-c\');*/\nvar fooBar = require(\'foo-bar\');');
Returns:
[ { line: 2,
variable: 'fooBar',
module: 'foo-bar',
original: 'var fooBar = require(\'foo-bar\');' } ]
You may also pass a custom function for stripping code comments and/or quoted strings.
var str = '/* require(\'a-b-c\');*/\nvar fooBar = require(\'foo-bar\');';
re(str, function(content) {
return require('my-own-comment-stripper')(content);
});
Benchmarks
See the generated output that each lib produces for each benchmark.
crequire.js x 153,480 ops/sec ±0.71% (97 runs sampled)
detective.js x 55,015 ops/sec ±1.04% (91 runs sampled)
match-requires.js x 2,439,019 ops/sec ±0.77% (98 runs sampled)
crequire.js x 391,131 ops/sec ±0.63% (96 runs sampled)
detective.js x 102,594 ops/sec ±0.84% (99 runs sampled)
match-requires.js x 2,674,151 ops/sec ±0.83% (94 runs sampled)
crequire.js x 102,220 ops/sec ±0.93% (96 runs sampled)
detective.js x 41,635 ops/sec ±0.81% (97 runs sampled)
match-requires.js x 1,535,496 ops/sec ±0.59% (99 runs sampled)
crequire.js x 121,816 ops/sec ±0.82% (93 runs sampled)
detective.js x 43,148 ops/sec ±1.02% (96 runs sampled)
match-requires.js x 1,750,488 ops/sec ±0.72% (98 runs sampled)
crequire.js x 268,212 ops/sec ±0.64% (97 runs sampled)
detective.js x 55,454 ops/sec ±0.98% (96 runs sampled)
match-requires.js x 2,567,571 ops/sec ±0.77% (95 runs sampled)
crequire.js x 70,591 ops/sec ±0.54% (96 runs sampled)
detective.js x 21,315 ops/sec ±0.96% (98 runs sampled)
match-requires.js x 893,286 ops/sec ±0.77% (96 runs sampled)
Run tests
Install dev dependencies:
npm i -d && npm test
Author
Jon Schlinkert
License
Copyright (c) 2015 Jon Schlinkert
Released under the MIT license
This file was generated by verb on January 22, 2015.