refactor: group composables

zio/stable
Anthony Fu 2022-12-02 16:52:00 +08:00
parent 97983e6d11
commit 9e9f2bde09
8 changed files with 54 additions and 56 deletions

View File

@ -1,7 +0,0 @@
import type { MastoClient } from 'masto'
export const useMasto = () => useNuxtApp().$masto.api as MastoClient
export const setMasto = (masto: MastoClient) => {
useNuxtApp().$masto?.replace(masto)
}

View File

@ -12,7 +12,6 @@ const scopes = [
'Navigation', 'Navigation',
'Preferences', 'Preferences',
'Account', 'Account',
'Languages', 'Languages',
'Switch account', 'Switch account',
] as const ] as const

View File

@ -1,5 +1,49 @@
import type { MaybeRef } from '@vueuse/shared'
import type { MaybeComputedRef, UseTimeAgoOptions } from '@vueuse/core' import type { MaybeComputedRef, UseTimeAgoOptions } from '@vueuse/core'
const formatter = Intl.NumberFormat()
export const humanReadableNumber = (
num: number,
{ k, m }: { k: string; m: string } = { k: 'K', m: 'M' },
useFormatter: Intl.NumberFormat = formatter,
) => {
if (num < 10000)
return useFormatter.format(num)
if (num < 1000000)
return `${Math.floor(num / 1000)}${k}`
return `${Math.floor(num / 1000000)}${m}`
}
export const formattedNumber = (num: number, useFormatter: Intl.NumberFormat = formatter) => {
return useFormatter.format(num)
}
export const useHumanReadableNumber = () => {
const i18n = useI18n()
const numberFormatter = $computed(() => Intl.NumberFormat(i18n.locale.value))
return {
formatHumanReadableNumber: (num: MaybeRef<number>) => {
return humanReadableNumber(
unref(num),
{ k: i18n.t('common.kiloSuffix'), m: i18n.t('common.megaSuffix') },
numberFormatter,
)
},
formatNumber: (num: MaybeRef<number>) => {
return formattedNumber(
unref(num),
numberFormatter,
)
},
forSR: (num: MaybeRef<number>) => {
return unref(num) > 10000
},
}
}
export const useFormattedDateTime = ( export const useFormattedDateTime = (
value: MaybeComputedRef<string | Date | undefined | null>, value: MaybeComputedRef<string | Date | undefined | null>,
options: Intl.DateTimeFormatOptions = { dateStyle: 'long', timeStyle: 'medium' }, options: Intl.DateTimeFormatOptions = { dateStyle: 'long', timeStyle: 'medium' },

View File

@ -1,7 +1,13 @@
import type { Ref } from 'vue' import type { Ref } from 'vue'
import type { Account, Relationship, Status } from 'masto' import type { Account, MastoClient, Relationship, Status } from 'masto'
import { withoutProtocol } from 'ufo' import { withoutProtocol } from 'ufo'
export const useMasto = () => useNuxtApp().$masto.api as MastoClient
export const setMasto = (masto: MastoClient) => {
useNuxtApp().$masto?.replace(masto)
}
// @unocss-include // @unocss-include
export const STATUS_VISIBILITIES = [ export const STATUS_VISIBILITIES = [
{ {

View File

@ -1,44 +0,0 @@
import type { MaybeRef } from '@vueuse/shared'
const formatter = Intl.NumberFormat()
export const humanReadableNumber = (
num: number,
{ k, m }: { k: string; m: string } = { k: 'K', m: 'M' },
useFormatter: Intl.NumberFormat = formatter,
) => {
if (num < 10000)
return useFormatter.format(num)
if (num < 1000000)
return `${Math.floor(num / 1000)}${k}`
return `${Math.floor(num / 1000000)}${m}`
}
export const formattedNumber = (num: number, useFormatter: Intl.NumberFormat = formatter) => {
return useFormatter.format(num)
}
export const useHumanReadableNumber = () => {
const i18n = useI18n()
const numberFormatter = $computed(() => Intl.NumberFormat(i18n.locale.value))
return {
formatHumanReadableNumber: (num: MaybeRef<number>) => {
return humanReadableNumber(
unref(num),
{ k: i18n.t('common.kiloSuffix'), m: i18n.t('common.megaSuffix') },
numberFormatter,
)
},
formatNumber: (num: MaybeRef<number>) => {
return formattedNumber(
unref(num),
numberFormatter,
)
},
forSR: (num: MaybeRef<number>) => {
return unref(num) > 10000
},
}
}

View File

@ -1,3 +0,0 @@
export const useIsMac = () => computed(() =>
useRequestHeaders(['user-agent'])['user-agent']?.includes('Macintosh')
?? navigator?.platform?.includes('Mac') ?? false)

View File

@ -26,3 +26,6 @@ export function emojisArrayToObject(emojis: Emoji[]) {
export function noop() {} export function noop() {}
export const useIsMac = () => computed(() =>
useRequestHeaders(['user-agent'])['user-agent']?.includes('Macintosh')
?? navigator?.platform?.includes('Mac') ?? false)