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 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 lines = previousContent.split('\n');
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) {
return previousContent;
}
return [].concat(
lines.slice(0, openingTagIndex + 1),
newContent,
lines.slice(closingTagIndex)
).join('\n');
return injectContentBetween(lines, newContent, openingTagIndex + 1, closingTagIndex)
.join('\n');
});
function formatLine(contributors) {
return '| ' + contributors.join(' | ') + ' |';
}
function createColumnLine(options, contributors) {
var nbColumns = Math.min(options.contributorsPerLine, contributors.length);
return _.repeat(nbColumns, '| :---: ') + '|';
}
function generateContributorsList(options, contributors) {
return _.flow(
_.map(function formatEveryContributor(contributor) {
@ -29,11 +39,11 @@ function generateContributorsList(options, contributors) {
}),
_.chunk(options.contributorsPerLine),
_.map(formatLine),
_.join('\n'),
function appendColumns(content) {
var nbColumns = Math.min(options.contributorsPerLine, contributors.length);
return content + '\n' + _.repeat(nbColumns, '| :---: ') + '|';
}
function insertColumns(lines) {
var columnLine = createColumnLine(options, contributors);
return injectContentBetween(lines, columnLine, 1, 1);
},
_.join('\n')
)(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:',
'<!-- 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! |',
'<!-- ALL-CONTRIBUTORS-LIST:END -->',
'',
'Thanks a lot guys!'