main.js (1834B)
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 = '\\r?\\n'; 29 30 31 // MAIN // 32 33 /** 34 * Returns a regular expression to match a newline character sequence. 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_EOL = reEOL(); 45 * var bool = RE_EOL.test( '\r\n' ); 46 * // returns true 47 * 48 * @example 49 * var replace = require( '@stdlib/string/replace' ); 50 * 51 * var RE_EOL = reEOL({ 52 * 'flags': 'g' 53 * }); 54 * var str = '1\n2\n3'; 55 * var out = replace( str, RE_EOL, '' ); 56 */ 57 function reEOL( options ) { 58 var opts; 59 var err; 60 if ( arguments.length > 0 ) { 61 opts = {}; 62 err = validate( opts, options ); 63 if ( err ) { 64 throw err; 65 } 66 if ( opts.capture ) { 67 return new RegExp( '('+REGEXP_STRING+')', opts.flags ); 68 } 69 return new RegExp( REGEXP_STRING, opts.flags ); 70 } 71 return /\r?\n/; 72 } 73 74 75 // EXPORTS // 76 77 module.exports = reEOL;