Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: refactor getPropertyByStringMethod #38

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions common/changes/@educorvi/rita/develop_2024-09-20-13-44.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@educorvi/rita",
"comment": "refactor getPropertyByStringMethod",
"type": "patch"
}
],
"packageName": "@educorvi/rita"
}
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
Loading