ts-dynamic-query
Advanced tools
Comparing version 1.0.34 to 1.0.35
@@ -7,11 +7,15 @@ import { SortDescriptorBase } from "./sortDescriptorBase"; | ||
export declare class DynamicQuery<T> { | ||
type: new () => T; | ||
filters: FilterDescriptorBase[]; | ||
sorts: SortDescriptorBase[]; | ||
selectedProperties: string[]; | ||
constructor(); | ||
static createInstance<T>(): DynamicQuery<T>; | ||
static createQuery<T>(type: new () => T): DynamicQuery<T>; | ||
addFilters(filters: FilterDescriptorBase[]): DynamicQuery<T>; | ||
addFilter(option: FilterOptions<T>): DynamicQuery<T>; | ||
addFilterGroup(option: FilterGroupOptions<T>): DynamicQuery<T>; | ||
addFilterDescriptor(option: FilterOptions<T>): DynamicQuery<T>; | ||
addFilterGroupDescriptor(option: FilterGroupOptions<T>): DynamicQuery<T>; | ||
addSorts(sorts: SortDescriptorBase[]): DynamicQuery<T>; | ||
addSort(sortOption: SortOptions<T>): DynamicQuery<T>; | ||
addSortDescriptor(sortOption: SortOptions<T>): DynamicQuery<T>; | ||
selectProperty(property: keyof T): DynamicQuery<T>; | ||
selectProperties(...properties: (keyof T)[]): DynamicQuery<T>; | ||
query(datas: T[]): T[]; | ||
@@ -18,0 +22,0 @@ toJSON(): string; |
@@ -16,5 +16,8 @@ "use strict"; | ||
this.sorts = []; | ||
this.selectedProperties = []; | ||
} | ||
DynamicQuery.createInstance = function () { | ||
return new DynamicQuery(); | ||
DynamicQuery.createQuery = function (type) { | ||
var instance = new DynamicQuery(); | ||
instance.type = type; | ||
return instance; | ||
}; | ||
@@ -25,3 +28,3 @@ DynamicQuery.prototype.addFilters = function (filters) { | ||
}; | ||
DynamicQuery.prototype.addFilter = function (option) { | ||
DynamicQuery.prototype.addFilterDescriptor = function (option) { | ||
var filter = new filterDescriptor_1.FilterDescriptor(option); | ||
@@ -31,3 +34,3 @@ this.filters.push(filter); | ||
}; | ||
DynamicQuery.prototype.addFilterGroup = function (option) { | ||
DynamicQuery.prototype.addFilterGroupDescriptor = function (option) { | ||
var filterGroupDescriptor = new filterGroupDescriptor_1.FilterGroupDescriptor(); | ||
@@ -48,3 +51,3 @@ filterGroupDescriptor.condition = ts_commons_1.ObjectUtils.isNullOrUndefined(option.condition) | ||
}; | ||
DynamicQuery.prototype.addSort = function (sortOption) { | ||
DynamicQuery.prototype.addSortDescriptor = function (sortOption) { | ||
var sort = new sortDescriptor_1.SortDescriptor(sortOption); | ||
@@ -55,2 +58,16 @@ this.sorts.push(sort); | ||
}; | ||
DynamicQuery.prototype.selectProperty = function (property) { | ||
var propertyStr = property.toString(); | ||
this.selectedProperties.push(propertyStr); | ||
return this; | ||
}; | ||
DynamicQuery.prototype.selectProperties = function () { | ||
var properties = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
properties[_i] = arguments[_i]; | ||
} | ||
var propertyStrs = properties.map(function (x) { return x.toString(); }); | ||
this.selectedProperties = this.selectedProperties.concat(propertyStrs); | ||
return this; | ||
}; | ||
DynamicQuery.prototype.query = function (datas) { | ||
@@ -57,0 +74,0 @@ return queryProvider_1.QueryProvider.query(datas, this); |
import { DynamicQuery } from "../models/dynamicQuery"; | ||
export declare class QueryProvider { | ||
static query<T>(datas: T[], query: DynamicQuery<T>): T[]; | ||
private static pick; | ||
} |
@@ -10,2 +10,3 @@ "use strict"; | ||
QueryProvider.query = function (datas, query) { | ||
var _this = this; | ||
if (ts_commons_1.ArrayUtils.isEmpty(datas)) { | ||
@@ -28,4 +29,18 @@ return []; | ||
} | ||
if (!ts_commons_1.ArrayUtils.isEmpty(query.selectedProperties)) { | ||
cloneDatas = cloneDatas.map(function (x) { | ||
return _this.pick(query.type, x, query.selectedProperties); | ||
}); | ||
} | ||
return cloneDatas; | ||
}; | ||
QueryProvider.pick = function (type, data, selectedProperties) { | ||
var newObj = ts_commons_1.ObjectUtils.createObject(type); | ||
for (var key in data) { | ||
if (selectedProperties.indexOf(key) >= 0) { | ||
newObj[key] = data[key]; | ||
} | ||
} | ||
return newObj; | ||
}; | ||
return QueryProvider; | ||
@@ -32,0 +47,0 @@ }()); |
{ | ||
"name": "ts-dynamic-query", | ||
"version": "1.0.34", | ||
"version": "1.0.35", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
76591
54
1125