Chromium Code Reviews| Index: remoting/webapp/crd/js/typecheck.js |
| diff --git a/remoting/webapp/crd/js/typecheck.js b/remoting/webapp/crd/js/typecheck.js |
| index b44df12c4fe6e51aad0d4b765e5783edd33dd929..10e1959dd7b33fec2055708c5b0f5329eb0c5a03 100644 |
| --- a/remoting/webapp/crd/js/typecheck.js |
| +++ b/remoting/webapp/crd/js/typecheck.js |
| @@ -2,6 +2,148 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| + |
| +/** @suppress {duplicate} */ |
| +var remoting = remoting || {}; |
| + |
| +(function() { |
| +'use strict'; |
|
garykac
2015/03/18 17:28:36
All of our files should have 'use strict' at globa
kelvinp
2015/03/18 17:52:19
The convention (as least imposed by jshint and jsl
John Williams
2015/03/18 20:20:19
Since there's disagreement, I'm going to leave it
|
| + |
| +/** |
| + * @param {*} value |
| + * @return {boolean} |
| + */ |
| +var isArray = function(value) { |
| + return Array.isArray(value); |
| +}; |
| + |
| +/** |
| + * @param {*} value |
| + * @return {boolean} |
| + */ |
| +var isBoolean = function(value) { |
| + return typeof value == 'boolean'; |
| +}; |
| + |
| +/** |
| + * @param {*} value |
| + * @return {boolean} |
| + */ |
| +var isNumber = function(value) { |
| + return typeof value == 'number'; |
| +}; |
| + |
| +/** |
| + * @param {*} value |
| + * @return {boolean} |
| + */ |
| +var isObject = function(value) { |
| + return value != null && typeof value == 'object' && !Array.isArray(value); |
| +}; |
| + |
| +/** |
| + * @param {*} value |
| + * @return {boolean} |
| + */ |
| +var isString = function(value) { |
| + return typeof value == 'string'; |
| +}; |
| + |
| +/** |
| + * @param {*} value |
| + * @return {string} |
| + */ |
| +var jsonTypeOf = function(value) { |
| + if (typeof value == 'object') { |
| + if (value === null) { |
| + return 'null'; |
| + } else if (Array.isArray(value)) { |
| + return 'array'; |
| + } else { |
| + return 'object'; |
| + } |
| + } else { |
| + return typeof value; |
| + } |
| +}; |
| + |
| +/** |
| + * @param {*} value the value to check; must be an object |
| + * @param {function(*):boolean} pred |
| + * @param {string} typeDesc |
| + * @return {*} the argument |
| + */ |
| +var assertType = function(value, pred, typeDesc) { |
| + if (pred(value)) { |
| + return value; |
| + } else { |
| + throw new Error('Invalid data type' + |
| + ' (expected: ' + typeDesc + |
| + ', actual: ' + jsonTypeOf(value) + ')'); |
| + } |
| +}; |
| + |
| +/** |
| + * @param {*} value the value to check; must be an object |
| + * @return {!Array} the argument |
| + */ |
| +base.assertArray = function(value) { |
| + return /** @type {!Array} */ (assertType(value, isArray, 'array')); |
| +}; |
| + |
| +/** |
| + * @param {*} value the value to check; must be a boolean |
| + * @return {boolean} the argument |
| + */ |
| +base.assertBoolean = function(value) { |
| + return /** @type {boolean} */ (assertType(value, isBoolean, 'boolean')); |
| +}; |
| + |
| +/** |
| + * @param {*} value the value to check; must be a number |
| + * @return {number} the argument |
| + */ |
| +base.assertNumber = function(value) { |
| + return /** @type {number} */ (assertType(value, isNumber, 'number')); |
| +}; |
| + |
| +/** |
| + * @param {*} value the value to check; must be an object |
| + * @return {!Object} the argument |
| + */ |
| +base.assertObject = function(value) { |
| + return /** @type {!Object} */ (assertType(value, isObject, 'object')); |
| +}; |
| + |
| +/** |
| + * @param {*} value the value to check; must be a string |
| + * @return {string} the argument |
| + */ |
| +base.assertString = function(value) { |
| + return /** @type {string} */ (assertType(value, isString, 'string')); |
| +}; |
| + |
| +/** |
| + * @param {Object<string,*>} dict The dictionary containing the |key| |
| + * @param {string} key The key to typecheck in the |dict|. |
| + * @param {function(*):boolean} pred |
| + * @param {string} typeDesc |
| + * @param {*=} opt_default The value to return if pred returns false. |
| + * @return {*} The |key| attribute value. |
| + */ |
| +var getTypedAttr = function(dict, key, pred, typeDesc, opt_default) { |
| + var value = /** @type {*} */ (dict[key]); |
| + if (pred(value)) { |
| + return value; |
| + } else if (opt_default !== undefined) { |
| + return opt_default; |
| + } else { |
| + throw new Error('Invalid data type for ' + key + |
| + ' (expected: ' + typeDesc + ', actual: ' + |
| + jsonTypeOf(value) + ')'); |
| + } |
| +}; |
| + |
| /** |
| * Get the |key| attribute in the given |dict| and verify that it is an |
| * array value. |
| @@ -14,18 +156,10 @@ |
| * @param {Array=} opt_default The value to return if the key is not a bool. |
| * @return {Array} The |key| attribute value as an object. |
| */ |
| -function getArrayAttr(dict, key, opt_default) { |
| - var value = /** @type {Array} */ (dict[key]); |
| - if (!(value instanceof Array)) { |
| - if (opt_default === undefined) { |
| - throw 'Invalid data type for ' + key + |
| - ' (expected: array, actual: ' + typeof value + ')'; |
| - } else { |
| - return opt_default; |
| - } |
| - } |
| - return value; |
| -} |
| +base.getArrayAttr = function(dict, key, opt_default) { |
| + return /** @type {Array} */ ( |
| + getTypedAttr(dict, key, isArray, 'array', opt_default)); |
| +}; |
| /** |
| * Get the |key| attribute in the given |dict| and verify that it is a |
| @@ -39,21 +173,14 @@ function getArrayAttr(dict, key, opt_default) { |
| * @param {boolean=} opt_default The value to return if the key is not a bool. |
| * @return {boolean} The |key| attribute value as a boolean. |
| */ |
| -function getBooleanAttr(dict, key, opt_default) { |
| - var value = /** @type {boolean} */ (dict[key]); |
| +base.getBooleanAttr = function(dict, key, opt_default) { |
| + var value = /** @type {*} */ (dict[key]); |
| if (value == 'true' || value == 'false') { |
| - return (value == 'true'); |
| - } |
| - if (typeof value !== 'boolean') { |
| - if (opt_default === undefined) { |
| - throw 'Invalid data type for ' + key + |
| - ' (expected: boolean, actual: ' + typeof value + ')'; |
| - } else { |
| - return opt_default; |
| - } |
| + return value == 'true'; |
| } |
| - return value; |
| -} |
| + return /** @type {boolean} */ ( |
| + getTypedAttr(dict, key, isBoolean, 'boolean', opt_default)); |
| +}; |
| /** |
| * Get the |key| attribute in the given |dict| and verify that it is a |
| @@ -67,18 +194,10 @@ function getBooleanAttr(dict, key, opt_default) { |
| * @param {number=} opt_default The value to return if the key is not a number. |
| * @return {number} The |key| attribute value as a number. |
| */ |
| -function getNumberAttr(dict, key, opt_default) { |
| - var value = /** @type {number} */(dict[key]); |
| - if (typeof value != 'number') { |
| - if (opt_default === undefined) { |
| - throw 'Invalid data type for ' + key + |
| - ' (expected: number, actual: ' + typeof value + ')'; |
| - } else { |
| - return opt_default; |
| - } |
| - } |
| - return value; |
| -} |
| +base.getNumberAttr = function(dict, key, opt_default) { |
| + return /** @type {number} */ ( |
| + getTypedAttr(dict, key, isNumber, 'number', opt_default)); |
| +}; |
| /** |
| * Get the |key| attribute in the given |dict| and verify that it is an |
| @@ -90,20 +209,12 @@ function getNumberAttr(dict, key, opt_default) { |
| * @param {Object<string,*>} dict The dictionary containing the |key| |
| * @param {string} key The key to typecheck in the |dict|. |
| * @param {Object=} opt_default The value to return if the key is not a bool. |
| - * @return {Object} The |key| attribute value as an object. |
| - */ |
| -function getObjectAttr(dict, key, opt_default) { |
| - var value = /** @type {Object} */ (dict[key]); |
| - if (typeof value != 'object') { |
| - if (opt_default === undefined) { |
| - throw 'Invalid data type for ' + key + |
| - ' (expected: object, actual: ' + typeof value + ')'; |
| - } else { |
| - return opt_default; |
| - } |
| - } |
| - return value; |
| -} |
| + * @return {!Object} The |key| attribute value as an object. |
| + */ |
| +base.getObjectAttr = function(dict, key, opt_default) { |
| + return /** @type {!Object} */ ( |
| + getTypedAttr(dict, key, isObject, 'object', opt_default)); |
| +}; |
| /** |
| * Get the |key| attribute in the given |dict| and verify that it is a |
| @@ -117,18 +228,10 @@ function getObjectAttr(dict, key, opt_default) { |
| * @param {string=} opt_default The value to return if the key is not a string. |
| * @return {string} The |key| attribute value as a string. |
| */ |
| -function getStringAttr(dict, key, opt_default) { |
| - var value = /** @type {string} */ (dict[key]); |
| - if (typeof value != 'string') { |
| - if (opt_default === undefined) { |
| - throw 'Invalid data type for ' + key + |
| - ' (expected: string, actual: ' + typeof value + ')'; |
| - } else { |
| - return opt_default; |
| - } |
| - } |
| - return value; |
| -} |
| +base.getStringAttr = function(dict, key, opt_default) { |
| + return /** @type {string} */ ( |
| + getTypedAttr(dict, key, isString, 'string', opt_default)); |
| +}; |
| /** |
| * Return a JSON object parsed from a string. |
| @@ -139,10 +242,8 @@ function getStringAttr(dict, key, opt_default) { |
| * @param {string} jsonString The JSON string to parse. |
| * @return {Object} The JSON object created from the |jsonString|. |
| */ |
| -function getJsonObjectFromString(jsonString) { |
| - var value = base.jsonParseSafe(jsonString); |
| - if (typeof value != 'object') { |
| - throw 'Invalid data type (expected: Object, actual: ' + typeof value + ')'; |
| - } |
| - return value; |
| -} |
| +base.getJsonObjectFromString = function(jsonString) { |
| + return base.assertObject(base.jsonParseSafe(jsonString)); |
| +}; |
| + |
| +})(); |