3
0

Types.js 5.1 KB

1
  1. "use strict";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");Object.defineProperty(exports,"__esModule",{value:true});exports.Resolver=Resolver;exports["default"]=void 0;var _typeof2=_interopRequireDefault(require("@babel/runtime/helpers/typeof"));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};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 Text=function(_Type2){(0,_inherits2["default"])(Text,_Type2);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(_Type3){(0,_inherits2["default"])(Integer,_Type3);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(_Type4){(0,_inherits2["default"])(Real,_Type4);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(_Type5){(0,_inherits2["default"])(JSObject,_Type5);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((0,_typeof2["default"])(value)==="object")return true;else if((value===undefined||value===null)&&!this.isRequired)return true;return false;}}]);return JSObject;}(Type);var JSArray=function(_Type6){(0,_inherits2["default"])(JSArray,_Type6);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 _default2={Bool:Bool,Real:Real,Object:JSObject,Integer:Integer,Text:Text,Types:Types,Type:Type,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 value=field.parse(props[i]||field.defaultValue);if(field.resolve(value)){structure[i]=value;}else{}}return structure;}