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.
6 * Get the |key| attribute in the given |dict| and verify that it is an
9 * If the attribute is not an array, then an exception will be thrown unless
10 * a default value is specified in |opt_default|.
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.
17 function 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 + ')';
31 * Get the |key| attribute in the given |dict| and verify that it is a
34 * If the attribute is not a boolean, then an exception will be thrown unless
35 * a default value is specified in |opt_default|.
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.
42 function getBooleanAttr(dict, key, opt_default) {
43 var value = /** @type {boolean} */ (dict[key]);
44 if (value == 'true' || value == 'false') {
45 return (value == 'true');
47 if (typeof value !== 'boolean') {
48 if (opt_default === undefined) {
49 throw 'Invalid data type for ' + key +
50 ' (expected: boolean, actual: ' + typeof value + ')';
59 * Get the |key| attribute in the given |dict| and verify that it is a
62 * If the attribute is not a number, then an exception will be thrown unless
63 * a default value is specified in |opt_default|.
65 * @param {Object<string,*>} dict The dictionary containing the |key|
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.
68 * @return {number} The |key| attribute value as a number.
70 function getNumberAttr(dict, key, opt_default) {
71 var value = /** @type {number} */(dict[key]);
72 if (typeof value != 'number') {
73 if (opt_default === undefined) {
74 throw 'Invalid data type for ' + key +
75 ' (expected: number, actual: ' + typeof value + ')';
84 * Get the |key| attribute in the given |dict| and verify that it is an
87 * If the attribute is not an object, then an exception will be thrown unless
88 * a default value is specified in |opt_default|.
90 * @param {Object<string,*>} dict The dictionary containing the |key|
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.
93 * @return {Object} The |key| attribute value as an object.
95 function getObjectAttr(dict, key, opt_default) {
96 var value = /** @type {Object} */ (dict[key]);
97 if (typeof value != 'object') {
98 if (opt_default === undefined) {
99 throw 'Invalid data type for ' + key +
100 ' (expected: object, actual: ' + typeof value + ')';
109 * Get the |key| attribute in the given |dict| and verify that it is a
112 * If the attribute is not a string, then an exception will be thrown unless
113 * a default value is specified in |opt_default|.
115 * @param {Object<string,*>} dict The dictionary containing the |key|
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.
118 * @return {string} The |key| attribute value as a string.
120 function getStringAttr(dict, key, opt_default) {
121 var value = /** @type {string} */ (dict[key]);
122 if (typeof value != 'string') {
123 if (opt_default === undefined) {
124 throw 'Invalid data type for ' + key +
125 ' (expected: string, actual: ' + typeof value + ')';
134 * Return a JSON object parsed from a string.
136 * If the string cannot be parsed, or does not result in an object, then an
137 * exception will be thrown.
139 * @param {string} jsonString The JSON string to parse.
140 * @return {Object} The JSON object created from the |jsonString|.
142 function getJsonObjectFromString(jsonString) {
143 var value = base.jsonParseSafe(jsonString);
144 if (typeof value != 'object') {
145 throw 'Invalid data type (expected: Object, actual: ' + typeof value + ')';