Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

coerce.def 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. {{## def.coerceType:
  2. {{
  3. var $dataType = 'dataType' + $lvl
  4. , $coerced = 'coerced' + $lvl;
  5. }}
  6. var {{=$dataType}} = typeof {{=$data}};
  7. {{? it.opts.coerceTypes == 'array'}}
  8. if ({{=$dataType}} == 'object' && Array.isArray({{=$data}})) {{=$dataType}} = 'array';
  9. {{?}}
  10. var {{=$coerced}} = undefined;
  11. {{ var $bracesCoercion = ''; }}
  12. {{~ $coerceToTypes:$type:$i }}
  13. {{? $i }}
  14. if ({{=$coerced}} === undefined) {
  15. {{ $bracesCoercion += '}'; }}
  16. {{?}}
  17. {{? it.opts.coerceTypes == 'array' && $type != 'array' }}
  18. if ({{=$dataType}} == 'array' && {{=$data}}.length == 1) {
  19. {{=$coerced}} = {{=$data}} = {{=$data}}[0];
  20. {{=$dataType}} = typeof {{=$data}};
  21. /*if ({{=$dataType}} == 'object' && Array.isArray({{=$data}})) {{=$dataType}} = 'array';*/
  22. }
  23. {{?}}
  24. {{? $type == 'string' }}
  25. if ({{=$dataType}} == 'number' || {{=$dataType}} == 'boolean')
  26. {{=$coerced}} = '' + {{=$data}};
  27. else if ({{=$data}} === null) {{=$coerced}} = '';
  28. {{?? $type == 'number' || $type == 'integer' }}
  29. if ({{=$dataType}} == 'boolean' || {{=$data}} === null
  30. || ({{=$dataType}} == 'string' && {{=$data}} && {{=$data}} == +{{=$data}}
  31. {{? $type == 'integer' }} && !({{=$data}} % 1){{?}}))
  32. {{=$coerced}} = +{{=$data}};
  33. {{?? $type == 'boolean' }}
  34. if ({{=$data}} === 'false' || {{=$data}} === 0 || {{=$data}} === null)
  35. {{=$coerced}} = false;
  36. else if ({{=$data}} === 'true' || {{=$data}} === 1)
  37. {{=$coerced}} = true;
  38. {{?? $type == 'null' }}
  39. if ({{=$data}} === '' || {{=$data}} === 0 || {{=$data}} === false)
  40. {{=$coerced}} = null;
  41. {{?? it.opts.coerceTypes == 'array' && $type == 'array' }}
  42. if ({{=$dataType}} == 'string' || {{=$dataType}} == 'number' || {{=$dataType}} == 'boolean' || {{=$data}} == null)
  43. {{=$coerced}} = [{{=$data}}];
  44. {{?}}
  45. {{~}}
  46. {{= $bracesCoercion }}
  47. if ({{=$coerced}} === undefined) {
  48. {{# def.error:'type' }}
  49. } else {
  50. {{# def.setParentData }}
  51. {{=$data}} = {{=$coerced}};
  52. {{? !$dataLvl }}if ({{=$parentData}} !== undefined){{?}}
  53. {{=$parentData}}[{{=$parentDataProperty}}] = {{=$coerced}};
  54. }
  55. #}}