Skip to content

Commit 1a4460f

Browse files
committed
refactor: hoist typeof schema === "function"
1 parent 12ce122 commit 1a4460f

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/index.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@ class Field<T> {
4444
) {
4545
this.$_valueRef = isRef(value) ? value : shallowRef(value);
4646

47-
const initialSchema = typeof schema === "function" ? schema() : schema;
47+
const isSchemaTypeFunction = typeof schema === "function";
48+
const initialSchema = isSchemaTypeFunction ? schema() : schema;
4849
const isNumberSchemaField = initialSchema instanceof NumberSchema;
4950
this.$_errorRef = computed(
5051
schema
5152
? () => {
52-
const obtainedSchema =
53-
typeof schema === "function" ? schema() : schema;
53+
const obtainedSchema = isSchemaTypeFunction ? schema() : schema;
5454

5555
// Changing `<input type="number" v-model.number="foo" />` to blank, `foo` is set to empty string.
5656
const targetValue =
@@ -117,11 +117,13 @@ class FormsField<T extends (arg: any) => Form> {
117117
this.$_formsRef = shallowRef([]);
118118
this.$initialize(initialValueListOrLength);
119119

120+
const isSchemaTypeFunction = typeof schema === "function";
120121
this.$_errorRef = computed(
121122
schema
122123
? () => {
123-
const obtainedSchema =
124-
typeof schema === "function" ? schema(new ArraySchema()) : schema;
124+
const obtainedSchema = isSchemaTypeFunction
125+
? schema(new ArraySchema())
126+
: schema;
125127

126128
try {
127129
obtainedSchema.validateSync(
@@ -137,8 +139,9 @@ class FormsField<T extends (arg: any) => Form> {
137139
: () => undefined
138140
);
139141

140-
const initialSchema =
141-
typeof schema === "function" ? schema(new ArraySchema()) : schema;
142+
const initialSchema = isSchemaTypeFunction
143+
? schema(new ArraySchema())
144+
: schema;
142145
this.$label = initialSchema?.describe().label || "";
143146
}
144147

0 commit comments

Comments
 (0)