All files utils.ts

46.66% Statements 35/75
28.81% Branches 17/59
31.57% Functions 6/19
47.29% Lines 35/74

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172102x     1x 4x   6x             1x 2x   2x           1x   1x             1x 4x   4x 4x 144x         16x 16x   4x 4x   4x 4x   120x     4x       1x                                                                                                             1x       1x       1x       1x       1x       1x       1x       1x       1x       1x           1x             2x                    
/**
 * Return PascalCase version of snake_case string.
 */
export function snakeToPascal(name: string): string {
    return name
        .split('_')
        .map((v) => (v[0] ? v[0].toUpperCase() : '') + v.slice(1))
        .join('')
}
 
/**
 * Return camelCase version of snake_case string.
 */
export function snakeToCamel(name: string): string {
    const pascal = snakeToPascal(name)
 
    return (pascal[0] ? pascal[0].toLowerCase() : '') + pascal.slice(1)
}
 
/**
 * Print a warning message to console.
 **/
export function logWarn(...args: any[]) {
    // eslint-disable-next-line no-console
    console.warn('[anchor-link]', ...args)
}
 
/**
 * Generate a UUID.
 * */
 
export function uuid(): string {
    let uuid = '',
        ii
    const chars = '0123456789abcdef'
    for (ii = 0; ii < 36; ii += 1) {
        switch (ii) {
            case 8:
            case 13:
            case 18:
            case 23:
                uuid += '-'
                break
            case 14:
                uuid += '4'
                break
            case 19:
                uuid += chars[(Math.random() * 4) | (0 + 8)]
                break
            default:
                uuid += chars[(Math.random() * 16) | 0]
        }
    }
    return uuid
}
 
/** Generate a return url that Anchor will redirect back to w/o reload. */
export function generateReturnUrl(): string | undefined {
    // Return undefined for iOS React Native apps to prevent redirect to Safari
    Iif (isAppleHandheld() && isReactNativeApp()) {
        return undefined
    }
 
    Iif (isChromeiOS()) {
        // google chrome on iOS will always open new tab so we just ask it to open again as a workaround
        return 'googlechrome://'
    }
    Iif (isFirefoxiOS()) {
        // same for firefox
        return 'firefox:://'
    }
    Iif (isAppleHandheld() && isBrave()) {
        // and brave ios
        return 'brave://'
    }
    Iif (isAppleHandheld()) {
        // return url with unique fragment required for iOS safari to trigger the return url
        const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
        let rv = window.location.href.split('#')[0] + '#'
        for (let i = 0; i < 8; i++) {
            rv += alphabet.charAt(Math.floor(Math.random() * alphabet.length))
        }
        return rv
    }
 
    Iif (isAndroid() && isFirefox()) {
        return 'android-app://org.mozilla.firefox'
    }
 
    Iif (isAndroid() && isEdge()) {
        return 'android-app://com.microsoft.emmx'
    }
 
    Iif (isAndroid() && isOpera()) {
        return 'android-app://com.opera.browser'
    }
 
    Iif (isAndroid() && isBrave()) {
        return 'android-app://com.brave.browser'
    }
 
    Iif (isAndroid() && isAndroidWebView()) {
        return 'android-app://webview'
    }
 
    Iif (isAndroid() && isChromeMobile()) {
        return 'android-app://com.android.chrome'
    }
 
    return window.location.href
}
 
export function isAppleHandheld() {
    return /iP(ad|od|hone)/i.test(navigator.userAgent)
}
 
export function isChromeiOS() {
    return /CriOS/.test(navigator.userAgent)
}
 
export function isChromeMobile() {
    return /Chrome\/[.0-9]* Mobile/i.test(navigator.userAgent)
}
 
export function isFirefox() {
    return /Firefox/i.test(navigator.userAgent)
}
 
export function isFirefoxiOS() {
    return /FxiOS/.test(navigator.userAgent)
}
 
export function isOpera() {
    return /OPR/.test(navigator.userAgent) || /Opera/.test(navigator.userAgent)
}
 
export function isEdge() {
    return /Edg/.test(navigator.userAgent)
}
 
export function isBrave() {
    return navigator['brave'] && typeof navigator['brave'].isBrave === 'function'
}
 
export function isAndroid() {
    return /Android/.test(navigator.userAgent)
}
 
export function isReactNativeApp() {
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    return !!window.ReactNativeWebView
}
 
export function isAndroidWebView() {
    return (
        /wv/.test(navigator.userAgent) ||
        (/Android/.test(navigator.userAgent) && isReactNativeApp())
    )
}
 
export function isKnownMobile() {
    return (
        isAppleHandheld() ||
        isChromeiOS() ||
        isChromeMobile() ||
        isFirefoxiOS() ||
        isAndroid() ||
        isAndroidWebView()
    )
}