[ARVADOS-WORKBENCH2] updated: 1.1.4-179-ge68620d
Git user
git at public.curoverse.com
Tue Jul 3 07:06:25 EDT 2018
Summary of changes:
src/common/api/order-builder.test.ts | 23 +++++++++++++-----
src/common/api/order-builder.ts | 45 ++++++++++++++++++++++++++++--------
2 files changed, 52 insertions(+), 16 deletions(-)
via e68620ddab69983f6bf127d51017bed2f21e0d4a (commit)
from c0b733ed434c3a89ea3fdfb840dc1d8d6c302d8c (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
commit e68620ddab69983f6bf127d51017bed2f21e0d4a
Author: Michal Klobukowski <michal.klobukowski at contractors.roche.com>
Date: Tue Jul 3 13:06:09 2018 +0200
Add immutability and concatenation of order builder
Feature #13703
Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski <michal.klobukowski at contractors.roche.com>
diff --git a/src/common/api/order-builder.test.ts b/src/common/api/order-builder.test.ts
index c184ebc..b80756d 100644
--- a/src/common/api/order-builder.test.ts
+++ b/src/common/api/order-builder.test.ts
@@ -6,11 +6,22 @@ import OrderBuilder from "./order-builder";
describe("OrderBuilder", () => {
it("should build correct order query", () => {
- const orderBuilder = new OrderBuilder();
- const order = orderBuilder
- .addAsc("name")
- .addDesc("modified_at")
- .get();
- expect(order).toEqual(["name asc","modified_at desc"]);
+ const order = OrderBuilder
+ .create()
+ .addAsc("kind")
+ .addDesc("modifiedAt")
+ .getOrder();
+ expect(order).toEqual(["kind asc", "modified_at desc"]);
+ });
+
+ it("should combine results with other builder", () => {
+ const order = OrderBuilder
+ .create()
+ .addAsc("kind")
+ .concat(OrderBuilder
+ .create("properties")
+ .addDesc("modifiedAt"))
+ .getOrder();
+ expect(order).toEqual(["kind asc", "properties.modified_at desc"]);
});
});
diff --git a/src/common/api/order-builder.ts b/src/common/api/order-builder.ts
index cc3eadb..08d17b1 100644
--- a/src/common/api/order-builder.ts
+++ b/src/common/api/order-builder.ts
@@ -2,21 +2,46 @@
//
// SPDX-License-Identifier: AGPL-3.0
+import * as _ from "lodash";
+import { Resource } from "./common-resource-service";
-export default class OrderBuilder {
- private order: string[] = [];
+export default class OrderBuilder<T extends Resource = Resource> {
- addAsc(attribute: string) {
- this.order.push(`${attribute} asc`);
- return this;
+ static create<T extends Resource = Resource>(prefix?: string){
+ return new OrderBuilder<T>([], prefix);
}
- addDesc(attribute: string) {
- this.order.push(`${attribute} desc`);
- return this;
+ private constructor(
+ private order: string[] = [],
+ private prefix = ""){}
+
+ private getRule (direction: string, attribute: keyof T) {
+ const prefix = this.prefix ? this.prefix + "." : "";
+ return `${prefix}${_.snakeCase(attribute.toString())} ${direction}`;
+ }
+
+ addAsc(attribute: keyof T) {
+ return new OrderBuilder<T>(
+ [...this.order, this.getRule("asc", attribute)],
+ this.prefix
+ );
+ }
+
+ addDesc(attribute: keyof T) {
+ return new OrderBuilder<T>(
+ [...this.order, this.getRule("desc", attribute)],
+ this.prefix
+ );
+ }
+
+ concat(orderBuilder: OrderBuilder){
+ return new OrderBuilder<T>(
+ this.order.concat(orderBuilder.getOrder()),
+ this.prefix
+ );
}
- get() {
- return this.order;
+ getOrder() {
+ return this.order.slice();
}
}
-----------------------------------------------------------------------
hooks/post-receive
--
More information about the arvados-commits
mailing list