Types.js 4.6 KB

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