/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* 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.
*/

'use strict';

// MODULES //

var isBuffer = require( '@stdlib/assert/is-buffer' );
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var string2buffer = require( '@stdlib/buffer/from-string' );
var SHEBANGS = require( './shebangs.js' );


// MAIN //

/**
* Inserts a license header into a source code string.
*
* @param {(string|Buffer)} src - source code
* @param {(string|Buffer)} header - license header
* @throws {TypeError} first argument must be either a string or Buffer
* @throws {TypeError} second argument must be either a string or Buffer
* @returns {(string|Buffer)} transformed source code string
*
* @example
* var licenseHeader = require( '@stdlib/_tools/licenses/header' );
*
* var opts = {
*     'year': 2018,
*     'copyright': 'The Stdlib Authors'
* };
*
* var h = licenseHeader( 'Apache-2.0', 'js', opts );
* // returns <string>
*
* var src = [
*     '"use strict";',
*     '',
*     'var x = 3.14;',
*     ''
* ];
*
* var out = insertHeader( src.join( '\n' ), h );
* // returns <string>
*/
function insertHeader( src, header ) {
	var isStr;
	var line;
	var len;
	var i;

	isStr = isString( src );
	if ( !isStr && !isBuffer( src ) ) {
		throw new TypeError( 'invalid input argument. First argument must be either a string or Buffer. Value: `' + src + '`.' );
	} else {
		src = src.toString();
	}
	if ( !isString( header ) && !isBuffer( header ) ) {
		throw new TypeError( 'invalid input argument. Second argument must be either a string or Buffer. Value: `' + header + '`.' );
	} else {
		header = header.toString();
	}
	// Check for a "shebang"; if one is present, insert the header after the shebang...
	len = SHEBANGS.length;
	for ( i = 0; i < len; i++ ) {
		line = SHEBANGS[ i ][ 0 ];
		if ( src.substring( 0, line.length ) === line ) {
			break;
		}
	}
	// If no shebang, then prepend the header; otherwise, insert the header...
	if ( i === len ) {
		src = header + '\n' + src;
	} else {
		src = line + '\n' + SHEBANGS[i][1] + header + src.substring( line.length );
	}
	// If provided a source code buffer, return a source code buffer...
	if ( !isStr ) {
		return string2buffer( src );
	}
	return src;
} // end FUNCTION insertHeader()


// EXPORTS //

module.exports = insertHeader;

/**
* @license Apache-2.0
*
* Copyright (c) 2018 The Stdlib Authors.
*
* 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.
*/

'use strict';

// MODULES //

var isBuffer = require( '@stdlib/assert/is-buffer' );
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
var string2buffer = require( '@stdlib/buffer/from-string' );
var SHEBANGS = require( './shebangs.js' );


// MAIN //

/**
* Inserts a license header into a source code string.
*
* @param {(string|Buffer)} src - source code
* @param {(string|Buffer)} header - license header
* @throws {TypeError} first argument must be either a string or Buffer
* @throws {TypeError} second argument must be either a string or Buffer
* @returns {(string|Buffer)} transformed source code
*
* @example
* var licenseHeader = require( '@stdlib/_tools/licenses/header' );
*
* var opts = {
*     'year': 2018,
*     'copyright': 'The Stdlib Authors'
* };
*
* var h = licenseHeader( 'Apache-2.0', 'js', opts );
* // returns <string>
*
* var src = [
*     '"use strict";',
*     '',
*     'var x = 3.14;',
*     ''
* ];
*
* var out = insertHeader( src.join( '\n' ), h );
* // returns <string>
*/
function insertHeader( src, header ) {
	var isStr;
	var line;
	var len;
	var i;

	isStr = isString( src );
	if ( !isStr && !isBuffer( src ) ) {
		throw new TypeError( 'invalid input argument. First argument must be either a string or Buffer. Value: `' + src + '`.' );
	} else {
		src = src.toString();
	}
	if ( !isString( header ) && !isBuffer( header ) ) {
		throw new TypeError( 'invalid input argument. Second argument must be either a string or Buffer. Value: `' + header + '`.' );
	} else {
		header = header.toString();
	}
	// Check for a "shebang"; if one is present, insert the header after the shebang...
	len = SHEBANGS.length;
	for ( i = 0; i < len; i++ ) {
		line = SHEBANGS[ i ][ 0 ];
		if ( src.substring( 0, line.length ) === line ) {
			break;
		}
	}
	// If no shebang, then prepend the header; otherwise, insert the header...
	if ( i === len ) {
		src = header + '\n' + src;
	} else {
		src = line + '\n' + SHEBANGS[i][1] + header + src.substring( line.length );
	}
	// If provided a source code buffer, return a source code buffer...
	if ( !isStr ) {
		return string2buffer( src );
	}
	return src;
} // end FUNCTION insertHeader()


// EXPORTS //

module.exports = insertHeader;

Update return value description