tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

dictionary-helper.js (2715B)


      1 'use strict';
      2 
      3 // Helper assertion functions to validate dictionary fields
      4 // on dictionary objects returned from APIs
      5 
      6 function assert_unsigned_int_field(object, field) {
      7  const num = object[field];
      8  assert_true(Number.isInteger(num) && (num >= 0),
      9    `Expect dictionary.${field} to be unsigned integer`);
     10 }
     11 
     12 function assert_int_field(object, field) {
     13  const num = object[field];
     14  assert_true(Number.isInteger(num),
     15    `Expect dictionary.${field} to be integer`);
     16 }
     17 
     18 function assert_string_field(object, field) {
     19  const str = object[field];
     20  assert_equals(typeof str, 'string',
     21    `Expect dictionary.${field} to be string`);
     22 }
     23 
     24 function assert_number_field(object, field) {
     25  const num = object[field];
     26  assert_equals(typeof num, 'number',
     27    `Expect dictionary.${field} to be number`);
     28 }
     29 
     30 function assert_boolean_field(object, field) {
     31  const bool = object[field];
     32  assert_equals(typeof bool, 'boolean',
     33    `Expect dictionary.${field} to be boolean`);
     34 }
     35 
     36 function assert_array_field(object, field) {
     37  assert_true(Array.isArray(object[field]),
     38    `Expect dictionary.${field} to be array`);
     39 }
     40 
     41 function assert_dict_field(object, field) {
     42  assert_equals(typeof object[field], 'object',
     43    `Expect dictionary.${field} to be plain object`);
     44 
     45  assert_not_equals(object[field], null,
     46    `Expect dictionary.${field} to not be null`);
     47 }
     48 
     49 function assert_enum_field(object, field, validValues) {
     50  assert_string_field(object, field);
     51  assert_true(validValues.includes(object[field]),
     52    `Expect dictionary.${field} to have one of the valid enum values: ${validValues}`);
     53 }
     54 
     55 function assert_optional_unsigned_int_field(object, field) {
     56  if(object[field] !== undefined) {
     57    assert_unsigned_int_field(object, field);
     58  }
     59 }
     60 
     61 function assert_optional_int_field(object, field) {
     62  if(object[field] !== undefined) {
     63    assert_int_field(object, field);
     64  }
     65 }
     66 
     67 function assert_optional_string_field(object, field) {
     68  if(object[field] !== undefined) {
     69    assert_string_field(object, field);
     70  }
     71 }
     72 
     73 function assert_optional_number_field(object, field) {
     74  if(object[field] !== undefined) {
     75    assert_number_field(object, field);
     76  }
     77 }
     78 
     79 function assert_optional_boolean_field(object, field) {
     80  if(object[field] !== undefined) {
     81    assert_boolean_field(object, field);
     82  }
     83 }
     84 
     85 function assert_optional_array_field(object, field) {
     86  if(object[field] !== undefined) {
     87    assert_array_field(object, field);
     88  }
     89 }
     90 
     91 function assert_optional_dict_field(object, field) {
     92  if(object[field] !== undefined) {
     93    assert_dict_field(object, field);
     94  }
     95 }
     96 
     97 function assert_optional_enum_field(object, field, validValues) {
     98  if(object[field] !== undefined) {
     99    assert_enum_field(object, field, validValues);
    100  }
    101 }