fix: add locale option and sort using locale (closes #349)

This commit is contained in:
Martin Kopeček 2023-01-27 13:54:46 +01:00
parent 96534ec59b
commit 408a0f93dd
2 changed files with 29 additions and 10 deletions

View file

@ -22,12 +22,22 @@ const yargv = yargs
.alias('v', 'version') .alias('v', 'version')
.version() .version()
.recommendCommands() .recommendCommands()
.command('generate', `Generate the list of contributors\n\nUSAGE: all-contributors generate`) .command(
.command('add', `Add a new contributor\n\nUSAGE: all-contributors add <username> <comma-separated contributions>`) 'generate',
.command('init', `Prepare the project to be used with this tool\n\nUSAGE: all-contributors init`) `Generate the list of contributors\n\nUSAGE: all-contributors generate`,
)
.command(
'add',
`Add a new contributor\n\nUSAGE: all-contributors add <username> <comma-separated contributions>`,
)
.command(
'init',
`Prepare the project to be used with this tool\n\nUSAGE: all-contributors init`,
)
.command( .command(
'check', 'check',
`Compare contributors from the repository with the ones credited in .all-contributorsrc'\n\nUSAGE: all-contributors check`) `Compare contributors from the repository with the ones credited in .all-contributorsrc'\n\nUSAGE: all-contributors check`,
)
.boolean('commit') .boolean('commit')
.default('files', ['README.md']) .default('files', ['README.md'])
.default('contributorsPerLine', 7) .default('contributorsPerLine', 7)
@ -37,6 +47,12 @@ const yargv = yargs
description: description:
'Sort the list of contributors alphabetically in the generated list', 'Sort the list of contributors alphabetically in the generated list',
}) })
.option('contributorsSortLocale', {
type: 'string',
default: undefined,
description:
'Locale to use for sorting the contributors alphabetically in the generated list',
})
.default('contributors', []) .default('contributors', [])
.default('config', defaultRCFile) .default('config', defaultRCFile)
.config('config', configPath => { .config('config', configPath => {

View file

@ -69,18 +69,21 @@ function generateContributorsList(options, contributors) {
let tableFooterContent = '' let tableFooterContent = ''
return _.flow( return _.flow(
_.sortBy(contributor => { c => {
if (options.contributorsSortAlphabetically) { if (options.contributorsSortAlphabetically) {
return contributor.name c.sort((a, b) =>
a.name.localeCompare(b.name, options.contributorsSortLocale),
)
} }
}), return c
},
_.map(function formatEveryContributor(contributor) { _.map(function formatEveryContributor(contributor) {
return formatContributor(options, contributor) return formatContributor(options, contributor)
}), }),
_.chunk(options.contributorsPerLine), _.chunk(options.contributorsPerLine),
_.map((currentLineContributors) => formatLine( _.map(currentLineContributors =>
options, currentLineContributors formatLine(options, currentLineContributors),
)), ),
_.join('\n </tr>\n <tr>\n '), _.join('\n </tr>\n <tr>\n '),
newContent => { newContent => {
if (options.linkToUsage) { if (options.linkToUsage) {