@@ -4,19 +4,33 @@ import { ComputeProbabilityMap } from "../../probability";
4
4
import Template from "../../templates/template" ;
5
5
import { isDirective , isModuleSource } from "../../util/compare" ;
6
6
import {
7
+ AssignmentExpression ,
8
+ BinaryExpression ,
7
9
CallExpression ,
10
+ ExpressionStatement ,
8
11
FunctionDeclaration ,
9
12
FunctionExpression ,
10
13
Identifier ,
14
+ IfStatement ,
11
15
Literal ,
12
16
MemberExpression ,
17
+ ObjectExpression ,
18
+ Property ,
13
19
ReturnStatement ,
14
20
VariableDeclaration ,
15
21
VariableDeclarator ,
16
22
} from "../../util/gen" ;
17
23
import { append , prepend } from "../../util/insert" ;
18
24
import Transform from "../transform" ;
19
25
import { predictableFunctionTag } from "../../constants" ;
26
+ import {
27
+ chance ,
28
+ choice ,
29
+ getRandomFalseExpression ,
30
+ getRandomInteger ,
31
+ getRandomString ,
32
+ splitIntoChunks ,
33
+ } from "../../util/random" ;
20
34
21
35
function LZ_encode ( c ) {
22
36
ok ( c ) ;
@@ -141,15 +155,72 @@ export default class StringCompression extends Transform {
141
155
)
142
156
) ;
143
157
144
- append (
145
- tree ,
146
- FunctionDeclaration (
147
- getStringName ,
148
- [ ] ,
149
- [ ReturnStatement ( Literal ( encoded ) ) ]
150
- )
158
+ var keys = new Set < string > ( ) ;
159
+ var keysToMake = getRandomInteger ( 4 , 14 ) ;
160
+ for ( var i = 0 ; i < keysToMake ; i ++ ) {
161
+ keys . add ( getRandomString ( getRandomInteger ( 4 , 14 ) ) ) ;
162
+ }
163
+
164
+ var objectExpression = ObjectExpression (
165
+ Array . from ( keys ) . map ( ( key ) => {
166
+ return Property ( Literal ( key ) , getRandomFalseExpression ( ) , true ) ;
167
+ } )
168
+ ) ;
169
+
170
+ // Get string function
171
+ var getStringBody = [ ] ;
172
+ var splits = splitIntoChunks (
173
+ encoded ,
174
+ Math . floor ( encoded . length / getRandomInteger ( 3 , 6 ) )
175
+ ) ;
176
+
177
+ getStringBody . push (
178
+ VariableDeclaration ( VariableDeclarator ( "str" , Literal ( splits . shift ( ) ) ) )
179
+ ) ;
180
+
181
+ getStringBody . push (
182
+ VariableDeclaration ( VariableDeclarator ( "objectToTest" , objectExpression ) )
151
183
) ;
152
184
185
+ const addIfStatement = ( testingFor , literalValueToBeAppended ) => {
186
+ getStringBody . push (
187
+ IfStatement (
188
+ BinaryExpression (
189
+ "in" ,
190
+ Literal ( testingFor ) ,
191
+ Identifier ( "objectToTest" )
192
+ ) ,
193
+ [
194
+ ExpressionStatement (
195
+ AssignmentExpression (
196
+ "+=" ,
197
+ Identifier ( "str" ) ,
198
+ Literal ( literalValueToBeAppended )
199
+ )
200
+ ) ,
201
+ ]
202
+ )
203
+ ) ;
204
+ } ;
205
+
206
+ for ( const split of splits ) {
207
+ if ( chance ( 50 ) ) {
208
+ var fakeKey ;
209
+ do {
210
+ fakeKey = getRandomString ( getRandomInteger ( 4 , 14 ) ) ;
211
+ } while ( keys . has ( fakeKey ) || fakeKey in { } ) ;
212
+
213
+ addIfStatement ( fakeKey , getRandomString ( split . length ) ) ;
214
+ }
215
+
216
+ addIfStatement ( choice ( Array . from ( keys ) ) , split ) ;
217
+ }
218
+
219
+ // Return computed string
220
+ getStringBody . push ( ReturnStatement ( Identifier ( "str" ) ) ) ;
221
+
222
+ append ( tree , FunctionDeclaration ( getStringName , [ ] , getStringBody ) ) ;
223
+
153
224
append (
154
225
tree ,
155
226
FunctionDeclaration (
0 commit comments