From dc12c44895c0d4f5ea11bcc0ed7548d77f7b04da Mon Sep 17 00:00:00 2001 From: Artemy Egorov Date: Sat, 3 Aug 2024 12:56:51 +0300 Subject: [PATCH] refactor: rename root to tag, remove not supported typescript features --- libs/rust/README.md | 1 - libs/typescript/package.json | 2 +- libs/typescript/src/daletl/main.ts | 48 ------------------- libs/typescript/src/daletl/normalizers.ts | 30 ------------ libs/typescript/src/daletl/tags/el.ts | 16 ------- libs/typescript/src/daletl/tags/heading.ts | 17 ------- libs/typescript/src/daletl/types.ts | 43 +---------------- libs/typescript/src/daletpack.ts | 18 ------- libs/typescript/src/index.ts | 0 libs/typescript/src/utils.ts | 56 ---------------------- specification/daletl.md | 17 ++++--- specification/daletpack.md | 2 +- 12 files changed, 14 insertions(+), 236 deletions(-) delete mode 100644 libs/rust/README.md delete mode 100644 libs/typescript/src/daletl/main.ts delete mode 100644 libs/typescript/src/daletl/normalizers.ts delete mode 100644 libs/typescript/src/daletl/tags/el.ts delete mode 100644 libs/typescript/src/daletl/tags/heading.ts delete mode 100644 libs/typescript/src/daletpack.ts delete mode 100644 libs/typescript/src/index.ts delete mode 100644 libs/typescript/src/utils.ts diff --git a/libs/rust/README.md b/libs/rust/README.md deleted file mode 100644 index 7e82684..0000000 --- a/libs/rust/README.md +++ /dev/null @@ -1 +0,0 @@ -# Dalet-rs diff --git a/libs/typescript/package.json b/libs/typescript/package.json index 499acd2..27bbcde 100644 --- a/libs/typescript/package.json +++ b/libs/typescript/package.json @@ -1,6 +1,6 @@ { "name": "@txtdot/dalet", - "version": "1.0.0-pre1", + "version": "1.0.0-pre2", "description": "Dalet implementation in typescript", "main": "dist/lib.js", "types": "dist/lib.d.ts", diff --git a/libs/typescript/src/daletl/main.ts b/libs/typescript/src/daletl/main.ts deleted file mode 100644 index 4c0b909..0000000 --- a/libs/typescript/src/daletl/main.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { encode, decode } from "../daletpack"; -import { ParseError, CommonTag, Body, Root, Tag, CommonBody } from "./types"; -import { TagNormalizers } from "./normalizers"; - -export function parseTag(tag: Tag): CommonTag { - return TagNormalizers[tag.id](tag); -} - -export function parseBody(body: Body): CommonBody { - if (body === null) { - return null; - } - - if (typeof body === "string") { - return body; - } - - return body.map((t) => parseTag(t)); -} - -export function parse(root_data: Uint8Array): RootClass { - const root = decode(root_data); - - if (!Array.isArray(root)) { - throw new ParseError("Daletl root must be array"); - } - - return new RootClass(root.map(parseTag)); -} - -export class RootClass { - root: CommonTag[]; - constructor(root: CommonTag[]) { - this.root = root; - } - - get raw(): Root { - return this.root.map((t) => t.raw); - } - - encode(): Uint8Array { - return encode(this.raw); - } - - toHtml(classes?: boolean): string { - return this.root.map((t) => t.toHtml(classes)).join(""); - } -} diff --git a/libs/typescript/src/daletl/normalizers.ts b/libs/typescript/src/daletl/normalizers.ts deleted file mode 100644 index 1a1eb33..0000000 --- a/libs/typescript/src/daletl/normalizers.ts +++ /dev/null @@ -1,30 +0,0 @@ -import { parseBody } from "./main"; -import El from "./tags/el"; -import Heading from "./tags/heading"; -import { z } from "zod"; -import { Tag } from "./types"; - -const textOrTag = z.custom((b) => b !== null); -const text = z.string(); - -const TagNormalizers = [ - n(textOrTag, z.any(), El), - n(text, z.number().int().min(1).max(6).nullable(), Heading), -]; - -export { TagNormalizers }; - -// eslint-disable-next-line @typescript-eslint/no-explicit-any -function n(body: z.ZodTypeAny, argument: z.ZodTypeAny, T: any) { - return (tag: Tag) => { - const parsedBody = parseBody(tag.body); - - z.object({ id: z.number().int(), body, argument }).parse({ - id: tag.id, - body: parsedBody, - argument, - }); - - return new T(parsedBody, argument); - }; -} diff --git a/libs/typescript/src/daletl/tags/el.ts b/libs/typescript/src/daletl/tags/el.ts deleted file mode 100644 index ffc8423..0000000 --- a/libs/typescript/src/daletl/tags/el.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { bodyToRaw, chtml } from "../../utils"; -import { CommonTag, Tag } from "../types"; - -export default class El extends CommonTag { - constructor(body: CommonTag[]) { - super(0, body, null); - } - - get raw(): Tag { - return { id: this.id, body: bodyToRaw(this.body), argument: null }; - } - - toHtml(classes?: boolean): string { - return chtml("section", "el", classes, this.body); - } -} diff --git a/libs/typescript/src/daletl/tags/heading.ts b/libs/typescript/src/daletl/tags/heading.ts deleted file mode 100644 index e7ba515..0000000 --- a/libs/typescript/src/daletl/tags/heading.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { chtml } from "../../utils"; -import { CommonTag } from "../types"; - -export default class Heading extends CommonTag { - constructor(body: CommonTag[], argument?: number | null) { - super(1, body, argument || null); - } - - toHtml(classes?: boolean): string { - return chtml( - `h${this.argument || 1}`, - `h hl${this.argument || 1}`, - classes, - this.body - ); - } -} diff --git a/libs/typescript/src/daletl/types.ts b/libs/typescript/src/daletl/types.ts index 2b29770..9c01fec 100644 --- a/libs/typescript/src/daletl/types.ts +++ b/libs/typescript/src/daletl/types.ts @@ -1,48 +1,9 @@ -import { encodeTag } from "../daletpack"; -import { bodyToRaw } from "../utils"; - -export type Root = Tag[]; -export type Argument = string | number | null; +export type Page = Tag[]; export type Body = string | Tag[] | null; +export type Argument = string | number | null; export interface Tag { id: number; body: Body; argument: Argument; } - -export class ParseError extends Error { - constructor(message: string = "Parse error") { - super(message); - this.name = "ParseError"; - } -} - -export abstract class CommonTag { - id: number; - body: CommonBody; - argument: Argument; - constructor(id: number, body: CommonBody, argument: Argument) { - this.id = id; - this.body = body; - this.argument = argument; - } - get raw(): Tag { - return { - id: this.id, - body: bodyToRaw(this.body), - argument: this.argument, - }; - } - encode(): Uint8Array { - return encodeTag(this.raw); - } - // eslint-disable-next-line @typescript-eslint/no-unused-vars - toHtml(classes?: boolean): string { - return ""; - } -} - -export type CommonBody = string | CommonTag[] | null; - -export type TagNormalizer = (tag: Tag) => CommonTag; diff --git a/libs/typescript/src/daletpack.ts b/libs/typescript/src/daletpack.ts deleted file mode 100644 index ff634e7..0000000 --- a/libs/typescript/src/daletpack.ts +++ /dev/null @@ -1,18 +0,0 @@ -/* eslint-disable @typescript-eslint/no-unused-vars */ -import { Root, Tag } from "./daletl/types"; - -export function encodeTag(tag: Tag): Uint8Array { - throw new Error("encodeTag is not implemented"); -} - -export function encode(root: Root): Uint8Array { - throw new Error("encode is not implemented"); -} - -export function decodeTag(data: Uint8Array): Tag { - throw new Error("decodeTag is not implemented"); -} - -export function decode(data: Uint8Array): Root { - throw new Error("decode is not implemented"); -} diff --git a/libs/typescript/src/index.ts b/libs/typescript/src/index.ts deleted file mode 100644 index e69de29..0000000 diff --git a/libs/typescript/src/utils.ts b/libs/typescript/src/utils.ts deleted file mode 100644 index 8b6fd03..0000000 --- a/libs/typescript/src/utils.ts +++ /dev/null @@ -1,56 +0,0 @@ -import { Body, CommonBody, CommonTag, Tag } from "./daletl/types"; - -export function bodyToRaw(body: CommonBody): Body { - if (body === null) { - return null; - } - - if (typeof body === "string") { - return body; - } - - return body.map((t) => t.raw); -} - -export function bodyToHtml(body: CommonBody): string { - if (typeof body === "string") { - return body; - } - - if (Array.isArray(body)) { - return body.map((t) => t.toHtml()).join(""); - } - - return ""; -} - -export function getRaw(t: CommonTag): Tag { - return t.raw; -} - -interface Props { - [key: string]: string | undefined; -} - -export function chtml( - tag: string, - classNames: string, - classes: boolean = true, - body?: CommonBody, - props?: Props -) { - const classProp = classes ? { class: classNames } : {}; - return html(tag, body, { ...props, ...classProp }); -} - -function html(tag: string, body?: CommonBody, props?: Props) { - const pr = Object.entries(props || {}) - .map(([key, value]) => `${key}="${value}"`) - .join(" "); - - if (!body) { - return `<${tag}${pr ? " " + pr : ""}/>`; - } - - return `<${tag}${pr ? " " + pr : ""}>${bodyToHtml(body)}`; -} diff --git a/specification/daletl.md b/specification/daletl.md index f21da17..955e562 100644 --- a/specification/daletl.md +++ b/specification/daletl.md @@ -4,12 +4,12 @@ Daletl is data representation of serialized/deserialized [DaletPack](./daletpack.md). -### Root +### Page -Daletl root is array of tags. For convenience, we will use the typescript notation. +Daletl page is array of tags. For convenience, we will use the typescript notation. ```typescript -type Root = Tag[]; +type Page = Tag[]; ``` ### Tag @@ -17,17 +17,20 @@ type Root = Tag[]; All tags specification is in [Tags](./tags.md). ```typescript -interface Tag { +export type Body = string | Tag[] | null; +export type Argument = string | number | null; + +export interface Tag { id: number; - body: string | Tag[] | null; - argument: string | number | null; + body: Body; + argument: Argument; } ``` ### Example ```typescript -const root: Root = [ +const page: Page = [ { id: 1, body: "I am Heading with level 1", diff --git a/specification/daletpack.md b/specification/daletpack.md index dafb005..ecb7245 100644 --- a/specification/daletpack.md +++ b/specification/daletpack.md @@ -6,7 +6,7 @@ All apps that supports Dalet must use this format when transmitting data between All data must be compressed with [zstd](https://datatracker.ietf.org/doc/html/rfc8878). -Root data format is array of tags (see [daletl specification](./daletl.md)), each element reads sequentially. Type definition for root is not needed. +Page data format is array of tags (see [daletl specification](./daletl.md)), each element reads sequentially. Type definition for page is not needed. Mime type: `application/dalet-pack`