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 11import { One , Two , Three } from '../../1-digit' ;
22
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+ */
314export function _digit ( list ) {
15+ assert ( Number . isInteger ( list . length ) && list . length >= 1 && list . length <= 3 ) ;
416 switch ( list . length ) {
517 case 1 :
618 return new One ( list [ 0 ] ) ;
719 case 2 :
820 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- ) ;
1721 default :
18- throw new Error ( `cannot make digit for length ${ list . length } ` ) ;
22+ return new Three ( list [ 0 ] , list [ 1 ] , list [ 2 ] ) ;
1923 }
2024}
You can’t perform that action at this time.
0 commit comments