Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

creating pure functions in search.js and testing them. plus removal o… #84

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Autocomplete",
"main": "index.js",
"scripts": {
"start":"node ./src/server.js",
"start": "node ./src/server.js",
"test": "node ./tests/*.js | tap-spec",
"devStart": "nodemon src/server.js",
"coverage": "./node_modules/.bin/istanbul cover tape ./tests/*.js"
Expand Down
4 changes: 2 additions & 2 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

<!-- <audio controls autoplay loop><source src='../media/01.-pokemon-theme.mp3' type='audio/mpeg'> Your Browser's crap</audio> -->
<form id='js-pokeForm'>
<h2> Search for your Pokemon!</h2>
<label>Search for your Pokemon!
<h1> Poké Autocomplete</h1>
<label> Search for your Pokemon!
<input list="search_bar" id='js-pokeInput' class-='pokeInput'>
</input>
</label>
Expand Down
12 changes: 6 additions & 6 deletions public/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,22 @@ button{

.img{
display: flex;
flex-wrap: nowrap;
box-sizing: border-box;
background-repeat: no-repeat;
}

.ash{
float: left;
flex-wrap: nowrap;
transform: scale(1.25, 1.25);
z-index: -1;
margin-top: 120px;
margin-left: 25px;
width: 25%;
}

.ashAndDex{
display: flex;
justify-content: space-around;
margin-top: 75px;
}

.ho-oh{
Expand All @@ -56,12 +59,9 @@ width: 25%;
animation: bob 10s infinite linear, fly 10s infinite linear, bigger 10s infinite linear;
}
.pokeImage{
flex-wrap: nowrap;
height:400px;
margin-left: 206px;
margin-top: 200px;
width:30%;
height: 30%;
}

@keyframes bob {
Expand Down
4 changes: 2 additions & 2 deletions src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const query = function(request, response, filePath) {
response.writeHead(200, {'Content-Type': 'application/javascript'});
const urlEndpoint = urlLink.parse(request.url, true);
const searchTerm = urlEndpoint.query;
search(filePath, searchTerm.q, 10, (error, res) => {
search.searchFunction(filePath, searchTerm.q, 10, (error, res) => {
response.end(res);
});
};
Expand All @@ -23,7 +23,7 @@ const index = function(request, response) {
return;
}
response.end(file);
}
}
);
};

Expand Down
29 changes: 17 additions & 12 deletions src/search.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,30 @@

const path = require('path');
const fs = require('fs');

const searchFunction = function (file, query, number, callback) {

const findMatches = function(searchTerm, data, number) {
//below regex searches for any word following the entered string
const regEx = new RegExp(`\\b(${query}).*\[a-z-2]`, 'gi');
// match takes reg expression and pulls out all the matches from the stringified text file
const pokeMatches = data.match(regEx);
return pokeMatches ? pokeMatches.slice(0, number) : '';
};

const searchFunction = function(file, query, number, callback) {
const fileSplit = file.split('/');
const filePath = path.join(__dirname, '..', ...fileSplit);
fs.readFile(filePath, (error, fileResult) => {
if (error) {
console.log(error);
return;
}
const fileString = fileResult.toString();
console.log(typeof fileString);
console.log('end');
// below regex searches for any word following the entered string
const regEx = new RegExp(`\\b(${query}).*\[a-z-2]`, 'gi');// /.*\s*:\s*.*/g
// match takes reg expression and pulls out all the matches from the stringified text file
const pokeMatches = fileString.match(regEx);
const pokeTenMatches = pokeMatches ? pokeMatches.slice(0, number) : '';
callback(null, JSON.stringify({ pokeTenMatches }));

callback(null, JSON.stringify(findMatches(query, fileResult, number)));
});
};

module.exports = searchFunction;

module.exports = {
searchFunction,
findMatches
}
44 changes: 29 additions & 15 deletions tests/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,37 @@ var test = require('tape');
var search = require("../src/search.js");

// test RegExp
test("regEx should return words with the entered start letters", function(t) {
test("findMatches", function(t) {
var testString = "james, minesh, dak, jen"
var query = 'ja';
var regEx = new RegExp('\\b(' + query + ')\\w*', 'gi');
var expected = ['james'];
var actual = testString.match(regEx);
t.deepEqual(actual, expected, "entering 'ja' shoud return James")
var actual = Array.isArray(search.findMatches(query, testString, 10));
t.ok(actual, "returns array")
t.end()
});

// testing that pokeTenMatches returns correct number
test("pokeTenMatches should return the correct number of pokemon", function(t) {
var testString = "james, james, james, james, james, jam, jam, jam"
var query = 'ja';
var expected = ['james', 'james'];
var regEx = new RegExp('\\b(' + query + ')\\w*', 'gi');
var actual = testString.match(regEx).slice(0,2);
t.deepEqual(actual, expected, "entering 'ja' shoud return James")
t.end()
});
test("findMatches", function(t){
var testString = "james, james, jam, ja, minesh, dak, jen"
var query = 'ja';
var expected = ['james', 'james', 'jam', 'ja'];
var actual = search.findMatches(query, testString, 10)
t.deepEqual(actual, expected, "entering 'ja' should return ['james', 'james', 'jam', 'ja']")
t.end()
})

test("findMatches", function(t){
var testString = "james, james, jam, ja, minesh, dak, jen, raja, baja, adjacent"
var query = 'ja';
var expected = ['james', 'james', 'jam', 'ja'];
var actual = search.findMatches(query, testString, 10)
t.deepEqual(actual, expected, "entering 'ja' should not return words with 'ja' in the middle")
t.end()
})

test("findMatches", function(t){
var testString = "james, james, james, james, james, james, james, james, james, james, james, james, james, james, james, james, james, james, james, james, james, james,jam, ja, minesh, dak, jen, raja, baja, adjacent"
var query = 'ja';
var expected = ['james', 'james', 'james', 'james', 'james', 'james', 'james', 'james', 'james', 'james'];
var actual = search.findMatches(query, testString, 10)
t.deepEqual(actual, expected, "entering 'ja' should return a maximum of 10 matches")
t.end()
})