@cesium/engine
Version:
CesiumJS is a JavaScript library for creating 3D globes and 2D maps in a web browser without a plugin.
213 lines (196 loc) • 6.37 kB
JavaScript
import defined from "./defined.js";
import DeveloperError from "./DeveloperError.js";
/**
* Contains functions for checking that supplied arguments are of a specified type
* or meet specified conditions
*/
const Check = {};
/**
* Contains type checking functions, all using the typeof operator
*/
Check.typeOf = {};
function getUndefinedErrorMessage(name) {
return `${name} is required, actual value was undefined`;
}
function getFailedTypeErrorMessage(actual, expected, name) {
return `Expected ${name} to be typeof ${expected}, actual typeof was ${actual}`;
}
/**
* Throws if test is not defined
*
* @param {string} name The name of the variable being tested
* @param {*} test The value that is to be checked
* @exception {DeveloperError} test must be defined
*/
Check.defined = function (name, test) {
if (!defined(test)) {
throw new DeveloperError(getUndefinedErrorMessage(name));
}
};
/**
* Throws if test is not typeof 'function'
*
* @param {string} name The name of the variable being tested
* @param {*} test The value to test
* @exception {DeveloperError} test must be typeof 'function'
*/
Check.typeOf.func = function (name, test) {
if (typeof test !== "function") {
throw new DeveloperError(
getFailedTypeErrorMessage(typeof test, "function", name),
);
}
};
/**
* Throws if test is not typeof 'string'
*
* @param {string} name The name of the variable being tested
* @param {*} test The value to test
* @exception {DeveloperError} test must be typeof 'string'
*/
Check.typeOf.string = function (name, test) {
if (typeof test !== "string") {
throw new DeveloperError(
getFailedTypeErrorMessage(typeof test, "string", name),
);
}
};
/**
* Throws if test is not typeof 'number'
*
* @param {string} name The name of the variable being tested
* @param {*} test The value to test
* @exception {DeveloperError} test must be typeof 'number'
*/
Check.typeOf.number = function (name, test) {
if (typeof test !== "number") {
throw new DeveloperError(
getFailedTypeErrorMessage(typeof test, "number", name),
);
}
};
/**
* Throws if test is not typeof 'number' and less than limit
*
* @param {string} name The name of the variable being tested
* @param {*} test The value to test
* @param {number} limit The limit value to compare against
* @exception {DeveloperError} test must be typeof 'number' and less than limit
*/
Check.typeOf.number.lessThan = function (name, test, limit) {
Check.typeOf.number(name, test);
if (test >= limit) {
throw new DeveloperError(
`Expected ${name} to be less than ${limit}, actual value was ${test}`,
);
}
};
/**
* Throws if test is not typeof 'number' and less than or equal to limit
*
* @param {string} name The name of the variable being tested
* @param {*} test The value to test
* @param {number} limit The limit value to compare against
* @exception {DeveloperError} test must be typeof 'number' and less than or equal to limit
*/
Check.typeOf.number.lessThanOrEquals = function (name, test, limit) {
Check.typeOf.number(name, test);
if (test > limit) {
throw new DeveloperError(
`Expected ${name} to be less than or equal to ${limit}, actual value was ${test}`,
);
}
};
/**
* Throws if test is not typeof 'number' and greater than limit
*
* @param {string} name The name of the variable being tested
* @param {*} test The value to test
* @param {number} limit The limit value to compare against
* @exception {DeveloperError} test must be typeof 'number' and greater than limit
*/
Check.typeOf.number.greaterThan = function (name, test, limit) {
Check.typeOf.number(name, test);
if (test <= limit) {
throw new DeveloperError(
`Expected ${name} to be greater than ${limit}, actual value was ${test}`,
);
}
};
/**
* Throws if test is not typeof 'number' and greater than or equal to limit
*
* @param {string} name The name of the variable being tested
* @param {*} test The value to test
* @param {number} limit The limit value to compare against
* @exception {DeveloperError} test must be typeof 'number' and greater than or equal to limit
*/
Check.typeOf.number.greaterThanOrEquals = function (name, test, limit) {
Check.typeOf.number(name, test);
if (test < limit) {
throw new DeveloperError(
`Expected ${name} to be greater than or equal to ${limit}, actual value was ${test}`,
);
}
};
/**
* Throws if test is not typeof 'object'
*
* @param {string} name The name of the variable being tested
* @param {*} test The value to test
* @exception {DeveloperError} test must be typeof 'object'
*/
Check.typeOf.object = function (name, test) {
if (typeof test !== "object") {
throw new DeveloperError(
getFailedTypeErrorMessage(typeof test, "object", name),
);
}
};
/**
* Throws if test is not typeof 'boolean'
*
* @param {string} name The name of the variable being tested
* @param {*} test The value to test
* @exception {DeveloperError} test must be typeof 'boolean'
*/
Check.typeOf.bool = function (name, test) {
if (typeof test !== "boolean") {
throw new DeveloperError(
getFailedTypeErrorMessage(typeof test, "boolean", name),
);
}
};
/**
* Throws if test is not typeof 'bigint'
*
* @param {string} name The name of the variable being tested
* @param {*} test The value to test
* @exception {DeveloperError} test must be typeof 'bigint'
*/
Check.typeOf.bigint = function (name, test) {
if (typeof test !== "bigint") {
throw new DeveloperError(
getFailedTypeErrorMessage(typeof test, "bigint", name),
);
}
};
/**
* Throws if test1 and test2 is not typeof 'number' and not equal in value
*
* @param {string} name1 The name of the first variable being tested
* @param {string} name2 The name of the second variable being tested against
* @param {*} test1 The value to test
* @param {*} test2 The value to test against
* @exception {DeveloperError} test1 and test2 should be type of 'number' and be equal in value
*/
Check.typeOf.number.equals = function (name1, name2, test1, test2) {
Check.typeOf.number(name1, test1);
Check.typeOf.number(name2, test2);
if (test1 !== test2) {
throw new DeveloperError(
`${name1} must be equal to ${name2}, the actual values are ${test1} and ${test2}`,
);
}
};
export default Check;