| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- import { toPng } from 'html-to-image'
- import { isIOS } from '~/utils/ua'
- export const formatNumber = (value: number) =>
- new Intl.NumberFormat().format(value)
- export interface IdrFormatOptions {
- /** Format with currency symbol (Rp) */
- withSymbol?: boolean
- /** Minimum fraction digits */
- minimumFractionDigits?: number
- /** Maximum fraction digits */
- maximumFractionDigits?: number
- /** Fallback string for invalid input */
- fallback?: string
- }
- export const idrFormat = (
- value: number | string | null | undefined,
- options: IdrFormatOptions = {},
- ): string => {
- const {
- withSymbol = false,
- minimumFractionDigits = 0,
- maximumFractionDigits = 0,
- fallback = '0',
- } = options
- const n = typeof value === 'number'
- ? value
- : Number(String(value ?? '').replace(/,/g, '').trim())
- if (!Number.isFinite(n)) return fallback
- if (withSymbol) {
- return new Intl.NumberFormat('id-ID', {
- style: 'currency',
- currency: 'IDR',
- minimumFractionDigits,
- maximumFractionDigits,
- }).format(n)
- }
- return new Intl.NumberFormat('id-ID', {
- style: 'decimal',
- minimumFractionDigits,
- maximumFractionDigits,
- }).format(n)
- }
- export const sleep = (ms: number): Promise<void> => new Promise(resolve => setTimeout(resolve, ms))
- export const buildPng = async (element: HTMLElement) => {
- let dataUrl = ''
- const minDataLength = 100000
- let i = 0
- const minAttempts = 3
- const maxAttempts = 10
- while (i < minAttempts || (dataUrl.length < minDataLength && i < maxAttempts)) {
- dataUrl = await toPng(element, {
- cacheBust: true,
- })
- i += 1
- }
- return dataUrl
- }
- export const getDeviceLanguage = () => {
- try {
- const language = navigator.language
- if (language.includes('id')) {
- return 'id'
- }
- else if (language.includes('zh')) {
- return 'zh'
- }
- else {
- return 'en'
- }
- }
- catch {
- return 'en'
- }
- }
- export const isNativeContext = () => {
- return typeof window !== 'undefined' && window.GAMI_BRIDGE !== undefined
- }
- export const callNativeScheme = (scheme: string) => {
- if (import.meta.client && isNativeContext()) {
- window.location.href = scheme
- }
- }
- export const callDeepLink = (url: string) => {
- if (import.meta.client && !isIOS()) {
- window.location.href = url
- }
- }
|