1
1
import LZString from "./node_modules/lz-string/libs/lz-string.min.js" ;
2
2
3
+ const data_folder : string = "../test_src/data" ;
4
+
3
5
enum CompressOption {
4
6
INVALID_UTF16 = "INVALID_UTF16" ,
5
7
VALID_UTF16 = "VALID_UTF16" ,
@@ -8,6 +10,39 @@ enum CompressOption {
8
10
UINT8ARRAY = "UINT8ARRAY" ,
9
11
}
10
12
13
+ function convert_to_Uint8Array ( data : string ) : Uint8Array {
14
+ const is_odd = data . charCodeAt ( data . length - 1 ) % 256 === 0 ;
15
+
16
+ const buffer = new Uint8Array ( data . length * 2 - ( is_odd ? 1 : 0 ) ) ;
17
+
18
+ for ( let i = 0 ; i < data . length ; ++ i ) {
19
+ const current_value = data . charCodeAt ( i ) ;
20
+
21
+ buffer [ i * 2 ] = current_value >>> 8 ;
22
+
23
+ if ( ! is_odd || i < data . length - 1 ) {
24
+ buffer [ i * 2 + 1 ] = current_value % 256 ;
25
+ }
26
+ }
27
+
28
+ return buffer ;
29
+ }
30
+
31
+ function convert_from_Uint8Array ( data : Uint8Array ) : string {
32
+ const length = Math . floor ( data . byteLength / 2 ) ;
33
+ const arr = [ ] ;
34
+
35
+ for ( let i = 0 ; i < length ; ++ i ) {
36
+ arr . push ( String . fromCharCode ( data [ i * 2 ] * 256 + data [ i * 2 + 1 ] ) ) ;
37
+ }
38
+
39
+ if ( data . byteLength & 1 ) {
40
+ arr . push ( String . fromCharCode ( data [ data . byteLength - 1 ] * 256 ) ) ;
41
+ }
42
+
43
+ return arr . join ( "" ) ;
44
+ }
45
+
11
46
const hello_world = "Hello World !!!" ;
12
47
13
48
const all_ascii =
@@ -62,15 +97,26 @@ const temp_json_float = {
62
97
} ,
63
98
} ;
64
99
65
- function get_file_info (
100
+ function write_result (
66
101
test_name : string ,
67
102
value : string ,
68
103
option : CompressOption ,
69
- ) : string {
70
- let print_string = `${ test_name } \n{` ;
71
-
104
+ ) {
72
105
const char_codes : number [ ] = [ ] ;
73
- let result : string = "" ;
106
+ const test_folder = `${ data_folder } /${ test_name } ` ;
107
+
108
+ try {
109
+ Deno . mkdirSync ( test_folder ) ;
110
+ } catch ( error ) {
111
+ }
112
+
113
+ Deno . writeTextFileSync ( `${ test_folder } /data.bin` , value ) ;
114
+
115
+ const test_filepath = `${ test_folder } /${
116
+ CompressOption [ option ] . toLowerCase ( )
117
+ } .bin`;
118
+
119
+ let result : string | Uint8Array ;
74
120
75
121
switch ( option ) {
76
122
case CompressOption . INVALID_UTF16 :
@@ -85,47 +131,69 @@ function get_file_info(
85
131
case CompressOption . URI :
86
132
result = LZString . compressToEncodedURIComponent ( value ) ;
87
133
break ;
134
+ case CompressOption . UINT8ARRAY :
135
+ result = new Uint8Array ( LZString . compressToUint8Array ( value ) ) ;
136
+
137
+ Deno . writeFileSync (
138
+ test_filepath ,
139
+ result ,
140
+ ) ;
141
+ return ;
88
142
}
89
143
90
- if ( option !== CompressOption . UINT8ARRAY ) {
144
+ if (
145
+ option === CompressOption . BASE64 ||
146
+ option === CompressOption . URI
147
+ ) {
91
148
for ( let i = 0 ; i < result . length ; ++ i ) {
92
149
char_codes . push ( result . charCodeAt ( i ) ) ;
93
150
}
94
151
95
- print_string += new Uint16Array ( char_codes ) . join ( ", " ) ;
96
- } else {
97
- print_string += new Uint8Array ( LZString . compressToUint8Array ( value ) ) . join (
98
- ", " ,
152
+ Deno . writeFileSync (
153
+ test_filepath ,
154
+ new Uint8Array ( char_codes ) ,
99
155
) ;
100
- }
101
156
102
- print_string += "}" ;
157
+ return ;
158
+ }
103
159
104
- return print_string ;
160
+ const converted = convert_to_Uint8Array ( result ) ;
161
+ Deno . writeFileSync ( test_filepath , converted ) ;
105
162
}
106
163
107
164
if ( import . meta. main ) {
108
165
const options : Set < string > = new Set < string > ( Object . keys ( CompressOption ) ) ;
109
166
110
167
const text_strings : Map < string , string > = new Map < string , string > ( ) ;
111
- text_strings . set ( "hello world" , hello_world ) ;
112
- text_strings . set ( "all ascii" , all_ascii ) ;
113
- text_strings . set ( "temp json" , JSON . stringify ( temp_json ) ) ;
114
- text_strings . set ( "temp json float" , JSON . stringify ( temp_json_float ) ) ;
115
168
116
- options . forEach ( ( option ) => {
117
- const text : string [ ] = [ ] ;
169
+ text_strings . set ( "hello_world" , hello_world ) ;
170
+ text_strings . set ( "all_ascii" , all_ascii ) ;
171
+ text_strings . set ( "temp_json" , JSON . stringify ( temp_json ) ) ;
172
+ text_strings . set ( "temp_json_float" , JSON . stringify ( temp_json_float ) ) ;
173
+ text_strings . set (
174
+ "repeated" ,
175
+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" ,
176
+ ) ;
177
+ text_strings . set (
178
+ "pi" ,
179
+ Deno . readTextFileSync ( `${ data_folder } /pi.bin` ) ,
180
+ ) ;
181
+ text_strings . set (
182
+ "lorem_ipsum" ,
183
+ Deno . readTextFileSync ( `${ data_folder } /lorem_ipsum.bin` ) ,
184
+ ) ;
185
+ text_strings . set (
186
+ "tattoo" ,
187
+ Deno . readTextFileSync ( `${ data_folder } /tattoo.bin` ) ,
188
+ ) ;
118
189
190
+ options . forEach ( ( option ) => {
119
191
text_strings . forEach ( ( test , test_name ) =>
120
- text . push (
121
- get_file_info (
122
- test_name ,
123
- test ,
124
- CompressOption [ option as keyof typeof CompressOption ] ,
125
- ) ,
192
+ write_result (
193
+ test_name ,
194
+ test ,
195
+ CompressOption [ option as keyof typeof CompressOption ] ,
126
196
)
127
197
) ;
128
-
129
- Deno . writeTextFileSync ( `${ option } .txt` , text . join ( "\n\n" ) ) ;
130
198
} ) ;
131
199
}
0 commit comments