# Example 1 - Update current query's `tab` value
Starting URL: https://github.com/justsml?tab=repositories
Transform W/: {tab: 'activity'}
Updated URL: https://github.com/justsml?tab=activity
# Example 2 - Update `tab` + Add `sort` key-value
Starting URL: https://github.com/justsml?tab=repositories
Transform W/: {tab: 'activity', sort: 'newest'}
Updated URL: https://github.com/justsml?tab=activity&sort=newest
# Example 3 - Leave existing value `foo` + Update `hello` to an escaped str value
Starting URL: https://github.com/justsml?tab=repositories&foo=bar&hello=world
Transform W/: {tab: 'activity', sort: 'newest', hello: 'Mrs. Robinson'}
Updated URL: https://github.com/justsml?tab=activity&sort=newest&foo=bar&hello=Mrs%2E%20Robinson
- Use Hash/Object Map to Define QueryString Changes ( predictable +1 )
- Preserve Current QueryString Params - Only change/add specified key-values. ( Code re-use, module portability +1 )
- Hide messy & unsafe string hackery, slicing or concatenation. (Functional pattern implemented as NodeJS/Express middleware) +1 +1 +1
- Url transform function must be idempotent ( predictable +1 )
- Use webpack/browserify on client. Nodejs on the server. ( Always a +10 )
- Make template code more human readable. (Adhoc URL hacks/concats can be hard to follow & globally search)
- Ideally support adhoc or arbitrary URL mutations (up to any number of QueryString key-value pairs)
For API (single fn), see transformUrl
function below.
UI implementation example features 2 common use-cases, implemented as a Jade template. Jade is not required - could be run anywhere JS is spoken (incl. these template engines: EJS, swig, dot.js, Mustache, etc.).
AJAX-ify the pattern by using transformUrl
to return client-side URL patterns, perhaps either like:
- Use the Url Hash, instead of the querystring, OR
- Intercept page redirects, OR
- Use a custom attribute on links - e.g.
<a href-ajax="/leads/?limit=10">Go</a>
- then intercept clicks which target$('a[href-ajax]')