feat: add isCompressed() detection API with heuristics and tests #275
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.
Summary
This PR introduces a new utility function
isCompressed()for LZ-String, which provides a universal heuristic-based detection for strings orUint8Arraythat are likely compressed by LZ-String. It covers all main compression outputs:compress()→ UTF-16 characterscompressToUTF16()→ special leading headercompressToBase64()→ Base64-encodedcompressToEncodedURIComponent()→ URI-safe alphabetcompressToUint8Array()→ binary arrayMotivation
Currently, there is no built-in way to determine if a string or buffer has been compressed by LZ-String without attempting decompression. This utility:
Offers a lightweight, non-destructive check
Supports all standard LZ-String compression formats
Enhances usability for developers building pipelines or validating inputs
Implementation
Added
src/isCompressed/isCompressed.tsAdded heuristic helper functions:
looksLikeUTF16,looksLikeUTF16Special,looksLikeBase64,looksLikeURIEncodedAdded comprehensive Vitest test suite in
src/isCompressed/__test__/isCompressed.test.tsIncludes JSDoc for clear documentation
Fully backward-compatible, no changes to existing compression/decompression logic
Tests
All tests pass with Vitest:
npx vitest run src/isCompressed/__test__/isCompressed.test.tsCovers:
Positive detection for all compression methods
Negative detection for raw strings, Base64/URI-like strings not compressed
Edge cases: empty string, non-string inputs,
Uint8ArrayinputsNotes
Non-breaking change
Minimal footprint, fully optional for users