Skip to content

Commit

Permalink
fixed parser issue
Browse files Browse the repository at this point in the history
  • Loading branch information
jp-cen committed Dec 16, 2024
1 parent 1a350d6 commit cacf5ff
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 80 deletions.
124 changes: 62 additions & 62 deletions main.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -71,30 +71,38 @@ export const FormulaCell = (
};

return parsedValue.type == "boolean" ? (
<BooleanCell {...props} editMode={CellEditMode.EditModeReadOnly} />
) : props.initialValue?.length == 0 ? (
<BooleanCell
{...props}
initialValue={initialValue}
editMode={CellEditMode.EditModeReadOnly}
/>
) : initialValue?.length == 0 ? (
<></>
) : parsedValue.type == "image" ? (
<ImageCell
{...props}
initialValue={initialValue}
editMode={CellEditMode.EditModeReadOnly}
multi={true}
/>
) : parsedValue.type == "icon" ? (
<IconCell
{...props}
initialValue={initialValue}
multi={true}
editMode={CellEditMode.EditModeReadOnly}
></IconCell>
) : parsedValue.type == "link" ? (
<LinkCell
{...props}
initialValue={initialValue}
multi={true}
editMode={CellEditMode.EditModeReadOnly}
></LinkCell>
) : (
<OptionCell
{...props}
initialValue={initialValue}
editMode={CellEditMode.EditModeReadOnly}
multi={true}
source={props.source}
Expand Down
36 changes: 24 additions & 12 deletions src/core/utils/formula/formulas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,9 @@ export const formulasInfos : Record<string, FormulaInfo> = {
difficulty: 2
},

items: {
name: "items",
fn: "items",
spaceItems: {
name: "spaceItems",
fn: "spaceItems",
args: [{name: 'path', types: ["text"]}],
returnType: "link-multi",
description: "Get the items inside of a path",
Expand Down Expand Up @@ -612,26 +612,34 @@ const path = (args: MathNode[], math: any, scope: Map<string, any>) => {
const res = args.map(function (arg) {
return arg.compile().evaluate(scope)
})
const value = (scope.get("$paths") as Map<string, PathState>).get(res[0]);
let path = res[0];
if (typeof res[0] != 'string' && res[0].path) {
path = res[0].path;
}
const value = (scope.get("$paths") as Map<string, PathState>).get(path);
return value;

}
path.rawArgs = true;

const items = (args: MathNode[], math: any, scope: Map<string, any>) => {
const spaceItems = (args: MathNode[], math: any, scope: Map<string, any>) => {
if (args.length !== 1) {
return "";
}
const res = args.map(function (arg) {
return arg.compile().evaluate(scope)
})
const value = (scope.get("$items") as Map<string, Set<string>>).get(res[0]);
let path = res[0];
if (typeof res[0] != 'string' && res[0].path) {
path = res[0].path;
}
const value = (scope.get("$items") as Map<string, Set<string>>).get(path);
const paths = (scope.get("$paths") as Map<string, PathState>);
const result = [...(value ?? [])].map(f => paths.get(f));
return result;

}
items.rawArgs = true;
spaceItems.rawArgs = true;

const spaces = (args: MathNode[], math: any, scope: Map<string, any>) => {
if (args.length !== 1) {
Expand All @@ -640,7 +648,11 @@ const spaces = (args: MathNode[], math: any, scope: Map<string, any>) => {
const res = args.map(function (arg) {
return arg.compile().evaluate(scope)
})
const value = (scope.get("$spaces") as Map<string, Set<string>>).get(res[0]);
let path = res[0];
if (typeof res[0] != 'string' && res[0].path) {
path = res[0].path;
}
const value = (scope.get("$spaces") as Map<string, Set<string>>).get(path);
const paths = (scope.get("$paths") as Map<string, PathState>);
const result = [...(value ?? [])].map(f => paths.get(f));
return result;
Expand Down Expand Up @@ -951,8 +963,7 @@ export const formulas = {
"now": () => {
return new Date();
},
"items": items,
"spaces": spaces,

"minute": (date: Date) => {
return date.getMinutes()
},
Expand Down Expand Up @@ -1097,6 +1108,7 @@ export const formulas = {
flat: flat,
path: path,
let: letFunction,
lets: letsFunction

lets: letsFunction,
spaceItems: spaceItems,
spaces: spaces,
};
3 changes: 3 additions & 0 deletions src/core/utils/formula/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ export const runFormulaWithContext = (runContext: math.MathJsInstance, paths: Ma
runContext.evaluate("current = _current()", scope)
value = runContext.evaluate(formula, scope)
value = parseProperty("", value)
if (typeof value != "string") {
if (emitError) throw(value)
}
} catch (e) {
value = ""
if (emitError) throw(e)
Expand Down
6 changes: 5 additions & 1 deletion src/core/utils/strings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ export function ensureStringValueFromSet(value: unknown, values: string[], defau
export function ensureString(value: unknown): string {
if (!value) return ""
if (typeof value !== 'string') {
return value.toString();
const newValue = value.toString();
if (typeof newValue === 'string') {
return newValue;
}
return '';
}
return value;
}
Expand Down
17 changes: 14 additions & 3 deletions src/utils/parsers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { ContextLookup, PathPropertyName } from "core/types/context";
import { ensureArray, indexOfCharElseEOS } from "core/utils/strings";
import { ensureArray, ensureString, indexOfCharElseEOS } from "core/utils/strings";
import { format } from "date-fns";
import { detectPropertyType } from "./properties";
import { serializeMultiDisplayString, serializeMultiString } from "./serializers";

export const parseMultiString = (str: string): string[] => str?.startsWith("[") ? ensureArray(safelyParseJSON(str)) : parseMultiDisplayString(str)
export const parseMultiString = (str: string): string[] => str?.startsWith("[") ? ensureArray(safelyParseJSON(str)).map(f => ensureString(f)) : parseMultiDisplayString(str)

export const parseMultiDisplayString = (str: string):string[] => (str?.replace('\\,', ',')?.match(/(\\.|[^,])+/g) ?? []).map(f => f.trim());
export const parseProperty = (field: string, value: any, type?: string) : string => {
Expand All @@ -16,8 +16,19 @@ export const parseMultiString = (str: string): string[] => str?.startsWith("[")
}
break;
case "object":
case "object-multi":
case "object-multi":
{
if (Array.isArray(value)) {
if (value[0].path) {
return JSON.stringify(value.map((v: any) => v.path));
}
} else {
if (value.path) {
return value.path;
}
}
return JSON.stringify(value);
}
break;
case "number":
return (value as number).toString();
Expand Down

0 comments on commit cacf5ff

Please sign in to comment.