feat(alpha-sort): Add a parameter contributorsSortAlphabetically (#249)

This parameter allows the user to sort the list of contributors in
alphabetical order of their name.
This commit is contained in:
Alex Chan 2020-02-23 17:32:21 +00:00 committed by GitHub
parent 8dd551a3d3
commit b3d0995a65
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 0 deletions

View file

@ -35,6 +35,12 @@ const yargv = yargs
.boolean('commit')
.default('files', ['README.md'])
.default('contributorsPerLine', 7)
.option('contributorsSortAlphabetically', {
type: 'boolean',
default: false,
description:
'Sort the list of contributors alphabetically in the generated list',
})
.default('contributors', [])
.default('config', defaultRCFile)
.config('config', configPath => {

View file

@ -60,6 +60,26 @@ test('split contributors into multiples lines when there are too many', () => {
expect(result).toMatchSnapshot()
})
test('sorts the list of contributors if contributorsSortAlphabetically=true', () => {
const {kentcdodds, bogas04} = contributors
const {options, jfmengels, content} = fixtures()
const resultPreSorted = generate(
options,
[bogas04, jfmengels, kentcdodds],
content,
)
options.contributorsSortAlphabetically = true
const resultAutoSorted = generate(
options,
[jfmengels, kentcdodds, bogas04],
content,
)
expect(resultPreSorted).toEqual(resultAutoSorted)
})
test('not inject anything if there is no tags to inject content in', () => {
const {kentcdodds} = contributors
const {options} = fixtures()

View file

@ -45,6 +45,11 @@ function formatLine(contributors) {
function generateContributorsList(options, contributors) {
return _.flow(
_.sortBy(function(contributor) {
if (options.contributorsSortAlphabetically) {
return contributor.name
}
}),
_.map(function formatEveryContributor(contributor) {
return formatContributor(options, contributor)
}),