From 37e450895d30863b7f1aac33c071f1f5abd55cc9 Mon Sep 17 00:00:00 2001 From: Danielo Rodriguez Date: Sat, 16 Dec 2023 12:56:56 +0100 Subject: [PATCH] fix: prevent chalk to be loaded in bundle --- src/core/template/templateParser.ts | 197 ++++++++++++++++------------ 1 file changed, 111 insertions(+), 86 deletions(-) diff --git a/src/core/template/templateParser.ts b/src/core/template/templateParser.ts index ca6936d2..54a74d31 100644 --- a/src/core/template/templateParser.ts +++ b/src/core/template/templateParser.ts @@ -1,112 +1,137 @@ -import * as E from 'fp-ts/Either'; -import * as St from 'fp-ts/string' -import * as P from 'parser-ts/Parser' -import { run } from 'parser-ts/code-frame' -import * as C from 'parser-ts/char' -import * as S from 'parser-ts/string' -import * as A from 'fp-ts/Array' -import { Either, O, pipe } from '@std'; -import { TemplateText, TemplateVariable } from './templateSchema'; -import { absurd } from 'fp-ts/function'; -import { ModalFormData } from '../FormResult'; -type Token = TemplateText | TemplateVariable +import * as E from "fp-ts/Either"; +import * as St from "fp-ts/string"; +import * as P from "parser-ts/Parser"; +import * as C from "parser-ts/char"; +import * as S from "parser-ts/string"; +import * as A from "fp-ts/Array"; +import { Either, O, pipe } from "@std"; +import { TemplateText, TemplateVariable } from "./templateSchema"; +import { absurd } from "fp-ts/function"; +import { ModalFormData } from "../FormResult"; +type Token = TemplateText | TemplateVariable; export type ParsedTemplate = Token[]; function TemplateText(value: string): TemplateText { - return { _tag: 'text', value } + return { _tag: "text", value }; } function TemplateVariable(value: string): TemplateVariable { - return { _tag: 'variable', value } + return { _tag: "variable", value }; } - // === Parsers === -type TokenParser = P.Parser +type TokenParser = P.Parser; // A parser that returns an empty string when it reaches the end of the input. // required to keep the same type as the other parsers. const EofStr = pipe( - P.eof(), - P.map(() => '')) -const open = S.fold([S.string('{{'), S.spaces]) -const close = P.expected(S.fold([S.spaces, S.string('}}')]), 'closing variable tag: "}}"') -const identifier = S.many1(C.alphanum) + P.eof(), + P.map(() => ""), +); +const open = S.fold([S.string("{{"), S.spaces]); +const close = P.expected( + S.fold([S.spaces, S.string("}}")]), + 'closing variable tag: "}}"', +); +const identifier = S.many1(C.alphanum); const templateIdentifier: TokenParser = pipe( - identifier, - P.between(open, close), - P.map(TemplateVariable), -) - + identifier, + P.between(open, close), + P.map(TemplateVariable), +); -export const OpenOrEof = P.either(open, () => EofStr) -export const anythingUntilOpenOrEOF = P.many1Till(P.item(), P.lookAhead(OpenOrEof)) +export const OpenOrEof = P.either(open, () => EofStr); +export const anythingUntilOpenOrEOF = P.many1Till( + P.item(), + P.lookAhead(OpenOrEof), +); const text: TokenParser = pipe( - anythingUntilOpenOrEOF, - P.map((value) => TemplateText(value.join('')))) + anythingUntilOpenOrEOF, + P.map((value) => TemplateText(value.join(""))), +); // function parseTemplate(template: string): E.Either { const TextOrVariable: TokenParser = pipe( - templateIdentifier, - P.alt(() => text), -) + templateIdentifier, + P.alt(() => text), +); const Template = pipe( - P.many(TextOrVariable), - P.apFirst(P.eof()), -) + P.many(TextOrVariable), + P.apFirst(P.eof())); /** * Given a template string, parse it into an array of tokens. * Templates look like this: * "Hello {{name}}! You are {{age}} years old." * @param template a template string to convert into an array of tokens or an error */ -export function parseTemplate(template: string): Either { - return run((Template), template) - // return S.run(template)(P.many(Template)) +export function parseTemplate( + template: string, +): Either { + return pipe( + Template, + S.run(template), + E.fold( + ({ expected }) => E.left(`Expected ${expected.join(" or ")}`), + (result) => E.right(result.value), + ), + ); + // return S.run(template)(P.many(Template)) } -export function templateVariables(parsedTemplate: ReturnType): string[] { - return pipe( - parsedTemplate, - E.fold( - () => [], - A.filterMap((token) => { - if (token._tag === 'variable') { - return O.some(token.value) - } - return O.none - })) - ) +export function templateVariables( + parsedTemplate: ReturnType, +): string[] { + return pipe( + parsedTemplate, + E.fold( + () => [], + A.filterMap((token) => { + if (token._tag === "variable") { + return O.some(token.value); + } + return O.none; + }), + ), + ); } -export function templateError(parsedTemplate: ReturnType): string | undefined { - return pipe( - parsedTemplate, - E.fold( - (error) => error, - () => undefined - ) - ) +export function templateError( + parsedTemplate: ReturnType, +): string | undefined { + return pipe( + parsedTemplate, + E.fold( + (error) => error, + () => undefined, + ), + ); } function tokenToString(token: Token): string { - const tag = token._tag - switch (tag) { - case 'text': return token.value - case 'variable': return `{{${token.value}}}` - default: - return absurd(tag) - } + const tag = token._tag; + switch (tag) { + case "text": + return token.value; + case "variable": + return `{{${token.value}}}`; + default: + return absurd(tag); + } } -function matchToken(onText: (value: string) => T, onVariable: (variable: string) => T) { - return (token: Token): T => { - switch (token._tag) { - case 'text': return onText(token.value) - case 'variable': return onVariable(token.value) - default: - return absurd(token) - } +function matchToken( + onText: (value: string) => T, + onVariable: (variable: string) => T, +) { + return (token: Token): T => { + switch (token._tag) { + case "text": + return onText(token.value); + case "variable": + return onVariable(token.value); + default: + return absurd(token); } + }; } /** @@ -118,18 +143,18 @@ function matchToken(onText: (value: string) => T, onVariable: (variable: stri * @returns string */ export function parsedTemplateToString(parsedTemplate: ParsedTemplate): string { - return pipe( - parsedTemplate, - A.foldMap(St.Monoid)(tokenToString) - ) + return pipe( + parsedTemplate, + A.foldMap(St.Monoid)(tokenToString)); } -export function executeTemplate(parsedTemplate: ParsedTemplate, formData: ModalFormData) { - return pipe( - parsedTemplate, - A.filterMap( - matchToken(O.some, (key) => O.fromNullable(formData[key])) - ), - A.foldMap(St.Monoid)(String) - ) +export function executeTemplate( + parsedTemplate: ParsedTemplate, + formData: ModalFormData, +) { + return pipe( + parsedTemplate, + A.filterMap(matchToken(O.some, (key) => O.fromNullable(formData[key]))), + A.foldMap(St.Monoid)(String), + ); }