BlueGit

blue-star danique-bot

Files

danique-bot / node_modules / ajv / lib / dot / coerce.def

coerce.def

Import template
timfa Tim · pushed by timfa · 4 years ago
c788ad20 History
coerce.def 2.2 KiB Download Raw
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
11 var {{=$coerced}} = undefined;
12
13 {{ var $bracesCoercion = ''; }}
14 {{~ $coerceToTypes:$type:$i }}
15 {{? $i }}
16 if ({{=$coerced}} === undefined) {
17 {{ $bracesCoercion += '}'; }}
18 {{?}}
19
20 {{? it.opts.coerceTypes == 'array' && $type != 'array' }}
21 if ({{=$dataType}} == 'array' && {{=$data}}.length == 1) {
22 {{=$coerced}} = {{=$data}} = {{=$data}}[0];
23 {{=$dataType}} = typeof {{=$data}};
24 /*if ({{=$dataType}} == 'object' && Array.isArray({{=$data}})) {{=$dataType}} = 'array';*/
25 }
26 {{?}}
27
28 {{? $type == 'string' }}
29 if ({{=$dataType}} == 'number' || {{=$dataType}} == 'boolean')
30 {{=$coerced}} = '' + {{=$data}};
31 else if ({{=$data}} === null) {{=$coerced}} = '';
32 {{?? $type == 'number' || $type == 'integer' }}
33 if ({{=$dataType}} == 'boolean' || {{=$data}} === null
34 || ({{=$dataType}} == 'string' && {{=$data}} && {{=$data}} == +{{=$data}}
35 {{? $type == 'integer' }} && !({{=$data}} % 1){{?}}))
36 {{=$coerced}} = +{{=$data}};
37 {{?? $type == 'boolean' }}
38 if ({{=$data}} === 'false' || {{=$data}} === 0 || {{=$data}} === null)
39 {{=$coerced}} = false;
40 else if ({{=$data}} === 'true' || {{=$data}} === 1)
41 {{=$coerced}} = true;
42 {{?? $type == 'null' }}
43 if ({{=$data}} === '' || {{=$data}} === 0 || {{=$data}} === false)
44 {{=$coerced}} = null;
45 {{?? it.opts.coerceTypes == 'array' && $type == 'array' }}
46 if ({{=$dataType}} == 'string' || {{=$dataType}} == 'number' || {{=$dataType}} == 'boolean' || {{=$data}} == null)
47 {{=$coerced}} = [{{=$data}}];
48 {{?}}
49 {{~}}
50
51 {{= $bracesCoercion }}
52
53 if ({{=$coerced}} === undefined) {
54 {{# def.error:'type' }}
55 } else {
56 {{# def.setParentData }}
57 {{=$data}} = {{=$coerced}};
58 {{? !$dataLvl }}if ({{=$parentData}} !== undefined){{?}}
59 {{=$parentData}}[{{=$parentDataProperty}}] = {{=$coerced}};
60 }
61#}}