Skip to content

Commit 5ce530d

Browse files
🚀 perf(_digit): Replace throws by optional assert.
1 parent dfb8a36 commit 5ce530d

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

src/0-core/_fast/_digit.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
import {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+
*/
314
export 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
}

0 commit comments

Comments
 (0)