URL manipulation made simple.
65
Apply layered modifications to a query string while preserving insertion order and multi-valued keys. Start from the provided base query, optionally replace it entirely, append additional parameters without losing existing ones, and drop selected keys before encoding the result.
Operations occur in this order: replace the whole query when a reset is provided, append new parameters while expanding list-like inputs into repeated keys, remove specified keys (or everything when asked), then encode using the requested delimiter. Return both the encoded query string and convenient access to a specific key's first and all values.
tag=alpha&tag=beta&sort=asc, applying an append plan of [('tag', 'gamma'), ('filter', 'new'), ('tag', 'delta')] returns tag=alpha&tag=beta&sort=asc&tag=gamma&filter=new&tag=delta when encoded with the default delimiter. It reports the first value for tag as alpha and all values for tag as ['alpha', 'beta', 'gamma', 'delta']. @testpage=2&page=3&lang=en, applying a reset plan of {'lang': 'fr', 'page': ['10', '11']} followed by appending {'extra': 'yes'} returns lang=fr&page=10&page=11&extra=yes. The first value for page becomes 10, and all values for page are ['10', '11']. @testmode=full&mode=lite&keep=true, appending {'keep': 'false', 'mode': 'debug'} and dropping ['mode'] returns keep=true;keep=false when encoded with the ; delimiter. The first value for mode is None and the list of values for mode is empty. @test@generates
from typing import Any, Mapping
Plan = Mapping[str, Any]
def apply_query_plan(base_query: str,
plan: Plan,
target_key: str,
delimiter: str = "&") -> tuple[str, str | None, list[str]]:
"""
Apply a sequence of operations to a query string.
The plan supports:
- 'reset': optional query-like input that replaces the entire query before other steps.
- 'append': optional query-like input that adds parameters without replacing existing values.
- 'drop': optional list of keys to remove, or True to clear everything.
Returns the encoded query string using the requested delimiter, the first value for target_key (or None),
and the list of all values for target_key in insertion order.
"""Provides URL and query manipulation utilities.
Install with Tessl CLI
npx tessl i tessl/pypi-furlevals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10