refactor: extract normalize function
parent
0981de9790
commit
8939ee45db
|
@ -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,
|
||||||
|
|
|
@ -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
|
||||||
|
};
|
Loading…
Reference in New Issue