Refactor option category grouping

master
Simon Lydell 2017-09-12 17:14:10 +02:00 committed by Ika
parent c2847d90cd
commit 2b9e1f80c5
1 changed files with 9 additions and 6 deletions

View File

@ -342,12 +342,7 @@ function createUsage() {
.apply([], optionsWithOpposites)
.filter(Boolean);
const groupedOptions = flattenedOptions.reduce((current, option) => {
const category = option.category;
const group = (current[category] = current[category] || []);
group.push(option);
return current;
}, {});
const groupedOptions = groupBy(flattenedOptions, option => option.category);
const usageSummary = "Usage: prettier [opts] [filename ...]";
@ -413,6 +408,14 @@ function indent(str, spaces) {
return str.replace(/^/gm, " ".repeat(spaces));
}
function groupBy(array, getKey) {
return array.reduce((obj, item) => {
const key = getKey(item);
const previousItems = key in obj ? obj[key] : [];
return Object.assign({}, obj, { [key]: previousItems.concat(item) });
}, Object.create(null));
}
function normalizeArgv(rawArgv, options) {
options = options || {};