Skip to content

Commit

Permalink
refactor: change to use valibot
Browse files Browse the repository at this point in the history
  • Loading branch information
xpadev-net committed Jan 4, 2024
1 parent 2e15e83 commit fbbb886
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 30 deletions.
35 changes: 26 additions & 9 deletions src/@types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,12 @@ type NicoScriptReverse = {
start: number;
end: number;
};
export type NicoScriptReverseTarget = "コメ" | "投コメ" | "全";
export const ZNicoScriptReverseTarget = union([
literal("\u30b3\u30e1"), //コメ
literal("\u6295\u30b3\u30e1"), //投コメ
literal("\u5168"), //全
]);
export type NicoScriptReverseTarget = Output<typeof ZNicoScriptReverseTarget>;
export type NicoScriptReplace = {
start: number;
long: number | undefined;
Expand All @@ -204,14 +209,26 @@ export type NicoScriptReplace = {
loc: CommentLoc | undefined;
no: number;
};
export type NicoScriptReplaceRange = "単" | "全";
export type NicoScriptReplaceTarget =
| "コメ"
| "投コメ"
| "全"
| "含まない"
| "含む";
export type NicoScriptReplaceCondition = "完全一致" | "部分一致";
export const ZNicoScriptReplaceRange = union([
literal("\u5358"), //単
literal("\u5168"), //全
]);
export type NicoScriptReplaceRange = Output<typeof ZNicoScriptReplaceRange>;
export const ZNicoScriptReplaceTarget = union([
literal("\u30b3\u30e1"), //コメ
literal("\u6295\u30b3\u30e1"), //投コメ
literal("\u5168"), //全
literal("\u542b\u307e\u306a\u3044"), //含まない
literal("\u542b\u3080"), //含む
]);
export type NicoScriptReplaceTarget = Output<typeof ZNicoScriptReplaceTarget>;
export const ZNicoScriptReplaceCondition = union([
literal("\u90e8\u5206\u4e00\u81f4"), //部分一致
literal("\u5b8c\u5168\u4e00\u81f4"), //完全一致
]);
export type NicoScriptReplaceCondition = Output<
typeof ZNicoScriptReplaceCondition
>;
type NicoScriptBan = {
start: number;
end: number;
Expand Down
34 changes: 13 additions & 21 deletions src/typeGuard.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import {
array,
custom,
instance,
is,
literal,
optional,
regex,
safeParse,
string,
union,
} from "valibot";
Expand Down Expand Up @@ -34,7 +35,6 @@ import type {
V1Comment,
V1Thread,
} from "@/@types/";
import { ZInputFormatType } from "@/@types/";
import {
ZApiChat,
ZApiGlobalNumRes,
Expand All @@ -48,7 +48,12 @@ import {
ZFormattedComment,
ZFormattedLegacyComment,
ZHTML5Fonts,
ZInputFormatType,
ZMeasureInput,
ZNicoScriptReplaceCondition,
ZNicoScriptReplaceRange,
ZNicoScriptReplaceTarget,
ZNicoScriptReverseTarget,
ZOwnerComment,
ZRawApiResponse,
ZV1Comment,
Expand Down Expand Up @@ -142,27 +147,15 @@ const typeGuard = {
nicoScript: {
range: {
target: (i: unknown): i is NicoScriptReverseTarget =>
is(string([regex(/^(?:\u6295?\u30b3\u30e1|\u5168)$/)]), i),
is(ZNicoScriptReverseTarget, i),
},
replace: {
range: (i: unknown): i is NicoScriptReplaceRange =>
is(string([regex(/^[\u5358\u5168]$/)]), i),
is(ZNicoScriptReplaceRange, i),
target: (i: unknown): i is NicoScriptReplaceTarget =>
is(
string([
regex(
/^(?:\u30b3\u30e1|\u6295\u30b3\u30e1|\u5168|\u542b\u3080|\u542b\u307e\u306a\u3044)$/,
),
]),
i,
),
is(ZNicoScriptReplaceTarget, i),
condition: (i: unknown): i is NicoScriptReplaceCondition =>
is(
string([
regex(/^(?:\u90e8\u5206\u4e00\u81f4|\u5b8c\u5168\u4e00\u81f4)$/),
]),
i,
),
is(ZNicoScriptReplaceCondition, i),
},
},
comment: {
Expand Down Expand Up @@ -209,9 +202,8 @@ const typeGuard = {
keepCA: isBoolean,
scale: isNumber,
config: isObject,
format: (i) => safeParse(ZInputFormatType, i).success,
video: (i: unknown) =>
typeof i === "object" && (i as HTMLVideoElement).nodeName === "VIDEO",
format: (i) => is(ZInputFormatType, i),
video: (i: unknown) => is(optional(instance(HTMLVideoElement)), i),
};
for (const key of Object.keys(keys)) {
if (
Expand Down

0 comments on commit fbbb886

Please sign in to comment.