twitst4tz

twitter statistics web application
Log | Files | Refs | README | LICENSE

uniqueItems.jst (1673B)


      1 {{# def.definitions }}
      2 {{# def.errors }}
      3 {{# def.setupKeyword }}
      4 {{# def.$data }}
      5 
      6 
      7 {{? ($schema || $isData) && it.opts.uniqueItems !== false }}
      8   {{? $isData }}
      9     var {{=$valid}};
     10     if ({{=$schemaValue}} === false || {{=$schemaValue}} === undefined)
     11       {{=$valid}} = true;
     12     else if (typeof {{=$schemaValue}} != 'boolean')
     13       {{=$valid}} = false;
     14     else {
     15   {{?}}
     16 
     17   var i = {{=$data}}.length
     18     , {{=$valid}} = true
     19     , j;
     20   if (i > 1) {
     21     {{
     22       var $itemType = it.schema.items && it.schema.items.type
     23         , $typeIsArray = Array.isArray($itemType);
     24     }}
     25     {{? !$itemType || $itemType == 'object' || $itemType == 'array' ||
     26         ($typeIsArray && ($itemType.indexOf('object') >= 0 || $itemType.indexOf('array') >= 0)) }}
     27       outer:
     28       for (;i--;) {
     29         for (j = i; j--;) {
     30           if (equal({{=$data}}[i], {{=$data}}[j])) {
     31             {{=$valid}} = false;
     32             break outer;
     33           }
     34         }
     35       }
     36     {{??}}
     37       var itemIndices = {}, item;
     38       for (;i--;) {
     39         var item = {{=$data}}[i];
     40         {{ var $method = 'checkDataType' + ($typeIsArray ? 's' : ''); }}
     41         if ({{= it.util[$method]($itemType, 'item', true) }}) continue;
     42         {{? $typeIsArray}}
     43           if (typeof item == 'string') item = '"' + item;
     44         {{?}}
     45         if (typeof itemIndices[item] == 'number') {
     46           {{=$valid}} = false;
     47           j = itemIndices[item];
     48           break;
     49         }
     50         itemIndices[item] = i;
     51       }
     52     {{?}}
     53   }
     54 
     55   {{? $isData }}  }  {{?}}
     56 
     57   if (!{{=$valid}}) {
     58     {{# def.error:'uniqueItems' }}
     59   } {{? $breakOnError }} else { {{?}}
     60 {{??}}
     61   {{? $breakOnError }} if (true) { {{?}}
     62 {{?}}