UNPKG

@simongrasinovski/str-utils

Version:

A lightweight utility library for effortless string manipulation. Simplify common tasks like case validation, conversion and formatting with easy-to-use functions that enhance your JavaScript applications

29 lines (27 loc) 1.2 kB
"use strict"; var _require = require('./utils'), ensureString = _require.ensureString; var _require2 = require('./constants'), DECIMALS_REGEX = _require2.DECIMALS_REGEX, INTEGERS_REGEX = _require2.INTEGERS_REGEX; /** * Extracts numbers (either integers or decimals) from a string. * * @function * @param {string} input - The string to analyze. * @param {object} [options={}] - An optional configuration object. * @param {boolean} [options.decimals=false] - If true, extracts decimal numbers; if false, extracts only integers. * @returns {Array|null} Array of matching numbers, or null if no numbers found. * * @throws {TypeError} Throws an error if the input is not a string. * * @example * const result = exctractNumber('hello 123 times 98.75 from the 456 world 78.9', { decimals: false }); * console.log(result); // ['123', '456'] */ module.exports = ensureString(function (input) { var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; var _options$decimals = options.decimals, decimals = _options$decimals === void 0 ? false : _options$decimals; return input.match(decimals ? DECIMALS_REGEX : INTEGERS_REGEX); });