1+ function  compress ( input )  { 
2+  if  ( input  ===  null  ||  input  ===  undefined  ||  input  ===  "" )  // Handling empty input. 
3+  return  "" ; 
4+ 5+  let  dic  =  { } ; 
6+  let  character  =  w  =  wc  =  output  =  "" ; 
7+  let  enlargeIn  =  bitsCount  =  2 ; 
8+  let  dictSize  =  3 ; 
9+  let  val  =  pos  =  0 ; 
10+ 11+  function  encode ( bitsCount ,  v ,  noShift )  { 
12+  let  m  =  noShift  ? 0xFFFFFFFFFFFF  : 1 ;  // dictionary wont go over 48Bits???  
13+ 14+  for  ( let  i  =  0 ;  i  <  bitsCount ;  ++ i )  { 
15+  val  =  ( val  <<  1 )  |  ( v  &  m ) ; 
16+ 17+  if  ( pos  ===  15 )  { 
18+  pos  =  0 ; 
19+  output  +=  String . fromCharCode ( val ) ; 
20+  val  =  0 ; 
21+  }  else 
22+  pos ++ ; 
23+ 24+  if  ( noShift ) 
25+  v  =  0 ; 
26+  else 
27+  v  >>=  1 ; 
28+  } 
29+  } 
30+ 31+  for  ( let  i  =  0 ;  i  <  input . length ;  ++ i )  { 
32+  character  =  input . charAt ( i ) ; 
33+ 34+  if  ( dic [ character ]  ===  undefined ) 
35+  dic [ character ]  =  { 
36+  size : dictSize ++ , 
37+  create : true 
38+  } ; 
39+  wc  =  w  +  character ; 
40+ 41+  if  ( dic [ wc ]  !==  undefined ) 
42+  w  =  wc ; 
43+  else  { 
44+  if  ( dic [ w ] . create )  { 
45+  if  ( w . charCodeAt ( 0 )  <  256 )  { 
46+  encode ( bitsCount ,  0 ) ; 
47+  encode ( 8 ,  w . charCodeAt ( 0 ) ) ; 
48+  }  else  { 
49+  encode ( bitsCount ,  1 ,  true ) 
50+  encode ( 16 ,  w . charCodeAt ( 0 ) ) ; 
51+  } 
52+  enlargeIn -- ; 
53+ 54+  if  ( enlargeIn  ===  0 )  { 
55+  enlargeIn  =  Math . pow ( 2 ,  bitsCount ) ; 
56+  bitsCount ++ ; 
57+  } 
58+  dic [ w ] . create  =  false ; 
59+  }  else 
60+  encode ( bitsCount ,  dic [ w ] . size ) ; 
61+  enlargeIn -- ; 
62+ 63+  if  ( enlargeIn  ===  0 )  { 
64+  enlargeIn  =  Math . pow ( 2 ,  bitsCount ) ; 
65+  bitsCount ++ ; 
66+  } 
67+ 68+  if  ( dic [ wc ]  !==  undefined ) 
69+  dic [ wc ] . size  =  dictSize ++ ; 
70+  else 
71+  dic [ wc ]  =  { 
72+  size : dictSize ++ , 
73+  create : false 
74+  } ; 
75+  w  =  String ( character ) ; 
76+  } 
77+  } 
78+ 79+  if  ( w  !==  "" )  { 
80+  if  ( dic [ w ] . create )  { 
81+  if  ( w . charCodeAt ( 0 )  <  256 )  { 
82+  encode ( bitsCount ,  0 ) ; 
83+  encode ( 8 ,  w . charCodeAt ( 0 ) ) ; 
84+  }  else  { 
85+  encode ( bitsCount ,  1 ,  true ) 
86+  encode ( 16 ,  w . charCodeAt ( 0 ) ) ; 
87+  } 
88+  enlargeIn -- ; 
89+ 90+  if  ( enlargeIn  ===  0 )  { 
91+  enlargeIn  =  Math . pow ( 2 ,  bitsCount ) ; 
92+  bitsCount ++ ; 
93+  } 
94+  dic [ w ] . create  =  false ; 
95+  }  else 
96+  encode ( bitsCount ,  dic [ w ] . size ) ; 
97+  enlargeIn -- ; 
98+ 99+  if  ( enlargeIn  ===  0 )  { 
100+  enlargeIn  =  Math . pow ( 2 ,  bitsCount ) ; 
101+  bitsCount ++ ; 
102+  } 
103+  } 
104+  encode ( bitsCount ,  2 ) ; 
105+ 106+  while  ( true )  { 
107+  val  <<=  1 ; 
108+ 109+  if  ( pos  ==  15 )  { 
110+  output  +=  String . fromCharCode ( val ) ; 
111+  break ; 
112+  }  else 
113+  pos ++ ; 
114+  } 
115+  return  output ; 
116+ } 
117+ 118+ function  decompress ( input )  { 
119+  if  ( input  ===  null  ||  input  ===  ""  ||  input  ===  undefined )  // Handling empty input. 
120+  return  "" ; 
121+ 122+  let  w ; 
123+  let  s  =  [ 256 ,  65536 ] ; 
124+  let  dic  =  [ 0 ,  1 ,  2 ] ; 
125+  let  enlargeIn  =  dicSize  =  4 ; 
126+  let  bitsCount  =  3 ; 
127+  let  character  =  entry  =  result  =  "" ; 
128+  let  pos  =  32768 ; 
129+  let  index  =  1 ; 
130+  let  val  =  input . charCodeAt ( 0 ) ; 
131+ 132+  function  decode ( maxP )  { 
133+  let  p  =  1 , 
134+  b  =  0 ; 
135+ 136+  while  ( p  !=  maxP )  { 
137+  b  |=  ( ( val  &  pos )  >  0  ? 1  : 0 )  *  p ; 
138+  p  <<=  1 ; 
139+  pos  >>=  1 ; 
140+ 141+  if  ( pos  ===  0 )  { 
142+  pos  =  32768 ; 
143+  val  =  input . charCodeAt ( index ++ ) ; 
144+  } 
145+  } 
146+  return  b ; 
147+  } 
148+ 149+  let  bits  =  decode ( 4 ) ; 
150+ 151+  if  ( bits  ===  2 ) 
152+  return  "" ; 
153+  else  if  ( bits  <  2 ) 
154+  character  =  String . fromCharCode ( decode ( s [ bits ] ) ) ; 
155+  dic [ 3 ]  =  w  =  result  =  character ; 
156+ 157+  while  ( true )  { 
158+  if  ( index  >  input . length ) 
159+  return  "" ; 
160+  character  =  bits  =  decode ( Math . pow ( 2 ,  bitsCount ) ) ; 
161+ 162+  if  ( bits  ===  2 ) 
163+  return  result ; 
164+  else  if  ( bits  <  2 )  { 
165+  bits  =  decode ( s [ bits ] ) ; 
166+  dic [ dicSize ++ ]  =  String . fromCharCode ( bits ) ; 
167+  character  =  dicSize  -  1 ; 
168+  enlargeIn -- ; 
169+  } 
170+ 171+  if  ( enlargeIn  ===  0 )  { 
172+  enlargeIn  =  Math . pow ( 2 ,  bitsCount ) ; 
173+  bitsCount ++ ; 
174+  } 
175+ 176+  if  ( dic [ character ] ) 
177+  entry  =  dic [ character ] ; 
178+  else  { 
179+  if  ( character  ===  dicSize ) 
180+  entry  =  w  +  w . charAt ( 0 ) ; 
181+  else 
182+  return  "" ; 
183+  } 
184+ 185+  result  +=  entry ; 
186+  dic [ dicSize ++ ]  =  w  +  entry . charAt ( 0 ) ; 
187+  enlargeIn -- ; 
188+  w  =  entry ; 
189+ 190+  if  ( enlargeIn  ===  0 )  { 
191+  enlargeIn  =  Math . pow ( 2 ,  bitsCount ) ; 
192+  bitsCount ++ ; 
193+  } 
194+  } 
195+ } 
196+ 197+ module . exports  =  { 
198+  compress, 
199+  decompress
200+ } 
0 commit comments