Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianWielga committed Dec 20, 2024
1 parent 83cf845 commit 5d5f320
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 141 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { parse } from "ts-spel";
import { Ast } from "ts-spel/lib/lib/Ast";

function getAst(input: string) {
try {
return parse(input, true);
} catch (e) {
return null;
}
}

function printFragment(text: string, el: { start: number; end: number }): string {
return text.slice(el.start, el.end).trim();
}

function mapToList(input: string, ast?: Ast): string[] | null {
if (ast?.type !== "CompoundExpression") return null;
if (ast.expressionComponents[0].type !== "InlineList") return null;
if (ast.expressionComponents[1].type !== "PropertyReference") return null;
if (ast.expressionComponents[1].propertyName !== "toString") return null;
return ast.expressionComponents[0].elements.map((el) => printFragment(input, el));
}

function mapToObject(input: string, ast?: Ast): Record<string, string> | null {
switch (ast?.type) {
case "InlineList":
return ast.elements.length < 1 ? {} : null;
case "InlineMap":
return Object.fromEntries(Object.entries(ast.elements).map(([key, el]) => [key, printFragment(input, el)]));
case "CompoundExpression":
if (ast.expressionComponents[0].type !== "VariableReference") return null;
if (ast.expressionComponents[0].variableName !== "AGG") return null;
if (ast.expressionComponents[1].type !== "MethodReference") return null;
if (ast.expressionComponents[1].methodName !== "map") return null;
return mapToObject(input, ast.expressionComponents[1].args[0]);
default:
return null;
}
}

export function parseToList(input: string): string[] {
return mapToList(input, getAst(input));
}

export function parseToObject(input: string): Record<string, string> {
return mapToObject(input, getAst(input));
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { parseToList, parseToObject } from "./pareserHelpers";

describe("parseHelpers", () => {
describe("parseToList", () => {
it.each([
[`aaa`, null],
[`"aaa"`, null],
[`{`, null],
[`{}`, null],
[`{123}`, null],
[`{}.xxxx`, null],
[`{}.toString`, []],
[`#COLLECTION.join({}, "|")`, null],
[`#xxxx.yyyy({}, "|")`, null],
[`{123}.toString`, ["123"]],
[`{ 123 }.toString`, ["123"]],
[`{123,456}.toString`, ["123", "456"]],
[`{ 123, 456 }.toString`, ["123", "456"]],
[`{ 123, aaa, 456 }.toString`, ["123", "aaa", "456"]],
[`{ 123, aaa.bbb }.toString`, ["123", "aaa.bbb"]],
[`{ 123, aaa_bbb }.toString`, ["123", "aaa_bbb"]],
[`{ 123, aaa bbb, ccc }.toString`, ["123", "aaa", "bbb", "ccc"]],
[`{ 123, "aaa bbb" }.toString`, ["123", `"aaa bbb"`]],
[`{ 123, 'aaa bbb' }.toString`, ["123", `'aaa bbb'`]],
[`{ 123, #aaa.bbb }.toString`, ["123", `#aaa.bbb`]],
[`{ 123, 123.bbb }.toString`, ["123", `123.bbb`]],
[`{ 123, 123.456.bbb }.toString`, ["123", `123.456.bbb`]],
[`{ 123, "{ 123, 456 }" }.toString`, ["123", `"{ 123, 456 }"`]],
[`{ 123, { 123, "456" } }.toString`, ["123", `{ 123, "456" }`]],
[`{ 123, { aaa: 123, bbb: "456" } }.toString`, ["123", `{ aaa: 123, bbb: "456" }`]],
[`{ 123, {123,456}.^[2 + 5].test(123) }.toString`, ["123", `{123,456}.^[2 + 5].test(123)`]],
[`#COLLECTION.join({ 123, 456 }, "|")`, null],
])("should parse: %s to: %s", (input, output) => {
expect(parseToList(input)).toEqual(output);
});
});

describe("parseToObject", () => {
it.each([
[`aaa`, null],
[`"aaa"`, null],
[`{}`, {}],
[`#AGG.map({})`, {}],
[`#AGG.map({:})`, {}],
[`#AGG2.map({})`, null],
[`{aaa:123}`, { aaa: "123" }],
[`#AGG.map({aaa:123})`, { aaa: "123" }],
[`#AGG.map({ aaa:123, bbb:456 })`, { aaa: "123", bbb: "456" }],
[`{ aaa: 123, bbb: "456" }`, { aaa: "123", bbb: `"456"` }],
[`{ a: 123, b c: 123 }`, { a: "123" }],
[`{ aaa: 1 2 3 }`, { aaa: "1" }],
[`{ "a a a": 123 }`, { "a a a": "123" }],
[`{ aaa: #aaa.bbb }`, { aaa: "#aaa.bbb" }],
[`{ aaa: aaa.bbb }`, { aaa: "aaa.bbb" }],
[`{ aaa: "aaa bbb" }`, { aaa: `"aaa bbb"` }],
[`{ aaa: {} }`, { aaa: `{}` }],
[`{ aaa: { bbb: 123, ccc: "456" } }`, { aaa: `{ bbb: 123, ccc: "456" }` }],
[`{ aaa: { 123, "456" } }`, { aaa: `{ 123, "456" }` }],
[`{ aaa: "{ 123, '456' }" }`, { aaa: `"{ 123, '456' }"` }],
[`{ aaa: '{ 123, "456" }' }`, { aaa: `'{ 123, "456" }'` }],
[`{ aaa: '{ 123, "456" }' }`, { aaa: `'{ 123, "456" }'` }],
[`{ aaa:\n { 123,\n "456" } }`, { aaa: `{ 123,\n "456" }` }],
[`{ aaa: {123,456}.![2 + 5] }`, { aaa: "{123,456}.![2 + 5]" }],
[`{ aaa: {123,456}.![2 + 5], bbb: 123 }`, { aaa: `{123,456}.![2 + 5]`, bbb: `123` }],
[`{ aaa: {123,456}.?[2 + 5] }`, { aaa: `{123,456}.?[2 + 5]` }],
[`{ aaa: {123,456}.^[2 + 5] }`, { aaa: `{123,456}.^[2 + 5]` }],
[`{ aaa: {123,456}.^[2 + 5].test(123) }`, { aaa: `{123,456}.^[2 + 5].test(123)` }],
[`{ aaa: 123 + 456 }`, { aaa: `123 + 456` }],
])("should parse: %s to: %s", (input, output) => {
expect(parseToObject(input)).toEqual(output);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { padStart } from "lodash";
import { useCallback, useMemo } from "react";
import { AggMapLikeParser } from "./aggMapLikeParser";
import { useCallback } from "react";
import { parseToList, parseToObject } from "./pareserHelpers";

export function useAggParamsSerializer(): [
(text: string) => Record<string, string>,
(paramName: string, map: Record<string, string>) => string,
] {
const parser = useMemo(() => new AggMapLikeParser(), []);

const deserialize = useCallback((input: string) => parser.parseObject(input), [parser]);

const serialize = useCallback((paramName: string, map: Record<string, string>): string => {
const entries = Object.entries(map || {}).map(([key, value]) => {
const trimmedKey = key.trim();
Expand All @@ -27,14 +23,10 @@ export function useAggParamsSerializer(): [
}
}, []);

return [deserialize, serialize];
return [parseToObject, serialize];
}

export function useGroupByParamsSerializer(): [(text: string) => string[], (paramName: string, arr: string[]) => string] {
const parser = useMemo(() => new AggMapLikeParser(), []);

const deserialize = useCallback((input: string) => parser.parseList(input), [parser]);

const serialize = useCallback((paramName: string, arr: string[]): string => {
const entries = arr.map((value) => {
return value?.trim();
Expand All @@ -50,5 +42,5 @@ export function useGroupByParamsSerializer(): [(text: string) => string[], (para
}
}, []);

return [deserialize, serialize];
return [parseToList, serialize];
}

0 comments on commit 5d5f320

Please sign in to comment.