@ta-interaktiv/browsercheck
Version:
Small library to check for various browsers and screen sizes.
144 lines (127 loc) • 3.74 kB
text/typescript
/* eslint-disable @typescript-eslint/class-name-casing */
/**
* Provides various test cases to check for the browser capabilities.
*/
export class browserCheck {
/**
* Is the window width lower than 600px?
* @returns {boolean}
*/
public static get isMobile(): boolean {
return window.innerWidth < 600
}
/**
* Is the window width between 600 and 900px?
* @returns {boolean}
*/
public static get isTablet(): boolean {
return window.innerWidth >= 600 && window.innerWidth < 900
}
/**
* Is the window width larger than 900px?
* @returns {boolean}
*/
public static get isDesktop(): boolean {
return window.innerWidth >= 900
}
/**
* Does the User Agent string contain «iPhone» or «iPod»?
* @returns {boolean}
*/
public static get isIPhone(): boolean {
return /i(Phone|Pod)/i.test(window.navigator.userAgent)
}
/**
* Does the User Agent string contain «iPad»?
* @returns {boolean}
*/
public static get isIPad(): boolean {
return /iPad/i.test(window.navigator.userAgent)
}
/**
* Does the User Agent string contain one of «iPhone», «iPad», or «iPod»?
* @returns {boolean}
*/
public static get isIOS(): boolean {
return browserCheck.isIPad || browserCheck.isIPhone
}
/**
* Does the User Agent string contain «Android»?
* @returns {boolean}
*/
public static get isAndroid(): boolean {
return /Android/i.test(window.navigator.userAgent)
}
/**
* Does the user agent string contain «app-ios-smartphone», as per iApp
* user agent string specs.
* @returns {boolean}
*/
public static get isIOSNativeApp(): boolean {
return /app-ios-smartphone/i.test(window.navigator.userAgent)
}
/**
* Does the user agent string contain «app-android-smartphone», as per
* iApp user agent string specs.
* @returns {boolean}
*/
public static get isAndroidNativeApp(): boolean {
return /app-android-smartphone/i.test(window.navigator.userAgent)
}
/**
* Returns whether this is either an iOS or Android smartphone-sized
* native app.
* @returns {boolean}
*/
public static get isNativeSmartphoneApp(): boolean {
return browserCheck.isIOSNativeApp || browserCheck.isAndroidNativeApp
}
/**
* Does the user agent string contain «app-ios-tablet»?
* @returns {boolean}
*/
public static get isIOSNativeTabletApp(): boolean {
return /app-ios-tablet/i.test(window.navigator.userAgent)
}
/**
* Doest the user agent string contain «app-android-tablet»?
* @returns {boolean}
*/
public static get isAndroidNativeTabletApp(): boolean {
return /app-android-tablet/i.test(window.navigator.userAgent)
}
/**
* Does the user agent suggest to be a native tablet app, either iOS or
* Android?
* @returns {boolean}
*/
public static get isNativeTabletApp(): boolean {
return (
browserCheck.isIOSNativeTabletApp || browserCheck.isAndroidNativeTabletApp
)
}
/**
* Is it one of the native apps, both smartphones and tablets.
* @returns {boolean}
*/
public static get isNativeApp(): boolean {
return browserCheck.isNativeSmartphoneApp || browserCheck.isNativeTabletApp
}
/**
* Is it an Android native app, either tablet- or smartphone-sized?
* @returns {boolean}
*/
public static get isAndroidApp(): boolean {
return (
browserCheck.isAndroidNativeApp || browserCheck.isAndroidNativeTabletApp
)
}
/**
* Is it an iOS native app, either tablet- or smartphone-sized?
* @returns {boolean}
*/
public static get isIOSApp(): boolean {
return browserCheck.isIOSNativeApp || browserCheck.isIOSNativeTabletApp
}
}
export default browserCheck