ts-dynamic-query
Advanced tools
Comparing version 1.0.14 to 1.0.15
export * from "./enums"; | ||
export * from "./models"; | ||
export * from "./providers"; | ||
export * from "./helpers"; |
@@ -9,2 +9,3 @@ "use strict"; | ||
__export(require("./providers")); | ||
__export(require("./helpers")); | ||
//# sourceMappingURL=index.js.map |
@@ -11,3 +11,3 @@ import { SortDescriptorBase } from "./sortDescriptorBase"; | ||
addFilters(filters: FilterDescriptorBase[]): DynamicQuery<T>; | ||
addFilter(filterOption: FilterOptions<T>): DynamicQuery<T>; | ||
addFilter(option: FilterOptions<T>): DynamicQuery<T>; | ||
addFilterGroup(option: FilterGroupOptions<T>): DynamicQuery<T>; | ||
@@ -14,0 +14,0 @@ addSorts(sorts: SortDescriptorBase[]): DynamicQuery<T>; |
@@ -20,4 +20,4 @@ "use strict"; | ||
} | ||
addFilter(filterOption) { | ||
const filter = new filterDescriptor_1.FilterDescriptor(filterOption); | ||
addFilter(option) { | ||
const filter = new filterDescriptor_1.FilterDescriptor(option); | ||
this.filters.push(filter); | ||
@@ -24,0 +24,0 @@ return this; |
export * from "./enums"; | ||
export * from "./models"; | ||
export * from "./providers"; | ||
export * from "./helpers"; |
@@ -9,2 +9,3 @@ "use strict"; | ||
__export(require("./providers")); | ||
__export(require("./helpers")); | ||
//# sourceMappingURL=index.js.map |
@@ -11,3 +11,3 @@ import { SortDescriptorBase } from "./sortDescriptorBase"; | ||
addFilters(filters: FilterDescriptorBase[]): DynamicQuery<T>; | ||
addFilter(filterOption: FilterOptions<T>): DynamicQuery<T>; | ||
addFilter(option: FilterOptions<T>): DynamicQuery<T>; | ||
addFilterGroup(option: FilterGroupOptions<T>): DynamicQuery<T>; | ||
@@ -14,0 +14,0 @@ addSorts(sorts: SortDescriptorBase[]): DynamicQuery<T>; |
@@ -20,4 +20,4 @@ "use strict"; | ||
}; | ||
DynamicQuery.prototype.addFilter = function (filterOption) { | ||
var filter = new filterDescriptor_1.FilterDescriptor(filterOption); | ||
DynamicQuery.prototype.addFilter = function (option) { | ||
var filter = new filterDescriptor_1.FilterDescriptor(option); | ||
this.filters.push(filter); | ||
@@ -24,0 +24,0 @@ return this; |
@@ -1,31 +0,180 @@ | ||
// import { expect } from "chai"; | ||
// import { FilterDescriptor, DynamicQuery } from "../../src/models"; | ||
// import { FilterOperator, FilterCondition } from "../../src/enums"; | ||
// describe(".FilterDescriptor", () => { | ||
// class Student { | ||
// name: string; | ||
// age: number; | ||
// } | ||
// describe("#serialize", () => { | ||
// it("should to json", () => { | ||
// const nameFilter = new FilterDescriptor<Student>({ | ||
// propertyPath: "name", | ||
// operator: FilterOperator.EQUAL, | ||
// value: "Frank" | ||
// }); | ||
// const query = new DynamicQuery<Student>(); | ||
// query.addFilters(nameFilter); | ||
// const result = query.toJSON(); | ||
// expect( | ||
// `{"filters":[{"condition":0,"type":"FilterDescriptor","operator":2,"propertyPath":"name","value":"Frank"}],"sorts":[]}` | ||
// ).to.be.eq(result); | ||
// }); | ||
// }); | ||
// describe("#deserialize", () => { | ||
// it("should to json", () => { | ||
// const json = `{"filters":[{"condition":0,"type":"FilterDescriptor","operator":2,"propertyPath":"name","value":"Frank"}],"sorts":[]}`; | ||
// const result = new DynamicQuery<Student>().fromJSON(json); | ||
// }); | ||
// }); | ||
// }); | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var chai_1 = require("chai"); | ||
var models_1 = require("../../src/models"); | ||
var enums_1 = require("../../src/enums"); | ||
var ts_commons_1 = require("ts-commons"); | ||
describe(".dynamicQuery", function () { | ||
var Student = /** @class */ (function () { | ||
function Student() { | ||
} | ||
return Student; | ||
}()); | ||
describe("#init", function () { | ||
it("should have default value", function () { | ||
var query = new models_1.DynamicQuery(); | ||
chai_1.expect(true).to.be.eq(ts_commons_1.ObjectUtils.isArray(query.filters)); | ||
chai_1.expect(true).to.be.eq(ts_commons_1.ObjectUtils.isArray(query.sorts)); | ||
}); | ||
}); | ||
describe("#addFilters", function () { | ||
it("should add filters", function () { | ||
var nameFilter = new models_1.FilterDescriptor({ | ||
propertyPath: "name", | ||
operator: enums_1.FilterOperator.EQUAL, | ||
value: "test" | ||
}); | ||
var query = new models_1.DynamicQuery().addFilters([nameFilter]); | ||
chai_1.expect(nameFilter).to.be.eq(query.filters[0]); | ||
}); | ||
}); | ||
describe("#addSorts", function () { | ||
it("should add sorts", function () { | ||
var ageSort = new models_1.SortDescriptor({ | ||
propertyPath: "age", | ||
direction: enums_1.SortDirection.DESC | ||
}); | ||
var query = new models_1.DynamicQuery().addSorts([ageSort]); | ||
chai_1.expect(ageSort).to.be.eq(query.sorts[0]); | ||
}); | ||
}); | ||
describe("#addFilter", function () { | ||
it("should add filter option", function () { | ||
var query = new models_1.DynamicQuery().addFilter({ | ||
propertyPath: "name", | ||
operator: enums_1.FilterOperator.EQUAL, | ||
value: true | ||
}); | ||
var filter = query.filters[0]; | ||
chai_1.expect("name").to.be.eq(filter.propertyPath); | ||
chai_1.expect(enums_1.FilterOperator.EQUAL).to.be.eq(filter.operator); | ||
chai_1.expect(true).to.be.eq(filter.value); | ||
console.log("========================", query.toJSON()); | ||
}); | ||
}); | ||
describe("#addFilterGroup", function () { | ||
it("should add filterGroup default value", function () { | ||
var query = new models_1.DynamicQuery().addFilterGroup({ | ||
options: [ | ||
{ | ||
propertyPath: "name", | ||
operator: enums_1.FilterOperator.EQUAL, | ||
value: "test" | ||
}, | ||
{ | ||
propertyPath: "name", | ||
operator: enums_1.FilterOperator.START_WITH, | ||
value: "aa" | ||
} | ||
] | ||
}); | ||
var groupFilter = query.filters[0]; | ||
var filter1 = groupFilter.filters[0]; | ||
var filter2 = groupFilter.filters[1]; | ||
chai_1.expect(2).to.be.eq(groupFilter.filters.length); | ||
chai_1.expect("name").to.be.eq(filter1.propertyPath); | ||
chai_1.expect(enums_1.FilterOperator.EQUAL).to.be.eq(filter1.operator); | ||
chai_1.expect("test").to.be.eq(filter1.value); | ||
chai_1.expect("name").to.be.eq(filter2.propertyPath); | ||
chai_1.expect(enums_1.FilterOperator.START_WITH).to.be.eq(filter2.operator); | ||
chai_1.expect("aa").to.be.eq(filter2.value); | ||
}); | ||
it("should add filterGroup default value", function () { | ||
var query = new models_1.DynamicQuery().addFilterGroup({ | ||
condition: enums_1.FilterCondition.OR, | ||
options: [ | ||
{ | ||
propertyPath: "name", | ||
operator: enums_1.FilterOperator.EQUAL, | ||
value: "test" | ||
}, | ||
{ | ||
propertyPath: "name", | ||
operator: enums_1.FilterOperator.START_WITH, | ||
value: "aa" | ||
} | ||
] | ||
}); | ||
var groupFilter = query.filters[0]; | ||
var filter1 = groupFilter.filters[0]; | ||
var filter2 = groupFilter.filters[1]; | ||
chai_1.expect(enums_1.FilterCondition.OR).to.be.eq(groupFilter.condition); | ||
chai_1.expect(2).to.be.eq(groupFilter.filters.length); | ||
chai_1.expect("name").to.be.eq(filter1.propertyPath); | ||
chai_1.expect(enums_1.FilterOperator.EQUAL).to.be.eq(filter1.operator); | ||
chai_1.expect("test").to.be.eq(filter1.value); | ||
chai_1.expect("name").to.be.eq(filter2.propertyPath); | ||
chai_1.expect(enums_1.FilterOperator.START_WITH).to.be.eq(filter2.operator); | ||
chai_1.expect("aa").to.be.eq(filter2.value); | ||
}); | ||
}); | ||
describe("#toJSON", function () { | ||
it("should get json", function () { | ||
var query = new models_1.DynamicQuery() | ||
.addFilter({ | ||
propertyPath: "age", | ||
operator: enums_1.FilterOperator.GREATER_THAN, | ||
value: 20 | ||
}) | ||
.addFilterGroup({ | ||
options: [ | ||
{ | ||
propertyPath: "name", | ||
operator: enums_1.FilterOperator.EQUAL, | ||
value: "test" | ||
}, | ||
{ | ||
propertyPath: "name", | ||
operator: enums_1.FilterOperator.START_WITH, | ||
value: "aa" | ||
} | ||
] | ||
}) | ||
.addSort({ | ||
propertyPath: "age", | ||
direction: enums_1.SortDirection.DESC | ||
}); | ||
var json = query.toJSON(); | ||
var expectStr = "{\"filters\":[{\"condition\":0,\"type\":\"FilterDescriptor\",\"operator\":5,\"propertyPath\":\"age\",\"ignoreCase\":false,\"value\":20},{\"condition\":0,\"type\":\"FilterGroupDescriptor\",\"filters\":[{\"condition\":0,\"type\":\"FilterDescriptor\",\"operator\":2,\"propertyPath\":\"name\",\"ignoreCase\":false,\"value\":\"test\"},{\"condition\":0,\"type\":\"FilterDescriptor\",\"operator\":6,\"propertyPath\":\"name\",\"ignoreCase\":false,\"value\":\"aa\"}]}],\"sorts\":[{\"direction\":1,\"type\":\"SortDescriptor\",\"propertyPath\":\"age\"}]}"; | ||
chai_1.expect(expectStr).to.be.eq(json); | ||
}); | ||
}); | ||
describe("#fromJSON", function () { | ||
it("get from json", function () { | ||
var json = "{\"filters\":[{\"condition\":0,\"type\":\"FilterDescriptor\",\"operator\":5,\"propertyPath\":\"age\",\"ignoreCase\":false,\"value\":20},{\"condition\":0,\"type\":\"FilterGroupDescriptor\",\"filters\":[{\"condition\":0,\"type\":\"FilterDescriptor\",\"operator\":2,\"propertyPath\":\"name\",\"ignoreCase\":false,\"value\":\"test\"},{\"condition\":0,\"type\":\"FilterDescriptor\",\"operator\":6,\"propertyPath\":\"name\",\"ignoreCase\":false,\"value\":\"aa\"}]}],\"sorts\":[{\"direction\":1,\"type\":\"SortDescriptor\",\"propertyPath\":\"age\"}]}"; | ||
var query = new models_1.DynamicQuery().fromJSON(json); | ||
var sort = query.sorts[0]; | ||
var filter = query.filters[0]; | ||
chai_1.expect("age").to.be.eq(sort.propertyPath); | ||
chai_1.expect(enums_1.SortDirection.DESC).to.be.eq(sort.direction); | ||
chai_1.expect("age").to.be.eq(filter.propertyPath); | ||
chai_1.expect(enums_1.FilterOperator.GREATER_THAN).to.be.eq(filter.operator); | ||
chai_1.expect(20).to.be.eq(filter.value); | ||
chai_1.expect(false).to.be.eq(filter.ignoreCase); | ||
}); | ||
}); | ||
describe("#query", function () { | ||
it("query student", function () { | ||
var students = [ | ||
{ | ||
name: "test", | ||
age: 20 | ||
}, | ||
{ | ||
name: "frank", | ||
age: 11 | ||
} | ||
]; | ||
var query = new models_1.DynamicQuery().addFilter({ | ||
propertyPath: "age", | ||
operator: enums_1.FilterOperator.GREATER_THAN, | ||
value: 12 | ||
}); | ||
var filteredStudents = query.query(students); | ||
var matchedStudent = filteredStudents[0]; | ||
chai_1.expect("test").to.be.eq(matchedStudent.name); | ||
chai_1.expect(20).to.be.eq(matchedStudent.age); | ||
}); | ||
}); | ||
}); | ||
//# sourceMappingURL=dynamicQuery.test.js.map |
@@ -17,5 +17,6 @@ "use strict"; | ||
operator: enums_1.FilterOperator.CONTAINS, | ||
value: "test" | ||
value: true | ||
}); | ||
chai_1.expect(enums_1.FilterCondition.AND).to.be.eq(nameFilter.condition); | ||
chai_1.expect(true).to.be.eq(nameFilter.value); | ||
}); | ||
@@ -22,0 +23,0 @@ it("condition will be set value", function () { |
{ | ||
"name": "ts-dynamic-query", | ||
"version": "1.0.14", | ||
"version": "1.0.15", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
export * from "./enums"; | ||
export * from "./models"; | ||
export * from "./providers"; | ||
export * from "./helpers"; |
@@ -28,4 +28,4 @@ import { SortDescriptorBase } from "./sortDescriptorBase"; | ||
public addFilter(filterOption: FilterOptions<T>): DynamicQuery<T> { | ||
const filter = new FilterDescriptor<T>(filterOption); | ||
public addFilter(option: FilterOptions<T>): DynamicQuery<T> { | ||
const filter = new FilterDescriptor<T>(option); | ||
this.filters.push(filter); | ||
@@ -32,0 +32,0 @@ return this; |
@@ -1,34 +0,209 @@ | ||
// import { expect } from "chai"; | ||
// import { FilterDescriptor, DynamicQuery } from "../../src/models"; | ||
// import { FilterOperator, FilterCondition } from "../../src/enums"; | ||
import { expect } from "chai"; | ||
import { | ||
FilterDescriptor, | ||
DynamicQuery, | ||
SortDescriptor, | ||
FilterGroupDescriptor | ||
} from "../../src/models"; | ||
import { | ||
FilterOperator, | ||
FilterCondition, | ||
SortDirection | ||
} from "../../src/enums"; | ||
import { ObjectUtils } from "ts-commons"; | ||
// describe(".FilterDescriptor", () => { | ||
// class Student { | ||
// name: string; | ||
// age: number; | ||
// } | ||
describe(".dynamicQuery", () => { | ||
class Student { | ||
name: string; | ||
age: number; | ||
} | ||
// describe("#serialize", () => { | ||
// it("should to json", () => { | ||
// const nameFilter = new FilterDescriptor<Student>({ | ||
// propertyPath: "name", | ||
// operator: FilterOperator.EQUAL, | ||
// value: "Frank" | ||
// }); | ||
describe("#init", () => { | ||
it("should have default value", () => { | ||
const query = new DynamicQuery(); | ||
expect(true).to.be.eq(ObjectUtils.isArray(query.filters)); | ||
expect(true).to.be.eq(ObjectUtils.isArray(query.sorts)); | ||
}); | ||
}); | ||
// const query = new DynamicQuery<Student>(); | ||
// query.addFilters(nameFilter); | ||
// const result = query.toJSON(); | ||
// expect( | ||
// `{"filters":[{"condition":0,"type":"FilterDescriptor","operator":2,"propertyPath":"name","value":"Frank"}],"sorts":[]}` | ||
// ).to.be.eq(result); | ||
// }); | ||
// }); | ||
describe("#addFilters", () => { | ||
it("should add filters", () => { | ||
const nameFilter = new FilterDescriptor<Student>({ | ||
propertyPath: "name", | ||
operator: FilterOperator.EQUAL, | ||
value: "test" | ||
}); | ||
// describe("#deserialize", () => { | ||
// it("should to json", () => { | ||
// const json = `{"filters":[{"condition":0,"type":"FilterDescriptor","operator":2,"propertyPath":"name","value":"Frank"}],"sorts":[]}`; | ||
// const result = new DynamicQuery<Student>().fromJSON(json); | ||
// }); | ||
// }); | ||
// }); | ||
const query = new DynamicQuery<Student>().addFilters([nameFilter]); | ||
expect(nameFilter).to.be.eq(query.filters[0]); | ||
}); | ||
}); | ||
describe("#addSorts", () => { | ||
it("should add sorts", () => { | ||
const ageSort = new SortDescriptor<Student>({ | ||
propertyPath: "age", | ||
direction: SortDirection.DESC | ||
}); | ||
const query = new DynamicQuery<Student>().addSorts([ageSort]); | ||
expect(ageSort).to.be.eq(query.sorts[0]); | ||
}); | ||
}); | ||
describe("#addFilter", () => { | ||
it("should add filter option", () => { | ||
const query = new DynamicQuery<Student>().addFilter({ | ||
propertyPath: "name", | ||
operator: FilterOperator.EQUAL, | ||
value: true | ||
}); | ||
const filter = query.filters[0] as FilterDescriptor<any>; | ||
expect("name").to.be.eq(filter.propertyPath); | ||
expect(FilterOperator.EQUAL).to.be.eq(filter.operator); | ||
expect(true).to.be.eq(filter.value); | ||
console.log("========================", query.toJSON()); | ||
}); | ||
}); | ||
describe("#addFilterGroup", () => { | ||
it("should add filterGroup default value", () => { | ||
const query = new DynamicQuery<Student>().addFilterGroup({ | ||
options: [ | ||
{ | ||
propertyPath: "name", | ||
operator: FilterOperator.EQUAL, | ||
value: "test" | ||
}, | ||
{ | ||
propertyPath: "name", | ||
operator: FilterOperator.START_WITH, | ||
value: "aa" | ||
} | ||
] | ||
}); | ||
const groupFilter = query.filters[0] as FilterGroupDescriptor<Student>; | ||
const filter1 = groupFilter.filters[0] as FilterDescriptor<Student>; | ||
const filter2 = groupFilter.filters[1] as FilterDescriptor<Student>; | ||
expect(2).to.be.eq(groupFilter.filters.length); | ||
expect("name").to.be.eq(filter1.propertyPath); | ||
expect(FilterOperator.EQUAL).to.be.eq(filter1.operator); | ||
expect("test").to.be.eq(filter1.value); | ||
expect("name").to.be.eq(filter2.propertyPath); | ||
expect(FilterOperator.START_WITH).to.be.eq(filter2.operator); | ||
expect("aa").to.be.eq(filter2.value); | ||
}); | ||
it("should add filterGroup default value", () => { | ||
const query = new DynamicQuery<Student>().addFilterGroup({ | ||
condition: FilterCondition.OR, | ||
options: [ | ||
{ | ||
propertyPath: "name", | ||
operator: FilterOperator.EQUAL, | ||
value: "test" | ||
}, | ||
{ | ||
propertyPath: "name", | ||
operator: FilterOperator.START_WITH, | ||
value: "aa" | ||
} | ||
] | ||
}); | ||
const groupFilter = query.filters[0] as FilterGroupDescriptor<Student>; | ||
const filter1 = groupFilter.filters[0] as FilterDescriptor<Student>; | ||
const filter2 = groupFilter.filters[1] as FilterDescriptor<Student>; | ||
expect(FilterCondition.OR).to.be.eq(groupFilter.condition); | ||
expect(2).to.be.eq(groupFilter.filters.length); | ||
expect("name").to.be.eq(filter1.propertyPath); | ||
expect(FilterOperator.EQUAL).to.be.eq(filter1.operator); | ||
expect("test").to.be.eq(filter1.value); | ||
expect("name").to.be.eq(filter2.propertyPath); | ||
expect(FilterOperator.START_WITH).to.be.eq(filter2.operator); | ||
expect("aa").to.be.eq(filter2.value); | ||
}); | ||
}); | ||
describe("#toJSON", () => { | ||
it("should get json", () => { | ||
const query = new DynamicQuery<Student>() | ||
.addFilter({ | ||
propertyPath: "age", | ||
operator: FilterOperator.GREATER_THAN, | ||
value: 20 | ||
}) | ||
.addFilterGroup({ | ||
options: [ | ||
{ | ||
propertyPath: "name", | ||
operator: FilterOperator.EQUAL, | ||
value: "test" | ||
}, | ||
{ | ||
propertyPath: "name", | ||
operator: FilterOperator.START_WITH, | ||
value: "aa" | ||
} | ||
] | ||
}) | ||
.addSort({ | ||
propertyPath: "age", | ||
direction: SortDirection.DESC | ||
}); | ||
const json = query.toJSON(); | ||
const expectStr = `{"filters":[{"condition":0,"type":"FilterDescriptor","operator":5,"propertyPath":"age","ignoreCase":false,"value":20},{"condition":0,"type":"FilterGroupDescriptor","filters":[{"condition":0,"type":"FilterDescriptor","operator":2,"propertyPath":"name","ignoreCase":false,"value":"test"},{"condition":0,"type":"FilterDescriptor","operator":6,"propertyPath":"name","ignoreCase":false,"value":"aa"}]}],"sorts":[{"direction":1,"type":"SortDescriptor","propertyPath":"age"}]}`; | ||
expect(expectStr).to.be.eq(json); | ||
}); | ||
}); | ||
describe("#fromJSON", () => { | ||
it("get from json", () => { | ||
const json = `{"filters":[{"condition":0,"type":"FilterDescriptor","operator":5,"propertyPath":"age","ignoreCase":false,"value":20},{"condition":0,"type":"FilterGroupDescriptor","filters":[{"condition":0,"type":"FilterDescriptor","operator":2,"propertyPath":"name","ignoreCase":false,"value":"test"},{"condition":0,"type":"FilterDescriptor","operator":6,"propertyPath":"name","ignoreCase":false,"value":"aa"}]}],"sorts":[{"direction":1,"type":"SortDescriptor","propertyPath":"age"}]}`; | ||
const query = new DynamicQuery<Student>().fromJSON(json); | ||
const sort = query.sorts[0] as SortDescriptor<Student>; | ||
const filter = query.filters[0] as FilterDescriptor<Student>; | ||
expect("age").to.be.eq(sort.propertyPath); | ||
expect(SortDirection.DESC).to.be.eq(sort.direction); | ||
expect("age").to.be.eq(filter.propertyPath); | ||
expect(FilterOperator.GREATER_THAN).to.be.eq(filter.operator); | ||
expect(20).to.be.eq(filter.value); | ||
expect(false).to.be.eq(filter.ignoreCase); | ||
}); | ||
}); | ||
describe("#query", () => { | ||
it("query student", () => { | ||
const students: Student[] = [ | ||
{ | ||
name: "test", | ||
age: 20 | ||
}, | ||
{ | ||
name: "frank", | ||
age: 11 | ||
} | ||
]; | ||
const query = new DynamicQuery<Student>().addFilter({ | ||
propertyPath: "age", | ||
operator: FilterOperator.GREATER_THAN, | ||
value: 12 | ||
}); | ||
const filteredStudents = query.query(students); | ||
const matchedStudent = filteredStudents[0]; | ||
expect("test").to.be.eq(matchedStudent.name); | ||
expect(20).to.be.eq(matchedStudent.age); | ||
}); | ||
}); | ||
}); |
@@ -16,5 +16,6 @@ import { expect } from "chai"; | ||
operator: FilterOperator.CONTAINS, | ||
value: "test" | ||
value: true | ||
}); | ||
expect(FilterCondition.AND).to.be.eq(nameFilter.condition); | ||
expect(true).to.be.eq(nameFilter.value); | ||
}); | ||
@@ -21,0 +22,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
203249
175
3375