Fix table formatting when there are multiple lines

This commit is contained in:
Jeroen Engels 2016-03-06 17:53:11 +01:00
parent 1e21af531d
commit f9979731fb
2 changed files with 21 additions and 11 deletions

View file

@ -4,6 +4,14 @@ var _ = require('lodash/fp');
var formatBadge = require('./formatBadge'); var formatBadge = require('./formatBadge');
var formatContributor = require('./formatContributor'); var formatContributor = require('./formatContributor');
function injectContentBetween(lines, content, startIndex, endIndex) {
return [].concat(
lines.slice(0, startIndex),
content,
lines.slice(endIndex)
);
}
var injectBetweenTags = _.curry(function(tag, newContent, previousContent) { var injectBetweenTags = _.curry(function(tag, newContent, previousContent) {
var lines = previousContent.split('\n'); var lines = previousContent.split('\n');
var openingTagIndex = _.findIndex(_.startsWith('<!-- ALL-CONTRIBUTORS-' + tag + ':START '), lines); var openingTagIndex = _.findIndex(_.startsWith('<!-- ALL-CONTRIBUTORS-' + tag + ':START '), lines);
@ -11,17 +19,19 @@ var injectBetweenTags = _.curry(function(tag, newContent, previousContent) {
if (openingTagIndex === -1 || closingTagIndex === -1) { if (openingTagIndex === -1 || closingTagIndex === -1) {
return previousContent; return previousContent;
} }
return [].concat( return injectContentBetween(lines, newContent, openingTagIndex + 1, closingTagIndex)
lines.slice(0, openingTagIndex + 1), .join('\n');
newContent,
lines.slice(closingTagIndex)
).join('\n');
}); });
function formatLine(contributors) { function formatLine(contributors) {
return '| ' + contributors.join(' | ') + ' |'; return '| ' + contributors.join(' | ') + ' |';
} }
function createColumnLine(options, contributors) {
var nbColumns = Math.min(options.contributorsPerLine, contributors.length);
return _.repeat(nbColumns, '| :---: ') + '|';
}
function generateContributorsList(options, contributors) { function generateContributorsList(options, contributors) {
return _.flow( return _.flow(
_.map(function formatEveryContributor(contributor) { _.map(function formatEveryContributor(contributor) {
@ -29,11 +39,11 @@ function generateContributorsList(options, contributors) {
}), }),
_.chunk(options.contributorsPerLine), _.chunk(options.contributorsPerLine),
_.map(formatLine), _.map(formatLine),
_.join('\n'), function insertColumns(lines) {
function appendColumns(content) { var columnLine = createColumnLine(options, contributors);
var nbColumns = Math.min(options.contributorsPerLine, contributors.length); return injectContentBetween(lines, columnLine, 1, 1);
return content + '\n' + _.repeat(nbColumns, '| :---: ') + '|'; },
} _.join('\n')
)(contributors); )(contributors);
} }

View file

@ -81,8 +81,8 @@ test('should split contributors into multiples lines when there are too many', t
'These people contributed to the project:', 'These people contributed to the project:',
'<!-- ALL-CONTRIBUTORS-LIST:START -->', '<!-- ALL-CONTRIBUTORS-LIST:START -->',
'| Kent C. Dodds is awesome! | Kent C. Dodds is awesome! | Kent C. Dodds is awesome! | Kent C. Dodds is awesome! | Kent C. Dodds is awesome! |', '| Kent C. Dodds is awesome! | Kent C. Dodds is awesome! | Kent C. Dodds is awesome! | Kent C. Dodds is awesome! | Kent C. Dodds is awesome! |',
'| Kent C. Dodds is awesome! | Kent C. Dodds is awesome! |',
'| :---: | :---: | :---: | :---: | :---: |', '| :---: | :---: | :---: | :---: | :---: |',
'| Kent C. Dodds is awesome! | Kent C. Dodds is awesome! |',
'<!-- ALL-CONTRIBUTORS-LIST:END -->', '<!-- ALL-CONTRIBUTORS-LIST:END -->',
'', '',
'Thanks a lot guys!' 'Thanks a lot guys!'