Skip to content

Commit 2364b2b

Browse files
committed
house keeping
1 parent d37b123 commit 2364b2b

File tree

1 file changed

+25
-35
lines changed

1 file changed

+25
-35
lines changed

src/utils/schema.ts

+25-35
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,29 @@ export function numericId(schema: NumberSchema = YupNumber()) {
4242
// Limited Character Sets
4343
// -----------------------------------------------------------------------------
4444

45-
export function limitCharSet(
45+
export function matchesCharSet(
4646
charSet: string,
4747
message: string,
4848
schema: StringSchema = YupString(),
4949
) {
5050
return schema.matches(new RegExp(`^[${charSet}]*$`), message)
5151
}
5252

53-
type CharSetOptions = Partial<{
53+
export type BuildCharSetOptions = Partial<{
5454
schema: StringSchema
5555
spaces: boolean
5656
specialChars: string
5757
}>
5858

59-
function charSet(
59+
export function buildCharSet(
6060
charSet: string,
61-
message: string,
62-
options: CharSetOptions = {},
61+
description: string,
62+
options: BuildCharSetOptions = {},
6363
) {
6464
const { schema, spaces = false, specialChars } = options
6565

66+
let message = `can only contain: ${description}`
67+
6668
if (spaces) {
6769
charSet += " "
6870
message += ", spaces"
@@ -72,57 +74,45 @@ function charSet(
7274
message += `, special characters (${specialChars})`
7375
}
7476

75-
return limitCharSet(charSet, message, schema)
77+
return matchesCharSet(charSet, message, schema)
7678
}
7779

78-
export function alphaString(options?: CharSetOptions) {
79-
return charSet(
80-
"a-zA-Z",
81-
"can only contain alpha characters (a-z, A-Z)",
82-
options,
83-
)
80+
export function alphaString(options?: BuildCharSetOptions) {
81+
return buildCharSet("a-zA-Z", "alpha characters (a-z, A-Z)", options)
8482
}
8583

86-
export function lowercaseAlphaString(options?: CharSetOptions) {
87-
return charSet(
88-
"a-z",
89-
"can only contain lowercase alpha characters (a-z)",
90-
options,
91-
)
84+
export function lowercaseAlphaString(options?: BuildCharSetOptions) {
85+
return buildCharSet("a-z", "lowercase alpha characters (a-z)", options)
9286
}
9387

94-
export function lowercaseAlphanumericString(options?: CharSetOptions) {
95-
return charSet(
88+
export function lowercaseAlphanumericString(options?: BuildCharSetOptions) {
89+
return buildCharSet(
9690
"a-z0-9",
97-
"can only contain lowercase alphanumeric characters (a-z, 0-9)",
91+
"lowercase alphanumeric characters (a-z, 0-9)",
9892
options,
9993
)
10094
}
10195

102-
export function uppercaseAlphaString(options?: CharSetOptions) {
103-
return charSet(
104-
"A-Z",
105-
"can only contain uppercase alpha characters (A-Z)",
106-
options,
107-
)
96+
export function uppercaseAlphaString(options?: BuildCharSetOptions) {
97+
return buildCharSet("A-Z", "uppercase alpha characters (A-Z)", options)
10898
}
10999

110-
export function uppercaseAlphanumericString(options?: CharSetOptions) {
111-
return charSet(
100+
export function uppercaseAlphanumericString(options?: BuildCharSetOptions) {
101+
return buildCharSet(
112102
"A-Z0-9",
113-
"can only contain uppercase alphanumeric characters (A-Z, 0-9)",
103+
"uppercase alphanumeric characters (A-Z, 0-9)",
114104
options,
115105
)
116106
}
117107

118-
export function numericString(options?: CharSetOptions) {
119-
return charSet("0-9", "can only contain numbers (0-9)", options)
108+
export function numericString(options?: BuildCharSetOptions) {
109+
return buildCharSet("0-9", "numbers (0-9)", options)
120110
}
121111

122-
export function alphanumericString(options?: CharSetOptions) {
123-
return charSet(
112+
export function alphanumericString(options?: BuildCharSetOptions) {
113+
return buildCharSet(
124114
"a-zA-Z0-9",
125-
"can only contain alphanumeric characters (a-z, A-Z, 0-9)",
115+
"alphanumeric characters (a-z, A-Z, 0-9)",
126116
options,
127117
)
128118
}

0 commit comments

Comments
 (0)