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

linenumber

v1.0.1

Published

Get the line number of one or more matches in a file

Downloads

8,984

Readme

Build Status downloads npm Code Climate Test Coverage dependencies Size

linenumber

Get the line number of one or more matches in a file

Installation

npm install --save linenumber

Summary

Pass in the contents of a file or a filename, along with a pattern and an optional callback, and get back an array that includes the line numbers of any matches in that file.

Usage

Pass the content to search (which can also be a filename) and the string or regular expression pattern to search for to linenumber, along with an optional callback.

For example, given lib/foo.json,

{
  "foo": 1,
  "bar": true,
  "baz": "a string"
}

any of the following will work:

/**
 * SYNC
 */
var linenumber = require('linenumber');

// With string content and query
linenumber('Foo\nbar\nbaz', 'bar'); // [{ line: 2, match: 'bar' }]

// With a filename
linenumber('lib/foo.json', 'bar'); // [{ line: 3, match: 'bar', file: 'lib/foo.json' }]

// With a regular expression
linenumber('lib/foo.json', /ba./g); // [{ line: 3, match: 'bar', file: 'lib/foo.json' }, { line: 4, match: 'baz', file: 'lib/foo.json' }]

// Without a match
linenumber('lib/foo.json', 'hello'); // null

/**
 * ASYNC
 */
// The other versions above will also work asynchronously if a callback is passed
linenumber('lib/foo.json', 'bar', function(err, results) { /*...*/ }); // results = [{ line: 3, match: 'bar', file: 'lib/foo.json' }]

By default, linenumber will use fs.readFile and fs.readFileSync to read in the contents of a file, but you can set it up to use a different file loader if desired. For instance, since our examples above use a .json file, we can make linenumber use require as the loader instead.

linenumber.loaderSync(require);
linenumber('lib/foo.json', 'bar');

If you are using linenumber on the client-side, these defaults obviously will not work, so you will have to load your own. There are too many client side libraries and frameworks to try to establish a reasonable default.

A note on async usage: linenumber looks at your callback length to try to determine whether to return null, results or just results. fs and many other node-based modules use the standard error-then-everything-else signature, but on the client-side that may not be the case. linenumber assumes that if a callback accepts only one parameter, it's the content, not an error (which makes sense).

Custom Loaders

As above, you can provide custom loaders for reading the contents of a "file" (which could really mean anything). There are several ways to do this.

Sync loader only

Call linenumber.loaderSync and pass the function.

linenumber.loaderSync(require);

Async loader only

Call linenumber.loader and pass the function.

// Contrived example
linenumber.loader(function(file, done) {
  fs.readFile(file, { encoding: 'utf8' }, function(err, contents) {
    done(null, contents.replace('hello', 'goodbye'));
  });
});

Provide both sync and async loaders

You can call either loader or loaderSync with an array or object to replace both the sync and async loaders.

linenumber.loader([sync, async]);
// or
linenumber.loader({ sync: sync, async: async });

Args

You can also provide arguments to be passed to loader functions.

// someSyncFunction will be invoked with the filename, 'foo', and 'bar'
linenumber.loaderSync(someSyncFunction, 'foo', 'bar');

Context

You can even set the context of the this object for the loader function:

linenumber.loader.apply(this, asyncFunc);

In this case, linenumber will store the this context and apply it whenever the loader is called.

Reset the loaders

If for some reason, you need to restore the original loaders, just call linenumber.reset().

Browser

Use whatever serving mechanism you prefer and serve dist/linenumber.js or dist/linenumber.min.js, then access it globally with linenumber.

<script src="/dist/linenumber.js"></script>
<script>
  var matches = linenumber('Foo\nbar\nbaz', 'bar');
</script>

Contributing

Please see the contribution guidelines.