-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
77 lines (68 loc) · 1.86 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const express = require('express');
const cors = require('cors');
const app = express();
const bodyParser = require('body-parser');
app.use(cors());
app.use(bodyParser.json());
// TBD: Postgres / kv store
let typeaheadResults = [
'polyurethane',
'lacquer nitrocellulose',
'lacquer acrylic',
'C neck',
'C chunky neck',
'D neck',
'V neck',
'U neck',
'baseball bat neck',
'Mint',
'Excellent Condition',
'Very Good Condition',
'Good Condition',
'Fair Condition',
'Poor Condition'
];
const addFrequencyCount = (arr) => {
return arr.reduce((acc, curr) => {
curr = curr.toLowerCase()
if (acc[curr]) {
acc[curr] += 1
} else {
acc[curr] = 1
}
return acc;
}, []);
}
const sortItems = (items) => items.sort( ([key, value], [key2, value2]) => value2 - value)
const searchTermsKeys = (keyValuePairs) => {
const sortedKeys = []
keyValuePairs.forEach(keyValPair => {
const [key, value] = keyValPair
sortedKeys.push(key)
})
return sortedKeys
}
const getSortedFreshData = (typeaheadResults) => {
// could have used a hash table to count frequency occurrences instead of reduce
if (typeaheadResults) {
const resultsWithCount = addFrequencyCount(typeaheadResults)
const keyValuePairs = Object.entries(resultsWithCount)
const sortedEntries = sortItems(keyValuePairs)
return searchTermsKeys(sortedEntries)
}
return []
}
app.get('/typeahead', (req, res) => {
res.status(200)
const updatedSearchTerms = getSortedFreshData(typeaheadResults)
res.send(updatedSearchTerms)
});
app.post('/add', (req, res) => {
// Maybe not just 201 (resource created + location). 200? 202/accepted?
res.status(201)
const obj = req.body
Object.values(obj).forEach(value => typeaheadResults.push(value))
const updatedSearchTerms = getSortedFreshData(typeaheadResults)
res.send(updatedSearchTerms)
})
module.exports = app