Types.js 5.0 KB

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