Skip to content

Commit 118b6bc

Browse files
committed
fixed get value by property string with sublevel object
1 parent 9edadf9 commit 118b6bc

File tree

3 files changed

+49
-17
lines changed

3 files changed

+49
-17
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{
22
"name": "lambda-expression",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
44
"description": "Utility to analyze function (js) and arrow functions (ts), and create metadata of expressions, the initial scope is that it seeks to solve simple expressions. And later advance to encompass complex expressions.",
55
"main": "./src/main.js",
66
"types": "./src/main.d.ts",
77
"scripts": {
88
"test": "tsc & mocha src/**/*.spec.js",
9+
"test-grep": "tsc & mocha src/**/*.spec.js --grep",
10+
"test-single": "tsc & mocha src/**/*.spec.js --grep Mapper --debug-brk",
911
"showcase": "cd .\\showcase\\ npm run start-showcase"
1012
},
1113
"keywords": [

src/expression-utils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class ExpressionUtils {
77

88
}
99

10-
public getValueByExpression<T>(instance: any, expression: Expression<T>) {
10+
public getValueByExpression<T>(instance: T, expression: Expression<T>) {
1111
return expression(instance);
1212
}
1313

@@ -20,6 +20,9 @@ export class ExpressionUtils {
2020
}
2121

2222
public getValue(instance: any, property: string) {
23+
if (property.indexOf(".") > -1) {
24+
return this.getValueByProperties(instance, property.split("."));
25+
}
2326
if (instance) {
2427
return instance[property];
2528
}

src/test/test.spec.ts

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ReferencesModelTest } from './models/reference-model-test';
12
import { LambdaExpressionMetadata } from './../metadatas';
23
import { ModelTest } from "./models/model-test";
34
import { expect } from "chai";
@@ -53,6 +54,47 @@ describe("Expression method", () => {
5354

5455
});
5556

57+
describe("Expression get value", () => {
58+
let instance = <ModelTest>{
59+
id: 1,
60+
name: "Test",
61+
description: "Descrição",
62+
date: new Date(),
63+
isValid: true,
64+
reference: <ReferencesModelTest>{
65+
id: 2,
66+
name: "Reference"
67+
}
68+
};
69+
70+
it("get description", () => {
71+
const result = _expressionUtils.getValue(instance, "description")
72+
expect(result).to.equal("Descrição");
73+
});
74+
75+
it("get reference.id", () => {
76+
const result = _expressionUtils.getValue(instance, "reference.id")
77+
expect(result).to.equal(2);
78+
});
79+
80+
it("get reference.name", () => {
81+
const result = _expressionUtils.getValue(instance, "reference.name")
82+
expect(result).to.equal("Reference");
83+
});
84+
85+
it("get by expression reference.name", () => {
86+
const result = _expressionUtils.getValueByExpression(instance, x => x.reference.name)
87+
expect(result).to.equal("Reference");
88+
});
89+
90+
it("get reference.abc not exist", () => {
91+
const result = _expressionUtils.getValue(instance, "reference.abc")
92+
expect(result).to.equal(void 0);
93+
});
94+
95+
96+
});
97+
5698
describe("Lambda Expression method", () => {
5799

58100
it("should return id == 0", () => {
@@ -81,19 +123,4 @@ describe("Lambda Expression method", () => {
81123
expect(result2.columnRight).to.equal(`'x.reference.id'`);
82124
});
83125

84-
85-
// it("should test", () => {
86-
// const result = ExpressionUsage.lambda<ModelTest>((x) => x.id >= x.reference.id);
87-
// expect(result.columnLeft).to.equal("x.id");
88-
// expect(result.operator).to.equal(">=");
89-
// expect(result.columnRight).to.equal("x.reference.id");
90-
91-
// console.log(_expressionUtils.getColumnByLambdaExpression<ModelTest>((x) => x.id >= x.reference.id));
92-
// console.log(_expressionUtils.getColumnByLambdaExpression<ModelTest>((x) => x.id >= 0));
93-
94-
// const propertyRight = _expressionUtils.getPropertiesByExpressionString(result.expressionRight);
95-
// console.log(propertyRight);
96-
// console.log(_expressionUtils.getColumnByProperties(propertyRight));
97-
// });
98-
99126
});

0 commit comments

Comments
 (0)