123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- let 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,
- }
- class Type {
- constructor(type) {
- this.type = type;
- }
- getType() {
- return Object.keys(Types)[this.type];
- }
- resolve(value) {
- console.log("Called resolve on base super Type component");
- return true;
- }
- default(value) {
- this.defaultValue = value;
- return this;
- }
- require() {
- this.isRequired = true;
- return this;
- }
- }
- class Bool extends Type {
- constructor() {
- super(Types.Boolean);
- }
- resolve(value) {
- if((value !== undefined || this.isRequired) && value !== true && value !== false)
- return false;
- return true;
- }
- }
- class Text extends Type {
- constructor() {
- super(Types.Text);
- }
- color() {
- this.isColor = true;
- return this;
- }
- resolve(value) {
- if(typeof value === "string")
- return true;
- else if ((value === undefined || value === null) && !this.isRequired)
- return true
- return true;
- }
- }
- class Integer extends Type {
- constructor() {
- super(Types.Integer);
- }
- resolve(value) {
- if (typeof value === "number" && parseInt(value) === value)
- return true;
- else if ((value === undefined || value === null) && !this.isRequired)
- return true;
- return false;
- }
- }
- class Real extends Type {
- constructor() {
- super(Types.Real);
- }
- resolve(value) {
- if (typeof value === "number" && parseFloat(value) === value)
- return true;
- else if ((value === undefined || value === null) && !this.isRequired)
- return true;
- return false;
- }
- }
- class JSObject extends Type {
- constructor() {
- super(Types.Object);
- }
- resolve(value) {
- if (typeof value === "object")
- return true;
- else if ((value === undefined || value === null) && !this.isRequired)
- return true;
- return false;
- }
- }
- export default {
- Bool,
- Real,
- Object: JSObject,
- Integer,
- Text,
- Types,
- Type
- }
- // export function Resolver (input, value) {
- // const msg = "Resolver cannot parse Input Field";
- // switch (input.type) {
- // case Types.Object:
- // input.value = value || input.value || {};
- // break;
- // case Types.Boolean:
- // if(value !== undefined && value !== null) {
- // input.value = !!value;
- // }
- // break;
- // case Types.Number:
- // try {
- // input.value = parseInt(value === undefined || value === null ? input.value : value);
- // } catch (e) {
- // Logger.warning(msg, { inputField: input });
- // }
- // break;
- // case Types.Real:
- // try {
- // input.value = parseFloat(value === undefined || value === null ? input.value : value);
- // } catch (e) {
- // Logger.warning(msg, { inputField: input });
- // }
- // break;
- // case Types.Text:
- // if(value !== undefined && value !== null) {
- // input.value = value;
- // }
- // break;
- // case Types.Array:
- // if (!Array.isArray(value)) {
- // try {
- // if(typeof value === "string") {
- // input.value = JSON.parse(value);
- // }
- // } catch (e) {
- // Logger.warning(msg, { inputField: input });
- // }
- // } else {
- // input.value = value;
- // }
- // break;
- // default:
- // input.value = value;
- // break;
- // }
- // return input.value;
- // }
- export function Resolver (Inputs, props) {
- const msg = "Resolver cannot parse Input Field";
- let structure = {};
- for(var i in Inputs) {
- let field = Inputs[i];
- let value = props[i] || field.defaultValue;
- if(field.resolve(value)){
- structure[i] = value;
- } else {
- console.log(`Field ${i} failed to resolve to ${field.getType()}`);
- }
- }
- return structure;
- }
|