@@ -28,46 +28,39 @@ export function toBase94(data, strictLength=true) {
28
28
29
29
if ( data instanceof ArrayBuffer ) {
30
30
data = new Uint8Array ( data ) ;
31
+ } else {
32
+ throw new OperationError ( `Invalid - Input not instanceof ArrayBuffer.` ) ;
31
33
}
32
- else
33
- {
34
- throw new OperationError ( `Invalid - Input not instanceof ArrayBuffer.` ) ;
35
- }
36
-
37
- let dataModLen = data . length % 4 ;
38
34
39
- if ( dataModLen > 0 && strictLength )
40
- {
35
+ const dataModLen = data . length % 4 ;
41
36
37
+ if ( dataModLen > 0 && strictLength ) {
42
38
throw new OperationError ( `Invalid - Input byte length must be a multiple of 4.` ) ;
43
-
44
39
}
45
40
46
41
let output = "" , i = 0 , j = 0 , acc = 0 ;
47
42
48
- let dataPad = new Uint8Array ( data . length + ( dataModLen > 0 ? ( 4 - dataModLen ) : 0 ) ) ;
43
+ const dataPad = new Uint8Array ( data . length + ( dataModLen > 0 ? ( 4 - dataModLen ) : 0 ) ) ;
49
44
50
- dataPad . set ( data , 0 ) ;
45
+ dataPad . set ( data , 0 ) ;
51
46
52
47
while ( i < dataPad . length ) {
53
48
54
49
acc = 0 ;
55
50
56
- for ( j = 0 ; j < 4 ; j ++ )
57
- {
51
+ for ( j = 0 ; j < 4 ; j ++ ) {
58
52
59
- acc *= 256 ;
53
+ acc *= 256 ;
60
54
61
- acc += dataPad [ i + ( 3 - j ) ] ;
55
+ acc += dataPad [ i + ( 3 - j ) ] ;
62
56
63
57
}
64
58
65
- for ( j = 0 ; j < 5 ; j ++ )
66
- {
59
+ for ( j = 0 ; j < 5 ; j ++ ) {
67
60
68
- output += String . fromCharCode ( ( acc % 94 ) + 32 ) ;
61
+ output += String . fromCharCode ( ( acc % 94 ) + 32 ) ;
69
62
70
- acc = Math . floor ( acc / 94 ) ;
63
+ acc = Math . floor ( acc / 94 ) ;
71
64
72
65
}
73
66
@@ -105,73 +98,66 @@ export function fromBase94(data, strictLength=true, removeInvalidChars=false) {
105
98
106
99
data = Utils . strToByteArray ( data ) ;
107
100
108
- }
109
- else {
101
+ } else {
110
102
111
- throw new OperationError ( `Invalid - typeof base94 input is not a string.` ) ;
103
+ throw new OperationError ( `Invalid - typeof base94 input is not a string.` ) ;
112
104
113
105
}
114
106
115
107
const re = new RegExp ( "[^\x20-\x7e]" , "g" ) ;
116
108
117
- if ( re . test ( data ) )
118
- {
119
- if ( removeInvalidChars ) {
120
- data = data . replace ( re , "" ) ;
121
- }
122
- else {
123
- throw new OperationError ( `Invalid content in Base94 string.` ) ;
124
- }
109
+ if ( re . test ( data ) ) {
110
+ if ( removeInvalidChars ) {
111
+ data = data . replace ( re , "" ) ;
112
+ } else {
113
+ throw new OperationError ( `Invalid content in Base94 string.` ) ;
114
+ }
125
115
}
126
116
127
117
let stringModLen = data . length % 5 ;
128
118
129
- if ( stringModLen > 0 )
130
- {
119
+ if ( stringModLen > 0 ) {
131
120
132
- if ( strictLength )
133
- {
134
- throw new OperationError ( `Invalid - Input string length must be a multiple of 5.` ) ;
135
- }
121
+ if ( strictLength ) {
122
+ throw new OperationError ( `Invalid - Input string length must be a multiple of 5.` ) ;
123
+ }
136
124
137
- stringModLen = 5 - stringModLen ;
125
+ stringModLen = 5 - stringModLen ;
138
126
139
- while ( stringModLen > 0 )
140
- {
127
+ while ( stringModLen > 0 ) {
141
128
142
- data . push ( 32 ) ;
129
+ data . push ( 32 ) ;
143
130
144
- stringModLen -= 1 ;
131
+ stringModLen -= 1 ;
145
132
146
- }
133
+ }
147
134
148
135
}
149
136
150
- let output = [ ] , i = 0 , j = 0 , acc = 0 ;
137
+ const output = [ ] ;
138
+ let i = 0 , j = 0 , acc = 0 ;
151
139
152
140
while ( i < data . length ) {
153
141
154
- acc = 0 ;
142
+ acc = 0 ;
155
143
156
- for ( j = 0 ; j < 5 ; j ++ )
157
- {
144
+ for ( j = 0 ; j < 5 ; j ++ ) {
158
145
159
146
acc = ( acc * 94 ) + data [ i + 4 - j ] - 32 ;
160
147
161
- }
148
+ }
162
149
163
- for ( j = 0 ; j < 4 ; j ++ )
164
- {
150
+ for ( j = 0 ; j < 4 ; j ++ ) {
165
151
166
152
output . push ( acc % 256 ) ;
167
153
168
154
acc = Math . floor ( acc / 256 ) ;
169
155
170
- }
156
+ }
171
157
172
- i += 5 ;
158
+ i += 5 ;
173
159
174
- }
160
+ }
175
161
176
162
return output ;
177
163
0 commit comments