Huge News!Announcing our $40M Series B led by Abstract Ventures.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.2 to 1.0.0

76

lib/locater.js

@@ -28,25 +28,69 @@ /**

module.exports = function (target, source) {
var sourceLines = source.split('\n');
var lineNumber = 1;
var results = [];
module.exports = {
find: function (target, source) {
var sourceLines = source.split('\n');
var lineNumber = 1;
var results = [];
sourceLines.forEach(function (line, idx) {
// Do not count the last line that is empty
if (idx === sourceLines.length - 1 && ! line) {
return;
}
sourceLines.forEach(function (line, idx) {
// Do not count the last line that is empty
if (idx === sourceLines.length - 1 && ! line) {
return;
}
var indices = findIndices(target, line);
var indices = findIndices(target, line);
indices.forEach(function (index) {
var position = {line: lineNumber, cursor: index + 1};
indices.forEach(function (index) {
var position = {line: lineNumber, cursor: index + 1};
results.push(position);
results.push(position);
});
lineNumber++;
});
lineNumber++;
});
return results;
},
return results;
findOne: function (target, source) {
var sourceLines = source.split('\n');
var position;
if (typeof target === 'string') {
for (var i = 0; i < sourceLines.length; i++) {
var index = sourceLines[i].indexOf(target);
if (index !== -1) {
position = {line: i + 1, cursor: index + 1};
return position;
}
}
// if for-loop terminates without returning a position, there is no match
return null;
} else if (target instanceof RegExp) {
for (var j = 0; j < sourceLines.length; j++) {
var match = target.exec(sourceLines[j]);
if (match) {
position = {line: j + 1, cursor: match.index + 1};
return position;
}
}
return null;
}
},
any: function (target, source) {
if (typeof target === 'string') {
var index = source.indexOf(target);
if (index === -1) {
return false;
} else {
return true;
}
} else if (target instanceof RegExp) {
return target.test(source);
}
}
};
{
"name": "locater",
"version": "0.1.2",
"version": "1.0.0",
"description": "Find line number and cursor of string or regex in a multi-line input",

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

@@ -13,4 +13,3 @@ # Locater

Locater supports string and regex matching. Below is an example showing both
usage with a given input.
Below is an example of how locater may be used.

@@ -29,12 +28,21 @@ *input.txt*

locater('my', input);
locater.find('my', input);
// => [{ line: 1, cursor: 4 }, { line: 2, cursor: 1 }, { line: 3, cursor: 27 }]
locater(/[a-zA-Z]{7}\s/g, input);
locater.find(/[a-zA-Z]{7}\s/g, input);
// => [{ line: 1, cursor: 7 }, { line: 1, cursor: 27 }, { line: 3, cursor: 11 }]
locater.findOne('my', input);
// => { line: 1, cursor: 4 }
locater.findOne('shiny unicorn', input);
// => null
locater.any('mind', input);
// => true
```
## Guide
## API
### locater(pattern, input)
### find(pattern, input)

@@ -49,2 +57,13 @@ Returns an array of positions of occurrences of `pattern` in `input`.

### 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

@@ -51,0 +70,0 @@

@@ -6,57 +6,107 @@ var expect = require('chai').expect;

describe("locater", function() {
it("matches string and returns locations of results", function() {
var input = fs.readFileSync(
'./test/fixtures/sample_input.txt', {encoding: 'utf8'});
describe(".find", function() {
it("matches string and returns locations of results", function() {
var input = fs.readFileSync(
'./test/fixtures/sample_input.txt', {encoding: 'utf8'});
var result = locater('my', input);
var result = locater.find('my', input);
expect(result[0].line).to.equal(1);
expect(result[0].cursor).to.equal(4);
expect(result[1].line).to.equal(2);
expect(result[1].cursor).to.equal(1);
expect(result[2].line).to.equal(3);
expect(result[2].cursor).to.equal(27);
});
expect(result[0].line).to.equal(1);
expect(result[0].cursor).to.equal(4);
expect(result[1].line).to.equal(2);
expect(result[1].cursor).to.equal(1);
expect(result[2].line).to.equal(3);
expect(result[2].cursor).to.equal(27);
});
it("matches regex and returns locations of results", function() {
var input = fs.readFileSync(
'./test/fixtures/sample_input.txt', {encoding: 'utf8'});
it("matches regex and returns locations of results", function() {
var input = fs.readFileSync(
'./test/fixtures/sample_input.txt', {encoding: 'utf8'});
var result = locater(/[a-zA-Z]{7}\s/g, input);
var result = locater.find(/[a-zA-Z]{7}\s/g, input);
expect(result[0].line).to.equal(1);
expect(result[0].cursor).to.equal(7);
expect(result[1].line).to.equal(1);
expect(result[1].cursor).to.equal(27);
expect(result[2].line).to.equal(3);
expect(result[2].cursor).to.equal(11);
});
expect(result[0].line).to.equal(1);
expect(result[0].cursor).to.equal(7);
expect(result[1].line).to.equal(1);
expect(result[1].cursor).to.equal(27);
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);
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.find('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);
});
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);
it("returns an empty array when no match is present", function() {
var result = locater.find('What', "Zed's dead, baby. Zed's dead.");
expect(result).to.have.length(0);
});
it("matches multiple occurences of a string in a line", function() {
var result = locater.find('my', 'my mind my soul');
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(9);
});
it("matches multiple occurences of a regex in a line", function() {
var result = locater.find(/[a-zA-Z]{6}/g, 'kaskade - 4AM');
expect(result.length).to.equal(2);
});
});
it("matches multiple occurences of a string in a line", function() {
var result = locater('my', 'my mind my soul');
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(9);
describe(".findOne", function() {
it("returns the position of the first match against string", function() {
var result = locater.findOne('world', "this is\nhello world");
expect(result).to.be.an('object');
expect(result.line).to.equal(2);
expect(result.cursor).to.equal(7);
});
it("returns null if there is no match against string", function() {
var result = locater.findOne('truck', "this is\nhello world");
expect(result).to.equal(null);
});
it("returns the position of the first match against regex", function() {
var result = locater.findOne(/\d{3}/, "this is\nexample 123 world");
expect(result).to.be.an('object');
expect(result.line).to.equal(2);
expect(result.cursor).to.equal(9);
});
it("returns null if there is no match against regex", function() {
var result = locater.findOne(/\d{3}/, "this is\nexample world");
expect(result).to.equal(null);
});
});
it("matches multiple occurences of a regex in a line", function() {
var result = locater(/[a-zA-Z]{6}/g, 'kaskade - 4AM');
expect(result.length).to.equal(2);
describe(".any", function() {
it("returns true if there is at least one match with string", function() {
var result = locater.any('hello', 'hello world');
expect(result).to.equal(true);
});
it("returns false if there is no match with string", function() {
var result = locater.any('foo', 'Say what again');
expect(result).to.equal(false);
});
it("returns true if there is at least one match with regex", function() {
var result = locater.any(/[a-z]{5}/g, 'hello world');
expect(result).to.equal(true);
});
it("returns false if there is no match with regex", function() {
var result = locater.any(/\d+/g, 'hello world');
expect(result).to.equal(false);
});
});
});
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