json-q
Version:
Retrieves values from JSON objects (and JavaScript objects) by css-selector-like query (includes attribute filters and array flattening).
1 lines • 10 kB
JavaScript
var jsonQ=function(t){function e(n){if(r[n])return r[n].exports;var o=r[n]={exports:{},id:n,loaded:!1};return t[n].call(o.exports,o,o.exports,e),o.loaded=!0,o.exports}var r={};return e.m=t,e.c=r,e.p="",e(0)}([function(t,e,r){"use strict";var n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},o=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t},i=r(1),f=r(3),s=f.dedup,a=f.deep_filter,u=f.flatten,c=r(4),p=c.parse,l=r(5),h=l.parse_filter,_=r(6),y=_.operator,d=r(7),v=d.pseudo,b=function(t,e,r){var n=g(i(t),p(e),r);return n},g=function t(e,r,n){r=o([],r);var i=[];if(e instanceof Array)e.forEach(function(e){i=i.concat(t(e,r,n))});else for(i=[e];r[0];)i=r[0].any?m(i,r[0].any,!0):r[0].next?m(i,r[0].next):[],r[0].transformation&&r[0].transformation.forEach(function(t){var e=i;t.filter&&(e=[],i.forEach(function(r){var o=w(r,t.filter,n);o&&(e=e.concat(o))})),t.pseudo&&(e=x(i,t.pseudo,n)),i=e}),r.splice(0,1);return i=s(i)},m=function t(e,r,o){var i=[];if(e)if(e instanceof Array)e.forEach(function(e){i=i.concat(t(e,r,o))});else if("*"===r)if("object"==("undefined"==typeof e?"undefined":n(e))){o&&i.push(e);for(var f in e)i.push(e[f]),o&&(i=i.concat(t(e[f],r,!0)))}else i.push(e);else if(e[r]&&(e[r]instanceof Array?i=i.concat(u(e[r])):i.push(u(e[r]))),o&&"object"==("undefined"==typeof e?"undefined":n(e)))for(var s in e)i=i.concat(t(e[s],r,!0));return i},x=function(t,e,r){var n=o({},v,(r||{}).pseudo);if(!n[e])throw new Error("Pseudo '"+e+"' not found.");var i=n[e]||function(){return t};return i(t)},w=function(t,e,r){var n=h(e,r);return n.left?E(t,n,r)?a(t,function(e,o,i){if(e instanceof Array){var f=[];if(o){var s=o[i],a=o instanceof Array?[]:{};for(var u in o)a[u]=o[u],o[u]=void 0;for(var c=0;c<s.length;c++)o[i]=[s[c]],E(t,n,r)&&f.push(s[c]);for(var p in a)o[p]=a[p];o[i]=f}else for(var l=0;l<e.length;l++)E(e[l],n,r)&&f.push(e[l]);e=f}return e}):void 0:t},E=function(t,e,r){var n=o({},y,(r||{}).operator),i=e.left,f=e.right,s=n[e.delimiter]||function(){},a=g(t,p(i),r);return s(a,f)};t.exports={get:b}},function(t,e,r){(function(e){var r=function(){"use strict";function t(r,n,o,i){function s(r,o){if(null===r)return null;if(0==o)return r;var a,l;if("object"!=typeof r)return r;if(t.__isArray(r))a=[];else if(t.__isRegExp(r))a=new RegExp(r.source,f(r)),r.lastIndex&&(a.lastIndex=r.lastIndex);else if(t.__isDate(r))a=new Date(r.getTime());else{if(p&&e.isBuffer(r))return a=e.allocUnsafe?e.allocUnsafe(r.length):new e(r.length),r.copy(a),a;"undefined"==typeof i?(l=Object.getPrototypeOf(r),a=Object.create(l)):(a=Object.create(i),l=i)}if(n){var h=u.indexOf(r);if(h!=-1)return c[h];u.push(r),c.push(a)}for(var _ in r){var y;l&&(y=Object.getOwnPropertyDescriptor(l,_)),y&&null==y.set||(a[_]=s(r[_],o-1))}return a}var a;"object"==typeof n&&(o=n.depth,i=n.prototype,a=n.filter,n=n.circular);var u=[],c=[],p="undefined"!=typeof e;return"undefined"==typeof n&&(n=!0),"undefined"==typeof o&&(o=1/0),s(r,o)}function r(t){return Object.prototype.toString.call(t)}function n(t){return"object"==typeof t&&"[object Date]"===r(t)}function o(t){return"object"==typeof t&&"[object Array]"===r(t)}function i(t){return"object"==typeof t&&"[object RegExp]"===r(t)}function f(t){var e="";return t.global&&(e+="g"),t.ignoreCase&&(e+="i"),t.multiline&&(e+="m"),e}return t.clonePrototype=function(t){if(null===t)return null;var e=function(){};return e.prototype=t,new e},t.__objToStr=r,t.__isDate=n,t.__isArray=o,t.__isRegExp=i,t.__getRegExpFlags=f,t}();"object"==typeof t&&t.exports&&(t.exports=r)}).call(e,r(2).Buffer)},function(t,e){t.exports={}},function(t,e){"use strict";var r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},n=function(t){return t instanceof Array?t.filter(function(t,e,r){return r.indexOf(t)===e}):t},o=function t(e){var r=e;return e instanceof Array&&(r=[],e.forEach(function(e){r=r.concat(t(e))})),r},i=function(t,e,r){if(t instanceof Array){var n=!1;for(var o in t)if(r(t[o],e)){n=!0;break}return n}return r(t,e)},f=function t(e,n,o,i,f){var s=e;if("object"==("undefined"==typeof e?"undefined":r(e))){if(n&&(s=n(s,i,f)),s instanceof Array)s=s.filter(function(e,r){return t(e,n,o,s,r)});else for(var a in s)s[a]=t(s[a],n,o,s,a);o&&(s=o(s,i,f))}return s},s=function t(e,n,o){if("object"==("undefined"==typeof e?"undefined":r(e))){n&&n(e);for(var i in e)"object"==r(e[i])&&t(e[i],n,o);o&&o(e)}},a=function t(e,n){if(e instanceof Array&&e.sort(n),"object"==("undefined"==typeof e?"undefined":r(e)))for(var o in e)"object"==r(e[o])&&t(e[o],n)};t.exports={dedup:n,flatten:o,true_if_one_is_true:i,deep_filter:f,deep_iterate:s,deep_sort:a}},function(t,e){"use strict";function r(){this.parsed=[],this.op_set_init_values()}var n={FILTER_OPEN_FAIL:"[ inside filter",FILTER_CLOSE_FAIL:"] outside filter",CHARS_AFTER_TRANSFORMATION:"something strange after filter",DOT_WITHOUT_LEVEL_NAME:"dot without level name"},o="none",i="filter",f="pseudo";r.prototype.getParsed=function(){return this.parsed},r.prototype.filter_start=function(t){switch(this.state){case i:throw new Error(n.FILTER_OPEN_FAIL);case f:this.op_pseudo_char(t);break;default:this.state=i,this.op_filter_new()}},r.prototype.filter_end=function(t){switch(this.state){case i:this.state=o;break;case f:this.op_pseudo_char(t);break;default:throw new Error(n.FILTER_CLOSE_FAIL)}},r.prototype.dot=function(t){switch(this.state){case i:this.op_filter_char(t);break;case f:this.op_new_level(t);break;default:this.op_new_level(t)}},r.prototype.space=function(t){switch(this.state){case i:this.op_filter_char(t);break;case f:this.op_new_level(t);break;default:this.op_new_level(t)}},r.prototype.pseudo_start=function(t){switch(this.state){case i:this.op_filter_char(t);break;case f:this.op_pseudo_new(t);break;default:this.state=f,this.op_pseudo_new(t)}},r.prototype.default=function(t){switch(this.state){case i:this.op_filter_char(t);break;case f:this.op_pseudo_char(t);break;default:if(this.transformation.length>0)throw new Error(n.CHARS_AFTER_TRANSFORMATION);this.op_expr_char(t)}},r.prototype.op_set_init_values=function(){this.state=o,this.expr="",this.next=!1,this.transformation=[]},r.prototype.op_filter_new=function(t){this.transformation.push({filter:""})},r.prototype.op_filter_char=function(t){var e=this.transformation[this.transformation.length-1];e.filter+=t},r.prototype.op_pseudo_new=function(t){this.transformation.push({pseudo:""})},r.prototype.op_pseudo_char=function(t){var e=this.transformation[this.transformation.length-1];e.pseudo+=t},r.prototype.op_expr_char=function(t){this.expr+=t},r.prototype.op_new_level=function(t){if(this.expr&&""!=this.expr||this.transformation.length>0){var e={};e[this.next?"next":"any"]=this.expr.replace(/\\/g,"")?this.expr.replace(/\\/g,""):"*",this.transformation.length>0&&(e.transformation=this.transformation.map(function(t){return t.filter&&(t.filter=t.filter.trim()),t.pseudo&&(t.pseudo=t.pseudo.trim()),t})),this.parsed.push(e)}else if(this.next)throw new Error(n.DOT_WITHOUT_LEVEL_NAME);this.op_set_init_values(),this.next="."===t},r.prototype.op_finalize=function(){(this.expr&&""!=this.expr||this.transformation.length>0)&&this.op_new_level()};var s=function(t){var e=new r;"string"!=typeof t&&(t+="");for(var n in t)0!=n&&"\\"==t[n-1]||"["!==t[n]?0!=n&&"\\"==t[n-1]||"]"!==t[n]?0!=n&&"\\"==t[n-1]||"."!==t[n]?0!=n&&"\\"==t[n-1]||" "!==t[n]?0!=n&&"\\"==t[n-1]||":"!==t[n]?e.default(t[n]):e.pseudo_start(t[n]):e.space(t[n]):e.dot(t[n]):e.filter_end(t[n]):e.filter_start(t[n]);return e.op_finalize(),e.getParsed()};t.exports={parse:s,Errors:n}},function(t,e,r){"use strict";var n=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t},o=r(6),i=o.operator,f=function(t,e){"undefined"==typeof t&&(t=""),"string"!=typeof t&&(t+="");for(var r={left:""},n=0;n<t.length;n++){var o=a(t,n,e);if(o){r.delimiter=o,r.right=t.substring(n+1),r.left=t.substring(0,n-o.length+1);break}}return r.right||(r.left=t),r.left=s(r.left,e),r.right&&(r.right=s(r.right,e)),r},s=function(t,e){var r=t.trim(),n=u(e);return n.forEach(function(t){r=r.replace("\\"+t,t)}),r},a=function(t,e,r){for(var n=void 0,o=u(r),i=0;i<o.length&&!n;i++){var f=c(t,e,o[i]);f&&(n=o[i])}return n},u=function(t){var e=n({},i,(t||{}).operator),r=Object.keys(e);return r.sort(function(t,e){return e.length-t.length}),r},c=function(t,e,r){var n=!1;if(e>=r.length-1){for(var o=0;o<r.length-1&&t[e-o]==r[r.length-1-o];)o++;o==r.length-1&&t[e-o]==r[r.length-1-o]&&(e==r.length-1?n=!0:"\\"!=t[e-1]&&(n=!0))}return n};t.exports={parse_filter:f}},function(t,e,r){"use strict";var n=r(3),o=n.true_if_one_is_true,i=o,f={"=":function(t,e){return i(t,e,function(t,e){return t==e})},"*=":function(t,e){return i(t,e,function(t,e){return"string"==typeof t&&t.indexOf(e)!=-1})},"^=":function(t,e){return i(t,e,function(t,e){return"string"==typeof t&&t.startsWith(e)})},"$=":function(t,e){return i(t,e,function(t,e){return"string"==typeof t&&t.endsWith(e)})},"~=":function(t,e){return i(t,e,function(t,e){if("string"==typeof t){var r=t.split(" ");return r.indexOf(e)!=-1}})},"|=":function(t,e){return i(t,e,function(t,r){if("string"==typeof t){var n=t.split("-");return n[0]==e}})},undefined:function(t,e){return t.length>0}};t.exports={operator:f}},function(t,e){"use strict";var r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},n={empty:function(t){return t.filter(function(t){return"object"!==("undefined"==typeof t?"undefined":r(t))})}};t.exports={pseudo:n}}]);