-
Notifications
You must be signed in to change notification settings - Fork 341
/
Copy pathutil.ts
77 lines (66 loc) · 2.08 KB
/
util.ts
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
import Storex from '@worldbrain/storex'
import { AnnotsByPageUrl, PageUrlsByDay } from './background/types'
export const URL_SEPARATOR = /[/?#=+& _.,\-|(\n)]+/
export const collections = (db: Storex) => Object.keys(db.registry.collections)
export const mergeAnnotsByPage = (
...objs: AnnotsByPageUrl[]
): AnnotsByPageUrl => {
const merged: AnnotsByPageUrl = {}
for (const obj of objs) {
for (const [pageUrl, annotations] of Object.entries(obj)) {
if (!merged[pageUrl]) {
merged[pageUrl] = annotations
continue
}
const existingUrls = new Set(merged[pageUrl].map((a) => a.url))
merged[pageUrl] = [
...merged[pageUrl],
...annotations.filter((a) => !existingUrls.has(a.url)),
]
}
}
return merged
}
export const mergeAnnotsByDay = (...objs: PageUrlsByDay[]): PageUrlsByDay => {
const merged: PageUrlsByDay = {}
for (const obj of objs) {
for (const [time, annotsByPage] of Object.entries(obj)) {
merged[time] = merged[time]
? mergeAnnotsByPage(merged[time], annotsByPage)
: annotsByPage
}
}
return merged
}
export const areAnnotsByPageObjsDifferent = (
a: AnnotsByPageUrl,
b: AnnotsByPageUrl,
): boolean => {
for (const [pageUrl, annotations] of Object.entries(a)) {
if (!b[pageUrl]) {
return true
}
if (b[pageUrl].length !== annotations.length) {
return true
}
const existingUrlsB = new Set(b[pageUrl].map((annot) => annot.url))
for (const annot of annotations) {
if (!existingUrlsB.has(annot.url)) {
return true
}
}
}
}
export const areAnnotsByDayObjsDifferent = (
a: PageUrlsByDay,
b: PageUrlsByDay,
): boolean => {
for (const [time, annotsByPage] of Object.entries(a)) {
if (!b[time]) {
return true
}
if (areAnnotsByPageObjsDifferent(b[time], annotsByPage)) {
return true
}
}
}