-
-
Save codeaid/eeca52172edcc194af875be8fc8ab3ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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