-
Notifications
You must be signed in to change notification settings - Fork 3
/
usertiming-decompression.min.js
1 lines (1 loc) · 3.66 KB
/
usertiming-decompression.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],o=e+s;if(-1!==o.indexOf("!",o.length-1)&&(o=o.substring(0,o.length-1)),"string"==typeof i)for(var a=i.split("|"),f=0;f<a.length;f++)t=t.concat(this.generateUserTimings(o,a[f]));else if("number"==typeof i)t=t.concat(this.generateUserTimings(o,""+i));else{var u=this.decompressUriTrie(i,o);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 o=s[i],a=0,f="";"-"===o[0]?(a="0"===o[1]?36+parseInt(o[2],36):36+parseInt(o.substr(1,2),36),f=o.substr(3)):(a=parseInt(o[0],36),f=o.substr(1));for(var u in e)if(e.hasOwnProperty(u)&&e[u]===a){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),o=0;o<i.length;o++){var a=i[o];s+=parseInt(a,36);var f={name:r,startTime:s};-1!==a.indexOf("_")?(f.duration=parseInt(a.substring(a.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 o=i[1];""===o&&(o=2);for(var a=0;a<o;a++)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 o(){var a,f,u;switch(e("~"),f=r[n]){case"(":if(n++,"~"===r[n])if(a=[],")"===r[n+1])n++;else do{a.push(o())}while("~"===r[n]);else if(a={},")"!==r[n])do{var p=t();a[p]=o()}while("~"===r[n]&&++n);e(")");break;case"'":n++,a=t();break;default:for(u=n++;n<s&&/[^)~]/.test(r[n]);)n++;var c=r.substring(u,n);if(/[\d\-]/.test(f))a=parseFloat(c);else if(void 0===(a=i[c]))throw new Error("bad value keyword: "+c)}return a}()},"function"==typeof define&&define.amd?define([],function(){return s}):"undefined"!=typeof module&&module.exports?module.exports=s:void 0!==e&&(e.UserTimingDecompression=s)}("undefined"!=typeof window?window:void 0);