-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ts
74 lines (66 loc) · 2.08 KB
/
utils.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
import * as R from "rambda";
import { Event } from "nostr-tools";
const URL_REGX =
/(?:https?):\/\/(\w+:?\w*)?(\S+)(:\d+)?(\/|\/([\w#!:.?+=&%!\-\/]))?/g;
const NOSTR_URI_REGX = /^nostr:.+/g;
const MASTADON_PY_REGX = /^mastadon:py.+/g;
const PHOTO_REGX = /^photo_.+/g;
export function unindexable(ev: Event) {
const contentSplitByLine = ev.content.split("\n");
const contentSplited = contentSplitByLine.map((content) =>
content.split(" ")
);
const contentSplitedFlat = contentSplited.flat();
const resArr = contentSplitedFlat.map((entry) => {
const res = entry.match(URL_REGX);
return res !== null;
});
return resArr.every((e) => e === true);
}
export function removeURL(ev: Event): Event {
const contentSplitByLine = ev.content.split("\n");
const contentSplited = contentSplitByLine.map((content) =>
content.split(" ")
);
for (let i = 0; i < contentSplited.length; i++) {
for (let j = 0; j < contentSplited[i].length; j++) {
if (
contentSplited[i][j].match(URL_REGX) ||
contentSplited[i][j].match(NOSTR_URI_REGX) ||
contentSplited[i][j].match(MASTADON_PY_REGX) ||
contentSplited[i][j].match(PHOTO_REGX)
) {
contentSplited[i][j] = "";
}
}
}
const contentMergedBySpace = contentSplited.map((contentByLine) =>
concatStringArr(contentByLine, " ")
);
const contentMerged = concatStringArr(contentMergedBySpace, "\n");
let copyEv = R.clone(ev);
copyEv.content = contentMerged;
return copyEv;
}
export function concatStringArr(strArr: string[], delimiter: string): string {
let res = "";
for (let i = 0; i < strArr.length; i++) {
if (i !== strArr.length - 1) {
res = res.concat(strArr[i], delimiter);
} else {
res = res.concat(strArr[i]);
}
}
return res;
}
export function removeDup<T extends Object, V>(elems: T[], key: keyof T): T[] {
const map = new Map<keyof T, T>();
for (const elem of elems) {
map.set(key, elem);
}
const iteratorValues = map.values();
return [...iteratorValues];
}
export function buildPostNamespace(pk: string) {
return `post-${pk}`;
}