UNPKG

diffusion

Version:

Diffusion JavaScript client

59 lines (58 loc) 2.04 kB
"use strict"; /** * @module Util.Check * * @brief Utility functions for checking arguments */ Object.defineProperty(exports, "__esModule", { value: true }); exports.requireNonNegativeInt = exports.requireNonNegative = exports.requireNonNull = void 0; var errors_1 = require("./../../errors/errors"); /** * Throw an error if the checked value is null/undefined. * * @param value the value to check * @param what description to include in thrown error * @returns the checked value * @throws a {@link NullValueError} if value is null or undefined */ function requireNonNull(value, what) { if (value === undefined || value === null) { throw new errors_1.NullValueError(what + ' is null'); } return value; } exports.requireNonNull = requireNonNull; /** * Require that a number is defined and is not negative * * @param i the number to check * @param what a description of the number to check * @return the number * @throws an {@link IllegalArgumentError} if the number is negative * @throws a {@link NullValueError} if the number is null or undefined */ function requireNonNegative(i, what) { var iChecked = requireNonNull(i, what); if (iChecked < 0) { throw new errors_1.IllegalArgumentError(what + " is negative: " + i); } return iChecked; } exports.requireNonNegative = requireNonNegative; /** * Require that a number is defined and is not negative * * @param i the number to check * @param what a description of the number to check * @return the number * @throws an {@link IllegalArgumentError} if the number is negative * @throws a {@link NullValueError} if the number is null or undefined */ function requireNonNegativeInt(i, what) { var iChecked = requireNonNull(i, what); if (!Number.isInteger(iChecked) || iChecked < 0) { throw new errors_1.IllegalArgumentError("Invalid argument '" + what + "': " + i); } return iChecked; } exports.requireNonNegativeInt = requireNonNegativeInt;