Types.js 3.8 KB

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