JSDoc: Source: noop.lzw.js

/**
 * NOOP LZW module.
 * @memberof noop
 * @namespace lzw
 * @author cisco211
 * @copyright © 2019 by "cisco211"
 * @version 0.100
 */
(function(noop, undefined)
{
	'use strict';

	// #region Private
	// #endregion Private

	// #region Public
	noop.lzw = new class LZW
	{
		constructor()
		{
			/**
			 * @summary Compress input.
			 * @function compress
			 * @memberof noop.lzw
			 * @param {string} input - An input string.
			 * @return {array.<number>} output - An array of char codes.
			 * @example var output = noop.lzw.compress('Hello World!');
			 */
			this.compress = function(input)
			{
				if (typeof input !== 'string')
					return;

				// Variables
				var c;
				var dictionary = {};
				var dictSize = 256;
				var i;
				var output = [];
				var w = '';
				var wc;

				// Build the dictionary
				for (i = 0; i < 256; i += 1)
					dictionary[String.fromCharCode(i)] = i;

				// Compress input
				for (i = 0; i < input.length; i += 1)
				{
					c = input.charAt(i);
					wc = w + c;
					if (dictionary.hasOwnProperty(wc))
						w = wc;
					else
					{
						output.push(dictionary[w]);
						dictionary[wc] = dictSize++;
						w = String(c);
					}
				}

				// Output the code for w
				if (w !== '')
					output.push(dictionary[w]);

				// Return output
				return output;
			}; // compress = function(input)

			/**
			 * @summary Decompress input.
			 * @function decompress
			 * @memberof noop.lzw
			 * @param {array.<number>} input - An input array of char codes.
			 * @return {string} output - An output string.
			 * @example var output = noop.lzw.decompress([72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]);
			 */
			this.decompress = function(input)
			{
				if (typeof input !== 'object')
					return null;

				// Variables
				var dictionary = [];
				var dictSize = 256;
				var entry = '';
				var i;
				var k;
				var w;
				var output;

				// Build the dictionary
				for (i = 0; i < 256; i += 1)
					dictionary[i] = String.fromCharCode(i);

				// Decompress input
				w = String.fromCharCode(input[0]);
				output = w;
				for (i = 1; i < input.length; i += 1)
				{
					k = input[i];
					if (dictionary[k])
						entry = dictionary[k];
					else
					{
						if (k === dictSize)
							entry = w + w.charAt(0);
						else
							return null;
					}
					output += entry;
					dictionary[dictSize++] = w + entry.charAt(0);
					w = entry;
				}

				// Return output
				return output;
			}; // decompress = function(input)
		} // constructor()
	}; // noop.lzw = new class LZW
	// #endregion Public

} // (function(noop, undefined)
(window.noop = window.noop || new class NOOP{}));

// EOF