doc: update daletl specification refactor all typescript code

This commit is contained in:
Artemy Egorov 2024-07-28 20:21:22 +03:00
parent 90db2e6c73
commit 9d8220f726
13 changed files with 99 additions and 396 deletions

View file

@ -39,7 +39,6 @@
"typescript-eslint": "^7.17.0"
},
"dependencies": {
"@msgpack/msgpack": "3.0.0-beta2",
"zod": "^3.23.8"
}
}

View file

@ -8,9 +8,6 @@ importers:
.:
dependencies:
'@msgpack/msgpack':
specifier: 3.0.0-beta2
version: 3.0.0-beta2
zod:
specifier: ^3.23.8
version: 3.23.8
@ -67,10 +64,6 @@ packages:
resolution: {integrity: sha512-d2CGZR2o7fS6sWB7DG/3a95bGKQyHMACZ5aW8qGkkqQpUoZV6C0X7Pc7l4ZNMZkfNBf4VWNe9E1jRsf0G146Ew==}
engines: {node: '>=18.18'}
'@msgpack/msgpack@3.0.0-beta2':
resolution: {integrity: sha512-y+l1PNV0XDyY8sM3YtuMLK5vE3/hkfId+Do8pLo/OPxfxuFAUwcGz3oiiUuV46/aBpwTzZ+mRWVMtlSKbradhw==}
engines: {node: '>= 14'}
'@nodelib/fs.scandir@2.1.5':
resolution: {integrity: sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g==}
engines: {node: '>= 8'}
@ -575,8 +568,6 @@ snapshots:
'@humanwhocodes/retry@0.3.0': {}
'@msgpack/msgpack@3.0.0-beta2': {}
'@nodelib/fs.scandir@2.1.5':
dependencies:
'@nodelib/fs.stat': 2.0.5

View file

@ -1,76 +1,36 @@
import { encode, decode } from "@msgpack/msgpack";
import {
ParseError,
RawTag,
Tag,
RawTagAsArray,
RawBody,
Body,
RootRaw,
} from "./types";
import El from "./tags/el";
import { encode, decode } from "../daletpack";
import { ParseError, CommonTag, Body, Root, Tag, CommonBody } from "./types";
import { TagNormalizers } from "./normalizers";
export function parseTag(raw_tag: RawTag): Tag {
if (typeof raw_tag === "string") {
return new El(raw_tag);
}
if (Array.isArray(raw_tag)) {
if (Array.isArray(raw_tag[0])) {
raw_tag = raw_tag as RawTag[];
return new El(raw_tag.map(parseTag));
}
if (typeof raw_tag[0] === "number") {
return TagNormalizers[(raw_tag as RawTagAsArray)[0]](
raw_tag as RawTagAsArray
);
}
}
throw new ParseError("Invalid tag");
export function parseTag(tag: Tag): CommonTag {
return TagNormalizers[tag.id](tag);
}
export function parseBody(body: RawBody): Body {
if (typeof body === "string") {
return body;
}
export function parseBody(body: Body): CommonBody {
if (body === null) {
return null;
}
if (Array.isArray(body)) {
if (Array.isArray(body[0])) {
return body.map((t) => parseTag(t as RawTag));
}
if (typeof body[0] === "number") {
return [parseTag(body)];
}
}
throw new ParseError("Invalid tag body");
return body.map((t) => parseTag(t));
}
export function parse(root_data: Uint8Array): Root {
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 Root(root.map(parseTag));
return new RootClass(root.map(parseTag));
}
export class Root {
root: Tag[];
constructor(root: Tag[]) {
export class RootClass {
root: CommonTag[];
constructor(root: CommonTag[]) {
this.root = root;
}
get raw(): RootRaw {
get raw(): Root {
return this.root.map((t) => t.raw);
}

View file

@ -1,8 +1,8 @@
import { parseBody } from "./main";
import El from "./tags/el";
import Heading from "./tags/heading";
import { RawTagAsArray } from "./types";
import { z } from "zod";
import { Tag } from "./types";
const textOrTag = z.custom((b) => b !== null);
const text = z.string();
@ -16,15 +16,15 @@ export { TagNormalizers };
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function n(body: z.ZodTypeAny, argument: z.ZodTypeAny, T: any) {
return (tag: RawTagAsArray) => {
const parsedBody = parseBody(tag[1]);
return (tag: Tag) => {
const parsedBody = parseBody(tag.body);
z.tuple([z.number().int(), body, argument]).parse([
tag[0],
parsedBody,
tag[2],
]);
z.object({ id: z.number().int(), body, argument }).parse({
id: tag.id,
body: parsedBody,
argument,
});
return new T(parsedBody, tag[2]);
return new T(parsedBody, argument);
};
}

View file

@ -1,13 +1,13 @@
import { bodyToRaw, chtml } from "../../utils";
import { RawTag, Tag } from "../types";
import { CommonTag, Tag } from "../types";
export default class El extends Tag {
constructor(body: string | Tag[]) {
export default class El extends CommonTag {
constructor(body: CommonTag[]) {
super(0, body, null);
}
get raw(): RawTag {
return bodyToRaw(this.body)!;
get raw(): Tag {
return { id: this.id, body: bodyToRaw(this.body), argument: null };
}
toHtml(classes?: boolean): string {

View file

@ -1,8 +1,8 @@
import { chtml } from "../../utils";
import { Tag } from "../types";
import { CommonTag } from "../types";
export default class Heading extends Tag {
constructor(body: string, argument?: number | null) {
export default class Heading extends CommonTag {
constructor(body: CommonTag[], argument?: number | null) {
super(1, body, argument || null);
}

View file

@ -1,13 +1,15 @@
import { encode } from "@msgpack/msgpack";
import { encodeTag } from "../daletpack";
import { bodyToRaw } from "../utils";
export type RawBody = string | null | RawTag[] | RawTag;
export type RawId = number;
export type RawArgument = number | string | null;
export type RawTagAsArray = [RawId, RawBody, RawArgument] | [RawId, RawBody];
export type Root = Tag[];
export type Argument = string | number | null;
export type Body = Tag[] | null;
export type RawTag = RawTagAsArray | number | string | RawTag[];
export type RootRaw = RawTag[];
export interface Tag {
id: number;
body: Body;
argument: Argument;
}
export class ParseError extends Error {
constructor(message: string = "Parse error") {
@ -16,27 +18,24 @@ export class ParseError extends Error {
}
}
export abstract class Tag {
export abstract class CommonTag {
id: number;
body: Body;
body: CommonBody;
argument: Argument;
constructor(id: number, body: Body, argument: Argument) {
constructor(id: number, body: CommonBody, argument: Argument) {
this.id = id;
this.body = body;
this.argument = argument;
}
get raw(): RawTag {
if (this.argument == null) {
if (this.body == null) {
return this.id;
}
return [this.id, bodyToRaw(this.body)];
}
return [this.id, bodyToRaw(this.body), this.argument];
get raw(): Tag {
return {
id: this.id,
body: bodyToRaw(this.body),
argument: this.argument,
};
}
encode(): Uint8Array {
return encode(this.raw);
return encodeTag(this.raw);
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
toHtml(classes?: boolean): string {
@ -44,7 +43,6 @@ export abstract class Tag {
}
}
export type Body = string | Tag[] | null;
export type Argument = RawArgument;
export type CommonBody = CommonTag[] | null;
export type TagNormalizer = (tag: RawTagAsArray) => Tag;
export type TagNormalizer = (tag: Tag) => CommonTag;

View file

@ -0,0 +1,18 @@
/* 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,12 +0,0 @@
import { parse, Root, El, Heading } from "./lib";
const data = new Root([
new El("I am Element"),
new Heading("I am heading", 1),
]).encode();
const root = parse(data);
console.log(root.raw, "\n");
console.log(root.toHtml());
console.log(root.toHtml(false));

View file

@ -1,5 +1,5 @@
import El from "./daletl/tags/el";
import Heading from "./daletl/tags/heading";
export { parse, Root } from "./daletl/main";
export { parse, RootClass } from "./daletl/main";
export { El, Heading };

View file

@ -1,20 +1,14 @@
import { Body, RawBody, RawTag, Tag } from "./daletl/types";
import { Body, CommonBody, CommonTag, Tag } from "./daletl/types";
export function bodyToRaw(body: Body): RawBody {
if (typeof body === "string") {
return body;
export function bodyToRaw(body: CommonBody): Body {
if (body === null) {
return null;
}
if (Array.isArray(body)) {
if (Array.isArray(body[0])) {
return body.map(getRaw);
}
}
return null;
return body.map((t) => t.raw);
}
export function bodyToHtml(body: Body): string {
export function bodyToHtml(body: CommonBody): string {
if (typeof body === "string") {
return body;
}
@ -26,7 +20,7 @@ export function bodyToHtml(body: Body): string {
return "";
}
export function getRaw(t: Tag): RawTag {
export function getRaw(t: CommonTag): Tag {
return t.raw;
}
@ -38,14 +32,14 @@ export function chtml(
tag: string,
classNames: string,
classes: boolean = true,
body?: Body,
body?: CommonBody,
props?: Props
) {
const classProp = classes ? { class: classNames } : {};
return html(tag, body, { ...props, ...classProp });
}
function html(tag: string, body?: Body, props?: Props) {
function html(tag: string, body?: CommonBody, props?: Props) {
const pr = Object.entries(props || {})
.map(([key, value]) => `${key}="${value}"`)
.join(" ");