utils/number.js

import _ from 'lodash';

/**
 * Utilities for working with numbers.
 * @module NumberUtils
 */
const NumberUtils = {
    /**
     * Rounds a given number down to the nearest specified number.
     * @static
     * @function roundDownToNearestStep
     * @param {Number} num - The number to round.
     * @param {Number} step - The step to round down to.
     * @example
     * NumberUtils.roundDownToNearestStep(57, 30); // 30
     * NumberUtils.roundDownToNearestStep(161, 15); // 150
     * @returns {Number} - The rounded number.
     */
    roundDownToNearestStep(num, step) {
        if (!_.isNumber(num)) {
            throw new Error('You can only round numbers.');
        }

        if (_.isUndefined(step)) {
            throw new Error('You must provide a step to round down to.');
        }

        return step * Math.floor(num / step);
    }
};

export default NumberUtils;