File tree Expand file tree Collapse file tree 1 file changed +13
-9
lines changed Expand file tree Collapse file tree 1 file changed +13
-9
lines changed Original file line number Diff line number Diff line change 1
1
import { One , Two , Three } from '../../1-digit' ;
2
2
3
+ import assert from 'assert' ;
4
+
5
+ /**
6
+ * Creates a Digit from as small list.
7
+ *
8
+ * It should never be called on length 4 lists since it is only called
9
+ * on results of splitDigit which outputs lists of length at most 3.
10
+ *
11
+ * @param {Array } list A list of length 1, 2, or 3.
12
+ * @return {Digit } A digit containing the elements of list in order.
13
+ */
3
14
export function _digit ( list ) {
15
+ assert ( Number . isInteger ( list . length ) && list . length >= 1 && list . length <= 3 ) ;
4
16
switch ( list . length ) {
5
17
case 1 :
6
18
return new One ( list [ 0 ] ) ;
7
19
case 2 :
8
20
return new Two ( list [ 0 ] , list [ 1 ] ) ;
9
- case 3 :
10
- return new Three ( list [ 0 ] , list [ 1 ] , list [ 2 ] ) ;
11
- // Potential optimization by commenting out this section
12
- // and defaulting for case 3
13
- case 4 :
14
- throw new Error (
15
- '_digit(.) should never be called on length 4 lists since it is only called on results of splitDigit which outputs lists of length at most 3'
16
- ) ;
17
21
default :
18
- throw new Error ( `cannot make digit for length ${ list . length } ` ) ;
22
+ return new Three ( list [ 0 ] , list [ 1 ] , list [ 2 ] ) ;
19
23
}
20
24
}
You can’t perform that action at this time.
0 commit comments