Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/**
* @license Apache-2.0
*
Expand All @@ -24,6 +23,7 @@
var filter = require( '@stdlib/array/base/filter' );
var trim = require( '@stdlib/string/trim' );
var map = require( '@stdlib/utils/map' );
var reEOL = require( '@stdlib/regexp/eol' );
var collectField = require( './collect_field.js' );
var sectionStart = require( './section_start.js' );
var sectionEnd = require( './section_end.js' );
Expand All @@ -33,10 +33,51 @@ var heading = require( './heading.js' );
// VARIABLES //

var STDLIB_GITHUB_URL = 'https://github.com/stdlib-js/stdlib/commit';
var RE_YAML_DELIMITER = /^\s*---\s*$/;


// FUNCTIONS //

/**
* Processes commit message text by removing YAML blocks and squashing multiple empty lines into a single empty line.
*
* @private
* @param {Array<string>} lines - text lines to process
* @returns {Array<string>} processed commit message lines
*/
function stripYamlBlocks( lines ) {
var previousLineEmpty;
var inYaml;
var out;
var i;

previousLineEmpty = false;
inYaml = false;
out = [];
for ( i = 0; i < lines.length; i++ ) {
// Check for YAML delimiter (---) with optional whitespace:
if ( RE_YAML_DELIMITER.test( lines[ i ] ) ) {
inYaml = !inYaml; // Toggle YAML block state
continue;
}
// Only include lines that are not part of YAML blocks:
if ( inYaml ) {
continue;
}
if ( trim( lines[ i ] ) === '' ) {
// Only add an empty line if the previous line wasn't empty:
if ( !previousLineEmpty && out.length > 0 ) {
previousLineEmpty = true;
out.push( '' );
}
} else {
previousLineEmpty = false;
out.push( lines[ i ] ); // Preserve original line formatting
}
}
return out;
}

/**
* Formats a breaking change.
*
Expand All @@ -54,13 +95,26 @@ var STDLIB_GITHUB_URL = 'https://github.com/stdlib-js/stdlib/commit';
* // returns '- [`abcdef1`](https://github.com/stdlib-js/stdlib/commit/abcdef1234567890): beep'
*/
function formatBreakingChange( note ) {
var parts = note.text.split( '\n' );
var hash = trim( note.hash );
var out = '- [`'+hash.substring( 0, 7 )+'`]('+STDLIB_GITHUB_URL+'/'+hash+'): '+parts[ 0 ];
if ( parts.length > 1 ) {
out +='\n\n';
out += ' - ';
out += parts.slice( 1 ).join( '\n ' );
var contentLines;
var textLines;
var firstLine;
var restLines;
var hash;
var out;

textLines = note.text.split( reEOL.REGEXP );
contentLines = stripYamlBlocks( textLines );

// If we have content, first element is the first line:
firstLine = contentLines[ 0 ] || '';
restLines = contentLines.slice( 1 );
hash = trim( note.hash );

// Construct output string:
out = '- [`' + hash.substring( 0, 7 ) + '`](' + STDLIB_GITHUB_URL + '/' + hash + '): ' + firstLine;
if ( restLines.length > 0 ) {
out += '\n\n';
out += ' - ' + restLines.join( '\n ' );
out += '\n';
}
return out;
Expand Down Expand Up @@ -88,18 +142,18 @@ function isBreakingChange( note ) {
* @returns {string} list of breaking changes
*/
function breakingChanges( commits ) {
var breakingChanges;
var changes;
var notes;
var out;

notes = collectField( commits, 'notes' );
breakingChanges = filter( notes, isBreakingChange );
if ( breakingChanges.length === 0 ) {
changes = filter( notes, isBreakingChange );
if ( changes.length === 0 ) {
return '';
}
out = sectionStart( 'breaking-changes' );
out += heading( 'BREAKING CHANGES', 3 );
out += map( breakingChanges, formatBreakingChange ).join( '\n' );
out += map( changes, formatBreakingChange ).join( '\n' );
out += sectionEnd( 'breaking-changes' );
return out;
}
Expand Down