Types.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. class JSObject extends Type {
  87. constructor() {
  88. super(Types.Object);
  89. }
  90. resolve(value) {
  91. if (typeof value === "object")
  92. return true;
  93. else if ((value === undefined || value === null) && !this.isRequired)
  94. return true;
  95. return false;
  96. }
  97. }
  98. export default {
  99. Bool,
  100. Real,
  101. Object: JSObject,
  102. Integer,
  103. Text,
  104. Types,
  105. Type
  106. }
  107. // export function Resolver (input, value) {
  108. // const msg = "Resolver cannot parse Input Field";
  109. // switch (input.type) {
  110. // case Types.Object:
  111. // input.value = value || input.value || {};
  112. // break;
  113. // case Types.Boolean:
  114. // if(value !== undefined && value !== null) {
  115. // input.value = !!value;
  116. // }
  117. // break;
  118. // case Types.Number:
  119. // try {
  120. // input.value = parseInt(value === undefined || value === null ? input.value : value);
  121. // } catch (e) {
  122. // Logger.warning(msg, { inputField: input });
  123. // }
  124. // break;
  125. // case Types.Real:
  126. // try {
  127. // input.value = parseFloat(value === undefined || value === null ? input.value : value);
  128. // } catch (e) {
  129. // Logger.warning(msg, { inputField: input });
  130. // }
  131. // break;
  132. // case Types.Text:
  133. // if(value !== undefined && value !== null) {
  134. // input.value = value;
  135. // }
  136. // break;
  137. // case Types.Array:
  138. // if (!Array.isArray(value)) {
  139. // try {
  140. // if(typeof value === "string") {
  141. // input.value = JSON.parse(value);
  142. // }
  143. // } catch (e) {
  144. // Logger.warning(msg, { inputField: input });
  145. // }
  146. // } else {
  147. // input.value = value;
  148. // }
  149. // break;
  150. // default:
  151. // input.value = value;
  152. // break;
  153. // }
  154. // return input.value;
  155. // }
  156. export function Resolver (Inputs, props) {
  157. const msg = "Resolver cannot parse Input Field";
  158. let structure = {};
  159. for(var i in Inputs) {
  160. let field = Inputs[i];
  161. let value = props[i] || field.defaultValue;
  162. if(field.resolve(value)){
  163. structure[i] = value;
  164. } else {
  165. console.log(`Field ${i} failed to resolve to ${field.getType()}`);
  166. }
  167. }
  168. return structure;
  169. }