ljharb/qs

option to parse/unparse from array of pairs handling ordering and duplicate keys or key values like URLSearchParams

Open

#267 opened on Jun 26, 2018

View on GitHub
 (1 comment) (0 reactions) (0 assignees)JavaScript (8,015 stars) (744 forks)batch import
feature requesthelp wantedparsestringify

Description

I want to be able to write a function that works on any browser/node like:

const nodeUrl = require('url');
const assert = require('assert');

function setQuery(url, key, value) {
  const u = new nodeUrl.URL(url);
  u.searchParams.set(key, value);
  return u.toString();
}

const result = setQuery('https://example.com?ham=spam&eggs=chips&eggs=chips&ham=spam', 'eggs', 'zap');
assert.equal(result, 'ham=spam&eggs=zap&ham=spam');

is this something qs can help with?

Contributor guide