main.js (2378B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 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 = '[\u0009\u000A\u000B\u000C\u000D\u0020\u0085\u00A0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]'; 29 30 31 // MAIN // 32 33 /** 34 * Returns a regular expression to match a white space character. 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 wrap a regular expression matching white space characters with a capture group 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_WHITESPACE = reWhitespace(); 45 * 46 * var bool = RE_WHITESPACE.test( ' ' ); 47 * // returns true 48 * 49 * bool = RE_WHITESPACE.test( '\t' ); 50 * // returns true 51 * 52 * @example 53 * var replace = require( '@stdlib/string/replace' ); 54 * var RE_WHITESPACE = reWhitespace({ 55 * 'capture': true 56 * }); 57 * 58 * var str = 'Duplicate capture'; 59 * var out = replace( str, RE_WHITESPACE, '$1$1' ); 60 * // returns 'Duplicate capture' 61 */ 62 function reWhitespace( options ) { 63 var opts; 64 var err; 65 if ( arguments.length > 0 ) { 66 opts = {}; 67 err = validate( opts, options ); 68 if ( err ) { 69 throw err; 70 } 71 if ( opts.capture ) { 72 return new RegExp( '('+REGEXP_STRING+')', opts.flags ); 73 } 74 return new RegExp( REGEXP_STRING, opts.flags ); 75 } 76 return /[\u0009\u000A\u000B\u000C\u000D\u0020\u0085\u00A0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200A\u2028\u2029\u202F\u205F\u3000\uFEFF]/; // eslint-disable-line no-control-regex 77 } 78 79 80 // EXPORTS // 81 82 module.exports = reWhitespace;