Comments (9)
Can lz-string provide a way to alleviate the size increase by using the lz algorithm during the conversion process?
This is very exactly what LZ-String is doing. It is using the lzw algorithm to compress the input, using the symbols in the array to store the bits the algorithm produced. So in effect, lz.compress('hello', {map: '0123456789ABCDEF'});
does not look like base16, it is a base16 representation of the compressed stream.
from lz-string.
Hello. I am not sure what your question is or what you are trying to achieve. Could you maybe explain a bit more ?
from lz-string.
It seems that this is a function of converting a string to only contain certain characters specified.
Example: Pseudocode
lz.compress(`hello`, {
map: `0123456789ABCDEF`,
});
The output of the above code is: 68656C6C6F
, they do not contain characters outside the map.
Can lz-string provide a way to alleviate the size increase by using the lz algorithm during the conversion process?
Here are some more examples:
pseudocode: looks like morse code
lz.compress(`hello`, {
map: `. -`,
});
// .... . .-.. .-.. ---
pseudocode: looks like Base16
lz.compress(`hello`, {
map: `0123456789ABCDEF`,
});
// 68656C6C6F
pseudocode: Array
lz.compress(`hello`, {
map: ["公正", "爱国", "平等", "诚信", "文明"],
});
// 公正爱国公正平等公正诚信文明公正诚信文明公正诚信平等
pseudocode: Array
lz.compress(`hello`, {
map: `👨👩👧`,
});
// 👨👨👧👨👨👧👨👧👩👨👧👨
from lz-string.
You have provided the following functions, but none of them meet the requirements, because in my use case only specified characters are allowed to appear in the compression result.
- compress
- compressToUTF16
- compressToBase64
- compressToEncodedURIComponent
- compressToUint8Array
Transmission of characters other than 12AB is not allowed in my use case.
Suppose I need to compress BBC
, without lz-string, I might do this:
var userDict = `12AB`
var split = userDict.slice(0, 1)
var sysDict = userDict.slice(1)
var table = {
A: sysDict.repeat(1), // 2AB
B: sysDict.repeat(2), // 2AB2AB
C: sysDict.repeat(3), // 2AB2AB2AB
}
var table2 = Object.entries(table).reduce((acc, [key, val]) => (acc[val]= key, acc), {})
// the string to be compressed
var str = `BBC`
// ['2AB2AB', '2AB2AB', '2AB2AB2AB'].join('1')
var compressed = str.split(``).map(str => table[str]).join(split)
// compressed = '2AB2AB12AB2AB12AB2AB2AB'
var decompressed = compressed.split(split).map(str => table2[str]).join(``)
However, I managed to control the encoding result within the range of 12AB, but failed to compress the data. How can I get help with lz-string?
from lz-string.
Right, sorry about that, I thought you already looked into the code since you described exactly the way LZString works. If you look at the implementation of compressToBase64, you see this line:
LZString._compress(input, 6, function(a){return keyStrBase64.charAt(a);})
The _compress
function takes three arguments:
- The string to be compressed
- The number of bits per character
- A function giving the character to be used for a value
So in your case you would call:
LZString._compress(input, 2, function(a){return "12AB".charAt(a);})
To do it in hexadecimal:
LZString._compress(input, 4, function(a){return "0123456789ABCDEF".charAt(a);})
Edit: Ah, and there is a corresponding _decompress
function obviously. Let me know if you need any more assistance with that one.
from lz-string.
I saw the _compress method here a few days ago. Since the author raises some issues with transferring data, I think _compress is not the method I want. Now it looks like I was wrong.
Also, I think _compress is like a treasure trove. Worth showing in readme or homepage.
Although I don't understand why you need to pass a few extra parameters (whether it is possible to design a preset value?), or why _decompress should be charAt or charCodeAt, but I will try to understand it first.
Thanks for reminding me to use _compress again.
from lz-string.
I am the author. It's compress
that has issues because it generates invalid UTF-16 characters that some JS engines fail at storing and retrieving. _compress
is the one doing the real job.
from lz-string.
Although I don't understand why you need to pass a few extra parameters (whether it is possible to design a preset value?), or why _decompress should be charAt or charCodeAt, but I will try to understand it first.
The function you pass as an argument to _decompress
has one job: Providing the meaningful bits in the input string. If it's hexadecimal, it needs to give out 0 for '0', 1 for '1' etc up to 15 for 'F'. That's its job, so it has to read the input string, hence the charAt
. After that, you can do a switch
, a bunch of if
, a dictionary lookup (as in the decompressFromBase64
function) etc. It's really up to you.
from lz-string.
Hey I am trying to add a custom compress/decompress function. I fail to understand the needed number given for _decompress.
For compress, the number of bits can be calculated as Math.ceil(Math.log2(dict.length))
But how do I calculate the resetValue? Math.ceil(dict.length/2)
?
dict
should be a string, e.g. "0123456789ABCDEF"
from lz-string.
Related Issues (20)
- Crash with large strings when running on Hermes (React Native)
- How to realize that only specified characters are allowed to appear in the compression result? HOT 1
- LZ-String port to current typescript HOT 9
- Tag for v1.5.0 HOT 2
- Version 2 TODO list HOT 15
- Feature request: compress to source-compatible character set HOT 1
- New port for a linux executable HOT 1
- Can utf-8 codes be avoided in the output? HOT 10
- `decompressFromBase64` can return `null` HOT 7
- Docs: JSPerf.com links are no longer valid
- Docs: A Complete Demo for various compression/decompression HOT 3
- Docs: TSDoc HOT 3
- License Inconsistencies HOT 5
- Community Question: Breaking Changes and Version 2! HOT 10
- [email protected] build with warnings HOT 4
- Feature request: a non minified js file as a result of building v2 HOT 2
- Chrome virus warning with compressToEncodedURIComponent URL HOT 2
- CLI support for decompressing base64 encoded files HOT 1
- Please investigate the slow (irresponsible) LZString issue in Edge HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lz-string.