Class: StringCompressor
Constructor
1new StringCompressor(dictionary = {}, flag = '$')
Parameters:
dictionary– Optional initial dictionary mapping original strings to compressed keys. Defaults to{}.flag– Prefix for compressed keys. Defaults to$.
Example:
1const compressor = new StringCompressor({}, '#');
setDictionary(dictionary)
Sets a new dictionary and automatically generates a reverse dictionary for decompression.
Parameters:
dictionary– Object mapping original strings to short keys.
Example:
1compressor.setDictionary({ hello: '$a', world: '$b' });
compress(str, dictionary = null)
Compresses a string using the current dictionary. Optionally, a new dictionary can be passed.
Parameters:
str– The string to compress.dictionary– Optional dictionary to override current one.
Example:
1const compressed = compressor.compress("hello world hello");2// "$a $b $a"
decompress(str, dictionary = null)
Decompresses a string using the current reverse dictionary. Optionally, a new dictionary can be passed.
Parameters:
str– The string to decompress.dictionary– Optional dictionary to override current one.
Example:
1const decompressed = compressor.decompress("$a $b $a");2// "hello world hello"
generateMap(str, minLength = 4, minFreq = 2, flag)
Generates a compression dictionary automatically based on word frequency in the input string.
Parameters:
str– Input string.minLength– Minimum length of words to consider. Defaults to4.minFreq– Minimum frequency of words to include. Defaults to2.flag– Optional flag prefix for keys. Defaults to the instance flag.
Returns: Dictionary object mapping words to short keys.
Example:
1const map = compressor.generateMap("hello world hello world", 3, 2);2// { hello: '$a', world: '$b' }
generateAndSetDictionary(str, minLength = 4, minFreq = 2, flag)
Generates a frequency-based dictionary and sets it as the current dictionary.
Returns: Dictionary object.
Example:
1compressor.generateAndSetDictionary("hello world hello world");2// Dictionary now set: { hello: '$a', world: '$b' }
stats(originalStr, compressedStr)
Provides statistics on compression efficiency.
Parameters:
originalStr– Original string.compressedStr– Compressed string.
Returns: Object with:
originalLength– Length of original string.compressedLength– Length of compressed string.saved– Number of characters saved.percentSaved– Percentage reduction as string.
Example:
1const stats = compressor.stats("hello world hello", "$a $b $a");2console.log(stats);3// { originalLength: 17, compressedLength: 9, saved: 8, percentSaved: '47.06%' }
replace(str, map)
Replaces occurrences of keys in a string based on a mapping object.
Example:
1compressor.replace("hello world", { hello: '$a', world: '$b' });2// "$a $b"
Edge Cases & Notes:
- Supports Unicode words via
\p{L}regex. - Generates short keys using patterns like
a, b, ..., z, aa, ab. - Words shorter than
minLengthor below frequency threshold are ignored. - Safe to use on strings without dictionary; returns original string unchanged if mapping is empty.