Types.js 6.1 KB

1
  1. var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.Resolver=Resolver;exports.default=void 0;var _possibleConstructorReturn2=_interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));var _getPrototypeOf2=_interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));var _inherits2=_interopRequireDefault(require("@babel/runtime/helpers/inherits"));var _classCallCheck2=_interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));var _createClass2=_interopRequireDefault(require("@babel/runtime/helpers/createClass"));var Types={Boolean:0,Text:1,Integer:2,URL:3,somethingelse:4,Link:5,Array:6,View:7,Object:8,Real:9,Float:10,Modules:11,Whatever:12,Action:13,Module:14};var Type=function(){function Type(type){(0,_classCallCheck2.default)(this,Type);this.type=type;}(0,_createClass2.default)(Type,[{key:"getType",value:function getType(){return Object.keys(Types)[this.type];}},{key:"parse",value:function parse(value){return value;}},{key:"resolve",value:function resolve(value){console.log("Called resolve on base super Type component");return true;}},{key:"default",value:function _default(value){this.defaultValue=value;return this;}},{key:"require",value:function require(){this.isRequired=true;return this;}}]);return Type;}();var Bool=function(_Type){(0,_inherits2.default)(Bool,_Type);function Bool(){(0,_classCallCheck2.default)(this,Bool);return(0,_possibleConstructorReturn2.default)(this,(0,_getPrototypeOf2.default)(Bool).call(this,Types.Boolean));}(0,_createClass2.default)(Bool,[{key:"resolve",value:function resolve(value){if((value!==undefined||this.isRequired)&&value!==true&&value!==false)return false;return true;}},{key:"parse",value:function parse(value){return value==='true'||value==="1"||value===1||value===true;}}]);return Bool;}(Type);var Action=function(_Type2){(0,_inherits2.default)(Action,_Type2);function Action(){(0,_classCallCheck2.default)(this,Action);return(0,_possibleConstructorReturn2.default)(this,(0,_getPrototypeOf2.default)(Action).call(this,Types.Action));}(0,_createClass2.default)(Action,[{key:"resolve",value:function resolve(value){if(typeof value!=="object")return false;if(typeof value.type!=="string")return false;return true;}},{key:"parse",value:function parse(value){if(!value)return{_Action:{empty:true}};var type=value.type,data=value.data;return{_Action:{initial:value},type:type,data:data};}}]);return Action;}(Type);var Text=function(_Type3){(0,_inherits2.default)(Text,_Type3);function Text(){(0,_classCallCheck2.default)(this,Text);return(0,_possibleConstructorReturn2.default)(this,(0,_getPrototypeOf2.default)(Text).call(this,Types.Text));}(0,_createClass2.default)(Text,[{key:"color",value:function color(){this.isColor=true;return this;}},{key:"resolve",value:function resolve(value){if(typeof value==="string")return true;else if((value===undefined||value===null)&&!this.isRequired)return true;return true;}}]);return Text;}(Type);var Integer=function(_Type4){(0,_inherits2.default)(Integer,_Type4);function Integer(){(0,_classCallCheck2.default)(this,Integer);return(0,_possibleConstructorReturn2.default)(this,(0,_getPrototypeOf2.default)(Integer).call(this,Types.Integer));}(0,_createClass2.default)(Integer,[{key:"parse",value:function parse(value){try{return parseInt(value);}catch(e){return this.defaultValue||0;}}},{key:"resolve",value:function resolve(value){if(typeof value==="number"&&parseInt(value)===value)return true;else if((value===undefined||value===null)&&!this.isRequired)return true;return false;}}]);return Integer;}(Type);var Real=function(_Type5){(0,_inherits2.default)(Real,_Type5);function Real(){(0,_classCallCheck2.default)(this,Real);return(0,_possibleConstructorReturn2.default)(this,(0,_getPrototypeOf2.default)(Real).call(this,Types.Real));}(0,_createClass2.default)(Real,[{key:"parse",value:function parse(value){try{return parseFloat(value);}catch(e){return this.defaultValue||0;}}},{key:"resolve",value:function resolve(value){if(typeof value==="number"&&parseFloat(value)===value)return true;else if((value===undefined||value===null)&&!this.isRequired)return true;return false;}}]);return Real;}(Type);var JSObject=function(_Type6){(0,_inherits2.default)(JSObject,_Type6);function JSObject(){(0,_classCallCheck2.default)(this,JSObject);return(0,_possibleConstructorReturn2.default)(this,(0,_getPrototypeOf2.default)(JSObject).call(this,Types.Object));}(0,_createClass2.default)(JSObject,[{key:"resolve",value:function resolve(value){if(typeof value==="object")return true;else if((value===undefined||value===null)&&!this.isRequired)return true;return false;}}]);return JSObject;}(Type);var JSArray=function(_Type7){(0,_inherits2.default)(JSArray,_Type7);function JSArray(){(0,_classCallCheck2.default)(this,JSArray);return(0,_possibleConstructorReturn2.default)(this,(0,_getPrototypeOf2.default)(JSArray).call(this,Types.Array));}(0,_createClass2.default)(JSArray,[{key:"resolve",value:function resolve(value){if(Array.isArray(value))return true;else if((value===undefined||value===null)&&!this.isRequired)return true;return false;}}]);return JSArray;}(Type);var Module=function(_Type8){(0,_inherits2.default)(Module,_Type8);function Module(){(0,_classCallCheck2.default)(this,Module);return(0,_possibleConstructorReturn2.default)(this,(0,_getPrototypeOf2.default)(Module).call(this,Types.Module));}(0,_createClass2.default)(Module,[{key:"parse",value:function parse(data){var _ref=data||{},value=_ref.value,namespace=_ref.namespace;return{value:value,namespace:namespace};}},{key:"resolve",value:function resolve(value){if(typeof value==="object"&&value.value)return true;else if((value===undefined||value===null)&&!this.isRequired)return true;return false;}}]);return Module;}(Type);var _default2={Action:Action,Bool:Bool,Real:Real,Object:JSObject,Integer:Integer,Text:Text,Types:Types,Type:Type,Module:Module,Array:JSArray};exports.default=_default2;function Resolver(Inputs,props){var msg="Resolver cannot parse Input Field";var structure={};for(var i in Inputs){var field=Inputs[i];var parseVal=props[i]===undefined?field.defaultValue:props[i];var value=field.parse(parseVal);if(field.resolve(value)){structure[i]=value;}else{}}return structure;}