All files / src/utils url.ts

100% Statements 31/31
100% Branches 11/11
100% Functions 9/9
100% Lines 31/31

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 10424x 24x         24x         15x 15x 15x 5x   10x               213x       213x             164x 164x             180x 180x 35x   145x             3x 1x     2x             14x             64x             3x 3x 2x 2x 1x   1x         24x   24x             15x 15x      
import qs from 'qs';
import { Router } from '../router';
import { IDictionary } from './interfaces';
/**
 * URL util
 */
export class URL {
	/**
	 * Updates query parameters
	 */
	public static updateQueryString(value: IDictionary<any>, replace = false) {
		const currentQuery = URL.getParsedQueryString(URL.getFormattedQueryStringFromUrl());
		const query = { ...currentQuery, ...value };
		if (replace) {
			Router.replace({ query, path: Router.getPath(), hash: Router.getHash() });
		} else {
			Router.push({ query, path: Router.getPath(), hash: Router.getHash() });
		}
	}
 
	/**
	 * Get formatted query parameters
	 */
	public static getFormattedQueryString(obj: IDictionary<any>, options?: qs.IStringifyOptions): string {
		const config: qs.IStringifyOptions = {
			...URL.stringifyOptions,
			...options,
		};
		return qs.stringify(obj, config);
	}
 
	/**
	 * Get parsed query string from URL
	 */
	public static getParsedQueryStringFromUrl(): IDictionary<any> {
		const qsValue = URL.getFormattedQueryStringFromUrl();
		return qs.parse(qsValue, URL.parseOptions);
	}
 
	/**
	 * Get formatted query string from URL
	 */
	public static getFormattedQueryStringFromUrl(): string {
		const queryString = Router.getFullPath().split('?')!;
		if (queryString.length > 1) {
			return queryString[1].split('#')[0];
		}
		return '';
	}
 
	/**
	 * Get the properties in array format
	 */
	public static getPropertyAsArray(prop: string | string[]): string[] {
		if (Array.isArray(prop)) {
			return Array.from(prop);
		}
 
		return prop ? [prop] : [];
	}
 
	/**
	 * Set the query parameters
	 */
	public static setQueryString(query: IDictionary<any>) {
		Router.push({ query, path: Router.getPath() });
	}
 
	/**
	 * Replace the query parameters
	 */
	public static replaceQueryString(query: IDictionary<any>) {
		Router.replace({ query, path: Router.getPath() });
	}
 
	/**
	 * Delete a query parameter
	 */
	public static deleteQueryParam(param: string, replace = false) {
		const query: any = { ...Router.getQuery() };
		if (param in query) {
			query[param] = undefined;
			if (replace) {
				URL.replaceQueryString(query);
			} else {
				URL.setQueryString(query);
			}
		}
	}
 
	private static parseOptions: qs.IParseOptions = { allowDots: true, depth: 20 };
 
	private static stringifyOptions: qs.IStringifyOptions = {
		encode: false,
		allowDots: true,
		arrayFormat: 'repeat',
	};
 
	private static getParsedQueryString(value: string, options?: qs.IParseOptions): IDictionary<any> {
		const config: qs.IParseOptions = { ...URL.parseOptions, ...options };
		return qs.parse(value, config);
	}
}