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