Types.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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(Template){
  146. super(Types.Array);
  147. this.Template = Template;
  148. }
  149. resolve(value){
  150. if(Array.isArray(value)) {
  151. for(var i=0;i<value.length;i++) {
  152. if(!this.Template.resolve(value[i])) {
  153. return false;
  154. }
  155. }
  156. return true;
  157. } else if ((value === undefined || value === null) && !this.isRequired)
  158. return true;
  159. return false;
  160. }
  161. }
  162. class Module extends Type{
  163. constructor(){
  164. super(Types.Module)
  165. }
  166. parse(data) {
  167. let {
  168. value,
  169. namespace
  170. } = data || {};
  171. return {
  172. value,
  173. namespace
  174. };
  175. }
  176. resolve(value){
  177. if(typeof value === "object" && value.value)
  178. return true;
  179. else if ((value === undefined || value === null) && !this.isRequired)
  180. return true;
  181. return false;
  182. }
  183. }
  184. export default {
  185. Action,
  186. Bool,
  187. Real,
  188. Object: JSObject,
  189. Integer,
  190. Text,
  191. Types,
  192. Type,
  193. Module,
  194. Array: JSArray
  195. }
  196. // export function Resolver (input, value) {
  197. // const msg = "Resolver cannot parse Input Field";
  198. // switch (input.type) {
  199. // case Types.Object:
  200. // input.value = value || input.value || {};
  201. // break;
  202. // case Types.Boolean:
  203. // if(value !== undefined && value !== null) {
  204. // input.value = !!value;
  205. // }
  206. // break;
  207. // case Types.Number:
  208. // try {
  209. // input.value = parseInt(value === undefined || value === null ? input.value : value);
  210. // } catch (e) {
  211. // Logger.warning(msg, { inputField: input });
  212. // }
  213. // break;
  214. // case Types.Real:
  215. // try {
  216. // input.value = parseFloat(value === undefined || value === null ? input.value : value);
  217. // } catch (e) {
  218. // Logger.warning(msg, { inputField: input });
  219. // }
  220. // break;
  221. // case Types.Text:
  222. // if(value !== undefined && value !== null) {
  223. // input.value = value;
  224. // }
  225. // break;
  226. // case Types.Array:
  227. // if (!Array.isArray(value)) {
  228. // try {
  229. // if(typeof value === "string") {
  230. // input.value = JSON.parse(value);
  231. // }
  232. // } catch (e) {
  233. // Logger.warning(msg, { inputField: input });
  234. // }
  235. // } else {
  236. // input.value = value;
  237. // }
  238. // break;
  239. // default:
  240. // input.value = value;
  241. // break;
  242. // }
  243. // return input.value;
  244. // }
  245. export function Resolver (Inputs, props) {
  246. const msg = "Resolver cannot parse Input Field";
  247. let structure = {};
  248. for(var i in Inputs) {
  249. let field = Inputs[i];
  250. let parseVal = (props[i] === undefined) ? field.defaultValue : props[i];
  251. let value = field.parse(parseVal);
  252. if(field.resolve(value)){
  253. structure[i] = value;
  254. // console.log(value);
  255. } else {
  256. // console.log(`Field ${i} failed to resolve to ${field.getType()}`);
  257. }
  258. }
  259. return structure;
  260. }