OLD | NEW |
| (Empty) |
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 /** @suppress {duplicate} */ | |
7 var remoting = remoting || {}; | |
8 | |
9 (function() { | |
10 'use strict'; | |
11 | |
12 /** | |
13 * @param {*} value | |
14 * @return {boolean} | |
15 */ | |
16 var isArray = function(value) { | |
17 return Array.isArray(value); | |
18 }; | |
19 | |
20 /** | |
21 * @param {*} value | |
22 * @return {boolean} | |
23 */ | |
24 var isBoolean = function(value) { | |
25 return typeof value == 'boolean'; | |
26 }; | |
27 | |
28 /** | |
29 * @param {*} value | |
30 * @return {boolean} | |
31 */ | |
32 var isNumber = function(value) { | |
33 return typeof value == 'number'; | |
34 }; | |
35 | |
36 /** | |
37 * @param {*} value | |
38 * @return {boolean} | |
39 */ | |
40 var isObject = function(value) { | |
41 return value != null && typeof value == 'object' && !Array.isArray(value); | |
42 }; | |
43 | |
44 /** | |
45 * @param {*} value | |
46 * @return {boolean} | |
47 */ | |
48 var isString = function(value) { | |
49 return typeof value == 'string'; | |
50 }; | |
51 | |
52 /** | |
53 * @param {*} value | |
54 * @return {string} | |
55 */ | |
56 var jsonTypeOf = function(value) { | |
57 if (typeof value == 'object') { | |
58 if (value === null) { | |
59 return 'null'; | |
60 } else if (Array.isArray(value)) { | |
61 return 'array'; | |
62 } else { | |
63 return 'object'; | |
64 } | |
65 } else { | |
66 return typeof value; | |
67 } | |
68 }; | |
69 | |
70 /** | |
71 * @param {*} value the value to check; must be an object | |
72 * @param {function(*):boolean} pred | |
73 * @param {string} typeDesc | |
74 * @return {*} the argument | |
75 */ | |
76 var assertType = function(value, pred, typeDesc) { | |
77 if (pred(value)) { | |
78 return value; | |
79 } else { | |
80 throw new Error('Invalid data type' + | |
81 ' (expected: ' + typeDesc + | |
82 ', actual: ' + jsonTypeOf(value) + ')'); | |
83 } | |
84 }; | |
85 | |
86 /** | |
87 * @param {*} value the value to check; must be an object | |
88 * @return {!Array} the argument | |
89 */ | |
90 base.assertArray = function(value) { | |
91 return /** @type {!Array} */ (assertType(value, isArray, 'array')); | |
92 }; | |
93 | |
94 /** | |
95 * @param {*} value the value to check; must be a boolean | |
96 * @return {boolean} the argument | |
97 */ | |
98 base.assertBoolean = function(value) { | |
99 return /** @type {boolean} */ (assertType(value, isBoolean, 'boolean')); | |
100 }; | |
101 | |
102 /** | |
103 * @param {*} value the value to check; must be a number | |
104 * @return {number} the argument | |
105 */ | |
106 base.assertNumber = function(value) { | |
107 return /** @type {number} */ (assertType(value, isNumber, 'number')); | |
108 }; | |
109 | |
110 /** | |
111 * @param {*} value the value to check; must be an object | |
112 * @return {!Object} the argument | |
113 */ | |
114 base.assertObject = function(value) { | |
115 return /** @type {!Object} */ (assertType(value, isObject, 'object')); | |
116 }; | |
117 | |
118 /** | |
119 * @param {*} value the value to check; must be a string | |
120 * @return {string} the argument | |
121 */ | |
122 base.assertString = function(value) { | |
123 return /** @type {string} */ (assertType(value, isString, 'string')); | |
124 }; | |
125 | |
126 /** | |
127 * @param {Object<string,*>} dict The dictionary containing the |key| | |
128 * @param {string} key The key to typecheck in the |dict|. | |
129 * @param {function(*):boolean} pred | |
130 * @param {string} typeDesc | |
131 * @param {*=} opt_default The value to return if pred returns false. | |
132 * @return {*} The |key| attribute value. | |
133 */ | |
134 var getTypedAttr = function(dict, key, pred, typeDesc, opt_default) { | |
135 var value = /** @type {*} */ (dict[key]); | |
136 if (pred(value)) { | |
137 return value; | |
138 } else if (opt_default !== undefined) { | |
139 return opt_default; | |
140 } else { | |
141 throw new Error('Invalid data type for ' + key + | |
142 ' (expected: ' + typeDesc + ', actual: ' + | |
143 jsonTypeOf(value) + ')'); | |
144 } | |
145 }; | |
146 | |
147 /** | |
148 * Get the |key| attribute in the given |dict| and verify that it is an | |
149 * array value. | |
150 * | |
151 * If the attribute is not an array, then an exception will be thrown unless | |
152 * a default value is specified in |opt_default|. | |
153 * | |
154 * @param {Object<string,*>} dict The dictionary containing the |key| | |
155 * @param {string} key The key to typecheck in the |dict|. | |
156 * @param {Array=} opt_default The value to return if the key is not a bool. | |
157 * @return {Array} The |key| attribute value as an object. | |
158 */ | |
159 base.getArrayAttr = function(dict, key, opt_default) { | |
160 return /** @type {Array} */ ( | |
161 getTypedAttr(dict, key, isArray, 'array', opt_default)); | |
162 }; | |
163 | |
164 /** | |
165 * Get the |key| attribute in the given |dict| and verify that it is a | |
166 * boolean value. | |
167 * | |
168 * If the attribute is not a boolean, then an exception will be thrown unless | |
169 * a default value is specified in |opt_default|. | |
170 * | |
171 * @param {Object<string,*>} dict The dictionary containing the |key| | |
172 * @param {string} key The key to typecheck in the |dict|. | |
173 * @param {boolean=} opt_default The value to return if the key is not a bool. | |
174 * @return {boolean} The |key| attribute value as a boolean. | |
175 */ | |
176 base.getBooleanAttr = function(dict, key, opt_default) { | |
177 var value = /** @type {*} */ (dict[key]); | |
178 if (value == 'true' || value == 'false') { | |
179 return value == 'true'; | |
180 } | |
181 return /** @type {boolean} */ ( | |
182 getTypedAttr(dict, key, isBoolean, 'boolean', opt_default)); | |
183 }; | |
184 | |
185 /** | |
186 * Get the |key| attribute in the given |dict| and verify that it is a | |
187 * number value. | |
188 * | |
189 * If the attribute is not a number, then an exception will be thrown unless | |
190 * a default value is specified in |opt_default|. | |
191 * | |
192 * @param {Object<string,*>} dict The dictionary containing the |key| | |
193 * @param {string} key The key to typecheck in the |dict|. | |
194 * @param {number=} opt_default The value to return if the key is not a number. | |
195 * @return {number} The |key| attribute value as a number. | |
196 */ | |
197 base.getNumberAttr = function(dict, key, opt_default) { | |
198 return /** @type {number} */ ( | |
199 getTypedAttr(dict, key, isNumber, 'number', opt_default)); | |
200 }; | |
201 | |
202 /** | |
203 * Get the |key| attribute in the given |dict| and verify that it is an | |
204 * object value. | |
205 * | |
206 * If the attribute is not an object, then an exception will be thrown unless | |
207 * a default value is specified in |opt_default|. | |
208 * | |
209 * @param {Object<string,*>} dict The dictionary containing the |key| | |
210 * @param {string} key The key to typecheck in the |dict|. | |
211 * @param {Object=} opt_default The value to return if the key is not a bool. | |
212 * @return {!Object} The |key| attribute value as an object. | |
213 */ | |
214 base.getObjectAttr = function(dict, key, opt_default) { | |
215 return /** @type {!Object} */ ( | |
216 getTypedAttr(dict, key, isObject, 'object', opt_default)); | |
217 }; | |
218 | |
219 /** | |
220 * Get the |key| attribute in the given |dict| and verify that it is a | |
221 * string value. | |
222 * | |
223 * If the attribute is not a string, then an exception will be thrown unless | |
224 * a default value is specified in |opt_default|. | |
225 * | |
226 * @param {Object<string,*>} dict The dictionary containing the |key| | |
227 * @param {string} key The key to typecheck in the |dict|. | |
228 * @param {string=} opt_default The value to return if the key is not a string. | |
229 * @return {string} The |key| attribute value as a string. | |
230 */ | |
231 base.getStringAttr = function(dict, key, opt_default) { | |
232 return /** @type {string} */ ( | |
233 getTypedAttr(dict, key, isString, 'string', opt_default)); | |
234 }; | |
235 | |
236 /** | |
237 * Return a JSON object parsed from a string. | |
238 * | |
239 * If the string cannot be parsed, or does not result in an object, then an | |
240 * exception will be thrown. | |
241 * | |
242 * @param {string} jsonString The JSON string to parse. | |
243 * @return {Object} The JSON object created from the |jsonString|. | |
244 */ | |
245 base.getJsonObjectFromString = function(jsonString) { | |
246 return base.assertObject(base.jsonParseSafe(jsonString)); | |
247 }; | |
248 | |
249 })(); | |
OLD | NEW |