Skip to content

Commit

Permalink
fixed get value by property string with sublevel object
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandocode committed Jan 30, 2018
1 parent 9edadf9 commit 118b6bc
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
{
"name": "lambda-expression",
"version": "0.1.1",
"version": "0.1.2",
"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.",
"main": "./src/main.js",
"types": "./src/main.d.ts",
"scripts": {
"test": "tsc & mocha src/**/*.spec.js",
"test-grep": "tsc & mocha src/**/*.spec.js --grep",
"test-single": "tsc & mocha src/**/*.spec.js --grep Mapper --debug-brk",
"showcase": "cd .\\showcase\\ npm run start-showcase"
},
"keywords": [
Expand Down
5 changes: 4 additions & 1 deletion src/expression-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class ExpressionUtils {

}

public getValueByExpression<T>(instance: any, expression: Expression<T>) {
public getValueByExpression<T>(instance: T, expression: Expression<T>) {
return expression(instance);
}

Expand All @@ -20,6 +20,9 @@ export class ExpressionUtils {
}

public getValue(instance: any, property: string) {
if (property.indexOf(".") > -1) {
return this.getValueByProperties(instance, property.split("."));
}
if (instance) {
return instance[property];
}
Expand Down
57 changes: 42 additions & 15 deletions src/test/test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ReferencesModelTest } from './models/reference-model-test';
import { LambdaExpressionMetadata } from './../metadatas';
import { ModelTest } from "./models/model-test";
import { expect } from "chai";
Expand Down Expand Up @@ -53,6 +54,47 @@ describe("Expression method", () => {

});

describe("Expression get value", () => {
let instance = <ModelTest>{
id: 1,
name: "Test",
description: "Descrição",
date: new Date(),
isValid: true,
reference: <ReferencesModelTest>{
id: 2,
name: "Reference"
}
};

it("get description", () => {
const result = _expressionUtils.getValue(instance, "description")
expect(result).to.equal("Descrição");
});

it("get reference.id", () => {
const result = _expressionUtils.getValue(instance, "reference.id")
expect(result).to.equal(2);
});

it("get reference.name", () => {
const result = _expressionUtils.getValue(instance, "reference.name")
expect(result).to.equal("Reference");
});

it("get by expression reference.name", () => {
const result = _expressionUtils.getValueByExpression(instance, x => x.reference.name)
expect(result).to.equal("Reference");
});

it("get reference.abc not exist", () => {
const result = _expressionUtils.getValue(instance, "reference.abc")
expect(result).to.equal(void 0);
});


});

describe("Lambda Expression method", () => {

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


// it("should test", () => {
// const result = ExpressionUsage.lambda<ModelTest>((x) => x.id >= x.reference.id);
// expect(result.columnLeft).to.equal("x.id");
// expect(result.operator).to.equal(">=");
// expect(result.columnRight).to.equal("x.reference.id");

// console.log(_expressionUtils.getColumnByLambdaExpression<ModelTest>((x) => x.id >= x.reference.id));
// console.log(_expressionUtils.getColumnByLambdaExpression<ModelTest>((x) => x.id >= 0));

// const propertyRight = _expressionUtils.getPropertiesByExpressionString(result.expressionRight);
// console.log(propertyRight);
// console.log(_expressionUtils.getColumnByProperties(propertyRight));
// });

});

0 comments on commit 118b6bc

Please sign in to comment.