jshow-device-detect
Version:
jshow detect device
67 lines • 1.77 kB
JavaScript
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import REGEXP from '../constants/regexp';
import { execRxp, getMajor } from '../utils';
/**
* UA 解析类。
*/
class UAParser {
constructor(ua) {
this._userAgent = '';
this.reset(ua);
}
get userAgent() {
return this._userAgent;
}
set userAgent(value) {
this.reset(value);
}
get browser() {
return this._cacheObject.browser;
}
get engine() {
return this._cacheObject.engine;
}
get os() {
return this._cacheObject.os;
}
get device() {
return this._cacheObject.device;
}
get cpu() {
return this._cacheObject.cpu;
}
reset(ua) {
this._userAgent = ua || (typeof window !== 'undefined' && window.navigator && window.navigator.userAgent) || '';
this._cacheObject = {
ua: this._userAgent,
browser: this.getBrowser(),
engine: this.getEngine(),
os: this.getOS(),
device: this.getDevice(),
cpu: this.getCPU(),
};
return this;
}
getBrowser(ua = this._userAgent) {
const browser = execRxp(ua, REGEXP.browser);
browser.major = getMajor(browser.version);
return browser;
}
getCPU(ua = this._userAgent) {
return execRxp(ua, REGEXP.cpu);
}
getDevice(ua = this._userAgent) {
return execRxp(ua, REGEXP.device);
}
getEngine(ua = this._userAgent) {
return execRxp(ua, REGEXP.engine);
}
getOS(ua = this._userAgent) {
return execRxp(ua, REGEXP.os);
}
toString() {
return this._cacheObject.toString();
}
}
export default UAParser;
//# sourceMappingURL=parser.js.map