lib/har/viewer/scripts/json-query/JSONQuery.js

Summary

Maintainability
A
0 mins
Test Coverage
/*
Copyright Jason E. Smith 2008 Licensed under the Apache License, Version 2.0 (the "License");
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*/

(function(){function map(e,t){var n=e.length;if(typeof t!="function")throw new TypeError;var r=new Array(n),i=arguments[2];for(var s=0;s<n;s++)s in e&&(r[s]=t.call(i,e[s],s,e));return r}function filter(e,t){var n=e.length;if(typeof t!="function")throw new TypeError;var r=new Array,i=arguments[2];for(var s=0;s<n;s++)if(s in e){var o=e[s];t.call(i,o,s,e)&&r.push(o)}return r}function slice(e,t,n,r){var i=e.length,s=[];n=n||i,t=t<0?Math.max(0,t+i):Math.min(i,t),n=n<0?Math.max(0,n+i):Math.min(i,n);for(var o=t;o<n;o+=r)s.push(e[o]);return s}function expand(e,t){function r(e){t&&(t!==!0||e instanceof Array?e[t]&&n.push(e[t]):n.push(e));for(var i in e){var s=e[i];t?s&&typeof s=="object"&&r(s):n.push(s)}}var n=[];if(t instanceof Array){if(t.length==1)return e[t[0]];for(var i=0;i<t.length;i++)n.push(e[t[i]])}else r(e);return n}function distinctFilter(e,t){var n=[],r={};for(var i=0,s=e.length;i<s;++i){var o=e[i];t(o,i,e)&&(typeof o=="object"&&o?o.__included||(o.__included=!0,n.push(o)):r[o+typeof o]||(r[o+typeof o]=!0,n.push(o)))}for(i=0,s=n.length;i<s;++i)n[i]&&delete n[i].__included;return n}var JSONQuery=function(query,obj){function call(e){prefix=e+"("+prefix}function makeRegex(e,t,n,r,i){return str[i].match(/[\*\?]/)||r=="~"?"/^"+str[i].substring(1,str[i].length-1).replace(/\\([btnfr\\"'])|([^\w\*\?])/g,"\\$1$2").replace(/([\*\?])/g,".$1")+(r=="~"?"$/i":"$/")+".test("+t+")":e}tokens=[];var depth=0,str=[];query=query.replace(/"(\\.|[^"\\])*"|'(\\.|[^'\\])*'|[\[\]]/g,function(e){return depth+=e=="["?1:e=="]"?-1:0,e=="]"&&depth>0?"`]":e.charAt(0)=='"'||e.charAt(0)=="'"?"`"+(str.push(e)-1):e});var prefix="";query.replace(/(\]|\)|push|pop|shift|splice|sort|reverse)\s*\(/,function(){throw new Error("Unsafe function call")}),query=query.replace(/([^=]=)([^=])/g,"$1=$2").replace(/@|(\.\s*)?[a-zA-Z\$_]+(\s*:)?/g,function(e){return e.charAt(0)=="."?e:e=="@"?"$obj":(e.match(/:|^(\$|Math|true|false|null)$/)?"":"$obj.")+e}).replace(/\.?\.?\[(`\]|[^\]])*\]|\?.*|\.\.([\w\$_]+)|\.\*/g,function(e,t,n){var r=e.match(/^\.?\.?(\[\s*\^?\?|\^?\?|\[\s*==)(.*?)\]?$/);if(r){var i="";return e.match(/^\./)&&(call("expand"),i=",true)"),call(r[1].match(/\=/)?"map":r[1].match(/\^/)?"distinctFilter":"filter"),i+",function($obj){return "+r[2]+"})"}return r=e.match(/^\[\s*([\/\\].*)\]/),r?".concat().sort(function(a,b){"+r[1].replace(/\s*,?\s*([\/\\])\s*([^,\\\/]+)/g,function(e,t,n){return"var av= "+n.replace(/\$obj/,"a")+",bv= "+n.replace(/\$obj/,"b")+";if(av>bv||bv==null){return "+(t=="/"?1:-1)+";}\n"+"if(bv>av||av==null){return "+(t=="/"?-1:1)+";}\n"})+"})":(r=e.match(/^\[(-?[0-9]*):(-?[0-9]*):?(-?[0-9]*)\]/),r?(call("slice"),","+(r[1]||0)+","+(r[2]||0)+","+(r[3]||1)+")"):e.match(/^\.\.|\.\*|\[\s*\*\s*\]|,/)?(call("expand"),(e.charAt(1)=="."?",'"+n+"'":e.match(/,/)?","+e:"")+")"):e)}).replace(/(\$obj\s*(\.\s*[\w_$]+\s*)*)(==|~)\s*`([0-9]+)/g,makeRegex).replace(/`([0-9]+)\s*(==|~)\s*(\$obj(\s*\.\s*[\w_$]+)*)/g,function(e,t,n,r,i){return makeRegex(e,r,i,n,t)}),query=prefix+(query.charAt(0)=="$"?"":"$")+query.replace(/`([0-9]+|\])/g,function(e,t){return t=="]"?"]":str[t]});var executor=eval("1&&function($,$1,$2,$3,$4,$5,$6,$7,$8,$9){var $obj=$;return "+query+"}");for(var i=0;i<arguments.length-1;i++)arguments[i]=arguments[i+1];return obj?executor.apply(this,arguments):executor};typeof namespace=="function"?namespace("json::JSONQuery",JSONQuery):window.JSONQuery=JSONQuery})();