@@ -8,6 +8,48 @@ import {
8
8
separateTimeOfArrival ,
9
9
} from './train-driver' ;
10
10
11
+ const customInspectSymbol = Symbol . for ( 'nodejs.util.inspect.custom' ) ;
12
+ const customLogSymbol = Symbol . for ( 'exercism.javascript.util.log' ) ;
13
+
14
+ // Follow the instructions in case you are stuck on "list.method is not a function"
15
+ class LimitedArray {
16
+ constructor ( values ) {
17
+ this . values = values ;
18
+ }
19
+
20
+ // Enables rest syntax and spread operator, as wel as for of, etc.
21
+ [ Symbol . iterator ] ( ) {
22
+ return this . values [ Symbol . iterator ] ( ) ;
23
+ }
24
+
25
+ // Log value in non-upgraded environments
26
+ toString ( ) {
27
+ return this . values . toString ( ) ;
28
+ }
29
+
30
+ // Overrides logging in node (ie. students working locally)
31
+ [ customInspectSymbol ] ( depth , inspectOptions , inspect ) {
32
+ const inner = this . values [ customInspectSymbol ]
33
+ ? this . values [ customInspectSymbol ] ( depth , inspectOptions , inspect )
34
+ : this . values . toString ( ) ;
35
+
36
+ return `List of (${ inner } )` ;
37
+ }
38
+
39
+ // Overrides log overrides in web environment (ie. students working in editor)
40
+ [ customLogSymbol ] ( depth , inspectOptions , inspect ) {
41
+ const inner = this . values [ customLogSymbol ]
42
+ ? this . values [ customLogSymbol ] ( depth , inspectOptions , inspect )
43
+ : this . values . toString ( ) ;
44
+
45
+ return `List of (${ inner } )` ;
46
+ }
47
+ }
48
+
49
+ function list ( ...values ) {
50
+ return new LimitedArray ( values ) ;
51
+ }
52
+
11
53
describe ( 'getListOfWagons' , ( ) => {
12
54
test ( 'returns the correct array' , ( ) => {
13
55
expect ( getListOfWagons ( 1 , 5 , 2 , 7 , 4 ) ) . toEqual ( [ 1 , 5 , 2 , 7 , 4 ] ) ;
@@ -30,29 +72,29 @@ describe('getListOfWagons', () => {
30
72
31
73
describe ( 'fixListOfWagons' , ( ) => {
32
74
test ( 'reorders the first 2 wagons to the end of the array' , ( ) => {
33
- const eachWagonsID = [ 3 , 7 , 1 , 14 , 10 , 4 , 12 , 6 , 23 , 17 , 13 , 20 , 8 , 19 ] ;
75
+ const eachWagonsID = list ( 3 , 7 , 1 , 14 , 10 , 4 , 12 , 6 , 23 , 17 , 13 , 20 , 8 , 19 ) ;
34
76
const expected = [ 1 , 14 , 10 , 4 , 12 , 6 , 23 , 17 , 13 , 20 , 8 , 19 , 3 , 7 ] ;
35
77
36
78
expect ( fixListOfWagons ( eachWagonsID ) ) . toEqual ( expected ) ;
37
79
} ) ;
38
80
39
81
test ( 'works when only 3 wagons given' , ( ) => {
40
- const eachWagonsID = [ 4 , 2 , 1 ] ;
82
+ const eachWagonsID = list ( 4 , 2 , 1 ) ;
41
83
42
84
expect ( fixListOfWagons ( eachWagonsID ) ) . toEqual ( [ 1 , 4 , 2 ] ) ;
43
85
} ) ;
44
86
45
87
test ( 'works for a few wagons' , ( ) => {
46
- const eachWagonsID = [ 3 , 4 , 1 , 5 , 7 , 9 , 10 ] ;
88
+ const eachWagonsID = list ( 3 , 4 , 1 , 5 , 7 , 9 , 10 ) ;
47
89
48
90
expect ( fixListOfWagons ( eachWagonsID ) ) . toEqual ( [ 1 , 5 , 7 , 9 , 10 , 3 , 4 ] ) ;
49
91
} ) ;
50
92
} ) ;
51
93
52
94
describe ( 'correctListOfWagons' , ( ) => {
53
95
test ( 'returns a wagon weight list with the inserted array of values' , ( ) => {
54
- const eachWagonsID = [ 1 , 6 , 11 , 15 , 13 , 14 , 17 , 22 , 2 , 16 , 19 , 21 ] ;
55
- const missingWagons = [ 8 , 10 , 5 , 9 , 3 , 7 , 20 ] ;
96
+ const eachWagonsID = list ( 1 , 6 , 11 , 15 , 13 , 14 , 17 , 22 , 2 , 16 , 19 , 21 ) ;
97
+ const missingWagons = list ( 8 , 10 , 5 , 9 , 3 , 7 , 20 ) ;
56
98
const expected = [
57
99
1 , 8 , 10 , 5 , 9 , 3 , 7 , 20 , 6 , 11 , 15 , 13 , 14 , 17 , 22 , 2 , 16 , 19 , 21 ,
58
100
] ;
@@ -61,16 +103,16 @@ describe('correctListOfWagons', () => {
61
103
} ) ;
62
104
63
105
test ( 'works for short arrays' , ( ) => {
64
- const eachWagonsID = [ 1 , 7 , 15 , 24 ] ;
65
- const missingWagons = [ 8 , 6 , 4 ] ;
106
+ const eachWagonsID = list ( 1 , 7 , 15 , 24 ) ;
107
+ const missingWagons = list ( 8 , 6 , 4 ) ;
66
108
const expected = [ 1 , 8 , 6 , 4 , 7 , 15 , 24 ] ;
67
109
68
110
expect ( correctListOfWagons ( eachWagonsID , missingWagons ) ) . toEqual ( expected ) ;
69
111
} ) ;
70
112
71
113
test ( 'works when missingWagons is longer' , ( ) => {
72
- const eachWagonsID = [ 1 , 7 , 15 , 24 ] ;
73
- const missingWagons = [ 8 , 6 , 4 , 5 , 9 , 21 , 2 , 13 ] ;
114
+ const eachWagonsID = list ( 1 , 7 , 15 , 24 ) ;
115
+ const missingWagons = list ( 8 , 6 , 4 , 5 , 9 , 21 , 2 , 13 ) ;
74
116
const expected = [ 1 , 8 , 6 , 4 , 5 , 9 , 21 , 2 , 13 , 7 , 15 , 24 ] ;
75
117
76
118
expect ( correctListOfWagons ( eachWagonsID , missingWagons ) ) . toEqual ( expected ) ;
0 commit comments