refactor: rename root to tag, remove not supported typescript features

This commit is contained in:
Artemy Egorov 2024-08-03 12:56:51 +03:00
parent 44ae5868b4
commit dc12c44895
12 changed files with 14 additions and 236 deletions

View file

@ -1 +0,0 @@
# Dalet-rs

View file

@ -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",

View file

@ -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("");
}
}

View file

@ -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);
};
}

View file

@ -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);
}
}

View file

@ -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
);
}
}

View file

@ -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;

View file

@ -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");
}

View file

@ -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)}</${tag}>`;
}

View file

@ -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",

View file

@ -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`