aboutsummaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authormat <github@matdoes.dev>2022-02-16 22:12:59 +0000
committermat <github@matdoes.dev>2022-02-16 22:12:59 +0000
commit81543e5307c98df0d44c1cf553bcdfefbfe4ea7f (patch)
tree8e7f50affea5920fe3607b1168f1703acf7cb194 /src/lib
parent36bbe64db012aaed7ff0fadf083f191b122fa4ad (diff)
downloadskyblock-stats-81543e5307c98df0d44c1cf553bcdfefbfe4ea7f.tar.gz
skyblock-stats-81543e5307c98df0d44c1cf553bcdfefbfe4ea7f.tar.bz2
skyblock-stats-81543e5307c98df0d44c1cf553bcdfefbfe4ea7f.zip
improvements
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/constants.ts11
-rw-r--r--src/lib/profile.ts7
-rw-r--r--src/lib/utils.ts15
3 files changed, 18 insertions, 15 deletions
diff --git a/src/lib/constants.ts b/src/lib/constants.ts
deleted file mode 100644
index dc84994..0000000
--- a/src/lib/constants.ts
+++ /dev/null
@@ -1,11 +0,0 @@
-import { API_URL } from '$lib/api'
-
-export let constants: any = {}
-
-async function updateConstants() {
- constants = await fetch(API_URL + 'constants').then(r => r.json())
- console.log('updated constants')
-}
-
-updateConstants()
-setInterval(updateConstants, 60 * 60 * 1000) // update every hour \ No newline at end of file
diff --git a/src/lib/profile.ts b/src/lib/profile.ts
index 6388128..973a6da 100644
--- a/src/lib/profile.ts
+++ b/src/lib/profile.ts
@@ -1,17 +1,16 @@
-import { constants } from './constants'
import { cleanId, millisecondsToTime } from './utils'
/**
* Convert milliseconds since epoch into a string, but if it was within the
* past week then show the timeago
*/
-function prettyTimestamp(ms: number) {
+export function prettyTimestamp(ms: number) {
const isWithinPastWeek = Date.now() - ms < 1000 * 60 * 60 * 24 * 7
const timeAsString = isWithinPastWeek ? (millisecondsToTime(Date.now() - ms) + ' ago') : (new Date(ms)).toUTCString()
return timeAsString
}
-export function generateMetaDescription(data) {
+export function generateInfobox(data, constants, opts: { meta: boolean }): string[] {
const result: string[] = []
result.push(`💾 Last save: ${prettyTimestamp(data.member.last_save * 1000)}`)
@@ -53,5 +52,5 @@ export function generateMetaDescription(data) {
`☠ ${mostSignificantDeathsStat.value.toLocaleString()} ${mostSignificantDeathsStat.unit || cleanId(mostSignificantDeathsStat.rawName).toLowerCase()}`
)
- return result.join('\n')
+ return result
} \ No newline at end of file
diff --git a/src/lib/utils.ts b/src/lib/utils.ts
index f93f68e..c2a955a 100644
--- a/src/lib/utils.ts
+++ b/src/lib/utils.ts
@@ -132,4 +132,19 @@ export function twemojiHtml(s: string) {
return `<img src="/emoji/${[...match].map(p => p.codePointAt(0).toString(16)).join('-')}.svg" class="emoji">`
})
return asTwemoji
+}
+
+export function formatNumber(n: number, digits = 3) {
+ // from https://stackoverflow.com/a/9462382 with some modifications
+ const numberSymbolsLookup = [
+ { value: 1, symbol: '' },
+ { value: 1e3, symbol: 'k' },
+ { value: 1e6, symbol: 'M' },
+ { value: 1e9, symbol: 'G' },
+ { value: 1e12, symbol: 'T' },
+ { value: 1e15, symbol: 'P' },
+ { value: 1e18, symbol: 'E' },
+ ]
+ const item = numberSymbolsLookup.slice().reverse().find(item => n >= item.value)
+ return (n / item.value).toPrecision(digits).replace(/\.0+$|(\.[0-9]*[1-9])0+$/, '$1') + item.symbol
} \ No newline at end of file