Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(136)

Side by Side Diff: remoting/webapp/crd/js/typecheck.js

Issue 1015553003: Added more typechecking functions and unit tests for existing code. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 * @param {*} value the value to check; must be an object
7 * @return {*} the argument
8 */
9 function assertDefined(value) {
10 if (value !== undefined) {
11 return value;
12 } else {
13 throw new Error('Undefined value');
kelvinp 2015/03/17 21:41:48 we have a function base.debug.assert which current
John Williams 2015/03/18 17:23:01 I tried rewriting in terms of assert, but it won't
14 }
15 }
16
17 /**
18 * @param {*} value the value to check; must be an object
19 * @return {!Array} the argument
20 */
21 function assertArray(value) {
22 if (value instanceof Array) {
kelvinp 2015/03/17 21:41:48 Nit: Array.isArray(value) as it is more robust acr
John Williams 2015/03/18 17:23:01 Done.
23 return /** @type {!Array} */ (value);
24 } else {
25 throw new Error('Invalid data type' +
26 ' (expected: array, actual: ' + typeof value + ')');
27 }
28 }
29
30 /**
31 * @param {*} value the value to check; must be a boolean
32 * @return {boolean} the argument
33 */
34 function assertBoolean(value) {
35 if (typeof value == 'boolean') {
36 return /** @type {boolean} */ (value);
37 } else {
38 throw new Error('Invalid data type' +
39 ' (expected: boolean, actual: ' + typeof value + ')');
40 }
41 }
42
43 /**
44 * @param {*} value the value to check; must be a number
45 * @return {number} the argument
46 */
47 function assertNumber(value) {
48 if (typeof value == 'number') {
49 return /** @type {number} */ (value);
50 } else {
51 throw new Error('Invalid data type' +
52 ' (expected: number, actual: ' + typeof value + ')');
53 }
54 }
55
56 /**
57 * @param {*} value the value to check; must be an object
58 * @return {!Object} the argument
59 */
60 function assertObject(value) {
61 if (value instanceof Object && !(value instanceof Array)) {
62 return /** @type {!Object} */ (value);
63 } else {
64 throw new Error('Invalid data type' +
65 ' (expected: object, actual: ' + typeof value + ')');
66 }
67 }
68
69 /**
70 * @param {*} value the value to check; must be a string
71 * @return {string} the argument
72 */
73 function assertString(value) {
74 if (typeof value == 'string') {
75 return /** @type {string} */ (value);
76 } else {
77 throw new Error('Invalid data type' +
78 ' (expected: string, actual: ' + typeof value + ')');
79 }
80 }
81
82 /**
6 * Get the |key| attribute in the given |dict| and verify that it is an 83 * Get the |key| attribute in the given |dict| and verify that it is an
7 * array value. 84 * array value.
kelvinp 2015/03/17 21:41:48 comment needs update. array value no longer applie
John Williams 2015/03/18 17:23:01 I just got rid of this function entirely.
8 * 85 *
86 * If the attribute is not an array, then an exception will be thrown unless
87 * a default value is specified in |opt_default|.
88 *
89 * @param {Object<string,*>} dict The dictionary containing the |key|
90 * @param {string} key The key to typecheck in the |dict|.
91 * @param {*=} opt_default The value to return if the key is not a bool.
92 * @return {*} The |key| attribute value as an object.
93 */
94 function getAttr(dict, key, opt_default) {
95 var value = /** @type {*} */ (dict[key]);
96 if (value === undefined) {
97 if (opt_default === undefined) {
98 throw new Error('Undefined value for ' + key);
99 } else {
100 return opt_default;
101 }
102 }
103 return value;
104 }
105
106 /**
107 * Get the |key| attribute in the given |dict| and verify that it is an
108 * array value.
109 *
9 * If the attribute is not an array, then an exception will be thrown unless 110 * If the attribute is not an array, then an exception will be thrown unless
10 * a default value is specified in |opt_default|. 111 * a default value is specified in |opt_default|.
11 * 112 *
12 * @param {Object<string,*>} dict The dictionary containing the |key| 113 * @param {Object<string,*>} dict The dictionary containing the |key|
13 * @param {string} key The key to typecheck in the |dict|. 114 * @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. 115 * @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. 116 * @return {Array} The |key| attribute value as an object.
16 */ 117 */
17 function getArrayAttr(dict, key, opt_default) { 118 function getArrayAttr(dict, key, opt_default) {
18 var value = /** @type {Array} */ (dict[key]); 119 var value = /** @type {Array} */ (dict[key]);
19 if (!(value instanceof Array)) { 120 if (!(value instanceof Array)) {
20 if (opt_default === undefined) { 121 if (opt_default === undefined) {
21 throw 'Invalid data type for ' + key + 122 throw new Error('Invalid data type for ' + key +
22 ' (expected: array, actual: ' + typeof value + ')'; 123 ' (expected: array, actual: ' + typeof value + ')');
23 } else { 124 } else {
24 return opt_default; 125 return opt_default;
25 } 126 }
26 } 127 }
27 return value; 128 return value;
28 } 129 }
29 130
30 /** 131 /**
31 * Get the |key| attribute in the given |dict| and verify that it is a 132 * Get the |key| attribute in the given |dict| and verify that it is a
32 * boolean value. 133 * boolean value.
33 * 134 *
34 * If the attribute is not a boolean, then an exception will be thrown unless 135 * If the attribute is not a boolean, then an exception will be thrown unless
35 * a default value is specified in |opt_default|. 136 * a default value is specified in |opt_default|.
36 * 137 *
37 * @param {Object<string,*>} dict The dictionary containing the |key| 138 * @param {Object<string,*>} dict The dictionary containing the |key|
38 * @param {string} key The key to typecheck in the |dict|. 139 * @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. 140 * @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. 141 * @return {boolean} The |key| attribute value as a boolean.
41 */ 142 */
42 function getBooleanAttr(dict, key, opt_default) { 143 function getBooleanAttr(dict, key, opt_default) {
43 var value = /** @type {boolean} */ (dict[key]); 144 var value = /** @type {boolean} */ (dict[key]);
44 if (value == 'true' || value == 'false') { 145 if (value == 'true' || value == 'false') {
45 return (value == 'true'); 146 return (value == 'true');
46 } 147 }
47 if (typeof value !== 'boolean') { 148 if (typeof value !== 'boolean') {
48 if (opt_default === undefined) { 149 if (opt_default === undefined) {
49 throw 'Invalid data type for ' + key + 150 throw new Error('Invalid data type for ' + key +
50 ' (expected: boolean, actual: ' + typeof value + ')'; 151 ' (expected: boolean, actual: ' + typeof value + ')');
51 } else { 152 } else {
52 return opt_default; 153 return opt_default;
53 } 154 }
54 } 155 }
55 return value; 156 return value;
56 } 157 }
57 158
58 /** 159 /**
59 * Get the |key| attribute in the given |dict| and verify that it is a 160 * Get the |key| attribute in the given |dict| and verify that it is a
60 * number value. 161 * number value.
61 * 162 *
62 * If the attribute is not a number, then an exception will be thrown unless 163 * If the attribute is not a number, then an exception will be thrown unless
63 * a default value is specified in |opt_default|. 164 * a default value is specified in |opt_default|.
64 * 165 *
65 * @param {Object<string,*>} dict The dictionary containing the |key| 166 * @param {Object<string,*>} dict The dictionary containing the |key|
66 * @param {string} key The key to typecheck in the |dict|. 167 * @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. 168 * @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. 169 * @return {number} The |key| attribute value as a number.
69 */ 170 */
70 function getNumberAttr(dict, key, opt_default) { 171 function getNumberAttr(dict, key, opt_default) {
71 var value = /** @type {number} */(dict[key]); 172 var value = /** @type {number} */(dict[key]);
72 if (typeof value != 'number') { 173 if (typeof value != 'number') {
73 if (opt_default === undefined) { 174 if (opt_default === undefined) {
74 throw 'Invalid data type for ' + key + 175 throw new Error('Invalid data type for ' + key +
75 ' (expected: number, actual: ' + typeof value + ')'; 176 ' (expected: number, actual: ' + typeof value + ')');
76 } else { 177 } else {
77 return opt_default; 178 return opt_default;
78 } 179 }
79 } 180 }
80 return value; 181 return value;
81 } 182 }
82 183
83 /** 184 /**
84 * Get the |key| attribute in the given |dict| and verify that it is an 185 * Get the |key| attribute in the given |dict| and verify that it is an
85 * object value. 186 * object value.
86 * 187 *
87 * If the attribute is not an object, then an exception will be thrown unless 188 * If the attribute is not an object, then an exception will be thrown unless
88 * a default value is specified in |opt_default|. 189 * a default value is specified in |opt_default|.
89 * 190 *
90 * @param {Object<string,*>} dict The dictionary containing the |key| 191 * @param {Object<string,*>} dict The dictionary containing the |key|
91 * @param {string} key The key to typecheck in the |dict|. 192 * @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. 193 * @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. 194 * @return {Object} The |key| attribute value as an object.
94 */ 195 */
95 function getObjectAttr(dict, key, opt_default) { 196 function getObjectAttr(dict, key, opt_default) {
96 var value = /** @type {Object} */ (dict[key]); 197 var value = /** @type {Object} */ (dict[key]);
97 if (typeof value != 'object') { 198 if (!(value instanceof Object) || value instanceof Array) {
98 if (opt_default === undefined) { 199 if (opt_default === undefined) {
99 throw 'Invalid data type for ' + key + 200 throw new Error('Invalid data type for ' + key +
100 ' (expected: object, actual: ' + typeof value + ')'; 201 ' (expected: object, actual: ' + typeof value + ')');
101 } else { 202 } else {
102 return opt_default; 203 return opt_default;
103 } 204 }
104 } 205 }
105 return value; 206 return value;
106 } 207 }
107 208
108 /** 209 /**
109 * Get the |key| attribute in the given |dict| and verify that it is a 210 * Get the |key| attribute in the given |dict| and verify that it is a
110 * string value. 211 * string value.
111 * 212 *
112 * If the attribute is not a string, then an exception will be thrown unless 213 * If the attribute is not a string, then an exception will be thrown unless
113 * a default value is specified in |opt_default|. 214 * a default value is specified in |opt_default|.
114 * 215 *
115 * @param {Object<string,*>} dict The dictionary containing the |key| 216 * @param {Object<string,*>} dict The dictionary containing the |key|
116 * @param {string} key The key to typecheck in the |dict|. 217 * @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. 218 * @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. 219 * @return {string} The |key| attribute value as a string.
119 */ 220 */
120 function getStringAttr(dict, key, opt_default) { 221 function getStringAttr(dict, key, opt_default) {
121 var value = /** @type {string} */ (dict[key]); 222 var value = /** @type {string} */ (dict[key]);
122 if (typeof value != 'string') { 223 if (typeof value != 'string') {
123 if (opt_default === undefined) { 224 if (opt_default === undefined) {
124 throw 'Invalid data type for ' + key + 225 throw new Error('Invalid data type for ' + key +
125 ' (expected: string, actual: ' + typeof value + ')'; 226 ' (expected: string, actual: ' + typeof value + ')');
126 } else { 227 } else {
127 return opt_default; 228 return opt_default;
128 } 229 }
129 } 230 }
130 return value; 231 return value;
131 } 232 }
132 233
133 /** 234 /**
134 * Return a JSON object parsed from a string. 235 * Return a JSON object parsed from a string.
135 * 236 *
136 * If the string cannot be parsed, or does not result in an object, then an 237 * If the string cannot be parsed, or does not result in an object, then an
137 * exception will be thrown. 238 * exception will be thrown.
138 * 239 *
139 * @param {string} jsonString The JSON string to parse. 240 * @param {string} jsonString The JSON string to parse.
140 * @return {Object} The JSON object created from the |jsonString|. 241 * @return {Object} The JSON object created from the |jsonString|.
141 */ 242 */
142 function getJsonObjectFromString(jsonString) { 243 function getJsonObjectFromString(jsonString) {
143 var value = base.jsonParseSafe(jsonString); 244 var value = base.jsonParseSafe(jsonString);
144 if (typeof value != 'object') { 245 if (typeof value != 'object') {
145 throw 'Invalid data type (expected: Object, actual: ' + typeof value + ')'; 246 throw new Error('Invalid data type (expected: Object, actual: ' +
247 typeof value + ')');
146 } 248 }
147 return value; 249 return value;
148 } 250 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698