Use unix days instead of time hashes

This commit is contained in:
Jon Staab
2025-02-05 15:15:50 -08:00
parent 65aabf5feb
commit a0c6e46184
3 changed files with 10 additions and 45 deletions
+3 -38
View File
@@ -1,6 +1,6 @@
import {hexToBytes, bytesToHex} from "@noble/hashes/utils"
import * as nip19 from "nostr-tools/nip19"
import {HOUR, MONTH, DAY} from "@welshman/lib"
import {range, DAY} from "@welshman/lib"
export const displayList = <T>(xs: T[], conj = "and", n = 6, locale = "en-US") => {
const stringItems = xs.map(String)
@@ -26,41 +26,6 @@ export const nsecDecode = (nsec: string) => {
return bytesToHex(data)
}
export const timeHash = (seconds: number, precision = 32) => {
const alphabet = "0123456789bcdefghjkmnpqrstuvwxyz"
const uint32 = Math.min(seconds >>> 0, 0xffffffff)
const binary = uint32.toString(2).padStart(32, "0")
const chunks = Math.min(Math.floor(precision / 5), 6)
export const day = (seconds: number) => Math.floor(seconds / DAY).toString()
let hash = ""
for (let i = 0; i < chunks * 5; i += 5) {
const chunk = binary.slice(i, i + 5)
const index = parseInt(chunk, 2)
hash += alphabet[index]
}
return hash
}
export const timeHashesBetween = (start: number, end: number, precisions = [10, 15, 20]) => {
const hashes = new Set<string>()
for (let seconds = start; seconds <= end; seconds += HOUR) {
for (const precision of precisions) {
hashes.add(timeHash(seconds, precision))
}
}
return Array.from(hashes).sort()
}
export const timeHashesForFilter = (start: number, end: number) => {
const diff = end - start
if (diff < 3 * DAY) return timeHashesBetween(start, end, [20])
if (diff < 3 * MONTH) return timeHashesBetween(start, end, [15])
return timeHashesBetween(start, end, [10])
}
export const daysBetween = (start: number, end: number) => [...range(start, end, DAY)].map(day)