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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « remoting/webapp/crd/js/it2me_host_facade.js ('k') | remoting/webapp/crd/js/typecheck_unittest.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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';
+
+/**
+ * @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));
+};
+
+})();
« no previous file with comments | « remoting/webapp/crd/js/it2me_host_facade.js ('k') | remoting/webapp/crd/js/typecheck_unittest.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698