diff --git a/src/cli-util.js b/src/cli-util.js index 4ac2e3e3..c5bccb69 100644 --- a/src/cli-util.js +++ b/src/cli-util.js @@ -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 || {};