UNPKG

timezonecomplete

Version:

DateTime, TimeZone, Duration and Period library aimed at providing a consistent and complete date-time interface, away from the original JavaScript Date class.

67 lines 1.98 kB
/** * Copyright(c) 2014 ABB Switzerland Ltd. * * Math utility functions */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.positiveModulo = exports.filterFloat = exports.roundSym = exports.isInt = void 0; var assert_1 = require("./assert"); /** * @return true iff given argument is an integer number * @throws nothing */ function isInt(n) { if (n === null || !isFinite(n)) { return false; } return (Math.floor(n) === n); } exports.isInt = isInt; /** * Rounds -1.5 to -2 instead of -1 * Rounds +1.5 to +2 * @throws timezonecomplete.Argument.N if n is not a finite number */ function roundSym(n) { (0, assert_1.default)(Number.isFinite(n), "Argument.N", "n must be a finite number but is: %d", n); if (n < 0) { return -1 * Math.round(-1 * n); } else { return Math.round(n); } } exports.roundSym = roundSym; /** * Stricter variant of parseFloat(). * @param value Input string * @return the float if the string is a valid float, NaN otherwise * @throws nothing */ function filterFloat(value) { if (/^(\-|\+)?([0-9]+(\.[0-9]+)?|Infinity)$/.test(value)) { return Number(value); } return NaN; } exports.filterFloat = filterFloat; /** * Modulo function that only returns a positive result, in contrast to the % operator * @param value * @param modulo * @throws timezonecomplete.Argument.Value if value is not finite * @throws timezonecomplete.Argument.Modulo if modulo is not a finite number >= 1 */ function positiveModulo(value, modulo) { (0, assert_1.default)(Number.isFinite(value), "Argument.Value", "value should be finite"); (0, assert_1.default)(Number.isFinite(modulo) && modulo >= 1, "Argument.Modulo", "modulo should be >= 1"); if (value < 0) { return ((value % modulo) + modulo) % modulo; } else { return value % modulo; } } exports.positiveModulo = positiveModulo; //# sourceMappingURL=math.js.map