main.js (1898B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2021 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var validate = require( './validate.js' ); 24 25 26 // VARIABLES // 27 28 var REGEXP_STRING = '[-+]{0,1}[0-9]*\\.[0-9]+'; 29 30 31 // MAIN // 32 33 /** 34 * Returns a regular expression to match a decimal number. 35 * 36 * @param {Options} [options] - function options 37 * @param {string} [options.flags=''] - regular expression flags 38 * @param {boolean} [options.capture=false] - boolean indicating whether to create a capture group for the match 39 * @throws {TypeError} options argument must be an object 40 * @throws {TypeError} must provide valid options 41 * @returns {RegExp} regular expression 42 * 43 * @example 44 * var RE_DECIMAL_NUMBER = reDecimalNumber(); 45 * 46 * var bool = RE_DECIMAL_NUMBER.test( 'beep 1.0 boop' ); 47 * // returns true 48 * 49 * @example 50 * var RE_DECIMAL_NUMBER = reDecimalNumber({ 51 * 'flags': 'gm' 52 * }); 53 * var bool = RE_DECIMAL_NUMBER.test( 'beep 1.0 boop' ); 54 * // returns true 55 */ 56 function reDecimalNumber( options ) { 57 var opts; 58 var err; 59 if ( arguments.length > 0 ) { 60 opts = {}; 61 err = validate( opts, options ); 62 if ( err ) { 63 throw err; 64 } 65 if ( opts.capture ) { 66 return new RegExp( '('+REGEXP_STRING+')', opts.flags ); 67 } 68 return new RegExp( REGEXP_STRING, opts.flags ); 69 } 70 return /[-+]{0,1}[0-9]*\.[0-9]+/; 71 } 72 73 74 // EXPORTS // 75 76 module.exports = reDecimalNumber;