Skip to content

Conversation

@zaenalcoders
Copy link

Summary

This PR introduces a new utility function isCompressed() for LZ-String, which provides a universal heuristic-based detection for strings or Uint8Array that are likely compressed by LZ-String. It covers all main compression outputs:

  • compress() → UTF-16 characters

  • compressToUTF16() → special leading header

  • compressToBase64() → Base64-encoded

  • compressToEncodedURIComponent() → URI-safe alphabet

  • compressToUint8Array() → binary array

Motivation

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.ts

  • Added heuristic helper functions: looksLikeUTF16, looksLikeUTF16Special, looksLikeBase64, looksLikeURIEncoded

  • Added comprehensive Vitest test suite in src/isCompressed/__test__/isCompressed.test.ts

  • Includes 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.ts

Covers:

  • Positive detection for all compression methods

  • Negative detection for raw strings, Base64/URI-like strings not compressed

  • Edge cases: empty string, non-string inputs, Uint8Array inputs

Notes

  • Non-breaking change

  • Minimal footprint, fully optional for users

Signed-off-by: zaenalcoders <zaenal.virus@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant