UNPKG

@mornya/platform-libs

Version:

Parses UA (user-agent) information of the current platform.

1 lines 3.35 kB
var r=this&&this.__read||function(e,r){var t="function"==typeof Symbol&&e[Symbol.iterator];if(!t)return e;var n,o,i=t.call(e),s=[];try{for(;(void 0===r||0<r--)&&!(n=i.next()).done;)s.push(n.value)}catch(e){o={error:e}}finally{try{n&&!n.done&&(t=i.return)&&t.call(i)}finally{if(o)throw o.error}}return s},t=this&&this.__spreadArray||function(e,r,t){if(t||2===arguments.length)for(var n,o=0,i=r.length;o<i;o++)!n&&o in r||((n=n||Array.prototype.slice.call(r,0,o))[o]=r[o]);return e.concat(n||Array.prototype.slice.call(r))},d=this&&this.__values||function(e){var r="function"==typeof Symbol&&Symbol.iterator,t=r&&e[r],n=0;if(t)return t.call(e);if(e&&"number"==typeof e.length)return{next:function(){return{value:(e=e&&n>=e.length?void 0:e)&&e[n++],done:!e}}};throw new TypeError(r?"Object is not iterable.":"Symbol.iterator is not defined.")},o=(Object.defineProperty(exports,"__esModule",{value:!0}),exports.resolveMatches=resolveMatches,exports.checkVersion=function checkVersion(e,r){for(var t=e.split(".").slice(0,3).map(function(e){return Number(e)}),n=r.split(".").slice(0,3).map(function(e){return Number(e)}),o=0;o<3;o++){if(t[o]<n[o])return 1;if(t[o]>n[o])return-1}return 0},exports.isTouchDevice=function isTouchDevice(){return"undefined"!=typeof window&&("ontouchstart"in window||0<navigator.maxTouchPoints||!!window.matchMedia&&window.matchMedia("(hover: none), (pointer: coarse)").matches&&"VisualViewport"in window)},exports.getInfo=function getInfo(e){var r={productName:"",productVersion:"",systemInfo:"",extras:""},t=e.match(/(.*?)\/(.*?)\s\((.*?)\)\s?(.*)/);t&&(r.productName=t[1],r.productVersion=t[2],r.systemInfo=t[3],r.extras=t[4]);var t=resolveMatches(o.OS,r.systemInfo),e=resolveMatches(o.Browser,e),n=["AOS","IOS","WOS"].includes(t.name)?"MOBILE":"DESKTOP";return{UA:r,OS:{vendor:t.name,version:getAsSemVer(t.matches.slice(1,4)).replace(/[x|y]/g,"0")},Browser:{type:n,vendor:e.name,version:getAsSemVer(e.matches.slice(1,4))},isDesktop:"DESKTOP"==n,isMobile:"MOBILE"==n,isAOS:"AOS"===t.name,isIOS:"IOS"===t.name}},{OS:{IOS:[/iPhone\sOS\s([0-9]+)_([0-9]+)_?([0-9]+)?/,/iPad;\sCPU\sOS\s([0-9]+)_([0-9]+)_?([0-9]+)?/],AOS:[/Android\s([0-9]+)(?:\.([0-9]+))?(?:\.([0-9]+))?/],WOS:[/Windows\sPhone\sOS\s([0-9]+)\.([0-9]+)\.?([0-9]+)?/],WIN:[/Windows\sNT\s([0-9]+)\.([0-9]+)/],MAC:[/Mac\sOS\sX\s?([0-9a-z]+)[_.]([0-9a-z]+)[_.]?([0-9]+)?/],LINUX:[/Linux\sx86_64/]},Browser:{OPERA:[/OPR\/([0-9]+)\.([0-9]+)\.([0-9]+)\.?([0-9]+)?$/],EDGE:[/Edge\/([0-9]+)\.([0-9]+)\.?([0-9]+)?$/],CHROME:[/Chrome\/([0-9]+)\.([0-9]+)\.([0-9]+)/],SAFARI:[/Safari\/([0-9]+)\.([0-9]+)\.?([0-9]+)?$/],FIREFOX:[/Firefox\/([0-9]+)\.([0-9]+)$/],IE:[/compatible;\sMSIE\s([0-9]+)\.([0-9]+);/,/Trident\/7.0;\srv:([0-9]+)\.([0-9]+)/]}});function getAsSemVer(e){e=null!=e&&e.length?t([],r(e.filter(Boolean)),!1):["0"];return e.length<2?(e.push("0"),e.push("0")):e.length<3&&e.push("0"),e.join(".")}function resolveMatches(e,r){var t,n,o,i;if(r)try{for(var s=d(Object.keys(e)),a=s.next();!a.done;a=s.next()){var c=a.value;try{o=void 0;for(var l=d(e[c]),u=l.next();!u.done;u=l.next()){var f=u.value,h=r.match(f);if(h)return{name:c,matches:h}}}catch(e){o={error:e}}finally{try{u&&!u.done&&(i=l.return)&&i.call(l)}finally{if(o)throw o.error}}}}catch(e){t={error:e}}finally{try{a&&!a.done&&(n=s.return)&&n.call(s)}finally{if(t)throw t.error}}return{name:"",matches:[]}}