more changes

This commit is contained in:
2024-01-05 01:17:28 +01:00
parent 46d4d67c52
commit d926e5f254
22 changed files with 620 additions and 392 deletions

View File

@@ -1,7 +1,19 @@
import { writable } from 'svelte/store';
import { browser } from '$app/environment';
export const currentStream = writable({});
export const currentSongIndex = writable(0);
export const favoritedStreams = writable(
new Set(JSON.parse((browser && localStorage.getItem('favoritedStreams')) || '[]'))
);
if (browser) {
favoritedStreams.subscribe((val) => {
localStorage.setItem('favoritedStreams', JSON.stringify(Array.from(val)));
});
}
export const tagList = [
'acoustic',
'electronic',
@@ -50,7 +62,6 @@ function locationOf(element, array, start, end) {
export function getSongAtTime(currentTime) {
return locationOf(currentTime, timestampList) - 1;
}
// less operations needed when doing regular lookups
@@ -58,19 +69,17 @@ export function updateCurrentSong(currentTime, songIndex) {
function updateCurrentSongRecurse(songIndex) {
if (currentTime >= timestampList[songIndex + 2]) {
return updateCurrentSongRecurse(songIndex + 1);
}
else if (currentTime < timestampList[songIndex + 1]) {
} else if (currentTime < timestampList[songIndex + 1]) {
return updateCurrentSongRecurse(songIndex - 1);
}
return songIndex;
}
currentSongIndex.set(updateCurrentSongRecurse(songIndex));
}
export function updateCurrentStream(stream) {
currentStream.set(stream);
timestampList = [-Infinity, ...stream.tracks.map(track => track[0]), Infinity];
timestampList = [-Infinity, ...stream.tracks.map((track) => track[0]), Infinity];
currentSongIndex.set(0);
}

37
src/lib/utils.js Normal file
View File

@@ -0,0 +1,37 @@
import Sqids from 'sqids';
let sqids = new Sqids({ minLength: 6 });
export function hashcode(str) {
for (var i = 0, h = 9; i < str.length; ) h = Math.imul(h ^ str.charCodeAt(i++), 9 ** 9);
return h ^ (h >>> 9);
}
// for mnemonic display
export function shorthandCode(str) {
return sqids.encode([hashcode(str) & 0xffff]);
}
// for tag display
export function hashColor(str) {
const hash = hashcode(str);
return `hsl(${hash % 360}, ${65 + (hash % 30) + 1}%, ${85 + (hash % 10) + 1}%)`;
}
export function formatSecondsToHms(s) {
s = Number(s);
var h = Math.floor(s / 3600);
var m = Math.ceil((s % 3600) / 60);
var hDisplay = h > 0 ? h + (h == 1 ? ' hr' : ' hrs') + (m > 0 ? ', ' : '') : '';
var mDisplay = m > 0 ? m + (m == 1 ? ' min' : ' mins') : '';
return hDisplay + mDisplay;
}
export function formatDate(unix_timestamp) {
return new Date(unix_timestamp).toISOString().split('T')[0];
}
export function formatTrackTime(s) {
return new Date(s * 1000).toISOString().slice(11, 19);
}