@@ -3,28 +3,24 @@ const encode = (input, options) => {
3
3
throw Error ( 'Key is too short! It must be at least 3 characters' )
4
4
}
5
5
6
- const heyKey = [ ...options . key ] . map ( toHexCharCodeConcatenationNumber )
6
+ const hexKey = [ ...options . key ] . map ( toHexCharCodeConcatenationNumber )
7
7
8
8
const chunkedString = input . match ( new RegExp ( `.{1,${ Math . round ( options . key . length / 2 ) } }` , 'g' ) )
9
9
10
10
const keyLengthRange = Array . from ( new Array ( options . key . length ) , ( v , i ) => i )
11
11
12
12
return chunkedString . map ( s =>
13
13
keyLengthRange . reduce ( ( acc , i ) => {
14
- switch ( i % ( options . isAer256 ? 3 : 4 ) ) {
15
- case 0 :
16
- acc += heyKey [ i ]
17
- break
18
- case 1 :
19
- acc /= heyKey [ i ]
20
- break
21
- case 2 :
22
- acc -= heyKey [ i ]
23
- break
24
- case 3 :
25
- acc *= 0.01 * heyKey [ i ]
26
- }
27
- return acc
14
+ const OPERATION_LOOKUP = [
15
+ ( v , k ) => v + k ,
16
+ ( v , k ) => v / k ,
17
+ ( v , k ) => v - k ,
18
+ ( v , k ) => v * ( 0.01 * k )
19
+ ]
20
+
21
+ const modifier = i % ( options . isAer256 ? 3 : 4 )
22
+
23
+ return OPERATION_LOOKUP [ modifier ] ( acc , hexKey [ i ] )
28
24
} , toHexCharCodeConcatenationNumber ( s ) )
29
25
) . join ( options . isAer256 ? ', ' : '+' )
30
26
}
@@ -41,20 +37,16 @@ const decode = (input, options) => {
41
37
return input . split ( options . isAer256 ? ', ' : '+' ) . map ( s => {
42
38
// Reverse arithmetic from encoding based on parsed float from string
43
39
const float = keyLengthRangeReversed . reduce ( ( acc , i ) => {
44
- switch ( i % ( options . isAer256 ? 3 : 4 ) ) {
45
- case 0 :
46
- acc -= hexKey [ i ]
47
- break
48
- case 1 :
49
- acc *= hexKey [ i ]
50
- break
51
- case 2 :
52
- acc += hexKey [ i ]
53
- break
54
- case 3 :
55
- acc /= 0.01 * hexKey [ i ]
56
- }
57
- return acc
40
+ const OPERATION_LOOKUP = [
41
+ ( v , k ) => v - k ,
42
+ ( v , k ) => v * k ,
43
+ ( v , k ) => v + k ,
44
+ ( v , k ) => v / ( 0.01 * k )
45
+ ]
46
+
47
+ const modifier = i % ( options . isAer256 ? 3 : 4 )
48
+
49
+ return OPERATION_LOOKUP [ modifier ] ( acc , hexKey [ i ] )
58
50
} , parseFloat ( s ) )
59
51
60
52
// Round output and convert it to hex
0 commit comments