-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonQ.min.js
10 lines (8 loc) · 11.4 KB
/
jsonQ.min.js
1
2
3
4
5
6
7
8
9
/*
*jsonQ.js v 1.2.0
*Author: Sudhanshu Yadav
*s-yadav.github.com
*Copyright (c) 2013 -2016 Sudhanshu Yadav.
*MIT licenses
*/
!function(a){var b=Function("return this")()||eval("this");"function"==typeof define&&define.amd?define(["jsonq"],function(n){return b.jsonQ=a()}):"object"==typeof module&&module.exports?module.exports=a():b.jsonQ=a()}(function(undefined){var jsonQ=function(n){return new jsonQ.fn.init(n)},error=function(n){throw n},stringify=JSON.stringify,parse=JSON.parse;function matchPath(n,r){return new RegExp("^"+n.join("~~"),"i").test(r.join("~~"))}function newFormat(n){var o=n.keyAdded||[],a=n.json,s=n.path,i=n.newJson;return jsonQ.each(a,function(n,r){var t=s?JSON.parse(JSON.stringify(s)):[];t.push(n),"object"==objType(a)&&(-1==o.indexOf(n)&&(o.push(n),i.jsonQ_path[n]=[]),i.jsonQ_path[n].push({path:t}));var e=objType(r);"object"!=e&&"array"!=e||newFormat({json:r,newJson:i,path:t,keyAdded:o})}),i}jsonQ.settings={sort:{order:"ASC",logic:function(n){return n},caseIgnore:!0,allLevel:!0}};var tFunc={topLevel:function(n){for(var r=this.jsonQ_current,t=this.cloneObj(jsonQ()),e=t.jsonQ_current=[],o="",a=n.key,s=n.method,i=0,u=r.length;i<u;i++){var l,c=!1,f=r[i].path.concat([]);if("parent"==s)0===f.length?c=!0:f.pop();else{var h=f.lastIndexOf(a);-1==h?c=!0:f=f.slice(0,h+1)}o==(l=JSON.stringify(f))||c||e.push({path:f}),o=l}return t.length=e.length,t.selector.push({method:s,key:a}),t},qualTrv:function(n){for(var r=this.jsonQ_current,t=this.cloneObj(jsonQ()),e=t.jsonQ_current=[],o=this.jsonQ_path,a=n.key,s=jsonQ.clone(o[a])||[],i=n.qualifier,u=objType(i),l=n.method,c="find"==l,f=0,h=r.length;f<h;f++){var j=r[f].path,p=[],y=!1;if(!c){if(0===j.length)continue;(p=j.concat([])).pop()}for(var v=0;v<s.length;v++){var g,b=s[v].path;if(c)g=matchPath(j,b);else{var d=b.concat([]);d.pop(),g=p.join()==d.join()}if(g)tFunc.qTest.call(this,u,i,b,e)&&(s.splice(v,1),v--),y=!0;else if(y)break}}return"string"==u&&(t=this.filter.call(t,i)),t.length=t.jsonQ_current.length,t.selector.push({method:l,key:a,qualifier:i}),t},qTest:function(n,r,t,e){var o="function"==n?r.call(this.pathValue(t)):"object"!=n||jsonQ.checkKeyValue(this.pathValue(t),r);return o&&e.push({path:t}),o}},sortFunc={baseConv:function(n,r,t){if("string"==n){if(t.caseIgnore)return r.toLowerCase()}else{if("array"==n)return r.join();if("object"==n)return stringify(jsonQ.order(r))}return r},sortAry:function(n,o,r){return n.sort(function(n,r){var t=o(n),e=o(r);return t<e?-1:e<t?1:0}),"desc"==r.order.toLowerCase()&&n.reverse(),n}};jsonQ.fn=jsonQ.prototype={init:function(n){var r;if(!n)return this;if("string"==(r=objType(n)))try{n=JSON.parse(n)}catch(n){error("Not a valid json string.")}else if("object"!=r&&"array"!=r)return error("Not a valid json object."),n;return this.jsonQ_root=n,this.jsonQ_path={},this.jsonQ_current=[{path:[]}],newFormat({json:n,newJson:this,refresh:!0}),this.length=this.jsonQ_current.length,this.selector=[],this},pathValue:function(n){return jsonQ.pathValue(this.jsonQ_root,n)},setPathValue:function(n,r){return jsonQ.setPathValue(this.jsonQ_root,n,r),this},clone:function(){return parse(stringify(this.jsonQ_current))},cloneObj:function(t){return t=t||{},jsonQ.each(this,function(n,r){t[n]=r}),t.selector=jsonQ.merge([],t.selector),t},value:function(n,r){var t=this.jsonQ_current;if(r=!1!==r,n){for(var e=objType(n),o=0,a=t.length;o<a;o++){var s,i=t[o].path;if("function"==e){var u=this.pathValue(i);s=r?jsonQ.clone(n(u)):n(u)}else s=r?jsonQ.clone(n):n;this.setPathValue(i,s)}return this}var l=[];return this.each(function(n,r,t){l.push(t)}),l},append:function(n,r){return this.appendAt("last",n,r)},prepend:function(n,r){return this.appendAt("first",n,r)},appendAt:function(n,r,t){var e=this.jsonQ_current;if(isNaN(n)&&"first"!=n&&"last"!=n)return error(n+"is not a valid index."),this;for(var o=0,a=e.length;o<a;o++){var s=e[o].path.concat([]),i=s.pop(),u=this.pathValue(s),l=objType(u[i]),c=u[i].length,f=n<0||"first"==n?0:c<n||"last"==n?c:n;if("array"==l)r=t?jsonQ.clone(r):r,u[i].splice(f,0,r);else if("string"==l){var h=u[i];u[i]=h.substring(0,f)+r+h.substring(f,c)}}return this},filter:function(n){var r=this.jsonQ_current,t=this.cloneObj(jsonQ()),e=t.jsonQ_current=[],o=objType(n);if(!n)return this;for(var a=0,s=r.length;a<s;a++){var i=r[a].path;tFunc.qTest.call(this,o,n,i,e)}if("string"==o){var u=/(nth|eq)\((.+)\)/.exec(n);e=u?jsonQ.nthElm(r,u[2],!0):jsonQ.nthElm(r,n,!0),t.jsonQ_current=e}return t.length=e.length,t.selector.push({method:"filter",qualifier:n}),t},find:function(n,r){return tFunc.qualTrv.call(this,{method:"find",key:n,qualifier:r})},sibling:function(n,r){return tFunc.qualTrv.call(this,{method:"sibling",key:n,qualifier:r})},parent:function(){return tFunc.topLevel.call(this,{method:"parent"})},closest:function(n){return tFunc.topLevel.call(this,{method:"closest",key:n})},path:function(){return this.jsonQ_current[0].path},firstElm:function(){return this.pathValue(this.jsonQ_current[0].path)},lastElm:function(){return this.pathValue(this.jsonQ_current[this.length-1].path)},nthElm:function(n,r){return jsonQ.nthElm(this.value(),n,r)},index:function(n,r){return jsonQ.index(this.value(),n,r)},createXML:function(){return jsonQ.createXML(this.value())},sort:function(n,t){t=jsonQ.merge({},jsonQ.settings.sort,t);var r,e,o=this.find(n),a=o.clone(),s=[],i=[],u=objType(o.pathValue(a[0].path)),l=function(n){for(;0!==n.length;){var r=n.pop();if(!isNaN(r)){var t=o.pathValue(n);if("array"==objType(t))return t}}return null};for(r=0,e=a.length;r<e;r++)s.push({pathHolder:a[r].path.concat([]),current:a[r].path.concat([])});for(var c=function(n){return s.splice(n,1),--n};0!==s.length;)for(0,r=0;r<s.length;r++){var f=s[r].current,h=s[r].pathHolder,j=l(f),p=f.join();if(0===f.length||-1!=i.indexOf(p))r=c(r);else{var y=h.slice(f.length+1,h.length);sortFunc.sortAry(j,function(n){var r=jsonQ.pathValue(n,y);return r=sortFunc.baseConv(u,r,t),t.logic(r)},t),t.allLevel?(h[f.length]=0,i.push(p)):r=c(r)}}return jsonQ(o.jsonQ_root).find(n)},each:function(n){for(var r=this.jsonQ_current,t=0,e=r.length;t<e;t++)n(t,r[t].path,this.pathValue(r[t].path));return this},unique:function(){return jsonQ.unique(this.value())},refresh:function(){for(var n=this.selector,r=jsonQ(this.jsonQ_root),t=0,e=n.length;t<e;t++){var o=n[t],a=[];o.key&&a.push(o.key),o.qualifier&&a.push(o.qualifier),r=r[o.method].apply(r,a)}return this.cloneObj.call(r,this),this},prettify:function(n){return jsonQ.prettify(this.value(),n)}},jsonQ.each=function(n,r){for(var t in n)n.hasOwnProperty(t)&&r(t,n[t])};var objType=jsonQ.objType=(ec={"[object Array]":"array","[object Object]":"object","[object String]":"string","[object Number]":"number","[object Boolean]":"boolean","[object Null]":"null","[object Function]":"function"},function(n){var r=Object.prototype.toString.call(n);return ec[r]}),ec;return jsonQ.merge=function(){var n=arguments,r=objType(n[0]),t=1,e=n.length,o=!1,a=n[0];if(0!==e&&("boolean"!=r||1!=e)){"boolean"==r&&(a=n[1],t=2,o=n[0]);for(var s=function(n,r){var t=objType(r),e=objType(a[n]);!o||"array"!=t&&"object"!=t?a[n]=r:(a[n]=t!=e||"array"!=e&&"object"!=e?"array"==t?[]:{}:a[n],jsonQ.merge(o,a[n],r))};t<e;t++)jsonQ.each(n[t],s);return a}},jsonQ.merge(jsonQ,{sort:function(n,t){if(t=jsonQ.merge({},jsonQ.settings.sort,t),"array"==objType(n)){return sortFunc.sortAry(n,function(n){var r=objType(n);return n=sortFunc.baseConv(r,n,t),t.logic(n)},t)}error("Only array is allowed to sort")},order:function(n){if("object"!=typeof n)return n;var u=function(n){return isNaN(n)||(n=parseInt(n)),n},l=function(n){var r=objType(n),t=Object.keys(n);"object"==r&&t.sort(function(n,r){var t=u(n),e=u(r);return t<e?-1:e<t?1:0});for(var e=0,o=t.length;e<o;e++){var a=t[e],s=n[a],i=objType(s);"object"!=i&&"array"!=i||l(s),"object"==r&&(delete n[a],n[a]=s)}};return l(n),n},clone:function(n){var r=objType(n);return"object"==r||"array"==r?parse(stringify(n)):n},index:function(n,r,t){var e=objType(r),o=n.length,a="object"==e||"array"==e||"function"==e;if("function"==e&&(t=!0),a&&!t)var s=stringify(jsonQ.order(r));for(var i=0;i<o;i++){var u=n[i];if(a){var l=objType(u);if(l!=e&&!t)continue;if(t){var c;if("function"==e)c=r.call(u);else if("object"==e&&"object"==l)c=jsonQ.checkKeyValue(u,r);else if("array"==l)if("array"==e)for(var f=0,h=r.length;f<h&&(c=-1!=jsonQ.index(u,r[f]));f++);else c=-1!=jsonQ.index(u,r);if(c)return i}else if(stringify(jsonQ.order(u))==s)return i}else if(r==u)return i}return-1},contains:function(n,r,t){return-1!=jsonQ.index(n,r,t)},checkKeyValue:function(n,r){for(var t in r)if(r.hasOwnProperty(t)&&!jsonQ.identical(r[t],n[t]))return!1;return!0},nthElm:function(array,arg,aryRetrn){var result;if(array[arg])result=array[arg];else if("last"==arg)result=array[array.length-1];else if("first"==arg)result=array[0];else if("random"==arg){var rand=Math.floor(Math.random()*array.length);result=array[rand]}else if("even"==arg)result=jsonQ.nthElm(array,"2n");else if("odd"==arg)result=jsonQ.nthElm(array,"2n+1");else try{var newArray=[],ln=array.length;if(!arg.match(/^[0-9n*+-\/]+$/))throw"";arg=arg.replace(/([0-9])n/g,function(n,r){return r?r+"*n":n});for(var n=0;n<ln;n++){var index=eval(arg);if(ln-1<index)break;newArray.push(array[index])}result=newArray}catch(n){result=array}return result=result||array,"array"!=objType(result)&&aryRetrn?[result]:result},prettify:function(n,r){if("object"!=typeof n)throw"Only valid json object is allowed.";return r?JSON.stringify(n,null,"\t").replace(/\n/g,"</br>").replace(/\t/g," "):JSON.stringify(n,null,3)},identical:function(n,r){function t(r){return"object"!=typeof r||null===r?r:Object.keys(r).sort().map(function(n){return{key:n,value:t(r[n])}})}return JSON.stringify(t(n))===JSON.stringify(t(r))},union:function(){for(var n=arguments,r=[],t=n.length,e=0;e<t;e++)for(var o=n[e].length,a=0;a<o;a++){var s=n[e][a];-1==jsonQ.index(r,s)&&r.push(s)}return r},intersection:function(){var n,r=arguments,t=[],e=r.length;if(1==e)t=r[0];else for(var o=0,a=r[0].length;o<a;o++){for(var s=r[0][o],i=n=1;i<e;i++)if(-1==jsonQ.index(r[i],s)){n=0;break}1==n&&t.push(s)}return t},shuffle:function(n){for(var r=1,t=n.length;r<t;r++){var e=Math.floor(Math.random()*(r+1)),o=n[r];n[r]=n[e],n[e]=o}return n},suffle:function(n){console.error("This method is deprecated. It was a miss spell of shuffle. Use jsonQ.shuffle instead."),jsonQ.shuffle(n)},unique:function(n){for(var r=n.length,t=[],e=0;e<r;e++)-1==jsonQ.index(t,n[e])&&t.push(n[e]);return t},pathValue:function(n,r){var t=0,e=r.length;if(null===n)return null;for(;t<e;){if(null===n[r[t]])return void(n=null);n=n[r[t]],t+=1}return n},setPathValue:function(n,r,t){var e=0,o=n,a=r.length;if(null===n)return null;for(;e<a;)"object"!=typeof o[r[e]]&&(o[r[e]]="number"==objType(r[e+1])?[]:{}),e==r.length-1&&(o[r[e]]=t),o=o[r[e]],e+=1;return n},createXML:function(n){var s=function(n,o){var r=0===(o=o||[]).length,a=objType(n);return r&&o.push('<?xml version="1.0" encoding="ISO-8859-1"?><jsonXML>'),jsonQ.each(n,function(n,r){var t="array"==a?"arrayItem":n,e=objType(r);o.push("<"+t+' type="'+e+'">'),"object"==e||"array"==e?s(r,o):o.push("<![CDATA["+r+"]]>"),o.push("</"+t+">")}),r?(o.push("</jsonXML>"),o.join("")):o};return s(n)},append:function(n,r,t){return jsonQ.appendAt(n,"last",r,t)},prepend:function(n,r,t){return jsonQ.appendAt(n,"first",r,t)},appendAt:function(n,r,t,e){if(!isNaN(r)||"first"==r||"last"==r){var o=objType(n),a=n.length,s=r<0||"first"==r?0:a<r||"last"==r?a:r;return"array"==o?(t=e?jsonQ.clone(t):t,n.splice(s,0,t)):"string"==o&&(n=n.substring(0,s)+t+n.substring(s,a)),n}error(r+"is not a valid index.")}}),jsonQ.fn.init.prototype=jsonQ.fn,jsonQ});