Types.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. parse(value) {
  24. return value;
  25. }
  26. resolve(value) {
  27. console.log("Called resolve on base super Type component");
  28. return true;
  29. }
  30. default(value) {
  31. this.defaultValue = value;
  32. return this;
  33. }
  34. require() {
  35. this.isRequired = true;
  36. return this;
  37. }
  38. }
  39. class Bool extends Type {
  40. constructor() {
  41. super(Types.Boolean);
  42. }
  43. resolve(value) {
  44. if((value !== undefined || this.isRequired) && value !== true && value !== false)
  45. return false;
  46. return true;
  47. }
  48. parse(value) {
  49. return value === 'true' || value === "1" || value === 1 || value === true;
  50. }
  51. }
  52. class Text extends Type {
  53. constructor() {
  54. super(Types.Text);
  55. }
  56. color() {
  57. this.isColor = true;
  58. return this;
  59. }
  60. resolve(value) {
  61. if(typeof value === "string")
  62. return true;
  63. else if ((value === undefined || value === null) && !this.isRequired)
  64. return true
  65. return true;
  66. }
  67. }
  68. class Integer extends Type {
  69. constructor() {
  70. super(Types.Integer);
  71. }
  72. parse(value) {
  73. try{
  74. return parseInt(value);
  75. }catch(e) {
  76. return this.defaultValue || 0;
  77. }
  78. }
  79. resolve(value) {
  80. if (typeof value === "number" && parseInt(value) === value)
  81. return true;
  82. else if ((value === undefined || value === null) && !this.isRequired)
  83. return true;
  84. return false;
  85. }
  86. }
  87. class Real extends Type {
  88. constructor() {
  89. super(Types.Real);
  90. }
  91. parse(value) {
  92. try{
  93. return parseFloat(value);
  94. }catch(e) {
  95. return this.defaultValue || 0;
  96. }
  97. }
  98. resolve(value) {
  99. if (typeof value === "number" && parseFloat(value) === value)
  100. return true;
  101. else if ((value === undefined || value === null) && !this.isRequired)
  102. return true;
  103. return false;
  104. }
  105. }
  106. class JSObject extends Type {
  107. constructor() {
  108. super(Types.Object);
  109. }
  110. resolve(value) {
  111. if (typeof value === "object")
  112. return true;
  113. else if ((value === undefined || value === null) && !this.isRequired)
  114. return true;
  115. return false;
  116. }
  117. }
  118. class JSArray extends Type{
  119. constructor(){
  120. super(Types.Array)
  121. }
  122. resolve(value){
  123. if(Array.isArray(value))
  124. return true;
  125. else if ((value === undefined || value === null) && !this.isRequired)
  126. return true;
  127. return false;
  128. }
  129. }
  130. export default {
  131. Bool,
  132. Real,
  133. Object: JSObject,
  134. Integer,
  135. Text,
  136. Types,
  137. Type,
  138. Array: JSArray
  139. }
  140. // export function Resolver (input, value) {
  141. // const msg = "Resolver cannot parse Input Field";
  142. // switch (input.type) {
  143. // case Types.Object:
  144. // input.value = value || input.value || {};
  145. // break;
  146. // case Types.Boolean:
  147. // if(value !== undefined && value !== null) {
  148. // input.value = !!value;
  149. // }
  150. // break;
  151. // case Types.Number:
  152. // try {
  153. // input.value = parseInt(value === undefined || value === null ? input.value : value);
  154. // } catch (e) {
  155. // Logger.warning(msg, { inputField: input });
  156. // }
  157. // break;
  158. // case Types.Real:
  159. // try {
  160. // input.value = parseFloat(value === undefined || value === null ? input.value : value);
  161. // } catch (e) {
  162. // Logger.warning(msg, { inputField: input });
  163. // }
  164. // break;
  165. // case Types.Text:
  166. // if(value !== undefined && value !== null) {
  167. // input.value = value;
  168. // }
  169. // break;
  170. // case Types.Array:
  171. // if (!Array.isArray(value)) {
  172. // try {
  173. // if(typeof value === "string") {
  174. // input.value = JSON.parse(value);
  175. // }
  176. // } catch (e) {
  177. // Logger.warning(msg, { inputField: input });
  178. // }
  179. // } else {
  180. // input.value = value;
  181. // }
  182. // break;
  183. // default:
  184. // input.value = value;
  185. // break;
  186. // }
  187. // return input.value;
  188. // }
  189. export function Resolver (Inputs, props) {
  190. const msg = "Resolver cannot parse Input Field";
  191. let structure = {};
  192. for(var i in Inputs) {
  193. let field = Inputs[i];
  194. let value = field.parse(props[i] || field.defaultValue);
  195. if(field.resolve(value)){
  196. structure[i] = value;
  197. // console.log(value);
  198. } else {
  199. // console.log(`Field ${i} failed to resolve to ${field.getType()}`);
  200. }
  201. }
  202. return structure;
  203. }