8000 GitHub - worka/vanilla-js-url at v2.0.0
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

A lightweight module to construct and parse query parameters of URLs. A set of functions for working with url. Easy to add parameters to url, easy to extract parameters from url. You can also get path from url.

License

Notifications You must be signed in to change notification settings

worka/vanilla-js-url

Repository files navigation

vanilla-js-url

Object for working with url GET parameters: simple get, simple add

jcurl.getParams('example.com?bar=1&foo');
jcurl.addParams('example.com', { bar: 1, foo: 2 });

jcurl.getParamsExtended('example.com?bar[roo][boo]=1&foo[puu]=test');
jcurl.addParamsExtended('example.com', { bar: { foo: 'test', joo: 2 } });

jcurl exactly, not jsurl, this is not a mistake :)

getParams(String url)

alias get()

If you have simple parameters like bar=1 or foo[]=3&foo[]=5, then use getParams().
In response, you will get a simple (single-level) object whose keys will contain either simple values or simple arrays. This function is suitable in 99% of cases.

jcurl.get('example.com');
// {}

jcurl.get('example.com?bar=1&foo');
// { bar: '1', foo: '' }

jcurl.get('example.com?bar=1&bar=2');
// { bar: '2' }

jcurl.get('example.com?bar[]=1&bar[]=2');
// { bar: ['1', '2'] }

jcurl.get('example.com?bar=1&bar[]=2');
// { bar: ['2'] }

addParams(String url, Object params)

alias add()

jcurl.add('example.com', { bar: 1, foo: 2 });
// example.com?bar=1&foo=2

jcurl.add('example.com?bar=1&foo', { bar: 2, foo: 2 });
// example.com?bar=2&foo=2

jcurl.add('example.com?bar=1', { bar: [2, 3] });
// example.com?bar[]=2&bar[]=3

jcurl.add('example.com?bar=1&bar[]=2', { bar: [3, 4] });
// example.com?bar[]=2&bar[]=3&bar[]=4

getParamsExtended(String url)

alias getExt()

If you have complex parameters like: bar[foo][too][poo]=3&bar[foo][goo]=4, then use getParamsExtended().
In response, you will get a multi-level object. Most likely you will not need this function.

jcurl.getExt('example.com?bar[t]=1&bar[j]=2');
// { bar: { t: '1', j: '2' } }

addParamsExtended(String url, Object params)

alias addExt()

jcurl.addExt('example.com', { bar: { foo: 'test', joo: 2 } });
// example.com?bar[foo]=test&bar[joo]=2

The getParamsExtended and addParamsExtended functions may not answer exactly. Please tell me which tests failed.

About

A lightweight module to construct and parse query parameters of URLs. A set of functions for working with url. Easy to add parameters to url, easy to extract parameters from url. You can also get path from url.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  
0