Types.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. let Types = {
  2. Boolean: 0,
  3. Text: 1,
  4. Integer: 2,
  5. URL: 3,
  6. somethingelse: 4,
  7. Link: 5,
  8. Array: 6,
  9. View: 7,
  10. Object: 8,
  11. Real: 9,
  12. Float: 10,
  13. Modules: 11,
  14. Whatever: 12,
  15. }
  16. class Type {
  17. constructor(type) {
  18. this.type = type;
  19. }
  20. getType() {
  21. return Object.keys(Types)[this.type];
  22. }
  23. resolve(value) {
  24. console.log("Called resolve on base super Type component");
  25. return true;
  26. }
  27. default(value) {
  28. this.defaultValue = value;
  29. return this;
  30. }
  31. require() {
  32. this.isRequired = true;
  33. return this;
  34. }
  35. }
  36. class Bool extends Type {
  37. constructor() {
  38. super(Types.Boolean);
  39. }
  40. resolve(value) {
  41. if((value !== undefined || this.isRequired) && value !== true && value !== false)
  42. return false;
  43. return true;
  44. }
  45. }
  46. class Text extends Type {
  47. constructor() {
  48. super(Types.Text);
  49. }
  50. color() {
  51. this.isColor = true;
  52. return this;
  53. }
  54. resolve(value) {
  55. if(typeof value === "string")
  56. return true;
  57. else if ((value === undefined || value === null) && !this.isRequired)
  58. return true
  59. return true;
  60. }
  61. }
  62. class Integer extends Type {
  63. constructor() {
  64. super(Types.Integer);
  65. }
  66. resolve(value) {
  67. if (typeof value === "number" && parseInt(value) === value)
  68. return true;
  69. else if ((value === undefined || value === null) && !this.isRequired)
  70. return true;
  71. return false;
  72. }
  73. }
  74. class Real extends Type {
  75. constructor() {
  76. super(Types.Real);
  77. }
  78. resolve(value) {
  79. if (typeof value === "number" && parseFloat(value) === value)
  80. return true;
  81. else if ((value === undefined || value === null) && !this.isRequired)
  82. return true;
  83. return false;
  84. }
  85. }
  86. export default {
  87. Bool,
  88. Real,
  89. Integer,
  90. Text,
  91. Types,
  92. Type
  93. }
  94. // export function Resolver (input, value) {
  95. // const msg = "Resolver cannot parse Input Field";
  96. // switch (input.type) {
  97. // case Types.Object:
  98. // input.value = value || input.value || {};
  99. // break;
  100. // case Types.Boolean:
  101. // if(value !== undefined && value !== null) {
  102. // input.value = !!value;
  103. // }
  104. // break;
  105. // case Types.Number:
  106. // try {
  107. // input.value = parseInt(value === undefined || value === null ? input.value : value);
  108. // } catch (e) {
  109. // Logger.warning(msg, { inputField: input });
  110. // }
  111. // break;
  112. // case Types.Real:
  113. // try {
  114. // input.value = parseFloat(value === undefined || value === null ? input.value : value);
  115. // } catch (e) {
  116. // Logger.warning(msg, { inputField: input });
  117. // }
  118. // break;
  119. // case Types.Text:
  120. // if(value !== undefined && value !== null) {
  121. // input.value = value;
  122. // }
  123. // break;
  124. // case Types.Array:
  125. // if (!Array.isArray(value)) {
  126. // try {
  127. // if(typeof value === "string") {
  128. // input.value = JSON.parse(value);
  129. // }
  130. // } catch (e) {
  131. // Logger.warning(msg, { inputField: input });
  132. // }
  133. // } else {
  134. // input.value = value;
  135. // }
  136. // break;
  137. // default:
  138. // input.value = value;
  139. // break;
  140. // }
  141. // return input.value;
  142. // }
  143. export function Resolver (Inputs, props) {
  144. const msg = "Resolver cannot parse Input Field";
  145. let structure = {};
  146. for(var i in Inputs) {
  147. let field = Inputs[i];
  148. let value = props[i] || field.defaultValue;
  149. if(field.resolve(value)){
  150. structure[i] = value;
  151. } else {
  152. console.log(`Field ${i} failed to resolve to ${field.getType()}`);
  153. }
  154. }
  155. return structure;
  156. }