@@ -8,88 +8,134 @@ import {
88 swapNamedPile ,
99} from './enchantments' ;
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 deck ( ...values ) {
50+ return new LimitedArray ( values ) ;
51+ }
52+
1153describe ( 'getFirstCard' , ( ) => {
1254 test ( 'from a deck with a single card' , ( ) => {
13- expect ( getFirstCard ( [ 3 ] ) ) . toBe ( 3 ) ;
55+ expect ( getFirstCard ( deck ( 3 ) ) ) . toBe ( 3 ) ;
1456 } ) ;
1557
1658 test ( 'from a deck with many cards' , ( ) => {
17- expect ( getFirstCard ( [ 8 , 3 , 9 , 5 ] ) ) . toBe ( 8 ) ;
59+ expect ( getFirstCard ( deck ( 8 , 3 , 9 , 5 ) ) ) . toBe ( 8 ) ;
1860 } ) ;
1961
2062 test ( 'from an empty deck' , ( ) => {
21- expect ( getFirstCard ( [ ] ) ) . toBe ( undefined ) ;
63+ expect ( getFirstCard ( deck ( ) ) ) . toBe ( undefined ) ;
2264 } ) ;
2365} ) ;
2466
2567describe ( 'getSecondCard' , ( ) => {
2668 test ( 'from a deck with two cards' , ( ) => {
27- expect ( getSecondCard ( [ 10 , 4 ] ) ) . toBe ( 4 ) ;
69+ expect ( getSecondCard ( deck ( 10 , 4 ) ) ) . toBe ( 4 ) ;
2870 } ) ;
2971
3072 test ( 'from a deck with many cards' , ( ) => {
31- expect ( getSecondCard ( [ 2 , 5 , 7 , 6 ] ) ) . toBe ( 5 ) ;
73+ expect ( getSecondCard ( deck ( 2 , 5 , 7 , 6 ) ) ) . toBe ( 5 ) ;
3274 } ) ;
3375
3476 test ( 'from an empty deck' , ( ) => {
35- expect ( getSecondCard ( [ ] ) ) . toBe ( undefined ) ;
77+ expect ( getSecondCard ( deck ( ) ) ) . toBe ( undefined ) ;
3678 } ) ;
3779
3880 test ( 'from a deck with one card' , ( ) => {
39- expect ( getSecondCard ( [ 8 ] ) ) . toBe ( undefined ) ;
81+ expect ( getSecondCard ( deck ( 8 ) ) ) . toBe ( undefined ) ;
4082 } ) ;
4183} ) ;
4284
4385describe ( 'swapTwoCards' , ( ) => {
4486 test ( 'swapping two numbered cards' , ( ) => {
45- expect ( swapTwoCards ( [ 3 , 6 ] ) ) . toStrictEqual ( [ 6 , 3 ] ) ;
87+ expect ( swapTwoCards ( deck ( 3 , 6 ) ) ) . toStrictEqual ( [ 6 , 3 ] ) ;
4688 } ) ;
4789
4890 test ( 'swapping a high card with a low card' , ( ) => {
49- expect ( swapTwoCards ( [ 10 , 2 ] ) ) . toStrictEqual ( [ 2 , 10 ] ) ;
91+ expect ( swapTwoCards ( deck ( 10 , 2 ) ) ) . toStrictEqual ( [ 2 , 10 ] ) ;
5092 } ) ;
5193
5294 test ( 'swapping a face card with a low card' , ( ) => {
53- expect ( swapTwoCards ( [ 'king' , 3 ] ) ) . toStrictEqual ( [ 3 , 'king' ] ) ;
95+ expect ( swapTwoCards ( deck ( 'king' , 3 ) ) ) . toStrictEqual ( [ 3 , 'king' ] ) ;
5496 } ) ;
5597} ) ;
5698
5799describe ( 'shiftThreeCardsAround' , ( ) => {
58100 test ( 'consecutive numbers' , ( ) => {
59- expect ( shiftThreeCardsAround ( [ 6 , 4 , 5 ] ) ) . toStrictEqual ( [ 4 , 5 , 6 ] ) ;
101+ expect ( shiftThreeCardsAround ( deck ( 6 , 4 , 5 ) ) ) . toStrictEqual ( [ 4 , 5 , 6 ] ) ;
60102 } ) ;
61103
62104 test ( 'drop the face card to the bottom' , ( ) => {
63- expect ( shiftThreeCardsAround ( [ 'king' , 5 , 2 ] ) ) . toStrictEqual ( [ 5 , 2 , 'king' ] ) ;
105+ expect ( shiftThreeCardsAround ( deck ( 'king' , 5 , 2 ) ) ) . toStrictEqual ( [
106+ 5 ,
107+ 2 ,
108+ 'king' ,
109+ ] ) ;
64110 } ) ;
65111} ) ;
66112
67113describe ( 'pickNamedPile' , ( ) => {
68114 test ( 'keeps the chosen pile' , ( ) => {
69- const chosen = [ 3 , 'jack' , 'queen' , 'king' , 10 , 7 ] ;
70- const disregarded = [ 4 , 5 , 6 , 8 , 9 ] ;
115+ const chosen = deck ( 3 , 'jack' , 'queen' , 'king' , 10 , 7 ) ;
116+ const disregarded = deck ( 4 , 5 , 6 , 8 , 9 ) ;
71117 const piles = { chosen, disregarded } ;
72118
73119 expect ( pickNamedPile ( piles ) ) . toStrictEqual ( chosen ) ;
74120 } ) ;
75121
76122 test ( 'returns the actual pile without recreating it' , ( ) => {
77- const chosen = [ 3 , 'jack' , 'queen' , 'king' , 10 , 7 ] ;
78- const disregarded = [ 4 , 5 , 6 , 8 , 9 ] ;
123+ const chosen = deck ( 3 , 'jack' , 'queen' , 'king' , 10 , 7 ) ;
124+ const disregarded = deck ( 4 , 5 , 6 , 8 , 9 ) ;
79125 const piles = { chosen, disregarded } ;
80126
81127 const result = pickNamedPile ( piles ) ;
82128
83- chosen . push ( 'joker' ) ;
129+ chosen . values . push ( 'joker' ) ;
84130
85131 expect ( result ) . toStrictEqual ( chosen ) ;
86132 } ) ;
87133} ) ;
88134
89135describe ( 'swapNamedPile' , ( ) => {
90136 test ( 'renames the piles' , ( ) => {
91- const face_pile = [ 3 , 'jack' , 'queen' , 'king' , 10 , 7 ] ;
92- const numbers_pile = [ 4 , 5 , 6 , 8 , 9 ] ;
137+ const face_pile = deck ( 3 , 'jack' , 'queen' , 'king' , 10 , 7 ) ;
138+ const numbers_pile = deck ( 4 , 5 , 6 , 8 , 9 ) ;
93139 const piles = { chosen : numbers_pile , disregarded : face_pile } ;
94140
95141 expect ( swapNamedPile ( piles ) ) . toStrictEqual ( {
@@ -99,14 +145,14 @@ describe('swapNamedPile', () => {
99145 } ) ;
100146
101147 test ( 'returns the actual piles without recreating them' , ( ) => {
102- const face_pile = [ 3 , 'jack' , 'queen' , 'king' , 10 , 7 ] ;
103- const numbers_pile = [ 4 , 5 , 6 , 8 , 9 ] ;
148+ const face_pile = deck ( 3 , 'jack' , 'queen' , 'king' , 10 , 7 ) ;
149+ const numbers_pile = deck ( 4 , 5 , 6 , 8 , 9 ) ;
104150 const piles = { chosen : numbers_pile , disregarded : face_pile } ;
105151
106152 const result = swapNamedPile ( piles ) ;
107153
108- face_pile . push ( 'joker' ) ;
109- numbers_pile . push ( 2 ) ;
154+ face_pile . values . push ( 'joker' ) ;
155+ numbers_pile . values . push ( 2 ) ;
110156
111157 expect ( result ) . toStrictEqual ( {
112158 chosen : face_pile ,
0 commit comments