assertate-debug
Version:
TypeScript assertion helpers
300 lines (299 loc) • 10.8 kB
JavaScript
////////////////////////////////////////////////////////////////////////////////
// Assertion Helpers
////////////////////////////////////////////////////////////////////////////////
Object.defineProperty(exports, "__esModule", { value: true });
exports.assert = exports.assertIsDefined = exports.assertIsNotUndefined = exports.assertIsUndefined = exports.assertIsSymbol = exports.assertIsObject = exports.assertIsNull = exports.assertIsArray = exports.assertIsBigInt = exports.assertIsNumber = exports.assertIsBoolean = exports.assertIsString = exports.isSymbol = exports.isNull = exports.isNotUndefined = exports.isUndefined = exports.isDefined = exports.isObject = exports.isArray = exports.isString = exports.isBoolean = exports.isBigInt = exports.isNumber = exports.getAssertionMessage = exports.setAssertionMessage = exports.getType = void 0;
/**
* Returns a refined type of an object. Defaults to `typeof` unless the value is
* null, in which case 'null' is returned
*
* @param value value to get the type of
*/
function getType(value) {
if (value === null) {
return "null";
}
return typeof value;
}
exports.getType = getType;
/**
* Generates an type assertion message for the given `value`
*
* @param value value being type-checked
* @param type the expected value as a string; eg 'string', 'boolean', 'number'
* @param variableName the name of the variable being type-checked
* @param additionalMessage further information on failure
*/
var AssertionMessage = function (value, type, variableName, additionalMessage) {
var message = variableName
? variableName + " must be of type '" + type + "', '" + getType(value) + "' provided"
: "expected value of type '" + type + "', '" + getType(value) + "' provided";
return additionalMessage ? message + ": " + additionalMessage : message;
};
/**
* Sets the global AssertionMessage with the `message` provided
*
* @param message assertion message generator to replace the current generator with
*/
function setAssertionMessage(message) {
AssertionMessage = message;
}
exports.setAssertionMessage = setAssertionMessage;
/**
* Gets the currently set global AssertionMessage
*/
function getAssertionMessage() {
return AssertionMessage;
}
exports.getAssertionMessage = getAssertionMessage;
/**
* Returns the boolean and hoisted type-check value that `value` is a number
*
* @param value value to type-check as a number
*/
function isNumber(value) {
return getType(value) === "number";
}
exports.isNumber = isNumber;
/**
* Returns the boolean and hoisted type-check value that `value` is a bigint
*
* @param value value to type-check as a bigint
*/
function isBigInt(value) {
return getType(value) === "bigint";
}
exports.isBigInt = isBigInt;
/**
* Returns the boolean and hoisted type-check value that `value` is a boolean
*
* @param value value to type-check as a boolean
*/
function isBoolean(value) {
return getType(value) === "boolean";
}
exports.isBoolean = isBoolean;
/**
* Returns the boolean and hoisted type-check value that `value` is a string
*
* @param value value to type-check as a string
*/
function isString(value) {
return getType(value) === "string";
}
exports.isString = isString;
/**
* Returns the boolean and hoisted type-check value that `value` is an array
*
* @param value value to type-check as an array
*/
function isArray(value) {
return Array.isArray(value);
}
exports.isArray = isArray;
/**
* Returns the boolean and hoisted type-check value that `value` is an object.
* null is not considered an 'object'.
*
* @param value value to type-check as an object
*/
function isObject(value) {
return getType(value) === "object";
}
exports.isObject = isObject;
/**
* Returns the boolean and hoisted type-check value that `value` is not undefined (NonNullable)
*
* @param value value to type-check as NonNullable
*/
function isDefined(value) {
return !(value === undefined || value === null);
}
exports.isDefined = isDefined;
/**
* Returns the boolean and hoisted type-check value that `value` is undefined
*
* @param value value to type-check as undefined
*/
function isUndefined(value) {
return getType(value) === "undefined";
}
exports.isUndefined = isUndefined;
/**
* Returns the boolean and hoisted type-check value that `value` is undefined
*
* @param value value to type-check as undefined
*/
function isNotUndefined(value) {
return getType(value) !== "undefined";
}
exports.isNotUndefined = isNotUndefined;
/**
* Returns the boolean and hoisted type-check value that `value` is null
*
* @param value value to type-check as null
*/
function isNull(value) {
return getType(value) === "null";
}
exports.isNull = isNull;
/**
* Returns the boolean and hoisted type-check value that `value` is a symbol
*
* @param value value to type-check as a symbol
*/
function isSymbol(value) {
return getType(value) === "symbol";
}
exports.isSymbol = isSymbol;
/**
* Type-checks the provided `value` to be a string, throws an Error if it is not
*
* @param value the value to type-check as a string
* @param variableName the name of the variable to be type-checked
* @param additionalMessage further information on failure
* @throws {Error}
*/
function assertIsString(value, variableName, additionalMessage) {
assert(isString(value), AssertionMessage(value, "string", variableName, additionalMessage));
}
exports.assertIsString = assertIsString;
/**
* Type-checks the provided `value` to be a boolean, throws an Error if it is
* not
*
* @param value the value to type-check as a boolean
* @param variableName the name of the variable to be type-checked
* @param additionalMessage further information on failure
* @throws {Error}
*/
function assertIsBoolean(value, variableName, additionalMessage) {
assert(isBoolean(value), AssertionMessage(value, "boolean", variableName, additionalMessage));
}
exports.assertIsBoolean = assertIsBoolean;
/**
* Type-checks the provided 'value' to be a number, throws an Error if it is not
*
* @param value the value to type-check as a number
* @param variableName the name of the variable to be type-checked
* @param additionalMessage further information on failure
* @throws {Error}
*/
function assertIsNumber(value, variableName, additionalMessage) {
assert(isNumber(value), AssertionMessage(value, "number", variableName, additionalMessage));
}
exports.assertIsNumber = assertIsNumber;
/**
* Type-checks the provided 'value' to be a bigint, throws an Error if it is not
*
* @param value the value to type-check as a bigint
* @param variableName the name of the variable to be type-checked
* @param additionalMessage further information on failure
* @throws {Error}
*/
function assertIsBigInt(value, variableName, additionalMessage) {
assert(isBigInt(value), AssertionMessage(value, "bigint", variableName, additionalMessage));
}
exports.assertIsBigInt = assertIsBigInt;
/**
* Type-checks the provided `value` to be an array, throws an Error if it is not
*
* @param value the value to type-check as an array
* @param variableName the name of the variable to be type-checked
* @param additionalMessage further information on failure
* @throws {Error}
*/
function assertIsArray(value, variableName, additionalMessage) {
assert(isArray(value), AssertionMessage(value, "array", variableName, additionalMessage));
}
exports.assertIsArray = assertIsArray;
/**
* Type-checks the provided `value` to be null, throws an Error if it is not
*
* @param value the value to type-check as null
* @param variableName the name of the variable to be type-checked
* @param additionalMessage further information on failure
* @throws {Error}
*/
function assertIsNull(value, variableName, additionalMessage) {
assert(isNull(value), AssertionMessage(value, "null", variableName, additionalMessage));
}
exports.assertIsNull = assertIsNull;
/**
* Type-checks the provided `value` to be an object, throws an Error if it is
* not
*
* @param value the value to type-check as an object
* @param variableName the name of the variable to be type-checked
* @param additionalMessage further information on failure
* @throws {Error}
*/
function assertIsObject(value, variableName, additionalMessage) {
assert(isObject(value), AssertionMessage(value, "object", variableName, additionalMessage));
}
exports.assertIsObject = assertIsObject;
/**
* Type-checks the provided `value` to be a symbol, throws an Error if it is not
*
* @param value the value to type-check as a symbol
* @param variableName the name of the variable to be type-checked
* @param additionalMessage further information on failure
* @throws {Error}
*/
function assertIsSymbol(value, variableName, additionalMessage) {
assert(isSymbol(value), AssertionMessage(value, "symbol", variableName, additionalMessage));
}
exports.assertIsSymbol = assertIsSymbol;
/**
* Type-checks the provided `value` to be undefined, throws an Error if it is
* not
*
* @param value the value to type-check as undefined
* @param variableName the name of the variable to be type-checked
* @param additionalMessage further information on failure
* @throws {Error}
*/
function assertIsUndefined(value, variableName, additionalMessage) {
assert(isUndefined(value), AssertionMessage(value, "undefined", variableName, additionalMessage));
}
exports.assertIsUndefined = assertIsUndefined;
/**
* Type-checks the provided `value` to be not undefined, throws an Error if it is
* not
*
* @param value the value to type-check as undefined
* @param variableName the name of the variable to be type-checked
* @param additionalMessage further information on failure
* @throws {Error}
*/
function assertIsNotUndefined(value, variableName, additionalMessage) {
assert(isNotUndefined(value), AssertionMessage(value, "not undefined", variableName, additionalMessage));
}
exports.assertIsNotUndefined = assertIsNotUndefined;
/**
* Type-checks the provided `value` to be defined. throws and Error if it is not
*
* @param value the value to type-check as as defined
* @param variableName teh name of the variable to be type-checked
* @param additionalMessage further information on failure
* @throws {Error}
*/
function assertIsDefined(value, variableName, additionalMessage) {
assert(isDefined(value), AssertionMessage(value, "defined", variableName, additionalMessage));
}
exports.assertIsDefined = assertIsDefined;
/**
* General assertion helper. Asserts the provided `condition` is met, if it is
* not, throws an Error with the provided `message`
*
* @param condition the condition to be asserted
* @throws {Error}
*/
function assert(condition, message) {
if (!condition) {
debugger;
throw Error(message);
}
}
exports.assert = assert;
;