objecture
Version:
❂ Objecture ⏣ Object Watcher, Property Manager ⊚ Capture property changes for object, array mutator methods. ⊚ Schematize and validate object, array properties. ⊚ Browser, NodeJS compatible. ⁘ Uses Core-Plex - Event Listener Manage
3 lines (2 loc) • 13.9 kB
JavaScript
const e={string:String,number:Number,boolean:Boolean,bigint:BigInt,undefined:void 0,null:null},t=(Object.keys(e),Object.values(e),{object:Object,array:Array}),n=(Object.keys(t),Object.values(t),Object.assign({},e,t));Object.keys(n),Object.values(n),e.String,e.Number,e.Boolean;var s=e=>Object.prototype.toString.call(e).slice(8,-1).toLowerCase();const r={depth:0,maxDepth:10,accessors:[{default:(e,t)=>void 0===t?e:e[t]}.default],ancestors:[]};function i(e,t){const n=[],s=Object.assign({},r,t,{ancestors:[].concat(t.ancestors)});if(s.depth++,s.depth>s.maxDepth)return n;for(const t of s.accessors){const r=t.bind(e)(e);if(r){s.ancestors.includes(r)||s.ancestors.unshift(r);for(const[e,t]of Object.entries(r))if(s.values?s.values&&n.push([e,t]):n.push(e),"object"==typeof t&&null!==t&&!Object.is(t,r)&&!s.ancestors.includes(t)){const r=i(t,s);if(s.values){if(s.values)for(const[t,s]of r){const r=[e,t].join(".");n.push([r,s])}}else for(const t of r){const s=[e,t].join(".");n.push(s)}}}}return n}function a(e,...t){if(!e)return e;for(const n of t)if(n)for(const[t,r]of Object.entries(n)){const n=s(e[t]),i=s(r);e[t]="object"===n&&"object"===i?a(e[t],r):r}return e}var o={default:(e,t)=>void 0===t?e:e[t],get:(e,t)=>void 0===t?e:e.get(t)};function c(e){if(e.length<3)return"{"+e+"}";for(var t=-1,n=2;n<e.length;n++)if("."===e[n]&&"."===e[n-1]&&(n<2||"\\"!==e[n-2])){if(t>-1)return"{"+e+"}";t=n-1}if(t>-1){var s=e.substr(0,t),r=e.substr(t+2);if(s.length>0&&r.length>0)return"["+e.substr(0,t)+"-"+e.substr(t+2)+"]"}return"{"+e+"}"}function l(e){if("string"!=typeof e)throw new TypeError("A pattern must be a string, but "+typeof e+" given");for(var t,n=!1,s=0,r=0,i=-1,a=[""],o=[],u=0;u<e.length;u++){var g=e[u];if("\\"!==g){if("{"===g)if(n)s++;else if(u>i&&!s){t=e.substring(i+1,u);for(var p=0;p<a.length;p++)a[p]+=t;o=[],i=u,n=!0,s++}else s--;else if("}"===g)if(n)r++;else if(1===r){if(t=e.substring(i+1,u),o.length>0){var h=[];o.push(l(t));for(p=0;p<a.length;p++)for(var f=0;f<o.length;f++)for(var d=0;d<o[f].length;d++)h.push(a[p]+o[f][d]);a=h}else{t=c(t);for(p=0;p<a.length;p++)a[p]+=t}i=u,r--}else r--;else n||","!==g||r-s!==1||(t=e.substring(i+1,u),o.push(l(t)),i=u);!n||r!==s&&u!==e.length-1||(n=!1,u=i-1)}else u++}if(-1===i)return[e];var b="{"===e[i]?i:i+1;if(b<e.length){t=e.substr(b);for(p=0;p<a.length;p++)a[p]+=t}return a}function u(e,t){var n,s=!1!==t["!"],r=!1!==t["()"],i=!1;if(s){for(n=0;n<e.length&&"!"===e[n];n++){if(r&&"("===e[n+1]){n--;break}i=!i}n>0&&(e=e.substr(n))}return{pattern:e,isNegated:i}}function g(e){return"-"===e||"^"===e||"$"===e||"+"===e||"."===e||"("===e||")"===e||"|"===e||"["===e||"]"===e||"{"===e||"}"===e||"*"===e||"?"===e||"\\"===e?"\\"+e:e}function p(e,t,n){var s=void 0===t.separator||t.separator,r="",i="",a=".";!0===s?(r="/",i="[/\\\\]",a="[^/\\\\]"):a=s?(i=function(e){for(var t="",n=0;n<e.length;n++)t+=g(e[n]);return t}(r=s)).length>1?"((?!"+(i="(?:"+i+")")+").)":"[^"+i+"]":".";var o=s?i+"+?":"",c=s?i+"*?":"",l=s?e.split(r):[e];return{source:e,segments:l,options:t,separator:s,separatorSplitter:r,separatorMatcher:i,optionalSeparator:c,requiredSeparator:o,wildcard:a,support:{qMark:!1!==t["?"],star:!1!==t["*"],globstar:s&&!1!==t["**"],brackets:!1!==t["[]"],extglobs:!1!==t["()"],excludeDot:n&&!1!==t.excludeDot}}}function h(e,t,n,s){return{source:e,isFirst:n,isLast:s,end:e.length-1}}var f="(?!\\.)";function d(e,t,n){return e.addToUnmatch&&(e.result.unmatch+=t),e.addToMatch&&(n&&!e.dotHandled&&(t=f+t),e.dotHandled=!0,e.result.match+=t),e.result}function b(e,t,n){var s=e.support,r=function(e,t,n){return{pattern:e,segment:t,result:n,openingBracket:t.end+1,closingBracket:-1,openingParens:0,closingParens:0,parensHandledUntil:-1,extglobModifiers:[],scanningForParens:!1,escapeChar:!1,addToMatch:!0,addToUnmatch:e.support.extglobs,dotHandled:!1,i:-1,char:"",nextChar:""}}(e,t,n),i=t.isLast?e.optionalSeparator:e.requiredSeparator;if(s.excludeDot||(r.dotHandled=!0),-1===t.end)return t.isLast&&!t.isFirst?n:d(r,i);if(s.globstar&&"**"===t.source)return d(r,"(?:"+((r.dotHandled?"":f)+e.wildcard+"*?"+i)+")*?");for(;++r.i<=t.end;){if(r.char=r.segment.source[r.i],r.nextChar=r.i<t.end?t.source[r.i+1]:"","\\"===r.char){if(r.i<r.segment.end){r.escapeChar=!0;continue}r.char=""}e=r.pattern,t=r.segment;var a=r.char,o=r.i;if(e.support.brackets&&!r.scanningForParens){if(o>r.openingBracket&&o<=r.closingBracket){r.escapeChar?d(r,g(a)):o===r.closingBracket?(d(r,"]"),r.openingBracket=t.source.length):"-"===a&&o===r.closingBracket-1?d(r,"\\-"):"!"===a&&o===r.openingBracket+1?d(r,"^"):d(r,"]"===a?"\\]":a),r.escapeChar=!1;continue}if(o>r.openingBracket){"]"===a&&!r.escapeChar&&o>r.openingBracket+1&&o>r.closingBracket?(r.closingBracket=o,r.i=r.openingBracket,e.separator?d(r,"(?!"+e.separatorMatcher+")[",!0):d(r,"[",!0)):o===t.end&&(d(r,"\\["),r.i=r.openingBracket,r.openingBracket=t.source.length,r.closingBracket=t.source.length),r.escapeChar=!1;continue}if("["===a&&!r.escapeChar&&o>r.closingBracket&&o<t.end){r.openingBracket=o,r.escapeChar=!1;continue}}if(r.pattern.support.extglobs){var c=r.extglobModifiers,l=(a=r.char,r.nextChar);o=r.i;if("("!==l||r.escapeChar||"@"!==a&&"?"!==a&&"*"!==a&&"+"!==a&&"!"!==a){if(")"!==a||r.escapeChar){if("|"===a&&r.closingParens&&!r.scanningForParens&&!r.escapeChar){d(r,"|");continue}}else if(r.scanningForParens)r.closingParens++;else if(c.length){var u=c.pop();if("!"===u&&-1!==c.indexOf("!"))throw new Error("Nested negated extglobs aren't supported");d(r,")"+(u="!"===u||"@"===u?"":u)),r.addToMatch=!0,r.addToUnmatch=!0,r.closingParens--;continue}}else if(r.scanningForParens)r.openingParens++;else if(o>r.parensHandledUntil&&!r.closingParens)r.parensHandledUntil=o,r.scanningForParens=!0,r.openingParens++;else{if(r.closingParens>=r.openingParens){"!"===a&&(r.addToMatch=!0,r.addToUnmatch=!1,d(r,r.pattern.wildcard+"*?",!0),r.addToMatch=!1,r.addToUnmatch=!0,r.result.useUnmatch=!0),c.push(a),d(r,"(?:",!0),r.openingParens--,r.i++;continue}r.openingParens--}if(r.scanningForParens){r.closingParens!==r.openingParens&&o!==r.segment.end||(r.scanningForParens=!1,r.i=r.parensHandledUntil-1),r.escapeChar=!1;continue}}s=(e=r.pattern).support;!r.escapeChar&&s.star&&"*"===r.char?r.i!==r.segment.end&&"*"===r.nextChar||d(r,e.wildcard+"*?",!0):!r.escapeChar&&s.qMark&&"?"===r.char?d(r,e.wildcard,!0):d(r,g(r.char)),r.escapeChar=!1}return d(r,i)}function v(e,t,n){for(var s=p(e,t,n),r={match:"",unmatch:"",useUnmatch:!1},i=s.segments,a=0;a<i.length;a++){b(s,h(i[a],0,0===a,a===i.length-1),r)}return r.useUnmatch?"(?!^"+r.unmatch+"$)"+r.match:r.match}function m(e,t){if("string"!=typeof t)throw new TypeError("Sample must be a string, but "+typeof t+" given");return e.test(t)}function y(e,t){if("string"!=typeof e&&!Array.isArray(e))throw new TypeError("The first argument must be a single pattern string or an array of patterns, but "+typeof e+" given");if("string"!=typeof t&&"boolean"!=typeof t||(t={separator:t}),2===arguments.length&&void 0!==t&&("object"!=typeof t||null===t||Array.isArray(t)))throw new TypeError("The second argument must be an options object or a string/boolean separator, but "+typeof t+" given");if("\\"===(t=t||{}).separator)throw new Error("\\ is not a valid separator");var n=function(e,t){e=Array.isArray(e)?e:[e],!1!==t["{}"]&&(e=function(e,t){for(var n=[],s=0;s<e.length;s++)for(var r=t(e[s]),i=0;i<r.length;i++)n.push(r[i]);return n}(e,l));for(var n=[],s=[],r="",i=0;i<e.length;i++){var a=u(e[i],t),o=v(a.pattern,t,!a.isNegated);a.isNegated?s.push(o):n.push(o)}return s.length&&(r="(?!(?:"+s.join("|")+")$)"),n.length>1?r+="(?:"+n.join("|")+")":1===n.length?r+=n[0]:r.length&&(r+=v("**",t,!0)),"^"+r+"$"}(e,t),s=new RegExp(n,t.flags),r=m.bind(null,s);return r.options=t,r.pattern=e,r.regexp=s,r}class E{#e;#t=!1;#n=[];#s=[];#r;#i;#a;constructor(e,t){if(!e||!t)return this;const n=((e={})=>{const t={enable:!1,assign:"addEventListener",deassign:"removeEventListener",transsign:"dispatchEvent",bindListener:!0,errorLog:!1,methods:{assign:{addEventListener:function(e,t){const{type:n,listener:s,settings:r}=e,{options:i,useCapture:a}=r;return t.addEventListener(n,s,i||a)},on:function(e,t){const{type:n,listener:s}=e;return t.on(n,s)},once:function(e,t){const{type:n,listener:s}=e;return t.once(n,s)}},deassign:{removeEventListener:function(e,t){const{type:n,listener:s,settings:r}=e,{options:i,useCapture:a}=r;return t.removeEventListener(n,s,i||a)},off:function(e,t){const{type:n,listener:s}=e;return t.off(n,s)}},transsign:{dispatchEvent:function(e,t,n){return t.dispatchEvent(n)},emit:function(e,t,n,...s){return t.emit(n,...s)}}}};for(const[n,s]of Object.entries(e))switch(n){case"methods":t[n]=a(t[n],s);break;case"enableEvents":break;default:t[n]=s}return t})(e);Object.defineProperties(this,{settings:{value:n},path:{value:n.path},type:{value:n.type},assigned:{value:[]},deassigned:{value:[]},transsigned:{value:[]},listener:{configurable:!0,get(){let e;if("string"===s(n.listener)){let r=t;e:for(const e of n.listener.split(".")){if(void 0===r[e])break e;r=r[e]}"function"===s(r)&&(e=r)}else e=n.listener;return!0===n.bindListener&&(e=e.bind(this.#e)),Object.defineProperty(this,"listener",{value:e}),e}}}),this.#e=t,this.enable=this.settings.enable}get enable(){return this.#t}set enable(e){const t=this.#o,n=this.assigned,s=this.deassigned;n.length=0,s.length=0;for(const r of t){const{path:t,target:i,enable:a}=r;this.settings;if(a!==e)if(!0===e)try{this.#c(i),r.enable=e,n.push(r)}catch(e){this.settings.errorLog&&console.error(e)}else if(!1===e)try{this.#l(i),r.enable=e,s.push(r)}catch(e){this.settings.errorLog&&console.error(e)}}this.#t=e}get#u(){return this.settings.target}get#o(){const e=this.#s,t=[];if(this.#u)for(const n of[].concat(this.#u)){const s=e.find((e=>e?.path===this.path));void 0!==s?t.push(s):void 0===s&&ptargets.push({path:this.path,target:n,enable:!1})}else if("string"===s(this.path)){const n=[];if(this.path===this.#g){const e={path:this.path,target:this.#e,enable:!1};t.push(e)}else{if(this.settings.compandTree){const e=this.#p,t=y(this.path,{separator:"."});for(const[s,r]of e){!0===t(s)&&n.push([s,r])}"*"===this.path.charAt(0)&&n.unshift([this.#g,this.#e])}else n.push(this.path);for(const[s,r]of n){const n=e.find((e=>e.path===s));let i,a=r;void 0!==a&&(a===n?.target?i=n:"object"==typeof a&&(i={path:s,target:a,enable:!1})),void 0!==i&&t.push(i)}}}return this.#s=t,this.#s}get#g(){return this.settings.compandTree.scopeKey}get#c(){return void 0!==this.#r||(this.#r=this.settings.methods.assign[this.settings.assign].bind(null,this)),this.#r}get#l(){return void 0!==this.#i||(this.#i=this.settings.methods.deassign[this.settings.deassign].bind(null,this)),this.#i}get#h(){return void 0!==this.#a||(this.#a=this.settings.methods.transsign[this.settings.transsign].bind(null,this)),this.#a}get#p(){if(!this.settings.compandTree)return null;const e=Object.assign(this.settings.compandTree,{values:!0});return i(this.#e,e)}emit(){const e=this.#o,t=this.transsigned,n=this.#n;t.length=0,n.length=0;for(const s of e){const{target:e}=s;try{this.#h(e,...arguments),t.push(s)}catch(e){n.push(s)}}return this}}class j extends EventTarget{static implement=function(e,t){if(!e||!t)return;const n=((e={})=>{const t={events:{},enableEvents:!1,compandTree:{accessors:[o.default],scopeKey:":scope",maxDepth:10},propertyDefinitions:{getEvents:"getEvents",addEvents:"addEvents",removeEvents:"removeEvents",enableEvents:"enableEvents",disableEvents:"disableEvents",reenableEvents:"reenableEvents",emitEvents:"emitEvents"}};for(const[n,s]of Object.entries(e))switch(n){case"propertyDefinitions":case"compandTree":t[n]=Object.assign(t[n],s);break;default:t[n]=s}return t})(t),s=[];return Object.defineProperties(e,{[n.propertyDefinitions.getEvents]:{enumerable:!1,writable:!1,value:function(){if(!arguments[0])return s;const e=[],t=[].concat(arguments[0]);for(const n of t)for(const t of s){let s;e:for(const[e,r]of Object.entries(n)){let n;if(n="listener"===e?t.settings[e]===r:t[e]===r,!1===s)break e;s=n}!0===s&&e.push(t)}return e}},[n.propertyDefinitions.addEvents]:{enumerable:!1,writable:!1,value:function(){if(!arguments.length)return e;let t=function(e,t=":scope"){if(Array.isArray(e)||void 0===e)return e;const n=[];for(const[s,r]of Object.entries(e)){const e=s.trim().split(" ");let i,a,o,c;1===e.length?(i=t,a=e[0]):e.length>1&&(i=e[0],a=e[1]),Array.isArray(r)?(o=r[0],c=r[1]):o=r;const l={type:a,path:i,listener:o,enable:!1};n.push(l)}return n}(arguments[0],n.compandTree.scopeKey),r=arguments[1]||!1;for(let i of t){const t={};for(const e of["assign","deassign","transsign","compandTree","bindListener"]){const s=n[e];void 0!==s&&(t[e]=s)}a(t,i);const o=new E(t,e);r&&(o.enable=!0),s.push(o)}return e}},[n.propertyDefinitions.removeEvents]:{enumerable:!1,writable:!1,value:function(){const t=e[n.propertyDefinitions.getEvents](arguments[0]);if(0===t.length)return e;let r=s.length-1;for(;r>-1;){const e=s[r];t.includes(e)&&(e.enable=!1,s.splice(r,1)),r--}return e}},[n.propertyDefinitions.enableEvents]:{enumerable:!1,writable:!1,value:function(){const t=e[n.propertyDefinitions.getEvents](arguments[0]);if(0===t.length)return e;for(const e of t)e.enable=!0;return e}},[n.propertyDefinitions.disableEvents]:{enumerable:!1,writable:!1,value:function(){const t=e[n.propertyDefinitions.getEvents](arguments[0]);if(0===t.length)return e;for(const e of t)e.enable=!1;return e}},[n.propertyDefinitions.reenableEvents]:{enumerable:!1,writable:!1,value:function(){const t=e[n.propertyDefinitions.getEvents](arguments[0]);for(const e of t)e.enable=!1,e.enable=!0;return e}},[n.propertyDefinitions.emitEvents]:{enumerable:!1,writable:!1,value:function(t,...s){const r=e[n.propertyDefinitions.getEvents](t);for(const e of r)e.emit(...s);return e}}}),n.events&&e[n.propertyDefinitions.addEvents](n.events),!0===n.enableEvents&&e[n.propertyDefinitions.enableEvents](),e};constructor(e={}){return super(),j.implement(this,e)}}export{j as default};
//# sourceMappingURL=core-plex.min.js.map