Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[実装] xml2js形式のサポート #86

Merged
merged 3 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions src/@types/format.xml2js.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { Output } from "valibot";
import { array, object, optional, string } from "valibot";

export const ZXml2jsChatItem = object({
_: string(),
$: object({
no: optional(string()),
vpos: string(),
date: optional(string(), "0"),
date_usec: optional(string(), "0"),
user_id: optional(string()),
owner: optional(string(), ""),
premium: optional(string(), ""),
mail: optional(string(), ""),
}),
});
export type Xml2jsChatItem = Output<typeof ZXml2jsChatItem>;

export const ZXml2jsChat = object({
chat: array(ZXml2jsChatItem),
});
export type Xml2jsChat = Output<typeof ZXml2jsChat>;

export const ZXml2jsPacket = object({
packet: ZXml2jsChat,
});
export type Xml2jsPacket = Output<typeof ZXml2jsPacket>;
29 changes: 19 additions & 10 deletions src/@types/options.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import type { Output } from "valibot";
import { literal, union } from "valibot";

import type {
Config,
FormattedComment,
Expand All @@ -6,19 +9,25 @@ import type {
RawApiResponse,
V1Thread,
} from "@/@types/";
import type { Xml2jsPacket } from "@/@types/format.xml2js";

export const ZInputFormatType = union([
literal("XMLDocument"),
literal("niconicome"),
literal("xml2js"),
literal("formatted"),
literal("legacy"),
literal("legacyOwner"),
literal("owner"),
literal("v1"),
literal("empty"),
literal("default"),
]);
export type InputFormatType = Output<typeof ZInputFormatType>;

export type InputFormatType =
| "XMLDocument"
| "niconicome"
| "formatted"
| "legacy"
| "legacyOwner"
| "owner"
| "v1"
| "empty"
| "default";
export type InputFormat =
| XMLDocument
| Xml2jsPacket
| FormattedComment[]
| FormattedLegacyComment[]
| RawApiResponse[]
Expand Down
2 changes: 2 additions & 0 deletions src/input/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { LegacyParser } from "./legacy";
import { LegacyOwnerParser } from "./legacyOwner";
import { OwnerParser } from "./owner";
import { V1Parser } from "./v1";
import { Xml2jsParser } from "./xml2js";
import { XmlDocumentParser } from "./xmlDocument";

export const parsers: InputParser[] = [
Expand All @@ -15,5 +16,6 @@ export const parsers: InputParser[] = [
LegacyOwnerParser,
OwnerParser,
V1Parser,
Xml2jsParser,
XmlDocumentParser,
];
46 changes: 46 additions & 0 deletions src/input/xml2js.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { parse } from "valibot";

import type { FormattedComment, InputParser } from "@/@types";
import type { Xml2jsPacket } from "@/@types/format.xml2js";
import { ZXml2jsPacket } from "@/@types/format.xml2js";

export const Xml2jsParser: InputParser = {
key: ["xml2js"],
parse: (input) => {
return fromXml2js(parse(ZXml2jsPacket, input));
},
};

const fromXml2js = (data: Xml2jsPacket): FormattedComment[] => {
const data_: FormattedComment[] = [],
userList: string[] = [];
let index = data.packet.chat.length;
for (const item of data.packet.chat) {
const tmpParam: FormattedComment = {
id: Number(item.$.no) || index++,
vpos: Number(item.$.vpos),
content: item._,
date: Number(item.$.date),
date_usec: Number(item.$.date_usec),
owner: !(item.$.owner === "0" || item.$.user_id),
premium: item.$.premium === "1",
mail: item.$.mail.split(/\s+/g),
user_id: -1,
layer: -1,
is_my_post: false,
};
if (tmpParam.content.startsWith("/") && tmpParam.owner) {
tmpParam.mail.push("invisible");
}
const userId = item.$.user_id ?? "";
const isUserExist = userList.indexOf(userId);
if (isUserExist === -1) {
tmpParam.user_id = userList.length;
userList.push(userId);
} else {
tmpParam.user_id = isUserExist;
}
data_.push(tmpParam);
}
return data_;
};
9 changes: 3 additions & 6 deletions src/inputParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ const convert2formattedComment = (
data: unknown,
type: InputFormatType,
): FormattedComment[] => {
for (const parser of parsers) {
if (parser.key.includes(type)) {
return sort(parser.parse(data));
}
}
throw new InvalidFormatError();
const parser = parsers.find((parser) => parser.key.includes(type));
if (!parser) throw new InvalidFormatError();
return sort(parser.parse(data));
};

/**
Expand Down
18 changes: 12 additions & 6 deletions src/typeGuard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { array, custom, is, literal, regex, string, union } from "valibot";
import {
array,
custom,
is,
literal,
regex,
safeParse,
string,
union,
} from "valibot";

import type {
ApiChat,
Expand All @@ -25,6 +34,7 @@ import type {
V1Comment,
V1Thread,
} from "@/@types/";
import { ZInputFormatType } from "@/@types/";
import {
ZApiChat,
ZApiGlobalNumRes,
Expand Down Expand Up @@ -199,11 +209,7 @@ const typeGuard = {
keepCA: isBoolean,
scale: isNumber,
config: isObject,
format: (i: unknown) =>
typeof i === "string" &&
!!RegExp(
/^(XMLDocument|niconicome|formatted|legacy|legacyOwner|owner|v1|default|empty)$/,
).exec(i),
format: (i) => safeParse(ZInputFormatType, i).success,
video: (i: unknown) =>
typeof i === "object" && (i as HTMLVideoElement).nodeName === "VIDEO",
};
Expand Down
Loading