Latest Threat Research:SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains.Details
Socket
Book a DemoInstallSign in
Socket

match-requires

Package Overview
Dependencies
Maintainers
2
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

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.

Source
npmnpm
Version
1.0.1
Version published
Weekly downloads
237
Maintainers
2
Weekly downloads
 
Created
Source

match-requires NPM version Build Status

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.

(TOC generated by verb using markdown-toc)

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\');' } ]

Code comments

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.

#1: after-return.js
  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)

#2: basic.js
  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)

#3: do-while.js
  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)

#4: if-statement.js
  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)

#5: in-method.js
  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)

#6: multiple.js
  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)

requires-regex: Regular expression for matching javascript require statements. | homepage

Running tests

Install dev dependencies:

$ npm i -d && npm test

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Author

Jon Schlinkert

License

Copyright © 2016 Jon Schlinkert Released under the MIT license.

This file was generated by verb on January 07, 2016.

Keywords

analyze

FAQs

Package last updated on 08 Jan 2016

Did you know?

Socket

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.

Install

Related posts