Comparing version 1.0.0 to 1.1.0
@@ -10,15 +10,10 @@ /** | ||
if (typeof target === 'string') { | ||
var index = 0; | ||
var offset = 0; | ||
//converts all targets to regex | ||
if (!(target instanceof RegExp)) { | ||
target = new RegExp('\\b' + target + '\\b', "g"); | ||
} | ||
while ((index = line.indexOf(target, offset)) !== -1) { | ||
indices.push(index); | ||
offset = index + 1; | ||
} | ||
} else if (target instanceof RegExp) { | ||
while ((match = target.exec(line)) !== null) { | ||
target.lastIndex = match.index + 1; | ||
indices.push(match.index); | ||
} | ||
while ((match = target.exec(line)) !== null) { | ||
target.lastIndex = match.index + 1; | ||
indices.push(match.index); | ||
} | ||
@@ -29,23 +24,35 @@ | ||
/** | ||
* Removes the last line break from a string. | ||
* @param source {String} - the string to remove line break from | ||
*/ | ||
function removeLastLineBreaks(source) { | ||
return source.replace(/[\n]+$/, ''); | ||
} | ||
/** | ||
* Converts sting to lines based on \n. Also removes last line break | ||
* @param source {String} - the string split | ||
*/ | ||
function splitIntoLines(source) { | ||
return source.split('\n'); | ||
} | ||
module.exports = { | ||
find: function (target, source) { | ||
var sourceLines = source.split('\n'); | ||
var lineNumber = 1; | ||
var sourceLines = splitIntoLines( removeLastLineBreaks(source) ); | ||
var results = []; | ||
sourceLines.forEach(function (line, idx) { | ||
// Do not count the last line that is empty | ||
if (idx === sourceLines.length - 1 && ! line) { | ||
return; | ||
} | ||
if (!(target instanceof Array)) { | ||
target = [target]; //convert all targets to array | ||
} | ||
var indices = findIndices(target, line); | ||
sourceLines.forEach(function (line, lineIdx) { | ||
target.forEach(function(currentValue) { | ||
var indices = findIndices(currentValue, line); | ||
indices.forEach(function (index) { | ||
var position = {line: lineNumber, cursor: index + 1}; | ||
results.push(position); | ||
indices.forEach(function (index) { | ||
results.push( { line: lineIdx + 1, cursor: index + 1 } ); | ||
}); | ||
}); | ||
lineNumber++; | ||
}); | ||
@@ -57,3 +64,3 @@ | ||
findOne: function (target, source) { | ||
var sourceLines = source.split('\n'); | ||
var sourceLines = splitIntoLines( removeLastLineBreaks(source) ); | ||
var position; | ||
@@ -60,0 +67,0 @@ |
{ | ||
"name": "locater", | ||
"version": "1.0.0", | ||
"description": "Find line number and cursor of string or regex in a multi-line input", | ||
"version": "1.1.0", | ||
"description": "Easily locate string or regex in a multi-line input", | ||
"main": "./lib/locater.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
@@ -30,5 +30,17 @@ # Locater | ||
locater.find(['my', 'me'], input); | ||
// => [ { line: 1, cursor: 4 }, { line: 2, cursor: 1 }, { line: 2, cursor: 16 }, | ||
// { 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); | ||
@@ -51,5 +63,9 @@ // => { line: 1, cursor: 4 } | ||
`pattern` can be either String or Regex. If it is a Regex, it should be declared | ||
as global. | ||
`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**. | ||
If there is no match, locater returns an empty array. | ||
@@ -56,0 +72,0 @@ |
@@ -7,6 +7,46 @@ var expect = require('chai').expect; | ||
describe(".find", function() { | ||
it("matches array of strings and returns locations of results", function() { | ||
var input = fs.readFileSync( | ||
'./test/fixtures/sample_input.txt', {encoding: 'utf8'}); | ||
var result = locater.find(['my', 'me'], 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(2); | ||
expect(result[2].cursor).to.equal(16); | ||
expect(result[3].line).to.equal(3); | ||
expect(result[3].cursor).to.equal(27); | ||
}); | ||
it("matches array of regexes and returns locations of results", function() { | ||
var input = "Bacon tastes gooood.\nPork chops taste gooood."; | ||
var result = locater.find([/[a-zA-Z]{6}\./g, /Pork/g], input); | ||
expect(result[0].line).to.equal(1); | ||
expect(result[0].cursor).to.equal(14); | ||
expect(result[1].line).to.equal(2); | ||
expect(result[1].cursor).to.equal(18); | ||
expect(result[2].line).to.equal(2); | ||
expect(result[2].cursor).to.equal(1); | ||
}); | ||
it("matches array of regexes and strings, and returns locations of results", function() { | ||
var input = "Bacon tastes gooood.\nPork chops taste gooood."; | ||
var result = locater.find([/[a-zA-Z]{6}\./g, 'Pork'], input); | ||
expect(result[0].line).to.equal(1); | ||
expect(result[0].cursor).to.equal(14); | ||
expect(result[1].line).to.equal(2); | ||
expect(result[1].cursor).to.equal(18); | ||
expect(result[2].line).to.equal(2); | ||
expect(result[2].cursor).to.equal(1); | ||
}); | ||
it("matches string and returns locations of results", function() { | ||
var input = fs.readFileSync( | ||
'./test/fixtures/sample_input.txt', {encoding: 'utf8'}); | ||
console.log(locater.find([/[a-zA-Z]{7}\s/g, 'me'], input)); | ||
var result = locater.find('my', input); | ||
@@ -13,0 +53,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12600
212
89