New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

locater

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

locater - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

42

lib/locater.js
/**
* Finds the index of the occurence of the target in the given string.
* Always returns -1 if there is no match.
* Returns an array of indices of the occurence of the target in the line.
* If there is no match, returns an empty array.
* @param target {String | RegExp} - the string or regex to match
* @param line {String} - the string to match against
*/
function findIndex(target, line) {
function findIndices(target, line) {
var indices = [];
if (typeof target === 'string') {
return line.indexOf(target);
var index = 0;
var offset = 0;
while (index != -1) {
index = line.indexOf(target, offset);
if (index != -1) {
indices.push(index);
offset = index + 1;
}
}
return indices;
} else if (target instanceof RegExp) {
var match = target.exec(line);
while ((match = target.exec(line)) !== null) {
indices.push(match.index);
}
if (match) {
return match.index;
} else {
return -1;
}
return indices;
}

@@ -32,7 +43,10 @@ }

var index = findIndex(target, line);
if (index !== -1) {
var position = {line: lineNumber, cursor: index + 1};
var indices = findIndices(target, line);
results.push(position);
if (indices.length > 0) {
indices.forEach(function (index) {
var position = {line: lineNumber, cursor: index + 1};
results.push(position);
});
}

@@ -39,0 +53,0 @@

{
"name": "locater",
"version": "0.1.0",
"version": "0.1.1",
"description": "Find line number and cursor of string or regex in a multi-line input",

@@ -5,0 +5,0 @@ "main": "./lib/locater.js",

@@ -23,3 +23,3 @@ # Locater

*js file*
*js*
```js

@@ -32,6 +32,18 @@ var locater = require('locater');

locater(/[a-zA-Z]{7}/, input);
// => [{ line: 1, cursor: 7 }, { line: 3, cursor: 11 }]
locater(/[a-zA-Z]{7}/g, input);
// => [{ line: 1, cursor: 7 }, { line: 1, cursor: 24 }, { line: 3, cursor: 11 }]
```
## Guide
### locater(pattern, input)
Returns an array of positions of occurrences of `pattern` in `input`.
A position is represented by an object with keys `line` and `cursor`.
`pattern` can be either String or Regex. If it is a Regex, it should be declared
as global.
If there is no match, locater returns an empty array.
## Contributing

@@ -38,0 +50,0 @@

@@ -24,9 +24,26 @@ var expect = require('chai').expect;

var result = locater(/[a-zA-Z]{7}/, input);
var result = locater(/[a-zA-Z]{7}/g, input);
expect(result[0].line).to.equal(1);
expect(result[0].cursor).to.equal(7);
expect(result[1].line).to.equal(3);
expect(result[1].cursor).to.equal(11);
expect(result[1].line).to.equal(1);
expect(result[1].cursor).to.equal(24);
expect(result[2].line).to.equal(3);
expect(result[2].cursor).to.equal(11);
});
it("can match more than once for the same line", function() {
var input = "What ain't no country I've ever heard of. They speak English in What?";
var result = locater('What', input);
expect(result[0].line).to.equal(1);
expect(result[0].cursor).to.equal(1);
expect(result[1].line).to.equal(1);
expect(result[1].cursor).to.equal(65);
});
it("returns an empty array when no match is present", function() {
var result = locater('What', "Zed's dead, baby. Zed's dead.");
expect(result).to.have.length(0);
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc