import { Inject, Injectable, } from '@angular/core'; import { DOCUMENT, } from '@angular/common'; @Injectable() export class QueryStringService { constructor( @Inject('window') private _window: Window, @Inject(DOCUMENT) private _document: Document, ) {} public getQueryStringKey(name: string) { const search = this._window.location.search; const processedName = name.replace(/[\[\]]/g, '\\$&'); const regex = new RegExp('[?&]' + processedName + '(=([^&#]*)|&|#|$)'); const [ fullQuery, queryValueWithEquals, queryValue ] = regex.exec(search) || [null, null, null]; return queryValue ? decodeURIComponent(queryValue.replace(/\+/g, ' ')) : null; } public clearQueryStrings() { const location = this._window.location; if (location.search) { this._window.history.replaceState( {}, this._document.title, location.origin + location.pathname + location.hash, ); } } public convertPropertyNameToQueryStringKey(objectKey: string) { return objectKey.replace(/([A-Z])/g, (g) => `-${g[0].toLowerCase()}`); } }