Skip to content

Commit

Permalink
feat(string-compression): ➕ use ArrayBuffer utils
Browse files Browse the repository at this point in the history
  • Loading branch information
TomKopp committed Sep 20, 2024
1 parent f7b5aa7 commit fac8bde
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
5 changes: 4 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions packages/string-compression/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { arrayBufferToBinaryString, binaryStringToArrayBuffer } from '@witzbould/utils-array-buffer';

/**
* Compress a string with browser native APIs into a binary string representation
*
Expand All @@ -13,7 +15,7 @@ export const compress = (data, encoding) => {
.pipeThrough(new CompressionStream(encoding))
)
.arrayBuffer()
.then((buffer) => Array.from(new Uint8Array(buffer), (x) => String.fromCodePoint(x)).join(''))
.then(arrayBufferToBinaryString)
);
};

Expand All @@ -26,9 +28,10 @@ export const compress = (data, encoding) => {
*/
export const decompress = (data, encoding) => {
return (
new Response(new Blob([Uint8Array.from(data, (m) => m.codePointAt(0))])
.stream()
.pipeThrough(new DecompressionStream(encoding))
new Response(
new Blob([binaryStringToArrayBuffer(data)])
.stream()
.pipeThrough(new DecompressionStream(encoding))
).text()
);
};
3 changes: 3 additions & 0 deletions packages/string-compression/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
"scripts": {
"publish-npm": "npm view \"$npm_package_name@$npm_package_version\" >/dev/null 2>/dev/null && echo \"$npm_package_name@$npm_package_version exists!\" || npm publish --access public"
},
"dependencies": {
"@witzbould/utils-array-buffer": "1.2.0"
},
"repository": {
"type": "git",
"url": "git+https://github.com/TomKopp/utils.git",
Expand Down

0 comments on commit fac8bde

Please sign in to comment.