refactor: extract normalize function

master
ikatyang 2017-09-08 10:57:29 +08:00
parent 0981de9790
commit 8939ee45db
2 changed files with 29 additions and 20 deletions

View File

@ -1,6 +1,8 @@
"use strict";
const options = sortAndAddNameKey({
const normalizer = require("./cli-normalizer");
const options = normalizer.normalizeDetailOptions({
"bracket-spacing": {
type: "boolean",
isFormatOption: true,
@ -188,31 +190,29 @@ const options = sortAndAddNameKey({
}
});
const optionArray = Object.keys(options).map(name => options[name]);
const booleanOptionNames = optionArray
const booleanOptionNames = options
.filter(option => option.isFormatOption && option.type === "boolean")
.map(option => option.name);
const stringOptionNames = optionArray
const stringOptionNames = options
.filter(option => option.isFormatOption && option.type !== "boolean")
.map(option => option.name);
const minimistOptions = {
boolean: optionArray
boolean: options
.filter(option => !option.isDocsOnly && option.type === "boolean")
.map(option => option.name),
string: optionArray
string: options
.filter(option => !option.isDocsOnly && option.type !== "boolean")
.map(option => option.name),
default: optionArray
default: options
.filter(option => option.default !== undefined)
.reduce(
(current, option) =>
Object.assign({ [option.name]: option.default }, current),
{}
),
alias: optionArray
alias: options
.filter(option => option.alias !== undefined)
.reduce(
(current, option) =>
@ -232,7 +232,7 @@ Usage: prettier [opts] [filename ...]
Available options:
${indent(
optionArray
options
.filter(option => !option.isHidden)
.map(createOptionUsage)
.join("\n"),
@ -282,16 +282,6 @@ function dedent(str) {
return str.replace(new RegExp(`^ {${spaces}}`, "gm"), "").trim();
}
function sortAndAddNameKey(obj) {
return Object.keys(obj)
.sort()
.reduce(
(current, name) =>
Object.assign(current, { [name]: Object.assign({ name }, obj[name]) }),
{}
);
}
module.exports = {
booleanOptionNames,
stringOptionNames,

19
src/cli-normalizer.js Normal file
View File

@ -0,0 +1,19 @@
"use strict";
function normalizeDetailOptions(detailOptions) {
const names = Object.keys(detailOptions).sort();
const normaliezdOptions = names.map(name =>
Object.assign({ name }, detailOptions[name])
);
normaliezdOptions.forEach(normalizedOption => {
normaliezdOptions[normalizedOption.name] = normalizedOption;
});
return normaliezdOptions;
}
module.exports = {
normalizeDetailOptions
};