-
Notifications
You must be signed in to change notification settings - Fork 3
/
usertiming-decompression.vanilla.min.js
1 lines (1 loc) · 3.53 KB
/
usertiming-decompression.vanilla.min.js
1
!function(r){"use strict";var e,t;void 0!==r&&(e=r,t=e.UserTimingDecompression);var n,s=n={};s.noConflict=function(){return e.UserTimingDecompression=t,s},s.decompressFromString=function(r,e){var t=[];if(e=e||{},"string"!=typeof r)return t;var s=r[0];return"~"===s||"{"===s||"("===s?t=n.decompressUriTrie(r):"0"===s?t=n.decompressUriArray(r.substring(1)):"1"===s&&(t=n.decompressUriMap(r.substring(1),e.map)),t},s.decompressUriTrie=function(r,e){var t=[];if("string"==typeof r)if("~"===r[0])r=n.jsUrl(r);else{if("{"!==r[0]&&"("!==r[0])return[];"("===r[0]&&(r=r.replace(/\(/g,"{").replace(/\)/g,"}").replace(/~/g,":").replace(/\-/g,",").replace(/'/g,'"'));try{r=JSON.parse(r)}catch(r){return[]}}e=e||"";for(var s in r)if(r.hasOwnProperty(s)){var i=r[s],a=e+s;if(-1!==a.indexOf("!",a.length-1)&&(a=a.substring(0,a.length-1)),"string"==typeof i)for(var o=i.split("|"),f=0;f<o.length;f++)t=t.concat(this.generateUserTimings(a,o[f]));else if("number"==typeof i)t=t.concat(this.generateUserTimings(a,""+i));else{var u=this.decompressUriTrie(i,a);t=t.concat(u)}}return t},s.decompressUriArray=function(r){var e=[];if("string"!=typeof r)return e;for(var t=r.split("~"),s=0;s<t.length;s+=2)e=e.concat(n.generateUserTimings(t[s],t[s+1]));return e},s.decompressUriMap=function(r,e){var t=[];if("string"!=typeof r)return t;if(void 0===e)return this.decompressUriArray(r);for(var s=r.split("~"),i=0;i<s.length;i++){var a=s[i],o=0,f="";"-"===a[0]?(o="0"===a[1]?36+parseInt(a[2],36):36+parseInt(a.substr(1,2),36),f=a.substr(3)):(o=parseInt(a[0],36),f=a.substr(1));for(var u in e)if(e.hasOwnProperty(u)&&e[u]===o){t=t.concat(n.generateUserTimings(u,f));break}}return t},s.generateUserTimings=function(r,e){var t=[],s=0;if("number"==typeof e&&(e=""+e),"string"!=typeof r||"string"!=typeof e)return t;r=r.replace("%7E","~");for(var i=n.decompressArray(e),a=0;a<i.length;a++){var o=i[a];s+=parseInt(o,36);var f={name:r,startTime:s};-1!==o.indexOf("_")?(f.duration=parseInt(o.substring(o.indexOf("_")+1),36),f.entryType="measure"):(f.duration=0,f.entryType="mark"),t.push(f)}return t},s.decompressArray=function(r){if("string"!=typeof r)return[];for(var e=[],t=r.split("."),n=0;n<t.length;n++){var s=t[n],i=s.split("*");if(1===i.length)e.push(""!==s?s:"0");else{s=i[0];var a=i[1];""===a&&(a=2);for(var o=0;o<a;o++)e.push(""!==s?s:"0")}}return e},s.decompressUserTiming=function(r,e){if("string"!=typeof r)return[];var t=n.decompressFromString(r,e);return t.sort(function(r,e){return r.startTime!==e.startTime?r.startTime-e.startTime:r.duration-e.duration}),t};var i={true:!0,false:!1,null:null};s.jsUrl=function(r){function e(e){if(r[n]!==e)throw new Error("bad JSURL syntax: expected "+e+", got "+(r&&r[n]));n++}function t(){for(var e,t=n,i="";n<s&&"~"!==(e=r[n])&&")"!==e;)switch(e){case"*":t<n&&(i+=r.substring(t,n)),"*"===r[n+1]?(i+=String.fromCharCode(parseInt(r.substring(n+2,n+6),16)),t=n+=6):(i+=String.fromCharCode(parseInt(r.substring(n+1,n+3),16)),t=n+=3);break;case"!":t<n&&(i+=r.substring(t,n)),i+="$",t=++n;break;default:n++}return i+r.substring(t,n)}if("string"!=typeof r)return r;var n=0,s=r.length;return function a(){var o,f,u;switch(e("~"),f=r[n]){case"(":if(n++,"~"===r[n])if(o=[],")"===r[n+1])n++;else do{o.push(a())}while("~"===r[n]);else if(o={},")"!==r[n])do{var p=t();o[p]=a()}while("~"===r[n]&&++n);e(")");break;case"'":n++,o=t();break;default:for(u=n++;n<s&&/[^)~]/.test(r[n]);)n++;var c=r.substring(u,n);if(/[\d\-]/.test(f))o=parseFloat(c);else if(void 0===(o=i[c]))throw new Error("bad value keyword: "+c)}return o}()},void 0!==e&&(e.UserTimingDecompression=s)}("undefined"!=typeof window?window:void 0);