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

locater

v1.3.0

Published

Easily locate string or regex in a multi-line input

Downloads

1,472

Readme

Locater

Build Status

Find line number and cursor of string or Regex in a multi-line input

Install

npm install locater

Usage

Below is an example of how locater may be used.

input.txt

In my younger and more vulnerable years
my father gave me some advice that
I've been turning over in my mind ever since.

js

var locater = require('locater');
var input = fs.readFileSync('./input.txt', {encoding: 'utf8'});

locater.find('my', input);
// => [{ line: 1, cursor: 4 }, { line: 2, cursor: 1 }, { line: 3, cursor: 27 }]

locater.find(['my', 'some'], input);
// => [ { line: 1, cursor: 4 }, { line: 2, cursor: 1 }, { line: 2, cursor: 19 },
//      { line: 3, cursor: 27 } ]

locater.find(/[a-zA-Z]{7}\s/g, input);
// => [{ line: 1, cursor: 7 }, { line: 1, cursor: 27 }, { line: 3, cursor: 11 }]

locater.find([/[a-zA-Z]{7}\s/g, /my/g], input);
// => [ { line: 1, cursor: 7 }, { line: 1, cursor: 27 }, { line: 1, cursor: 4 },
//      { line: 2, cursor: 1 }, { line: 3, cursor: 11 }, { line: 3, cursor: 27 } ]

locater.find([/[a-zA-Z]{7}\s/g, 'me'], input);
//=> [ { line: 1, cursor: 7 }, { line: 1, cursor: 27 }, { line: 2, cursor: 16 },
//     { line: 3, cursor: 11 } ]

locater.findOne('my', input);
// => { line: 1, cursor: 4 }

locater.findOne('shiny unicorn', input);
// => null

locater.any('mind', input);
// => true

API

find(pattern, input, [options])

Returns an array of positions of occurrences of pattern in input. A position is represented by an object with keys line and cursor. If there is no match, locater returns an empty array.

pattern can be either String, Regex or an Array.

An array can have a combination of String and Regex as its elements. For instance, you can provide [/[a-zA-Z]{5}/g, 'foo'] as an argument. Any Regex should be declared as global.

options is an optional. Default value is as follows:

{
  getGlobalIndices: false,
  getMatches: false
}
  • getGlobalIndices: return a global index of the match in the position object. The global index is the index of the match in the context of the whole input.

  • getMatches: return exact matches for the regexes in the position object.

e.g.

locater.find('gooood', 'Bacon tastes gooood.\nPork chops taste gooood.', {
  getGlobalIndices: true
});
// => [ { line: 1, cursor: 14, globalIndex: 13 },
//      { line: 2, cursor: 18, globalIndex: 38 } ]
locater.find(/[a-zA-Z]{6}\./g, input, {getMatches: true});
// => [ { line: 1, cursor: 14, match: 'gooood.' },
//      { line: 2, cursor: 18, match: 'gooood.' } ]

findOne(pattern, input)

Returns the position of the first occurrence of pattern in input.

More efficient than find if you are interested in the first occurrence only.

any(pattern, input)

Checks if there is any occurrence of pattern in input. Returns true if there is a match, and false otherwise.

Contributing

Run npm test to run tests.

Feel free to open issues with bugs and feature requests. If you submit a PR with a new feature, please write a simple test.

License

MIT