1// Copyright 2014 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5/** 6 * Get the |key| attribute in the given |dict| and verify that it is an 7 * array value. 8 * 9 * If the attribute is not an array, then an exception will be thrown unless 10 * a default value is specified in |opt_default|. 11 * 12 * @param {Object.<string,*>} dict The dictionary containing the |key| 13 * @param {string} key The key to typecheck in the |dict|. 14 * @param {Array=} opt_default The value to return if the key is not a bool. 15 * @return {Array} The |key| attribute value as an object. 16 */ 17function getArrayAttr(dict, key, opt_default) { 18 var value = /** @type {Array} */ (dict[key]); 19 if (!(value instanceof Array)) { 20 if (opt_default === undefined) { 21 throw 'Invalid data type for ' + key + 22 ' (expected: array, actual: ' + typeof value + ')'; 23 } else { 24 return opt_default; 25 } 26 } 27 return value; 28} 29 30/** 31 * Get the |key| attribute in the given |dict| and verify that it is a 32 * boolean value. 33 * 34 * If the attribute is not a boolean, then an exception will be thrown unless 35 * a default value is specified in |opt_default|. 36 * 37 * @param {Object.<string,*>} dict The dictionary containing the |key| 38 * @param {string} key The key to typecheck in the |dict|. 39 * @param {boolean=} opt_default The value to return if the key is not a bool. 40 * @return {boolean} The |key| attribute value as a boolean. 41 */ 42function getBooleanAttr(dict, key, opt_default) { 43 var value = /** @type {boolean} */ (dict[key]); 44 if (typeof value != 'boolean') { 45 if (opt_default === undefined) { 46 throw 'Invalid data type for ' + key + 47 ' (expected: boolean, actual: ' + typeof value + ')'; 48 } else { 49 return opt_default; 50 } 51 } 52 return value; 53} 54 55/** 56 * Get the |key| attribute in the given |dict| and verify that it is a 57 * number value. 58 * 59 * If the attribute is not a number, then an exception will be thrown unless 60 * a default value is specified in |opt_default|. 61 * 62 * @param {Object.<string,*>} dict The dictionary containing the |key| 63 * @param {string} key The key to typecheck in the |dict|. 64 * @param {number=} opt_default The value to return if the key is not a number. 65 * @return {number} The |key| attribute value as a number. 66 */ 67function getNumberAttr(dict, key, opt_default) { 68 var value = /** @type {number} */(dict[key]); 69 if (typeof value != 'number') { 70 if (opt_default === undefined) { 71 throw 'Invalid data type for ' + key + 72 ' (expected: number, actual: ' + typeof value + ')'; 73 } else { 74 return opt_default; 75 } 76 } 77 return value; 78} 79 80/** 81 * Get the |key| attribute in the given |dict| and verify that it is an 82 * object value. 83 * 84 * If the attribute is not an object, then an exception will be thrown unless 85 * a default value is specified in |opt_default|. 86 * 87 * @param {Object.<string,*>} dict The dictionary containing the |key| 88 * @param {string} key The key to typecheck in the |dict|. 89 * @param {Object=} opt_default The value to return if the key is not a bool. 90 * @return {Object} The |key| attribute value as an object. 91 */ 92function getObjectAttr(dict, key, opt_default) { 93 var value = /** @type {Object} */ (dict[key]); 94 if (typeof value != 'object') { 95 if (opt_default === undefined) { 96 throw 'Invalid data type for ' + key + 97 ' (expected: object, actual: ' + typeof value + ')'; 98 } else { 99 return opt_default; 100 } 101 } 102 return value; 103} 104 105/** 106 * Get the |key| attribute in the given |dict| and verify that it is a 107 * string value. 108 * 109 * If the attribute is not a string, then an exception will be thrown unless 110 * a default value is specified in |opt_default|. 111 * 112 * @param {Object.<string,*>} dict The dictionary containing the |key| 113 * @param {string} key The key to typecheck in the |dict|. 114 * @param {string=} opt_default The value to return if the key is not a string. 115 * @return {string} The |key| attribute value as a string. 116 */ 117function getStringAttr(dict, key, opt_default) { 118 var value = /** @type {string} */ (dict[key]); 119 if (typeof value != 'string') { 120 if (opt_default === undefined) { 121 throw 'Invalid data type for ' + key + 122 ' (expected: string, actual: ' + typeof value + ')'; 123 } else { 124 return opt_default; 125 } 126 } 127 return value; 128} 129 130/** 131 * Return a JSON object parsed from a string. 132 * 133 * If the string cannot be parsed, or does not result in an object, then an 134 * exception will be thrown. 135 * 136 * @param {string} jsonString The JSON string to parse. 137 * @return {Object} The JSON object created from the |jsonString|. 138 */ 139function getJsonObjectFromString(jsonString) { 140 var value = /** @type {Object} */ JSON.parse(jsonString); 141 if (typeof value != 'object') { 142 throw 'Invalid data type (expected: Object, actual: ' + typeof value + ')'; 143 } 144 return value; 145} 146