File

projects/lib/src/url-helper.service.ts

Index

Methods

Methods

Public getHashFragmentParams
getHashFragmentParams(customHashFragment?: string)
Parameters :
Name Type Optional
customHashFragment string Yes
Returns : object
Public parseQueryString
parseQueryString(queryString: string)
Parameters :
Name Type Optional
queryString string No
Returns : object
import { Injectable } from '@angular/core';

@Injectable()
export class UrlHelperService {
  public getHashFragmentParams(customHashFragment?: string): object {
    let hash = customHashFragment || window.location.hash;

    hash = decodeURIComponent(hash);

    if (hash.indexOf('#') !== 0) {
      return {};
    }

    const questionMarkPosition = hash.indexOf('?');

    if (questionMarkPosition > -1) {
      hash = hash.substr(questionMarkPosition + 1);
    } else {
      hash = hash.substr(1);
    }

    return this.parseQueryString(hash);
  }

  public parseQueryString(queryString: string): object {
    const data = {};
    let pair, separatorIndex, escapedKey, escapedValue, key, value;

    if (queryString === null) {
      return data;
    }

    const pairs = queryString.split('&');

    for (let i = 0; i < pairs.length; i++) {
      pair = pairs[i];
      separatorIndex = pair.indexOf('=');

      if (separatorIndex === -1) {
        escapedKey = pair;
        escapedValue = null;
      } else {
        escapedKey = pair.substr(0, separatorIndex);
        escapedValue = pair.substr(separatorIndex + 1);
      }

      key = decodeURIComponent(escapedKey);
      value = decodeURIComponent(escapedValue);

      if (key.substr(0, 1) === '/') {
        key = key.substr(1);
      }

      data[key] = value;
    }

    return data;
  }
}

results matching ""

    No results matching ""