Skip to content

Commit a07527e

Browse files
committed
update
1 parent 0e10b52 commit a07527e

File tree

8 files changed

+495
-432
lines changed

8 files changed

+495
-432
lines changed
Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
1-
import { describe, test } from "vitest"
2-
import type { Equal, Expect } from '@type-challenges/utils'
3-
import { AutoStore, computed, ComputedState, configurable, s, SchemaDescriptorBuilder } from "../../src"
4-
1+
import { describe, test } from "vitest";
2+
import type { Equal, Expect } from "@type-challenges/utils";
3+
import { type ComputedState, configurable, s, type SchemaDescriptorBuilder } from "../../src";
54

65
describe("ComputedState", () => {
7-
8-
test("Schmea计算状态", () => {
9-
const state = {
10-
order: {
11-
price: s.number(100, (val: any) => val > 10),
12-
count: configurable(100, (val: any) => val > 10)
13-
}
14-
}
15-
type RawType = typeof state
16-
type PriceType = RawType['order']['price']
17-
type CountType = RawType['order']['count']
18-
type isSchemaType = PriceType extends (...args: any[]) => any ? true : false
19-
type isSchemasType = PriceType extends SchemaDescriptorBuilder<infer X, any> ? X : false
20-
type dstate = ComputedState<RawType>
21-
22-
23-
})
24-
})
6+
test("Schmea计算状态", () => {
7+
const state = {
8+
order: {
9+
price: s.number(100, {
10+
onValidate: (val: any) => val > 10,
11+
}),
12+
count: configurable(100, {
13+
onValidate: (val: any) => val > 10,
14+
}),
15+
},
16+
};
17+
type RawType = typeof state;
18+
type PriceType = RawType["order"]["price"];
19+
type CountType = RawType["order"]["count"];
20+
type isSchemaType = PriceType extends (...args: any[]) => any ? true : false;
21+
type isSchemasType = PriceType extends SchemaDescriptorBuilder<infer X, any> ? X : false;
22+
type dstate = ComputedState<RawType>;
23+
});
24+
});

packages/core/src/schema/schema.ts

Lines changed: 81 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -25,39 +25,31 @@
2525
*
2626
*/
2727

28-
import { VALUE_SCHEMA_BUILDER_FLAG } from '../consts';
29-
import { forEachObject, isFunction, isPlainObject } from '../utils';
30-
import { markRaw } from '../utils/markRaw';
28+
import { VALUE_SCHEMA_BUILDER_FLAG } from "../consts";
29+
import { forEachObject, isFunction, isPlainObject } from "../utils";
30+
import { markRaw } from "../utils/markRaw";
3131

32-
import type {
33-
SchemaBuilder,
34-
SchemaDescriptorBuilder,
35-
SchemaOptions,
36-
SchemaValidator,
37-
} from './types';
32+
import type { SchemaBuilder, SchemaDescriptorBuilder, SchemaOptions, SchemaValidator } from "./types";
3833

3934
type SchemaArgs = {
40-
value: any;
41-
options: SchemaOptions;
42-
validator: SchemaValidator;
35+
value: any;
36+
options: SchemaOptions;
37+
validator: SchemaValidator;
4338
};
4439

4540
/**
4641
* 将options里面的on和render开头的函数标识为raw
47-
* @param options
42+
* @param options
4843
*/
4944
function markRawOptions(options: SchemaOptions) {
50-
if (isPlainObject(options)) {
51-
forEachObject(options, ({ value, key, parent }) => {
52-
if (
53-
(isFunction(value) &&
54-
(key.startsWith('on') || key.startsWith('render') || key.startsWith('to')))
55-
) {
56-
// @ts-ignore
57-
parent[key] = markRaw(value);
58-
}
59-
});
60-
}
45+
if (isPlainObject(options)) {
46+
forEachObject(options, ({ value, key, parent }) => {
47+
if (isFunction(value) && (key.startsWith("on") || key.startsWith("render") || key.startsWith("to"))) {
48+
// @ts-expect-error
49+
parent[key] = markRaw(value);
50+
}
51+
});
52+
}
6153
}
6254

6355
/**
@@ -69,83 +61,82 @@ function markRawOptions(options: SchemaOptions) {
6961
* 参数解析规则:
7062
*/
7163
function parseSchemaOptions(args: any[]): SchemaArgs {
72-
const finalArgs: any = {
73-
value: args[0],
74-
options: Object.assign(
75-
{
76-
onFail: 'throw-pass',
77-
},
78-
args[1],
79-
),
80-
};
81-
// 自动推断widget类型,但是如果指定了toInput参数,则不需要自动推断
82-
// 因为无法判断toInput对值进行如何转换,所以不需要自动推断widget类型
83-
if (!finalArgs.options.widget && typeof finalArgs.options.toInput !== 'function') {
84-
const datatype = typeof finalArgs.value;
85-
if (datatype === 'boolean') {
86-
finalArgs.options.widget = 'checkbox';
87-
} else if (datatype === 'number') {
88-
finalArgs.options.widget = 'number';
89-
}
90-
if (Array.isArray(finalArgs.options.select)) {
91-
finalArgs.options.widget = 'select';
92-
}
93-
}
94-
if ('onValidate' in finalArgs.options) {
95-
markRaw(finalArgs.options.onValidate);
96-
}
97-
markRawOptions(finalArgs.options);
98-
return finalArgs as SchemaArgs;
64+
const finalArgs: any = {
65+
value: args[0],
66+
options: Object.assign(
67+
{
68+
onFail: "throw-pass",
69+
},
70+
args[1],
71+
),
72+
};
73+
// 自动推断widget类型,但是如果指定了toInput参数,则不需要自动推断
74+
// 因为无法判断toInput对值进行如何转换,所以不需要自动推断widget类型
75+
if (!finalArgs.options.widget && typeof finalArgs.options.toInput !== "function") {
76+
const datatype = typeof finalArgs.value;
77+
if (datatype === "boolean") {
78+
finalArgs.options.widget = "checkbox";
79+
} else if (datatype === "number") {
80+
finalArgs.options.widget = "number";
81+
}
82+
if (Array.isArray(finalArgs.options.select)) {
83+
finalArgs.options.widget = "select";
84+
}
85+
}
86+
if ("onValidate" in finalArgs.options) {
87+
markRaw(finalArgs.options.onValidate);
88+
}
89+
markRawOptions(finalArgs.options);
90+
return finalArgs as SchemaArgs;
9991
}
10092

10193
export const schema = function () {
102-
const [initial, options] =
103-
arguments.length === 1 && typeof arguments[0] === 'object'
104-
? [undefined, arguments[0]]
105-
: [...arguments];
106-
const args = parseSchemaOptions([initial, options]);
107-
const value = initial;
108-
const datatype = Array.isArray(value) ? 'array' : typeof value;
109-
const builder = () => ({
110-
value,
111-
datatype,
112-
options: args.options,
113-
});
114-
builder[VALUE_SCHEMA_BUILDER_FLAG] = true;
115-
return builder as SchemaDescriptorBuilder;
94+
// const [initial, options] =
95+
// arguments.length === 1 && typeof arguments[0] === "object" && !Array.isArray(arguments[0])
96+
// ? [undefined, arguments[0]]
97+
// : [...arguments];
98+
const initial = arguments[0];
99+
const options = arguments[1];
100+
const args = parseSchemaOptions([initial, options]);
101+
const value = initial;
102+
const datatype = Array.isArray(value) ? "array" : typeof value;
103+
if (typeof value === "object") {
104+
markRaw(value);
105+
}
106+
const builder = () => ({
107+
value,
108+
datatype,
109+
options: args.options,
110+
});
111+
builder[VALUE_SCHEMA_BUILDER_FLAG] = true;
112+
return builder as SchemaDescriptorBuilder;
116113
} as unknown as SchemaBuilder;
117114

118115
export const configurable = schema;
119116
export const c = schema;
120117

121-
export function createTypeSchemaBuilder<Value = any>(
122-
isValid: (val: any) => boolean,
123-
defaultTips: string,
124-
) {
125-
const typeSchema = function () {
126-
const opts = Object.assign({}, arguments[1]);
127-
if (typeof opts.onValidate !== 'function') {
128-
opts.onValidate = isValid;
129-
}
130-
if (!opts.invalidTips) {
131-
opts.invalidTips = defaultTips;
132-
}
133-
return schema(arguments[0], opts);
134-
};
135-
return typeSchema as SchemaBuilder<Value>;
118+
export function createTypeSchemaBuilder<Value = any>(isValid: (val: any) => boolean, defaultTips: string) {
119+
const typeSchema = function () {
120+
const opts = Object.assign({}, arguments[1]);
121+
if (typeof opts.onValidate !== "function") {
122+
opts.onValidate = isValid;
123+
}
124+
if (!opts.invalidTips) {
125+
opts.invalidTips = defaultTips;
126+
}
127+
return schema(arguments[0], opts);
128+
};
129+
return typeSchema as SchemaBuilder<Value>;
136130
}
137131

138132
export const schemas = {
139-
number: createTypeSchemaBuilder<number>((val) => typeof val === 'number', 'must be a number'),
140-
string: createTypeSchemaBuilder<string>((val) => typeof val === 'string', 'must be a string'),
141-
boolean: createTypeSchemaBuilder<boolean>(
142-
(val) => typeof val === 'boolean',
143-
'must be a boolean',
144-
),
145-
date: createTypeSchemaBuilder<Date>((val) => val instanceof Date, 'must be a date'),
146-
bigint: createTypeSchemaBuilder<bigint>((val) => typeof val === 'bigint', 'must be a bigint'),
147-
array: createTypeSchemaBuilder<any[]>((val) => Array.isArray(val), 'must be an array'),
148-
object: createTypeSchemaBuilder<object>((val) => typeof val === 'object', 'must be an object'),
133+
number: createTypeSchemaBuilder<number>((val) => typeof val === "number", "must be a number"),
134+
string: createTypeSchemaBuilder<string>((val) => typeof val === "string", "must be a string"),
135+
boolean: createTypeSchemaBuilder<boolean>((val) => typeof val === "boolean", "must be a boolean"),
136+
date: createTypeSchemaBuilder<Date>((val) => val instanceof Date, "must be a date"),
137+
bigint: createTypeSchemaBuilder<bigint>((val) => typeof val === "bigint", "must be a bigint"),
138+
array: createTypeSchemaBuilder<any[]>((val) => Array.isArray(val), "must be an array"),
139+
object: createTypeSchemaBuilder<object>((val) => typeof val === "object", "must be an object"),
149140
};
150141

151142
export const s = schemas;

0 commit comments

Comments
 (0)