Skip to content

Instantly share code, notes, and snippets.

@codeaid
Created June 14, 2017 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeaid/eeca52172edcc194af875be8fc8ab3ed to your computer and use it in GitHub Desktop.
Save codeaid/eeca52172edcc194af875be8fc8ab3ed to your computer and use it in GitHub Desktop.
import {parse, stringify} from 'query-string';
export class SearchQuery {
private params: Object = {};
/**
* Class constructor
*
* @param {string} query Query string
*/
constructor(query: string) {
this.params = parse(query);
}
/**
* Check if the specified parameter exists
*
* @param {string} name Name of the parameter
* @return {boolean}
*/
has(name: string): boolean {
return name in this.params;
}
/**
* Get value of the specified parmeter or fallback value if not found
*
* @param {string} name Name of the parameter
* @param {any} fallback Fallback value
* @return {any}
*/
get(name: string, fallback: any = null): any {
let {params} = this;
// param exists
if (this.has(name)) {
return params[name];
}
return fallback;
}
/**
* Get all specified parameters
*
* @return {Object}
*/
getParams(): Object {
return {...this.params};
}
/**
* Convert current query parameters to a string
*
* @return {string}
*/
toString(): string {
return stringify(this.getParams());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment