Skip to content

refactor: simplify Hash class with structured parsing helpers#7113

Closed
pierreeurope wants to merge 1 commit intomaplibre:mainfrom
pierreeurope:refactor/hash-urlsearchparams
Closed

refactor: simplify Hash class with structured parsing helpers#7113
pierreeurope wants to merge 1 commit intomaplibre:mainfrom
pierreeurope:refactor/hash-urlsearchparams

Conversation

@pierreeurope
Copy link
Copy Markdown
Contributor

Summary

Refactor the Hash class to replace manual string building/parsing with structured _parseHash() and _serializeParams() helper methods.

Closes #7006

Motivation

As noted in #7006, the Hash class does a lot of manual string building and parsing, making it tricky to extend or maintain. This PR extracts the parsing and serialization into dedicated methods.

Approach

Rather than using URLSearchParams directly (which encodes slashes as %2F and colons as %3A, breaking the hash value format), this PR introduces two lightweight helpers:

  • _parseHash(): Parses the hash fragment into an Array<[string, string | null]> — preserving parameter order, the distinction between key (no equals) and key= (empty value), and unencoded characters in values
  • _serializeParams(): Serializes the array back to a &-separated string

These naturally handle edge cases (trailing/double/leading ampersands) through the split-and-filter approach.

Changes

  • getHashString(): Uses _parseHash() + find/update/push + _serializeParams() instead of manual .split('&').map().filter()
  • _getCurrentHash(): Uses _parseHash() + .find() instead of manual .split('&').map().forEach()
  • _removeHash(): Uses _parseHash() + .filter() + _serializeParams() instead of manual string replacement

Tests

All 52 existing tests pass unchanged — no behavior changes.

Replace the manual string manipulation in Hash's getHashString,
_getCurrentHash, and _removeHash with structured _parseHash and
_serializeParams helper methods.

The helpers parse the hash fragment into an array of [key, value]
pairs and serialize them back, preserving:
- Unencoded slashes and colons in values (unlike URLSearchParams)
- The distinction between 'key' (no equals) and 'key=' (empty value)
- Parameter ordering

This makes the code easier to follow and extend, and naturally handles
edge cases like trailing ampersands, double ampersands, and leading
ampersands through the split-and-filter approach.

Closes maplibre#7006
@HarelM
Copy link
Copy Markdown
Collaborator

HarelM commented Feb 12, 2026

Why is this PR better than the other that's already open?

@pierreeurope
Copy link
Copy Markdown
Contributor Author

You're right, I missed #7073 — apologies for the duplicate. That PR takes the correct approach of using actual URLSearchParams. Closing this in favor of that one.

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.

Refactor Hash to use URLSearchParams

2 participants