File tree Expand file tree Collapse file tree 3 files changed +72
-1
lines changed Expand file tree Collapse file tree 3 files changed +72
-1
lines changed Original file line number Diff line number Diff line change @@ -19,4 +19,5 @@ export {ApplicationContext} from "./lib/di/ApplicationContext";
19
19
export { Dispatcher } from "./lib/dispatcher/Dispatcher" ;
20
20
export { Interceptor } from "./lib/interceptors/InterceptorDecorator" ;
21
21
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" ;
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments