UNPKG

nhb-toolbox

Version:

A versatile collection of smart, efficient, and reusable utility functions, classes and types for everyday development needs.

64 lines (63 loc) 2.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getNumbersInRange = getNumbersInRange; const basics_1 = require("../array/basics"); const primitives_1 = require("../guards/primitives"); const index_1 = require("../utils/index"); const basics_2 = require("./basics"); const guards_1 = require("./guards"); const helpers_1 = require("./helpers"); const prime_1 = require("./prime"); function getNumbersInRange(type = 'any', options) { const { getAsString = false, min = 0, max = 100, includeMin = true, includeMax = true, separator = ', ', multiplesOf, } = options || {}; let output = []; const _applyRangeOptions = (start, end) => { let startNumber = start; let endNumber = end; if (start > end) { [startNumber, endNumber] = [end, start]; } const numbers = []; for (let i = startNumber; i <= endNumber; i++) { if (i >= startNumber && i <= endNumber && (includeMin || i > startNumber) && (includeMax || i < endNumber)) { numbers.push(i); } } return numbers; }; if (type === 'prime' && !(0, primitives_1.isUndefined)(multiplesOf)) { console.warn('Warning: The "multiplesOf" option is ignored when the type is "prime"!'); } switch (type) { case 'random': output = (0, basics_1.shuffleArray)(_applyRangeOptions(min, max).map((n) => (0, basics_2.getRandomNumber)({ min: n, max: n, includeMin, includeMax, }))); break; case 'prime': output = _applyRangeOptions(min, max).filter(prime_1.isPrime); break; case 'odd': output = _applyRangeOptions(min, max).filter(guards_1.isOdd); break; case 'even': output = _applyRangeOptions(min, max).filter(guards_1.isEven); break; case 'natural': output = _applyRangeOptions(Math.max(min, 1), max); break; default: output = _applyRangeOptions(min, max); break; } if (type !== 'prime') { output = (0, helpers_1._applyMultiples)(output, multiplesOf); } return (getAsString ? (0, index_1.convertArrayToString)(output, { separator }) : output); }