1/* 2 * lzwCompress.js 3 * 4 * Copyright (c) 2012-2016 floydpink 5 * Licensed under the MIT license. 6 * 7 * The MIT License (MIT) 8 * 9 * Permission is hereby granted, free of charge, to any person obtaining a copy 10 * of this software and associated documentation files (the "Software"), to deal 11 * in the Software without restriction, including without limitation the rights 12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 13 * copies of the Software, and to permit persons to whom the Software is 14 * furnished to do so, subject to the following conditions: 15 * 16 * The above copyright notice and this permission notice shall be included in all 17 * copies or substantial portions of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 25 * SOFTWARE. 26 */ 27 28'use strict'; 29 30(function () { 31 var root = this; 32 33 var lzwCompress = (function (Array, JSON, undefined) { 34 var _self = {}, 35 _lzwLoggingEnabled = false, 36 _lzwLog = function (message) { 37 try { 38 console.log('lzwCompress: ' + 39 (new Date()).toISOString() + ' : ' + (typeof(message) === 'object' ? JSON.stringify(message) : message)); 40 } catch (e) { 41 } 42 }; 43 44 // KeyOptimize 45 // http://stackoverflow.com/questions/4433402/replace-keys-json-in-javascript 46 (function (self, Array, JSON) { 47 48 var _keys = [], 49 comparer = function (key) { 50 return function (e) { 51 return e === key; 52 }; 53 }, 54 inArray = function (array,comparer) { 55 for (var i = 0; i < array.length; i++) { 56 if (comparer(array[i])) { 57 return true; 58 } 59 } 60 return false; 61 }, 62 pushNew = function (array,element, comparer) { 63 if (!inArray(array,comparer)) { 64 array.push(element); 65 } 66 }, 67 _extractKeys = function (obj) { 68 if (typeof obj === 'object') { 69 for (var key in obj) { 70 if (!Array.isArray(obj)) { 71 pushNew(_keys,key, comparer(key)); 72 } 73 _extractKeys(obj[key]); 74 } 75 } 76 }, 77 _encode = function (obj) { 78 if (typeof obj !== 'object') { 79 return obj; 80 } 81 for (var prop in obj) { 82 if (!Array.isArray(obj)) { 83 if (obj.hasOwnProperty(prop)) { 84 obj[_keys.indexOf(prop)] = _encode(obj[prop]); 85 delete obj[prop]; 86 } 87 } else { 88 obj[prop] = _encode(obj[prop]); 89 } 90 } 91 return obj; 92 }, 93 _decode = function (obj) { 94 if (typeof obj !== 'object') { 95 return obj; 96 } 97 for (var prop in obj) { 98 if (!Array.isArray(obj)) { 99 if (obj.hasOwnProperty(prop) && _keys[prop]) { 100 obj[_keys[prop]] = _decode(obj[prop]); 101 delete obj[prop]; 102 } 103 } else { 104 obj[prop] = _decode(obj[prop]); 105 } 106 } 107 return obj; 108 }, 109 compress = function (json) { 110 _keys = []; 111 var jsonObj = JSON.parse(json); 112 _extractKeys(jsonObj); 113 _lzwLoggingEnabled && _lzwLog('keys length : ' + _keys.length); 114 _lzwLoggingEnabled && _lzwLog('keys : ' + _keys); 115 return JSON.stringify({ __k : _keys, __v : _encode(jsonObj) }); 116 }, 117 decompress = function (minifiedJson) { 118 var obj = minifiedJson; 119 if (typeof(obj) !== 'object') { 120 return minifiedJson; 121 } 122 if (!obj.hasOwnProperty('__k')) { 123 return JSON.stringify(obj); 124 } 125 _keys = obj.__k; 126 return _decode(obj.__v); 127 }; 128 129 self.KeyOptimize = { 130 pack : compress, 131 unpack : decompress 132 }; 133 }(_self, Array, JSON)); 134 135 // LZWCompress 136 // http://stackoverflow.com/a/2252533/218882 137 // http://rosettacode.org/wiki/LZW_compression#JavaScript 138 (function (self, Array) { 139 var compress = function (uncompressed) { 140 if (typeof(uncompressed) !== 'string') { 141 return uncompressed; 142 } 143 var i, 144 dictionary = {}, 145 c, 146 wc, 147 w = '', 148 result = [], 149 dictSize = 256; 150 for (i = 0; i < 256; i += 1) { 151 dictionary[String.fromCharCode(i)] = i; 152 } 153 for (i = 0; i < uncompressed.length; i += 1) { 154 c = uncompressed.charAt(i); 155 wc = w + c; 156 if (dictionary[wc]) { 157 w = wc; 158 } else { 159 if (dictionary[w] === undefined) { 160 return uncompressed; 161 } 162 result.push(dictionary[w]); 163 dictionary[wc] = dictSize++; 164 w = String(c); 165 } 166 } 167 if (w !== '') { 168 result.push(dictionary[w]); 169 } 170 return result; 171 }, 172 decompress = function (compressed) { 173 if (!Array.isArray(compressed)) { 174 return compressed; 175 } 176 var i, 177 dictionary = [], 178 w, 179 result, 180 k, 181 entry = '', 182 dictSize = 256; 183 for (i = 0; i < 256; i += 1) { 184 dictionary[i] = String.fromCharCode(i); 185 } 186 w = String.fromCharCode(compressed[0]); 187 result = w; 188 for (i = 1; i < compressed.length; i += 1) { 189 k = compressed[i]; 190 if (dictionary[k]) { 191 entry = dictionary[k]; 192 } else { 193 if (k === dictSize) { 194 entry = w + w.charAt(0); 195 } else { 196 return null; 197 } 198 } 199 result += entry; 200 dictionary[dictSize++] = w + entry.charAt(0); 201 w = entry; 202 } 203 return result; 204 }; 205 206 self.LZWCompress = { 207 pack : compress, 208 unpack : decompress 209 }; 210 }(_self, Array)); 211 212 var _compress = function (obj) { 213 _lzwLoggingEnabled && _lzwLog('original (uncompressed) : ' + obj); 214 if (!obj || obj === true || obj instanceof Date) { 215 return obj; 216 } 217 var result = obj; 218 if (typeof obj === 'object') { 219 result = _self.KeyOptimize.pack(JSON.stringify(obj)); 220 _lzwLoggingEnabled && _lzwLog('key optimized: ' + result); 221 } 222 var packedObj = _self.LZWCompress.pack(result); 223 _lzwLoggingEnabled && _lzwLog('packed (compressed) : ' + packedObj); 224 return packedObj; 225 }, 226 _decompress = function (compressedObj) { 227 _lzwLoggingEnabled && _lzwLog('original (compressed) : ' + compressedObj); 228 if (!compressedObj || compressedObj === true || compressedObj instanceof Date) { 229 return compressedObj; 230 } 231 var probableJSON, result = _self.LZWCompress.unpack(compressedObj); 232 try { 233 probableJSON = JSON.parse(result); 234 } catch (e) { 235 _lzwLoggingEnabled && _lzwLog('unpacked (uncompressed) : ' + result); 236 return result; 237 } 238 if (typeof probableJSON === 'object') { 239 result = _self.KeyOptimize.unpack(probableJSON); 240 } 241 _lzwLoggingEnabled && _lzwLog('unpacked (uncompressed) : ' + result); 242 return result; 243 }, 244 _enableLogging = function (enable) { 245 _lzwLoggingEnabled = enable; 246 }; 247 248 return { 249 pack : _compress, 250 unpack : _decompress, 251 enableLogging : _enableLogging 252 }; 253 254 })(Array, JSON); 255 256 if (typeof module !== 'undefined' && module.exports) { 257 module.exports = lzwCompress; 258 } 259 else { 260 root.lzwCompress = lzwCompress; 261 } 262 263}).call(this);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.