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 (typeof value != 'boolean') {
45 if (opt_default === undefined) {
46 throw 'Invalid data type for ' + key +
47 ' (expected: boolean, actual: ' + typeof value + ')';
56 * Get the |key| attribute in the given |dict| and verify that it is a
59 * If the attribute is not a number, then an exception will be thrown unless
60 * a default value is specified in |opt_default|.
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.
67 function 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 + ')';
81 * Get the |key| attribute in the given |dict| and verify that it is an
84 * If the attribute is not an object, then an exception will be thrown unless
85 * a default value is specified in |opt_default|.
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.
92 function 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 + ')';
106 * Get the |key| attribute in the given |dict| and verify that it is a
109 * If the attribute is not a string, then an exception will be thrown unless
110 * a default value is specified in |opt_default|.
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.
117 function 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 + ')';
131 * Return a JSON object parsed from a string.
133 * If the string cannot be parsed, or does not result in an object, then an
134 * exception will be thrown.
136 * @param {string} jsonString The JSON string to parse.
137 * @return {Object} The JSON object created from the |jsonString|.
139 function 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 + ')';