Created
October 9, 2020 20:10
-
-
Save buddyeorl/46f7997907498458a8ab1c04c9b0dd80 to your computer and use it in GitHub Desktop.
middleware for Equipment Lookup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//importing data, if it's host locally, otherwise handle the db imports here | |
const data = require('./data'); | |
//this function will show exact results as default and then partial results if no exact results were found | |
module.exports = (req, res, next) => { | |
//default value | |
let queries = ['one', 'two', 'three'] | |
if (req.query && req.query.search && typeof req.query.search === 'object') { | |
queries = req.query.search | |
} | |
if (req.query && req.query.search && typeof req.query.search === 'string') { | |
queries = [req.query.search] | |
} | |
if (Object.keys(req.query) === 0 || req.query.search.length === 0) { | |
res.send({ message: 'nothing received, please try again', data: {} }); | |
return | |
} | |
let result = {}; | |
let partialMatches = {}; | |
let partialCounter = 0 | |
let exactMatches = {}; | |
let exactCounter = 0 | |
//if 2 or less search strings are received, at least one of them should be 3 or more characters long | |
if (queries.length === 0 || (!queries.every(item => item.length > 2) && queries.length < 2)) { | |
result.message = 'Please use at least a 3 letter word in your search' | |
result.data = {} | |
res.send(result); | |
return; | |
} | |
for (keys of queries) { | |
for (equipment in data) { | |
if (equipment.toLowerCase().includes(keys) || data[equipment].make.toLowerCase().includes(keys) || data[equipment].category.toLowerCase().includes(keys) || data[equipment].type.toLowerCase().includes(keys)) { | |
if (partialMatches[data[equipment].make]) { | |
//prevent count duplicates when equipment exists in several categories | |
if (!partialMatches[data[equipment].make][equipment]) { | |
partialCounter++; | |
} | |
partialMatches[data[equipment].make][equipment] = { | |
category: data[equipment].category, | |
type: data[equipment].type, | |
}; | |
} else { | |
partialMatches[data[equipment].make] = { [equipment]: { category: data[equipment].category, type: data[equipment].type } }; | |
partialCounter++; | |
} | |
//check exact match | |
if (queries.every((i) => (equipment.toLowerCase() + data[equipment].make.toLowerCase() + data[equipment].category.toLowerCase() + data[equipment].type.toLowerCase()).includes(i.toLowerCase()))) { | |
if (exactMatches[data[equipment].make]) { | |
//prevent count duplicates when equipment exists in several categories | |
if (!exactMatches[data[equipment].make][equipment]) { | |
exactCounter++; | |
} | |
exactMatches[data[equipment].make][equipment] = { | |
category: data[equipment].category, | |
type: data[equipment].type, | |
}; | |
} else { | |
exactMatches[data[equipment].make] = { [equipment]: { category: data[equipment].category, type: data[equipment].type } }; | |
exactCounter++; | |
} | |
} | |
} | |
} | |
} | |
if (exactCounter > 0) { | |
result.message = `Found ${exactCounter} equipment${exactCounter > 1 ? 's' : ''} that match your query` | |
result.data = exactMatches | |
} else if (partialCounter > 0) { | |
result.message = `Found ${partialCounter} equipment${partialCounter > 1 ? 's' : ''} that partially match your query` | |
result.data = partialMatches | |
} else if (exactCounter === 0 && partialCounter === 0) { | |
result.message = `Nothing Found, Please try again` | |
result.data = {} | |
} else { | |
result.message = `Your search result is too large, for better results at least one word should be 3 letters or more, example:'cat 140'` | |
result.data = {} | |
} | |
//set locals with the result | |
req.locals = result | |
next(); | |
return; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment