diff options
Diffstat (limited to 'src/lib')
| -rw-r--r-- | src/lib/utils.js | 80 |
1 files changed, 48 insertions, 32 deletions
diff --git a/src/lib/utils.js b/src/lib/utils.js index 7b10c638..049617f4 100644 --- a/src/lib/utils.js +++ b/src/lib/utils.js @@ -7,16 +7,16 @@ import { PUBLIC_COINOS_URL } from '$env/static/public'; import { page } from '$app/stores'; import { get as getStore } from 'svelte/store'; -export function scroll(section) { +export let scroll = (section) => { if (getStore(page).url.pathname !== '/') goto('/'); setTimeout(() => section.scrollIntoView({ behavior: 'smooth' }), 500); -} +}; -const base = browser ? '' : PUBLIC_COINOS_URL; +let base = browser ? '' : PUBLIC_COINOS_URL; -export const punk = (k) => Math.floor((parseInt(k.slice(-2), 16) / 256) * 64) + 1 + '.webp'; +export let punk = (k) => Math.floor((parseInt(k.slice(-2), 16) / 256) * 64) + 1 + '.webp'; -export const g = (url, fetch, headers) => +export let g = (url, fetch, headers) => fetch(base + url, { headers }) .then((r) => r.text()) .then((body) => { @@ -27,7 +27,7 @@ export const g = (url, fetch, headers) => } }); -export const get = (url, headers = {}) => +export let get = (url, headers = {}) => fetch(base + url, { headers }) .then(async (r) => { if (r.ok) return r.text(); @@ -41,7 +41,7 @@ export const get = (url, headers = {}) => } }); -export const post = (url, body, headers) => { +export let post = (url, body, headers) => { headers = { ...headers, 'content-type': 'application/json', accept: 'application/json' }; return fetch(base + url, { method: 'POST', body: JSON.stringify(body), headers }) .then(async (r) => { @@ -71,23 +71,23 @@ export const post = (url, body, headers) => { }); }; -export const copy = (text) => { +export let copy = (text) => { navigator.clipboard.writeText(text); success('Copied!'); }; -export function reverseFormat(val, locale) { +export let reverseFormat = (val, locale) => { let parts = new Intl.NumberFormat(locale).formatToParts(1111.1); let group = parts.find((part) => part.type === 'group').value; let decimal = parts.find((part) => part.type === 'decimal').value; let reversedVal = val.replace(new RegExp('\\' + group, 'g'), ''); reversedVal = reversedVal.replace(new RegExp('\\' + decimal, 'g'), '.'); return Number.isNaN(reversedVal) ? 0 : +reversedVal; -} +}; export let protectedRoutes = [/customers/, /settings/, /payments/]; -export const success = (m) => { +export let success = (m) => { toast.pop(); toast.push(m, { theme: { @@ -96,7 +96,7 @@ export const success = (m) => { }); }; -export const warning = (m) => { +export let warning = (m) => { toast.pop(); toast.push(m, { theme: { @@ -104,7 +104,7 @@ export const warning = (m) => { } }); }; -export const fail = (m) => { +export let fail = (m) => { toast.pop(); toast.push(m, { theme: { @@ -112,7 +112,7 @@ export const fail = (m) => { } }); }; -export const info = (m) => { +export let info = (m) => { toast.pop(); toast.push(m, { theme: { @@ -121,7 +121,7 @@ export const info = (m) => { }); }; -export const login = async (user, cookies) => { +export let login = async (user, cookies) => { let maxAge = 30 * 24 * 60 * 60; let res = await fetch(base + '/login', { @@ -145,17 +145,17 @@ export const login = async (user, cookies) => { cookies.set('token', token, opts); }; -export const auth = (cookies) => ({ authorization: `Bearer ${cookies.get('token')}` }); +export let auth = (cookies) => ({ authorization: `Bearer ${cookies.get('token')}` }); -export const fiat = (amount, rate) => (amount * rate) / sats; -export const fd = async (req) => { +export let fiat = (amount, rate) => (amount * rate) / sats; +export let fd = async (req) => { let obj = Object.fromEntries(await req.formData()); for (let k in obj) (obj[k] === 'undefined' && (obj[k] = undefined)) || (obj[k] === 'false' && (obj[k] = false)); return obj; }; -export const f = (s, currency) => +export let f = (s, currency) => new Intl.NumberFormat('en-US', { style: 'currency', currency @@ -163,8 +163,8 @@ export const f = (s, currency) => .format(s) .replace('CA', ''); -export const s = (s) => new Intl.NumberFormat('en-US', { maximumFractionDigits: 0 }).format(s); -export const sat = (s) => { +export let s = (s) => new Intl.NumberFormat('en-US', { maximumFractionDigits: 0 }).format(s); +export let sat = (s) => { s = Math.abs(s); let p = Math.floor(Math.log(s) / Math.LN10 + 0.000000001); let d = Math.floor((p + 1) / 3); @@ -179,20 +179,20 @@ export const sat = (s) => { ); }; -export const sats = 100000000; +export let sats = 100000000; -export const back = () => browser && history.go(-1); -export const focus = (el) => browser && screen.width > 1280 && setTimeout(() => el.focus(), 1); +export let back = () => browser && history.go(-1); +export let focus = (el) => browser && screen.width > 1280 && setTimeout(() => el.focus(), 1); -export const sleep = (n) => new Promise((r) => setTimeout(r, n)); -export const wait = async (f, n = 100, s = 300) => { +export let sleep = (n) => new Promise((r) => setTimeout(r, n)); +export let wait = async (f, n = 100, s = 300) => { let i = 0; while (!(await f()) && i < s) (await sleep(n)) && i++; if (i >= s) throw new Error('timeout'); return f(); }; -export const stretch = async (password, salt) => +export let stretch = async (password, salt) => crypto.subtle.deriveKey( { name: 'PBKDF2', @@ -212,18 +212,18 @@ export const stretch = async (password, salt) => ['encrypt', 'decrypt'] ); -export const bip32 = await BIP32Factory(ecc); +export let bip32 = await BIP32Factory(ecc); -function get_bigrams(string) { +export let get_bigrams = (string) => { var s = string.toLowerCase(); var v = s.split(''); for (var i = 0; i < v.length; i++) { v[i] = s.slice(i, i + 2); } return v; -} +}; -export function ss(str1, str2) { +export let ss = (str1, str2) => { if (str1.length > 0 && str2.length > 0) { var pairs1 = get_bigrams(str1); var pairs2 = get_bigrams(str2); @@ -240,10 +240,26 @@ export function ss(str1, str2) { return 0.0; } -export const types = { +export let types = { bitcoin: 'bitcoin', lightning: 'lightning', internal: 'internal', pot: 'pot', classic: 'classic' }; + +export let fadeScale = (node, { delay = 0, duration = 200, easing = (x) => x, baseScale = 0 }) => { + let o = +getComputedStyle(node).opacity; + let m = getComputedStyle(node).transform.match(/scale\(([0-9.]+)\)/); + let s = m ? m[1] : 1; + let is = 1 - baseScale; + + return { + delay, + duration, + css: (t) => { + let eased = easing(t); + return `opacity: ${eased * o}; transform: scale(${eased * s * is + baseScale})`; + } + }; +}; |
