-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstruct.min.js
More file actions
1 lines (1 loc) · 4.89 KB
/
Copy pathstruct.min.js
File metadata and controls
1 lines (1 loc) · 4.89 KB
1
var Struct={structs:{}};typeof module!="undefined"&&module.exports?module.exports=Struct:window.Struct=Struct,function(e){"use strict";function t(e){return Object.prototype.toString.call(e)}function n(e){return t(e)==="[object String]"}function r(e){return t(e)==="[object Number]"&&!isNaN(e)}function i(e){return t(e)==="[object Boolean]"}function s(e){return t(e)==="[object Function]"}function o(e){return t(e)==="[object Array]"}function u(e){return o(e)||e&&typeof e=="object"&&r(e.length)}function a(e){return t(e)==="[object Object]"}function f(e){return e!==null&&typeof e=="object"}function l(e){return t(e)==="[object RegExp]"}function c(e){return t(e)==="[object Date]"}function h(e){return e&&n(e.nodeName)&&u(e.childNodes)}function p(t){return t===null||t===e}function d(e,t){return Struct.typeChecker[e]?Struct.typeChecker[e](t):!1}Struct.typeChecker={string:n,number:r,"boolean":i,"function":s,array:o,arraylike:u,object:a,anyobject:f,regexp:l,date:c,domnode:h},Struct.util={isString:n,isObject:a,isObjectLike:f,isType:d,isNullOrUndefined:p}}(),function(e){"use strict";function t(t){return t===null||t===e}Struct._handlerMaker=function(n,r){var i="inspector",s=Struct.typeChecker;return{getOwnPropertyDescriptor:function(t){var r=Object.getOwnPropertyDescriptor(n,t);return r!==e&&(r.configurable=!0),r},getPropertyDescriptor:function(t){var r=Object.getPropertyDescriptor(n,t);return r!==e&&(r.configurable=!0),r},getOwnPropertyNames:function(){return Object.getOwnPropertyNames(n)},getPropertyNames:function(){return Object.getPropertyNames(n)},defineProperty:function(e,t){throw"Cannot fix this object"},"delete":function(e){if(e in r){var t=this.getOwnPropertyDescriptor(e);if(r[e].nullable===!1)throw e+" is not allowd null or undefined";return delete n[e]}throw e+" is not defined in this struct"},fix:function(){return Object.isFrozen(n)?Object.getOwnPropertyNames(n).map(function(e){return Object.getOwnPropertyDescriptor(n,e)}):e},has:function(e){return e in n},hasOwn:function(e){return Object.prototype.hasOwnProperty.call(n,e)},get:function(e,t){if(t in r||t===i)return n[t];throw t+" is not defined in this struct"},set:function(e,i,s){if(i in r){var o=this.getOwnPropertyDescriptor(i);if(o&&!o.writable)throw i+" is not writable property";if(r[i].nullable===!1&&t(s))throw i+" is not allowd null or undefined";var u=r[i].type;if(!(t(s)||Struct.isStructType(u,s)||Struct.util.isType(u,s)))throw i+" must be "+r[i].type+" type";if(r[i].cond&&!r[i].cond(s))throw"Invalid value:"+i+"="+String(s);return n[i]=s,!0}throw i+" is not defined in this struct"},enumerate:function(){var e=[];for(var t in n)e.push(t);return e},keys:function(){return Object.keys(n)}}}}(),function(e){"use strict";function o(e,n){return n=n||{},Object.defineProperty(n,t,{value:e,wriatble:!1,enumerable:!1}),n}function u(e,t){Object.keys(t).forEach(function(r){var s=t[r],o=e[r];if(i.isNullOrUndefined(o)){if(s.nullable===!1)throw r+" is not-nullable property but initial value is null";return}if(Struct.isStructType(s.type,o)||i.isType(s.type,o))return;var a=s.type.match(n);if(a){var f=a[1];u(o,Struct.structs[f]),e[r]=Struct.create(f,o);return}throw r+" must be "+t[r].type+" type. But initial value not matched"}),Object.keys(t).forEach(function(n){var r=t[n],i=e[n];if(r.cond&&!r.cond(i))throw"Invalid value:"+n+"="+String(i)}),Object.keys(e).forEach(function(e){if(!t.hasOwnProperty(e))throw"Invalid property found:"+e})}var t="__structName__",n=/^struct:(.+)/,r=typeof Proxy!="undefined",i=Struct.util;Struct.define=function(r,s){if(!i.isString(r))throw"First argument must be String type (Struct name)";if(!i.isObject(s))throw"Second argument must be Object type (Property settings)";if(this.structs[r])throw r+" is already defined";Object.keys(s).forEach(function(t){var r=s[t].type;if(!Struct.typeChecker.hasOwnProperty(r)&&!n.test(r))throw"Supported types are :"+Object.keys(Struct.typeChecker).join()+",struct:*";s[t].writable===e&&(s[t].writable=!0),i.isString(s[t].cond)&&(s[t].cond=new Function("v","return "+s[t].cond))}),s[t]={value:r,wriatble:!1,enumerable:!1},Object.freeze(s),this.structs[r]=s},Struct.ifdef=function(e){return!!this.structs.hasOwnProperty(e)},Struct.getType=function(n){if(!i.isObjectLike(n))throw"First argument must be object type";return i.isObject(n)&&n.hasOwnProperty(t)?n[t]:e},Struct.isStruct=function(e){return i.isObject(e)&&i.isString(e[t])};var s=Struct.create=function(e,t){if(!this.structs.hasOwnProperty(e))throw'Struct named "'+e+'" is not defined';var n=this.structs[e];t=t||{},u(t,n),Object.defineProperties(t,n);var i;return r?i=Proxy.create(Struct._handlerMaker(t,n)):i=Object.seal(t),i};Struct.configure=function(e){Object.keys(this.structs).length>0&&console.log("WARNING: Some structs are already defined.This configure does not applied them."),e["disable any check"]===!0&&(Struct.create=o),e["disable any check"]===!1&&(Struct.create=s)},Struct.isStructType=function(e,t){var r=e.match(n);return r&&Struct.isStruct(t)?Struct.getType(t)===r[1]:!1}}();