Skip to content

Commit 253357e

Browse files
committed
Added mongoose-text-search functionality
1 parent 0877e35 commit 253357e

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

lib/controllers/files.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ var fs = require('fs');
44
var request = require('request');
55
var codeParser = require('../../codeSnippitExtractor.js');
66
var async = require('async');
7+
var textSearch = require('mongoose-text-search');
78

89

910
var extractDependencies = function(content) {
10-
return content.match(/require\(\s*[\'\"][a-z]*[\'\"]\s*\)/g)
11-
.map(function(el) {
12-
var finStr = el.substring(8, el.length - 1).trim();
13-
return finStr.substring(1, finStr.length - 1);
14-
});
11+
var requireRegex = content.match(/require\(\s*[\'\"][a-z]*[\'\"]\s*\)/g);
12+
if (!requireRegex) {
13+
return [];
14+
}
15+
requireRegex.map(function(el) {
16+
var finStr = el.substring(8, el.length - 1).trim();
17+
return finStr.substring(1, finStr.length - 1);
18+
});
1519
};
1620

1721
exports.addToDB = function(req, res, next) {
@@ -46,20 +50,21 @@ exports.addToDB = function(req, res, next) {
4650
};
4751

4852
exports.findCode = function(req, res, next) {
49-
var library = req.query.library.toString();
50-
var libFunction = req.query.libFunction.toString();
53+
var library = req.query.library;
54+
var libFunction = req.query.libFunction;
5155

56+
var searchOptions = { filter: { dependencies: library } };
5257

53-
File.find({dependencies: library}, function(err, files){
54-
// console.log(files);
58+
File.textSearch(libFunction, searchOptions, function(err, files){
59+
console.log(files);
5560

5661
var snippIterator = function (doc, callback) {
57-
var docContent = doc.contents;
62+
var docContent = doc.obj.contents;
5863
var snippit = codeParser(docContent, library, libFunction);
5964
callback(null, snippit);
6065
};
6166

62-
async.map(files, snippIterator, function(err, snippitsArr) {
67+
async.map(files.results, snippIterator, function(err, snippitsArr) {
6368
var returnArr = [];
6469

6570
snippitsArr.forEach(function(snippitsArr) {

lib/models/file.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
22

33
var mongoose = require('mongoose'),
4-
Schema = mongoose.Schema;
4+
Schema = mongoose.Schema,
5+
textSearch = require('mongoose-text-search');
56

67
/**
78
* File Schema
@@ -13,4 +14,8 @@ var FileSchema = new Schema({
1314
dependencies: []
1415
});
1516

17+
FileSchema.plugin(textSearch);
18+
19+
FileSchema.index ( {contents: 'text' } );
20+
1621
mongoose.model('File', FileSchema);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"lodash": "~2.4.1",
1515
"method-override": "~1.0.0",
1616
"mongoose": "~3.8.8",
17+
"mongoose-text-search": "0.0.2",
1718
"morgan": "~1.0.0",
1819
"passport": "~0.2.0",
1920
"passport-local": "~0.1.6",

0 commit comments

Comments
 (0)