@@ -8,6 +8,48 @@ import {
88 separateTimeOfArrival ,
99} from './train-driver' ;
1010
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+
1153describe ( 'getListOfWagons' , ( ) => {
1254 test ( 'returns the correct array' , ( ) => {
1355 expect ( getListOfWagons ( 1 , 5 , 2 , 7 , 4 ) ) . toEqual ( [ 1 , 5 , 2 , 7 , 4 ] ) ;
@@ -30,29 +72,29 @@ describe('getListOfWagons', () => {
3072
3173describe ( 'fixListOfWagons' , ( ) => {
3274 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 ) ;
3476 const expected = [ 1 , 14 , 10 , 4 , 12 , 6 , 23 , 17 , 13 , 20 , 8 , 19 , 3 , 7 ] ;
3577
3678 expect ( fixListOfWagons ( eachWagonsID ) ) . toEqual ( expected ) ;
3779 } ) ;
3880
3981 test ( 'works when only 3 wagons given' , ( ) => {
40- const eachWagonsID = [ 4 , 2 , 1 ] ;
82+ const eachWagonsID = list ( 4 , 2 , 1 ) ;
4183
4284 expect ( fixListOfWagons ( eachWagonsID ) ) . toEqual ( [ 1 , 4 , 2 ] ) ;
4385 } ) ;
4486
4587 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 ) ;
4789
4890 expect ( fixListOfWagons ( eachWagonsID ) ) . toEqual ( [ 1 , 5 , 7 , 9 , 10 , 3 , 4 ] ) ;
4991 } ) ;
5092} ) ;
5193
5294describe ( 'correctListOfWagons' , ( ) => {
5395 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 ) ;
5698 const expected = [
5799 1 , 8 , 10 , 5 , 9 , 3 , 7 , 20 , 6 , 11 , 15 , 13 , 14 , 17 , 22 , 2 , 16 , 19 , 21 ,
58100 ] ;
@@ -61,16 +103,16 @@ describe('correctListOfWagons', () => {
61103 } ) ;
62104
63105 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 ) ;
66108 const expected = [ 1 , 8 , 6 , 4 , 7 , 15 , 24 ] ;
67109
68110 expect ( correctListOfWagons ( eachWagonsID , missingWagons ) ) . toEqual ( expected ) ;
69111 } ) ;
70112
71113 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 ) ;
74116 const expected = [ 1 , 8 , 6 , 4 , 5 , 9 , 21 , 2 , 13 , 7 , 15 , 24 ] ;
75117
76118 expect ( correctListOfWagons ( eachWagonsID , missingWagons ) ) . toEqual ( expected ) ;
0 commit comments