-
Notifications
You must be signed in to change notification settings - Fork 571
Added compressToCustom and decompressFromCustom function #183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e962fa4
Added compressToCustom and decompressFromCustom function to use custo…
HelloLudger 8d017bb
Merge branch 'master' of https://github.com/pieroxy/lz-string.git
HelloLudger 325d42e
Redone compressToCustom/decompressFromCustom as split file version.
HelloLudger 73bcd2c
Moved tests to src\custom.
HelloLudger 684a4e3
Merge branch 'pieroxy:master' into master
HelloLudger 2fd98ca
Removed special chars from long text
HelloLudger 80bc5a7
Added license header.
HelloLudger 391b732
Aligned test logic an added correct typing for compressToCustom.
HelloLudger 8018fa9
Added missing typing for input in decompressFromCustom.
HelloLudger File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { compress } from "../stock/compress"; | ||
|
||
export function compressToCustom (uncompressed: string, dict: string): string { | ||
const compressed: string = compress(uncompressed); | ||
const charsPerUnicodeChar: number = Math.ceil(Math.log(65536) / Math.log(dict.length)); | ||
let res: string = ""; | ||
|
||
for (let i = 0, TotalLen = compressed.length; i < TotalLen; i++) { | ||
let current_value = compressed.charCodeAt(i); | ||
|
||
for (let j = charsPerUnicodeChar-1; j >= 0; j--) { | ||
const selector = Math.floor(current_value / Math.pow(dict.length, j)); | ||
current_value = current_value % Math.pow(dict.length, j); | ||
res += dict.charAt(selector); | ||
} | ||
} | ||
|
||
return res; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { describe, expect, test } from "vitest"; | ||
import { compressToCustom, decompressFromCustom } from "."; | ||
import { test_longString_fn } from "../__tests__/testFunctions"; | ||
import { | ||
test_hw, | ||
test_repeat, | ||
test_tattooSource, | ||
test_complicated, | ||
} from "../../tests/testValues"; | ||
|
||
describe("custom", () => { | ||
const hex = "0123456789ABCDEF"; | ||
const base62 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | ||
|
||
test(`Custom : Hello World - something happens`, () => { | ||
expect(compressToCustom(test_hw, "0123456789")).not.toEqual(test_hw); | ||
}); | ||
|
||
|
||
|
||
test(`Custom : Hello World - hex : equals decompression of its compression`, () => { | ||
expect(decompressFromCustom(compressToCustom(test_hw, hex), hex)).toEqual(test_hw); | ||
}); | ||
|
||
test(`Custom : repeat - hex : equals decompression of its compression`, () => { | ||
expect(decompressFromCustom(compressToCustom(test_repeat, hex), hex)).toEqual(test_repeat); | ||
}); | ||
|
||
test(`Custom : tattooSource - hex : equals decompression of its compression`, () => { | ||
expect(decompressFromCustom(compressToCustom(test_tattooSource, hex), hex)).toEqual(test_tattooSource); | ||
}); | ||
|
||
test(`Custom : complicated - hex : equals decompression of its compression`, () => { | ||
expect(decompressFromCustom(compressToCustom(test_complicated, hex), hex)).toEqual(test_complicated); | ||
}); | ||
|
||
|
||
|
||
test(`Custom : Hello World - base62 : equals decompression of its compression`, () => { | ||
expect(decompressFromCustom(compressToCustom(test_hw, base62), base62)).toEqual(test_hw); | ||
}); | ||
|
||
test(`Custom : repeat - base62 : equals decompression of its compression`, () => { | ||
expect(decompressFromCustom(compressToCustom(test_repeat, base62), base62)).toEqual(test_repeat); | ||
}); | ||
|
||
test(`Custom : tattooSource - base62 : equals decompression of its compression`, () => { | ||
expect(decompressFromCustom(compressToCustom(test_tattooSource, base62), base62)).toEqual(test_tattooSource); | ||
}); | ||
|
||
test(`Custom : longString - base62 : equals decompression of its compression`, () => { | ||
const str = test_longString_fn() | ||
expect(decompressFromCustom(compressToCustom(str, base62), base62)).toEqual(str); | ||
}); | ||
|
||
test(`Custom : complicated - base62 : equals decompression of its compression`, () => { | ||
expect(decompressFromCustom(compressToCustom(test_complicated, base62), base62)).toEqual(test_complicated); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { decompress } from "../stock/decompress"; | ||
|
||
export function decompressFromCustom (compressed: string, dict: string): string | null { | ||
if (compressed == null) return ""; | ||
if (compressed == "") return null; | ||
if (dict.length < 2) return null; | ||
|
||
const charsPerUnicodeChar: number = Math.ceil(Math.log(65536) / Math.log(dict.length)); | ||
|
||
if (compressed.length % charsPerUnicodeChar != 0) return null; | ||
|
||
let res: string = ""; | ||
let current_value; | ||
let index; | ||
|
||
for (let i = 0, TotalLen = compressed.length; i < TotalLen; i=i+charsPerUnicodeChar) { | ||
current_value = 0; | ||
|
||
for (let j = 0; j < charsPerUnicodeChar; j++) { | ||
index = dict.indexOf(compressed[i+j]); | ||
current_value = current_value + (index * Math.pow(dict.length, charsPerUnicodeChar - 1 - j)); | ||
} | ||
|
||
res = res + String.fromCharCode(current_value); | ||
} | ||
|
||
return decompress(res); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { compressToCustom } from "./compressToCustom"; | ||
export { decompressFromCustom } from "./decompressFromCustom"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.