From b3d0995a65c2cca9c0132f14034123cbae93b2ff Mon Sep 17 00:00:00 2001 From: Alex Chan Date: Sun, 23 Feb 2020 17:32:21 +0000 Subject: [PATCH] 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. --- src/cli.js | 6 ++++++ src/generate/__tests__/index.js | 20 ++++++++++++++++++++ src/generate/index.js | 5 +++++ 3 files changed, 31 insertions(+) diff --git a/src/cli.js b/src/cli.js index b07cfb7..91212b1 100755 --- a/src/cli.js +++ b/src/cli.js @@ -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 => { diff --git a/src/generate/__tests__/index.js b/src/generate/__tests__/index.js index 5a8e3a1..15e5986 100644 --- a/src/generate/__tests__/index.js +++ b/src/generate/__tests__/index.js @@ -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() diff --git a/src/generate/index.js b/src/generate/index.js index 9112535..c5be533 100644 --- a/src/generate/index.js +++ b/src/generate/index.js @@ -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) }),