Skip to content

Commit 3af0637

Browse files
committed
[form] Export more functions from static-analysis submodule
1 parent d1834f0 commit 3af0637

File tree

1 file changed

+36
-39
lines changed

1 file changed

+36
-39
lines changed

packages/form/src/static-analysis.ts

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import { isObject } from "@/lib/object.js";
22
import {
3-
ARRAYS_OF_SUB_SCHEMAS,
43
isSchema,
54
makeSchemaDefinitionTraverser,
6-
RECORDS_OF_SUB_SCHEMAS,
75
SCHEMA_KEYS,
8-
SUB_SCHEMAS,
96
traverseSchemaValue,
107
type Path,
118
type Schema,
@@ -18,24 +15,42 @@ import {
1815
DEFAULT_PSEUDO_ID_SEPARATOR,
1916
} from "./form/id-schema.js";
2017

21-
const INDEX_REGEX = /^\d+$/;
18+
export const SEQUENCE_OF_DIGITS_REGEX = /^\d+$/;
2219

2320
export interface SchemaIssue {
2421
issuer: string;
2522
message: string;
2623
path: Path;
2724
}
2825

29-
const ID_CONFIG_ISSUER = "idConfig";
30-
const SCHEMA_ISSUER = "schema";
26+
export const ID_CONFIG_ISSUER = "idConfig";
27+
export const SCHEMA_ISSUER = "schema";
3128

32-
interface IdConfig {
29+
export interface IdConfig {
3330
prefix: string;
3431
separator: string;
3532
pseudoSeparator: string;
3633
}
3734

38-
function* idConfigAnalysis(idConfig: IdConfig): Generator<SchemaIssue> {
35+
export function makeIdConfig({
36+
prefix = DEFAULT_ID_PREFIX,
37+
pseudoSeparator = DEFAULT_PSEUDO_ID_SEPARATOR,
38+
separator = DEFAULT_ID_SEPARATOR
39+
}: Partial<IdConfig> = {}): IdConfig {
40+
return {
41+
prefix,
42+
separator,
43+
pseudoSeparator
44+
};
45+
}
46+
47+
export type IdSeparatorEntries = [string, string][];
48+
49+
export function makeIdSeparatorEntries(idConfig: IdConfig): [string, string][] {
50+
return Object.entries(idConfig).filter((e) => e[0] !== "prefix");
51+
}
52+
53+
export function* idConfigAnalysis(idConfig: IdConfig): Generator<SchemaIssue> {
3954
const keys = Object.keys(idConfig) as (keyof IdConfig)[];
4055
for (const key of keys) {
4156
const value = idConfig[key]!;
@@ -47,7 +62,7 @@ function* idConfigAnalysis(idConfig: IdConfig): Generator<SchemaIssue> {
4762
};
4863
continue;
4964
}
50-
if (INDEX_REGEX.test(value)) {
65+
if (SEQUENCE_OF_DIGITS_REGEX.test(value)) {
5166
yield {
5267
issuer: ID_CONFIG_ISSUER,
5368
message: `value "${value}" should not be a sequence of digits`,
@@ -71,33 +86,26 @@ function* idConfigAnalysis(idConfig: IdConfig): Generator<SchemaIssue> {
7186
}
7287
}
7388

74-
function* objectKeyAnalysis(
89+
export function* objectKeyAnalysis(
7590
issuer: string,
76-
idSeparatorEntries: [string, string][],
91+
idSeparatorEntries: IdSeparatorEntries,
7792
key: string,
7893
path: Path
7994
): Generator<SchemaIssue> {
80-
if (INDEX_REGEX.test(key)) {
81-
yield {
82-
issuer,
83-
message: `key "${key}" should not be a sequence of digits`,
84-
path,
85-
};
86-
}
87-
for (const [key, value] of idSeparatorEntries) {
88-
if (key.includes(value)) {
95+
for (const [separator, value] of idSeparatorEntries) {
96+
if (separator.includes(value)) {
8997
yield {
9098
issuer,
91-
message: `key "${key}" includes separator "${key}: ${value}", expected: key should not include separator value`,
99+
message: `key "${key}" includes separator "${separator}: ${value}", expected: key should not include separator value`,
92100
path,
93101
};
94102
}
95103
}
96104
}
97105

98-
function* schemaValueAnalysis(
106+
export function* schemaValueAnalysis(
99107
issuer: string,
100-
idSeparatorEntries: [string, string][],
108+
idSeparatorEntries: IdSeparatorEntries,
101109
value: SchemaValue,
102110
basePath: Path = []
103111
): Generator<SchemaIssue> {
@@ -116,8 +124,8 @@ function* schemaValueAnalysis(
116124
});
117125
}
118126

119-
function* schemaAnalysis(
120-
idSeparatorEntries: [string, string][],
127+
export function* schemaAnalysis(
128+
idSeparatorEntries: IdSeparatorEntries,
121129
schema: Schema
122130
): Generator<SchemaIssue> {
123131
yield* makeSchemaDefinitionTraverser(SCHEMA_KEYS, {
@@ -150,28 +158,17 @@ function* schemaAnalysis(
150158

151159
export interface StaticAnalysisOptions<T extends SchemaValue> {
152160
schema: Schema;
153-
idPrefix?: string;
154-
idSeparator?: string;
155-
idPseudoSeparator?: string;
161+
idConfig: IdConfig;
156162
initialValue?: T;
157163
}
158164

159165
export function* staticAnalysis<T extends SchemaValue>({
160166
schema,
161-
idPrefix = DEFAULT_ID_PREFIX,
162-
idSeparator = DEFAULT_ID_SEPARATOR,
163-
idPseudoSeparator = DEFAULT_PSEUDO_ID_SEPARATOR,
167+
idConfig,
164168
initialValue,
165169
}: StaticAnalysisOptions<T>): Generator<SchemaIssue> {
166-
const idConfig: IdConfig = {
167-
prefix: idPrefix,
168-
separator: idSeparator,
169-
pseudoSeparator: idPseudoSeparator,
170-
};
171170
yield* idConfigAnalysis(idConfig);
172-
const idSeparatorEntries: [string, string][] = Object.entries(
173-
idConfig
174-
).filter((e) => e[0] !== "prefix");
171+
const idSeparatorEntries = makeIdSeparatorEntries(idConfig);
175172
yield* schemaAnalysis(idSeparatorEntries, schema);
176173
if (initialValue !== undefined) {
177174
yield* schemaValueAnalysis(

0 commit comments

Comments
 (0)