-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
98 lines (90 loc) · 2.19 KB
/
index.js
File metadata and controls
98 lines (90 loc) · 2.19 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Hello hiring manager? :) nice to meet you. I didn't use persistent storage on this to simplify cloning it down and running it
const express = require('express')
const app = express()
const port = 8080
let zips = {}
const conform = (string) => {
//sanitizes the input ensuring only 5 digit integers can be processed
if (string.length !== 5) return false
let arr = string.split('')
for (i = 0; i < arr.length; i++) {
let digit = Number(arr[i])
arr[i] = digit
if (typeof arr[i] !== 'number') return false
}
arr = arr.join('')
return arr
}
app.get('/:method/:zip?',(req, res) => {
let method = req.params.method
let zip;
if (!method || (method !== 'has' && method !== 'display')) {
res.status(400).send()
}
if (req.params.zip) {
zip = conform(req.params.zip)
}
if (method === 'has'){
if (!zip) {
res.status(400).send()
return
}
if (zips[zip]) {
res.status(200).send(true)
return
}
res.status(200).send(false)
}
if (method === 'display') {
let zipsArr = Object.keys(zips)
if (!zipsArr.length) {
res.send()
return
}
zipsArr.sort()
let result = ''
let rangeStart = zipsArr[0]
let rangeEnd = zipsArr[0]
for (let i = 1; i < zipsArr.length; i++ ) {
let current = zipsArr[i]
if ( +rangeEnd + 1 === +current ) {
rangeEnd = current
continue
}
if (rangeStart === rangeEnd) {
result += `${rangeStart}, `
} else {
result += `${rangeStart}-${rangeEnd}, `
}
rangeStart = current
rangeEnd = current
}
if (rangeStart === rangeEnd) {
result += `${rangeStart}`
} else {
result += `${rangeStart}-${rangeEnd}`
}
res.send(result)
}
})
app.post('/:zip', (req, res) => {
let zip = conform(req.params.zip)
if (zip) {
zips[zip] = 1
res.send(`Zip code ${zip} inserted`)
return
}
res.status(400).send()
})
app.delete('/:zip', (req, res) => {
let zip = conform(req.params.zip)
if (zip) {
delete zips[zip]
res.send(`Zip code ${zip} deleted`)
return
}
res.status(400).send()
})
app.listen(port, () => {
console.log(`Hello Hiring Manager, server is listening on ${port}`)
})