-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.ts
65 lines (57 loc) · 1.83 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
import type { NostrEvent } from '@nostr/tools/pure'
import LRUCache from '@fiatjaf/lru-cache/lru-cache'
import type { CacheMap } from 'dataloader'
export function dataloaderCache<V>(): CacheMap<string, Promise<V>> {
const cache = new LRUCache<string, Promise<V>>(2000)
;(cache as any).delete = (_key: string) => {}
return cache as unknown as CacheMap<string, Promise<V>>
}
/**
* Gets the value of the first tag with the given name -- or returns a default value.
*/
export function getTagOr(event: NostrEvent, tagName: string, dflt: string = ''): string {
return event.tags.find(([t]) => t === tagName)?.[1] || dflt
}
/**
* Checks if a string is a 64-char lowercase hex string as most Nostr ids and pubkeys.
*/
export function isHex32(input: string): boolean {
return Boolean(input.match(/^[a-f0-9]{64}$/))
}
/**
* Checks if a string matches the structure of an "address", i.e. "<kind>:<pubkey>:<arbitrary-string>".
*/
export function isATag(input: string): boolean {
return Boolean(input.match(/^\d+:[0-9a-f]{64}:[^:]+$/))
}
/**
* Just an util to print relay URLs more prettily.
*/
export function urlWithoutScheme(url: string): string {
return url.replace('wss://', '').replace(/\/+$/, '')
}
/**
* Creates a new array and copies items over omitting duplicates.
*/
export function unique<A>(...arrs: A[][]): A[] {
const result: A[] = []
for (let i = 0; i < arrs.length; i++) {
const arr = arrs[i]
for (let j = 0; j < arr.length; j++) {
const item = arr[j]
if (result.indexOf(item) !== -1) continue
result.push(item)
}
}
return result
}
export function identity<A>(a: A): boolean {
return Boolean(a)
}
export function appendUnique<I>(target: I[], ...newItem: I[]) {
let max = newItem.length
for (let i = 0; i < max; i++) {
let ni = newItem[i]
if (target.indexOf(ni) === -1) target.push(ni)
}
}