"Fossies" - the Fresh Open Source Software Archive

Member "node-v12.18.4-win-x64/node_modules/npm/node_modules/ajv/lib/dot/coerce.def" (14 Feb 2020, 2282 Bytes) of package /windows/www/node-v12.18.4-win-x64.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Modula2 source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    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 #}}