Regular Expression Table

Generate an object mapping file types to license header regular expressions.

Usage

var createTable = require( '@stdlib/_tools/licenses/header-regexp-table' );

createTable( license[, types] )

Returns an object mapping file types to license header regular expressions.

var out = createTable( 'Apache-2.0' );
// returns {...}

By default, the function returns an object whose keys are filename extensions of known supported file types. To specify a custom list, provide a types argument.

var out = createTable( 'Apache-2.0', [ 'js', 'julia' ] );
// returns {...}

var re = out.js;
// returns <RegExp>

re = out.julia;
// returns <RegExp>

If provided the file type value 'default', the function generates a regular expression capable of matching multiple file types based on common comment markers.

var out = createTable( 'Apache-2.0', [ 'default' ] );
// returns {...}

var re = out.default;
// returns <RegExp>
/(?:[*!#] |)@license Apache\-2\.0
(?:[*!#]|)
(?:[*!#] |)Copyright \(c\) [0-9]{1,4} .+\.
(?:[*!#]|)
(?:[*!#] |)Licensed under the Apache License, Version 2\.0 \(the "License"\);
(?:[*!#] |)you may not use this file except in compliance with the License\.
(?:[*!#] |)You may obtain a copy of the License at
(?:[*!#]|)
(?:[*!#] |)   http:\/\/www\.apache\.org\/licenses\/LICENSE\-2\.0
(?:[*!#]|)
(?:[*!#] |)Unless required by applicable law or agreed to in writing, software
(?:[*!#] |)distributed under the License is distributed on an "AS IS" BASIS,
(?:[*!#] |)WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied\.
(?:[*!#] |)See the License for the specific language governing permissions and
(?:[*!#] |)limitations under the License\./g

Notes

  • If not provided a types argument, the returned object contains a default regular expression capable of matching license headers in multiple languages.

Examples

var readFileSync = require( '@stdlib/fs/read-file' ).sync;
var extname = require( '@stdlib/utils/extname' );
var createTable = require( '@stdlib/_tools/licenses/header-regexp-table' );

// Generate a license header regular expression table:
var table = createTable( 'Apache-2.0' );

// Read the current file:
var fopts = {
    'encoding': 'utf8'
};
var file = readFileSync( __filename, fopts );

// Determine the filename extension:
var ext = extname( __filename ).substring( 1 ); // omit the leading `.`

// Test if the file has a license header:
console.log( table[ ext ].test( file ) );
// => <boolean>