Skip to content

Commit

Permalink
fix: refactor getPropertyByStringMethod
Browse files Browse the repository at this point in the history
  • Loading branch information
neferin12 committed Sep 20, 2024
1 parent d08995b commit 7034d81
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions rita-core/src/logicElements/Atom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,14 @@ export class Atom extends Formula {
/**
* Get the value of an object property or array by a path that is passed as string
* @param object object
* @private
* @param path path
* @param context the context of the formula
*/
getPropertyByString(object: any): boolean | string | number {
let path = this.path;
static getPropertyByString(
object: any,
path: string,
context?: Formula
): Promise<FormulaResults | FormulaResults[]> {
path = path.replace(/\[(\w+)]/g, '.$1'); // convert indexes to properties
path = path.replace(/^\./, ''); // strip a leading dot
const a = path.split('.');
Expand All @@ -49,13 +53,24 @@ export class Atom extends Formula {
} else {
throw new UndefinedPathError(
'Undefinded path in data: ' + path,
this
context
);
}
}
return object;
}

/**
* Get the value of an object property or array by a path that is passed as string
* @param object object
* @private
*/
getPropertyByString(
object: any
): Promise<FormulaResults | FormulaResults[]> {
return Atom.getPropertyByString(object, this.path, this);
}

validate(): boolean {
return !!this.path;
}
Expand Down

0 comments on commit 7034d81

Please sign in to comment.