-
Notifications
You must be signed in to change notification settings - Fork 1
/
jobboard.js
68 lines (64 loc) · 1.4 KB
/
jobboard.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
export const normalizeJobBoard = raw => {
try {
const obj = JSON.parse(raw)
const list = obj.jobs.map(job => ({
id: job.id,
location: job.location,
title: job.title,
updated_at: job.updated_at,
}))
list.sort((a, b) => (a.id < b.id ? -1 : 1))
return list
} catch (e) {
return e
}
}
const newDiffItem = (op, job, timestamp) => ({
_op: op,
_at: timestamp || new Date(job.updated_at).toISOString(),
id: job.id,
location: job.location,
title: job.title,
})
export const diffJobBoards = (before, after, timestamp) => {
const nb = before.length,
na = after.length
let ib = 0,
ia = 0
const ret = []
let item
while (ib < nb || ia < na) {
if (ib >= nb) {
item = newDiffItem('+', after[ia++])
} else if (ia >= na) {
item = newDiffItem('-', before[ib++], timestamp)
} else {
const bi = before[ib],
ai = after[ia]
if (bi.id < ai.id) {
item = newDiffItem('-', bi, timestamp)
ib++
} else if (bi.id > ai.id) {
item = newDiffItem('+', ai)
ia++
} else {
// bi.id === ai.id
ib++
ia++
continue
}
}
ret.push(item)
}
return ret
}
export const sortDiffItemByTimestamp = (a, b) => {
if (a._at < b._at) {
return 1
}
if (b._at < a._at) {
return -1
}
return a.id < b.id ? 1 : -1
}
export const boardId = 'cloudflare'