Skip to content

Commit

Permalink
fix: prevent chalk to be loaded in bundle
Browse files Browse the repository at this point in the history
  • Loading branch information
danielo515 committed Dec 16, 2023
1 parent e09f166 commit 37e4508
Showing 1 changed file with 111 additions and 86 deletions.
197 changes: 111 additions & 86 deletions src/core/template/templateParser.ts
Original file line number Diff line number Diff line change
@@ -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<string, Token>
type TokenParser = P.Parser<string, Token>;
// 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<string>(),
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<string>(),
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<string>(), P.lookAhead(OpenOrEof))
export const OpenOrEof = P.either(open, () => EofStr);
export const anythingUntilOpenOrEOF = P.many1Till(
P.item<string>(),
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<ParseError, ParsedTemplate> {
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<string, ParsedTemplate> {
return run((Template), template)
// return S.run(template)(P.many(Template))
export function parseTemplate(
template: string,
): Either<string, ParsedTemplate> {
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<typeof parseTemplate>): 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<typeof parseTemplate>,
): 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<typeof parseTemplate>): string | undefined {
return pipe(
parsedTemplate,
E.fold(
(error) => error,
() => undefined
)
)
export function templateError(
parsedTemplate: ReturnType<typeof parseTemplate>,
): 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<T>(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<T>(
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);
}
};
}

/**
Expand All @@ -118,18 +143,18 @@ function matchToken<T>(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),
);
}

0 comments on commit 37e4508

Please sign in to comment.