1.0.5 • Published 4 years ago

simple-query-params v1.0.5

Weekly downloads
7
License
ISC
Repository
github
Last release
4 years ago

Simple query params

CircleCI

Maintainability

Simple, tiny, typed and tested library to manage with ease query param.

It can be used to add query params to url or modify those that url contains.

Download

npm install simple-query-params

API

This library exposes two classes, Url and QueryParams.

Url

// You can build one Url instance passing a url

const url = new Url('https://www.fakeurl.com?foo=bar');

// This object exposes multiples methods and properties:

url.baseUrl  // Returns base url, for this example 'https://www.fakeurl.com?'

url.originalUrl // Returns the url which used to create instance and keep it, never changes.

url.setValue(name: string, value: QueryParamType, mode?: 'append' | 'replace'); // Sets the value to existing query param or create it if doesn't exists. `mode` is used when query param its an array, then, with 'append' you push another value at the tail of the array, with 'replace', the array is overwitted by the value passed.

url.setValue('foo', 'baz'); // Change the value of 'foo' parameter to 'baz'

url.setValue('param', 123); // Creates new param

url.builtUrl // Returns the built url with query params applied. 
// https://www.fakeurl.com?foo=faz&param=123

url.getParam(name: string); // Gets parameter, finding it by it name

Query Param

Create new instance of this object make no sense, its usefull only when its accesed through Url#getParam.

  
  const queryParam = url.getParam('name');

  // This class exposes the following get/set methods:

  queryParam.stringified // returns formated parameter. p.e: 'name=value' instead {name, value}

  queryParam.value // returns parameter value

  queryParam.name // returns parameter name

  queryParam.type // returns the undelying type of parameter. p.e: 'value' will return 'string', 123 and '123' will return 'number'. And 'v,a,l,u,e' will return 'array

  queryParam.isString // Returns true if undelying value type is string, false otherwise

  queryParam.isNumber // Returns true if undelying value type is number, false otherwise

  queryParam.isArray // Returns true if undelying value type is string, false otherwise

  // Only exposes one method

  queryParam.setValue(value: QueryParamType, mode?: 'append' | 'replace') // change de value of parameter. mode argument works like Url#setValue
1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago