forked from gliviu/json-easy-filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jef-browser.min.js
1 lines (1 loc) · 3.68 KB
/
jef-browser.min.js
1
var Jef=function(){"use strict";var t=function(t,e){e||(e=".");for(var r="",i=0;i<t.length;i++)0!==i&&(r+=e),r+=t[i];return r},e=function(e,r){this.isRoot=!1,this.root=r,r.path===e.path&&(this.isRoot=!0),this.level=e.pathArray.length-r.pathArray.length,this.pathArray=e.pathArray.slice(r.pathArray.length,e.pathArray.length),this.path=t(this.pathArray)},r=function(s){this.isEmpty=function(){return 0===this.count},this.get=function(t){if(!t)return this;var e;return e=this.isRoot?t:this.path+"."+t,this._nodeHash[a+"."+e]},this._iterate=function(e){var r=this;n(this.value,function(i,n,a,s,h,o,f){var u=r._internalPath+(f?"":"."+t(a)),l=r._nodeHash[u];e(l)})},this._filter=function(t,r){var i=[],n=this;return this._iterate(function(a){var s=new e(a,n);if(!r||r&&s.level===r){var h=t(a,s);void 0!==h&&i.push(h)}}),i},this.filter=function(t){return this._filter(t)},this.filterFirst=function(t){return this._filter(t,1)},this.filterLevel=function(t,e){return this._filter(e,t)},this.validate=function(t){var e=!0;return this.filter(function(r,i){var n=t(r,i);n===!1&&(e=!1)}),e},this.remove=function(t){var e=this.filter(function(e,r){return t(e,r)}),i=!0,n=function(t){if(t instanceof Array)t.forEach(function(t){n(t)});else if(t instanceof r)if(t.isRoot)console.log("Root node cannot be deleted."),i=!1;else if("array"===t.parent.type()){var e=t.parent.value,a=e.indexOf(t.value);e.splice(a,1)}else delete t.parent.value[t.key];else console.log("Only JefNode objects must be returned from delete callback."),i=!1};return n(e),i},this.has=function(t){if(!this.value)return!1;if(t instanceof RegExp){if("object"===this.type()){for(var e in this.value)if(t.test(e))return!0;return!1}return!1}return void 0!==this.value[t]?!0:!1},this.getType=function(){return this.type()},this.type=function(){return{}.toString.call(this.value).match(/\s([a-zA-Z]+)/)[1].toLowerCase()},this.hasType=function(){if(1===arguments.length)return this.type()===arguments[0];for(var t=0;t<arguments.length;t++)if(this.type()===arguments[t])return!0;return!1},this.refresh=function(){var e={},i=this._internalPath+".";for(var s in this._nodeHash)0===s.indexOf(i)&&(e[s]=!0);var h=this;n(this.value,function(i,n,s,o,f,u,l,c,v){if(!l){var p=h._internalPath+"."+t(s);e[p]=!1;var d;if(h._nodeHash[p])d=h._nodeHash[p];else{d=new r,d._nodeHash=h._nodeHash,d.root=h._nodeHash[a],d._internalPath=p,d.pathArray=h.pathArray.concat(s),d.path=h.isRoot?t(d.pathArray):h.path+"."+t(d.pathArray),d.key=i,h._nodeHash[p]=d;var y,_=d.pathArray.slice(0,d.pathArray.length-1);y=0===_.length?h._nodeHash[a]:h._nodeHash[a+"."+t(_)],y&&(d.parent=y),d.parent.count++}d.value=n,d.level=h.level+u,d.isRoot=!1,d.isLeaf=c,d.isCircular=v}});for(var i in e)e[i]===!0&&delete this._nodeHash[i]},this._nodeHash=null,this._internalPath=null,this.print=function(t){var e=[],r=this;return this._iterate(function(i){e.push(t?i._internalPath.replace(a,"root")+" - "+JSON.stringify(r._nodeHash[i._internalPath].value):i._internalPath.replace(a,"root"))}),e},this.count=0,s&&(this.root=this,this._nodeHash={},this.pathArray=[],this.path="",this.value=s,this.level=0,this.isRoot=!0,this.isLeaf=i(s),this.isCircular=!1,this._internalPath=a,this._nodeHash[a]=this,this.parent=this,this.refresh())},i=function(t){var e=t instanceof Array,r="object"==typeof t&&!e,i=r&&null!==t||e;return!i},n=function(t,e){var r=[],n=function(t,a,s,h,o,f,u){var l=a instanceof Array,c="object"==typeof a&&!l,v=i(a),p=!1;if(-1!==r.indexOf(a)?p=!0:r.push(a),e(t,a,f,s,h,u,o,v,p),!p){var d,y;if(c)for(d in a)y=f.concat([d]),n(d,a[d],t,a,!1,y,u+1);if(l)for(var _=0;_<a.length;_++)d=""+_,y=f.concat([d]),n(d,a[_],t,a,!1,y,u+1)}};n(void 0,t,void 0,void 0,!0,[],0)},a="$root_4285190851";return{JefNode:r,traverse:n}}(),JefNode=Jef.JefNode,traverse=Jef.traverse;