npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

code-match

v1.0.0

Published

Search an object's code for pattern matches

Downloads

37

Readme

Search an object's code for pattern matches

Synopsis

function(pattern, options)

Parameters

  1. this (the execution context) is "the module," the object containing the code to search
  2. pattern is the required search argument, a string or (more typically) a RegExp pattern
  3. options is an optional options object (see below)

Returns

A string array containing the found matches.

Description

It is important to note that code-match can only search code in methods, getters, and setters. Code hidden in unexposed "inner" module functions is also hidden from code-match.

Although code-match cannot search inner functions, it can do a recursive walk through the context object's member objects. If you are aware of code included in such nested objects, set options.recurse to truthy. Note that circular references are excluded from recursion, as are all references to DOM nodes.

By default code-match searches a catalog that includes the execution context object and its prototype chain. To skip walking the prototype chain, set options.catalog.own to truthy.

(Don't confuse match recursion with cataloging the prototype chain. These are two different walk axes.)

Mix code-match into an object on which it will be routinely called. The reference to itself will be ignored.

Users should consider blacklisting keys associated with other foreign mix-ins. Simply include references to those mix-ins in options.greylist.black.

Example

var codeMatch = require('code-match');
var A = require('module'), B = require('submodule');

codeMatch.call(A, A.eventStringPattern); // ['click', 'keypress']
codeMatch.call(B, B.eventStringPattern); // ['keydown', 'keypress']

var options = { catalog: { own: true } }; // skip the prototype chain
codeMatch.call(B, B.eventStringPattern, options); // ['keydown']
module.js

A simple module containing a couple of methods and a search pattern definition:

module.exports = {
    handleMouseClick: function() { document.addEventListener('click', function() {...}); },
    handleKeyPress: function() { document.addEventListener('keypress', function() {...}); },
    eventStringPattern: /\.addEventListener\('([a-z]+)'/;
};
submodule.js

Here the two methods have been moved to the "superclass" (immediate predecessor in the prototype chain), with one of those methods being overridden by the descendant:

module.exports = Object.create(require('module'));
module.exports.handleMouseClick = function() { document.addEventListener('keydown', function() {...}); };

Options

options.captureGroup

Iff defined, index of a specific capture group to return for each match.

options.recurse

Equivalent to setting both recurseOwn and recurseAncestors.

options.recurseOwn

Recurse on own nested objects.

options.recurseAncestors

Recurse on prototype chain's nested objects.

options.greylist.white

A whitelist of permissible code matches. Only listed matches are included in the results. If undefined, all matches are included. If an empty array, all matches are blocked.

options.greylist.black

A blacklist of impermissible code matches. Listed object matches are excluded from the results. If undefined or an empty array, all matches are included.

options.catalog.own

If truthy, the resultant catalog is restricted to the execution context object only (excluding the prototype chain).

options.catalog.greylist.white

A whitelist of permissible object member keys to catalog. Only listed object members are cataloged. If undefined, all members are cataloged. If an empty array, all members are blocked.

options.catalog.greylist.black

A blacklist of impermissible object member keys. Listed object members are blocked. If undefined or an empty array, all members that passed the whitelist are included.

Regarding the greylist options

Each of the above .greylist.white or .greylist.black options may be:

  • a string; or
  • a regular expression; or
  • an object (i.e., its enumerable defined keys); or
  • a (nested) array of a mix of any of the above; or
  • an empty array; or
  • undefined (which is a no-op).

See greylist for more information.