find-in-files
Advanced tools
Comparing version 0.1.2 to 0.2.1
56
index.js
@@ -13,6 +13,10 @@ 'use strict'; | ||
return function(content) { | ||
var match = content.match(data.regex); | ||
var match = content.match(data.regex), | ||
linesMatch = content.match(data.lineRegEx) | ||
return { | ||
filename: data.filename, | ||
match: match | ||
match: match, | ||
lines: linesMatch | ||
}; | ||
@@ -22,4 +26,17 @@ }; | ||
exports.find = function (pattern, directory, fileFilter) { | ||
var regex = new RegExp(pattern, 'g'), | ||
exports.find = function(pattern, directory, fileFilter) { | ||
var flags, term, grabLineRegEx | ||
if (typeof pattern === 'object' && pattern.flags) { | ||
term = pattern.term | ||
flags = pattern.flags | ||
} else { | ||
term = pattern | ||
flags = 'g' | ||
} | ||
grabLineRegEx = "(.*" + term + ".*)" | ||
var regex = new RegExp(term, flags), | ||
lineRegEx = new RegExp(grabLineRegEx, flags), | ||
matchedFiles = [], | ||
@@ -31,3 +48,3 @@ results = [], | ||
} else if (typeof fileFilter === 'undefined') { | ||
fileFilter = new RegExp('.*'); | ||
fileFilter = new RegExp('.'); | ||
} | ||
@@ -37,19 +54,24 @@ find.file(fileFilter, directory, function(files) { | ||
matchedFiles.push(readFile(files[i]) | ||
.then(searchFile({regex: regex, filename: files[i]}))); | ||
.then(searchFile({ | ||
regex: regex, | ||
lineRegEx: lineRegEx, | ||
filename: files[i] | ||
}))); | ||
} | ||
Q.allSettled(matchedFiles) | ||
.then(function(content) { | ||
for (var i = 0; i < content.length; i++) { | ||
var fileMatch = content[i].value; | ||
if (fileMatch.match !== null) { | ||
results[fileMatch.filename] = { | ||
matches: fileMatch.match, | ||
count: fileMatch.match.length | ||
}; | ||
.then(function(content) { | ||
for (var i = 0; i < content.length; i++) { | ||
var fileMatch = content[i].value; | ||
if (fileMatch.match !== null) { | ||
results[fileMatch.filename] = { | ||
matches: fileMatch.match, | ||
count: fileMatch.match.length, | ||
line: fileMatch.lines | ||
}; | ||
} | ||
} | ||
} | ||
deferred.resolve(results); | ||
}); | ||
deferred.resolve(results); | ||
}); | ||
}); | ||
return deferred.promise; | ||
}; |
{ | ||
"name": "find-in-files", | ||
"version": "0.1.2", | ||
"version": "0.2.1", | ||
"description": "A simple tool to search text patterns across multiple files", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -19,4 +19,4 @@ find-in-files [![Build Status](https://travis-ci.org/kaesetoast/find-in-files.svg?branch=master)](https://travis-ci.org/kaesetoast/find-in-files) [![Coverage Status](https://img.shields.io/coveralls/kaesetoast/find-in-files.svg)](https://coveralls.io/r/kaesetoast/find-in-files) | ||
#### pattern [string] | ||
The string you want to search for. | ||
#### pattern [string|object] | ||
The string you want to search for or object to control regex flags | ||
@@ -33,3 +33,3 @@ #### directory [string] | ||
The find function returns a promise which will recieve the results object. The results object contains the matches and a count of matches per file. | ||
The find function returns a promise which will receive the results object. The results object contains the matches, count of matches per file and the lines that match. | ||
@@ -40,3 +40,4 @@ ```JavaScript | ||
matches: ['found string'], | ||
count: 1 | ||
count: 1, | ||
lines: ['This line contains a found string.'] | ||
} | ||
@@ -49,3 +50,3 @@ } | ||
```JavaScript | ||
findInFiles.find('I'm Brian, and so's my wife!', '.', '.txt$') | ||
findInFiles.find("I'm Brian, and so's my wife!", '.', '.txt$') | ||
.then(function(results) { | ||
@@ -62,4 +63,18 @@ for (var result in results) { | ||
```JavaScript | ||
// Use object to set flags on regular expression. This one will ignore case. | ||
findInFiles.find({'term': "I'm Brian, and so's my wife!", 'flags': 'ig'}, '.', '.txt$') | ||
.then(function(results) { | ||
for (var result in results) { | ||
var res = results[result]; | ||
console.log( | ||
'found "' + res.matches[0] + '" ' + res.count | ||
+ ' times in "' + result + '"' | ||
); | ||
} | ||
}); | ||
``` | ||
## License | ||
MIT © [Philipp Nowinski](http://philippnowinski.de) |
Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum. | ||
This is in both files | ||
This is duplicate This is duplicate | ||
This is duplicate This is duplicate Dolor Sit Amet |
157
test/test.js
'use strict'; | ||
var chai = require('chai'), | ||
os = require('os'), | ||
findInFiles = require('../index'), | ||
@@ -8,69 +9,131 @@ stringOne = 'dolor sit amet', | ||
stringThree = 'This is in both files', | ||
stringFour = 'This is duplicate'; | ||
stringFour = 'This is duplicate', | ||
ObjectOne = { | ||
'term': "dolor sit amet", | ||
'flags': "ig" | ||
}; | ||
chai.should(); | ||
describe('find some test strings', function () { | ||
it('should find stringOne only in fileOne exactly one time', function (done) { | ||
describe('find some test strings', function() { | ||
this.timeout(15000); | ||
it('should find stringOne only in fileOne exactly one time', function(done) { | ||
findInFiles.find(stringOne, '.', '.txt$') | ||
.then(function(result) { | ||
result['test/fileOne.txt'].count.should.equal(1); | ||
result.should.not.have.property('test/fileTwo.txt'); | ||
done(); | ||
}); | ||
.then(function(result) { | ||
var paths = constructPaths({ | ||
first: "fileOne.txt", | ||
second: "fileTwo.txt" | ||
}) | ||
result[paths.first].count.should.equal(1); | ||
result.should.not.have.property(paths.second); | ||
done(); | ||
}); | ||
}); | ||
it('should find stringTwo only in fileTwo exactly one time', function (done) { | ||
it('should find stringTwo only in fileTwo exactly one time', function(done) { | ||
findInFiles.find(stringTwo, '.', '.txt$') | ||
.then(function(result) { | ||
result['test/fileTwo.txt'].count.should.equal(1); | ||
result.should.not.have.property('test/fileOne.txt'); | ||
done(); | ||
}); | ||
.then(function(result) { | ||
var paths = constructPaths({ | ||
first: "fileTwo.txt", | ||
second: "fileOne.txt" | ||
}) | ||
result[paths.first].count.should.equal(1); | ||
result.should.not.have.property(paths.second); | ||
done(); | ||
}); | ||
}); | ||
it('should find stringThree in both files exactly one time', function (done) { | ||
it('should find stringThree in both files exactly one time', function(done) { | ||
findInFiles.find(stringThree, '.', '.txt$') | ||
.then(function(result) { | ||
result['test/fileOne.txt'].count.should.equal(1); | ||
result['test/fileTwo.txt'].count.should.equal(1); | ||
done(); | ||
}); | ||
.then(function(result) { | ||
var paths = constructPaths({ | ||
first: "fileOne.txt", | ||
second: "fileTwo.txt" | ||
}) | ||
result[paths.first].count.should.equal(1); | ||
result[paths.second].count.should.equal(1); | ||
done(); | ||
}); | ||
}); | ||
it('should find stringFour 2 times in fileOne and 3 times in fileTwo', function (done) { | ||
it('should find stringFour 2 times in fileOne and 3 times in fileTwo', function(done) { | ||
findInFiles.find(stringFour, '.', '.txt$') | ||
.then(function(result) { | ||
result['test/fileOne.txt'].count.should.equal(2); | ||
result['test/fileTwo.txt'].count.should.equal(3); | ||
done(); | ||
}); | ||
.then(function(result) { | ||
var paths = constructPaths({ | ||
first: "fileOne.txt", | ||
second: "fileTwo.txt" | ||
}) | ||
result[paths.first].count.should.equal(2); | ||
result[paths.second].count.should.equal(3); | ||
done(); | ||
}); | ||
}); | ||
it('should not find strings in the .js file', function (done) { | ||
it('should not find strings in the .js file', function(done) { | ||
findInFiles.find(stringOne, '.', '.txt$') | ||
.then(function(result) { | ||
result['test/fileOne.txt'].count.should.equal(1); | ||
result.should.not.have.property('test/fileOne.md'); | ||
done(); | ||
}); | ||
.then(function(result) { | ||
var paths = constructPaths({ | ||
first: "fileOne.txt", | ||
second: "fileOne.md" | ||
}) | ||
result[paths.first].count.should.equal(1); | ||
result.should.not.have.property(paths.second); | ||
done(); | ||
}); | ||
}); | ||
it('should accept a regex object for fileFilter', function (done) { | ||
findInFiles.find(stringOne, '.', /.txt$/) | ||
.then(function(result) { | ||
result['test/fileOne.txt'].count.should.equal(1); | ||
result.should.not.have.property('test/fileOne.md'); | ||
done(); | ||
}); | ||
}); | ||
it('should accept a regex object for fileFilter', function(done) { | ||
findInFiles.find(stringOne, '.', /.txt$/) | ||
.then(function(result) { | ||
it('should find strings in all files', function (done) { | ||
findInFiles.find(stringOne, '.') | ||
.then(function(result) { | ||
result['test/fileOne.txt'].count.should.equal(1); | ||
result['test/fileOne.md'].count.should.equal(1); | ||
done(); | ||
}); | ||
var paths = constructPaths({ | ||
first: "fileOne.txt", | ||
second: "fileOne.md" | ||
}) | ||
result[paths.first].count.should.equal(1); | ||
result.should.not.have.property(paths.second); | ||
done(); | ||
}); | ||
}); | ||
it('should find strings in all files', function(done) { | ||
findInFiles.find(stringOne, 'test/') | ||
.then(function(result) { | ||
var paths = constructPaths({ | ||
first: "fileOne.txt", | ||
second: "fileOne.md" | ||
}) | ||
result[paths.first].count.should.equal(1); | ||
result[paths.second].count.should.equal(1); | ||
done(); | ||
}); | ||
}); | ||
it('should find stringOne twice in fileOne since we are case insensative', function(done) { | ||
findInFiles.find(ObjectOne, '.', '.txt$') | ||
.then(function(result) { | ||
var paths = constructPaths({ | ||
first: "fileOne.txt", | ||
second: "fileOne.md" | ||
}) | ||
result[paths.first].count.should.equal(2); | ||
result.should.not.have.property(paths.second); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
function constructPaths(data) { | ||
var result = {}; | ||
if (os.platform() == 'win32') { | ||
result.first = 'test\\' + data.first; | ||
result.second = 'test\\' + data.second; | ||
} else { | ||
result.first = 'test/' + data.first; | ||
result.second = 'test/' + data.second; | ||
} | ||
return result; | ||
} |
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
20766
187
76