Skip to content

Commit f1e539d

Browse files
[no-issue] Order Decorator
- added @OrderDecorator and OrderUtil - added tests for OrderDecorator and OrderUtil - exported the decorator [reviewed by @saskodh]
1 parent a07c90a commit f1e539d

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ export {ApplicationContext} from "./lib/di/ApplicationContext";
1919
export {Dispatcher} from "./lib/dispatcher/Dispatcher";
2020
export {Interceptor} from "./lib/interceptors/InterceptorDecorator";
2121
export {ComponentDefinitionPostProcessor} from "./lib/processors/ComponentDefinitionPostProcessor";
22-
export {ComponentPostProcessor} from "./lib/processors/ComponentPostProcessor";
22+
export {ComponentPostProcessor} from "./lib/processors/ComponentPostProcessor";
23+
export {Order} from "./lib/decorators/OrderDecorator";

src/lib/decorators/OrderDecorator.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import * as _ from "lodash";
2+
3+
const ORDER_DECORATOR_TOKEN = Symbol('order_decorator_token');
4+
5+
export function Order(orderValue) {
6+
return function(target) {
7+
target[ORDER_DECORATOR_TOKEN] = orderValue;
8+
};
9+
}
10+
11+
export class OrderUtil {
12+
13+
static getOrder (target) {
14+
return target[ORDER_DECORATOR_TOKEN];
15+
}
16+
17+
static orderList (list): Array<any> {
18+
return _.sortBy(list, (element) => { return OrderUtil.getOrder(element) || Number.MAX_VALUE; });
19+
}
20+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { expect } from "chai";
2+
import { Order, OrderUtil } from "../../../src/lib/decorators/OrderDecorator";
3+
4+
describe('OrderDecorator', function () {
5+
6+
it('should add metadata', function () {
7+
// given
8+
@Order(1)
9+
class A {}
10+
11+
// when
12+
let orderValue = OrderUtil.getOrder(A);
13+
14+
// then
15+
expect(orderValue).to.be.eq(1);
16+
});
17+
});
18+
19+
describe('OrderUtil', function () {
20+
21+
it('should return metadata', function () {
22+
// given
23+
@Order(1)
24+
class A {}
25+
26+
class B {}
27+
28+
// when / then
29+
expect(OrderUtil.getOrder(A)).to.be.eq(1);
30+
expect(OrderUtil.getOrder(B)).to.be.undefined;
31+
});
32+
33+
it('should order given list according to the value passed with the @Order decorator', function () {
34+
// given
35+
@Order(5)
36+
class A {}
37+
38+
@Order(1)
39+
class B {}
40+
41+
class C {}
42+
let list = [A, B, C];
43+
44+
// when
45+
let orderedList = OrderUtil.orderList(list);
46+
47+
// then
48+
expect(orderedList).to.be.eql([B, A, C]);
49+
});
50+
});

0 commit comments

Comments
 (0)