main.js (1733B)
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 contains = require( '@stdlib/assert/contains' ); 24 var posix = require( './../../filename-posix' ); 25 var win32 = require( './../../filename-windows' ); 26 var IS_WINDOWS = require( '@stdlib/assert/is-windows' ); 27 28 29 // VARIABLES // 30 31 var PLATFORMS = [ 'posix', 'win32' ]; 32 33 34 // MAIN // 35 36 /** 37 * Returns a regular expression to split a filename. 38 * 39 * @param {string} [platform] - path platform (`win32` or `posix`) 40 * @throws {Error} platform must be `win32` or `posix` 41 * @returns {RegExp} regular expression 42 * 43 * @example 44 * var RE_FILENAME = reFilename( 'posix' ); 45 * var parts = RE_FILENAME.exec( '/foo/bar/index.js' ).slice(); 46 * // returns [ '/foo/bar/index.js', '/', 'foo/bar/', 'index.js', '.js' ] 47 */ 48 function reFilename( platform ) { 49 if ( arguments.length > 0 ) { 50 if ( !contains( PLATFORMS, platform ) ) { 51 throw new Error( 'invalid argument. Platform must be either `posix` or `win32`. Value: `' + platform + '`.' ); 52 } 53 } 54 if ( platform === 'win32' || IS_WINDOWS ) { 55 return win32(); 56 } 57 // Case: platform === 'posix' 58 return posix(); 59 } 60 61 62 // EXPORTS // 63 64 module.exports = reFilename;