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