-
Notifications
You must be signed in to change notification settings - Fork 571
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added compressToCustom and decompressFromCustom function (#183)
- Loading branch information
1 parent
ef6b696
commit 9811b2b
Showing
6 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,27 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2013 Pieroxy <pieroxy@pieroxy.net> | ||
* | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
import { compress } from "../stock/compress"; | ||
|
||
export function compressToCustom (uncompressed: string | null, dict: string): string { | ||
if (uncompressed == null) return ""; | ||
|
||
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 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,35 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2013 Pieroxy <pieroxy@pieroxy.net> | ||
* | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
import { describe } from "vitest"; | ||
import { compressToCustom, decompressFromCustom } from "."; | ||
import { runTestSet } from "../__tests__/testFunctions"; | ||
|
||
describe("custom", () => { | ||
describe("hex", () => { | ||
const hex = "0123456789ABCDEF"; | ||
const compress = (input: string | null) => compressToCustom(input, hex); | ||
const decompress = (input: string | null) => decompressFromCustom(input, hex); | ||
|
||
runTestSet<string>(compress, decompress, ""); | ||
}); | ||
|
||
describe("23 letter heterogram", () => { | ||
const heterogram = "BlockyDwarfZingsTheJump"; | ||
const compress = (input: string | null) => compressToCustom(input, heterogram); | ||
const decompress = (input: string | null) => decompressFromCustom(input, heterogram); | ||
|
||
runTestSet<string>(compress, decompress, ""); | ||
}); | ||
|
||
describe("base62", () => { | ||
const base62 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; | ||
const compress = (input: string | null) => compressToCustom(input, base62); | ||
const decompress = (input: string | null) => decompressFromCustom(input, base62); | ||
|
||
runTestSet<string>(compress, decompress, ""); | ||
}); | ||
}); |
This file contains 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,34 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2013 Pieroxy <pieroxy@pieroxy.net> | ||
* | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
import { decompress } from "../stock/decompress"; | ||
|
||
export function decompressFromCustom (compressed: string | null, 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 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,8 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2013 Pieroxy <pieroxy@pieroxy.net> | ||
* | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
|
||
export { compressToCustom } from "./compressToCustom"; | ||
export { decompressFromCustom } from "./decompressFromCustom"; |
This file contains 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 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