File

projects/storefrontlib/shared/components/generic-link/generic-link-component.service.ts

Index

Properties
Methods

Methods

isExternalUrl
isExternalUrl(url: string | any[])

Returns true when the @Input url is a string starting with http://, https://, mailto:, tel:.

Parameters :
Name Type Optional
url string | any[] No
Returns : boolean

Properties

Protected HTTP_PROTOCOL_REGEX
Type : RegExp
Default value : /^https?:\/\//i

Pattern matching string starting with http:// or https://.

Protected MAILTO_PROTOCOL_REGEX
Type : RegExp
Default value : /^mailto:/i

Pattern matching string starting with mailto:.

Protected TEL_PROTOCOL_REGEX
Type : RegExp
Default value : /^tel:/i

Pattern matching string starting with tel:.

import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root',
})
export class GenericLinkComponentService {
  /**
   * Pattern matching string starting with `http://` or `https://`.
   */
  protected HTTP_PROTOCOL_REGEX: RegExp = /^https?:\/\//i;

  /**
   * Pattern matching string starting with `mailto:`.
   */
  protected MAILTO_PROTOCOL_REGEX: RegExp = /^mailto:/i;

  /**
   * Pattern matching string starting with `tel:`.
   */
  protected TEL_PROTOCOL_REGEX: RegExp = /^tel:/i;

  /**
   * Returns true when the @Input `url` is a string starting with `http://`, `https://`, `mailto:`, `tel:`.
   */
  isExternalUrl(url: string | any[]): boolean {
    return (
      typeof url === 'string' &&
      (this.HTTP_PROTOCOL_REGEX.test(url) ||
        this.MAILTO_PROTOCOL_REGEX.test(url) ||
        this.TEL_PROTOCOL_REGEX.test(url))
    );
  }
}

result-matching ""

    No results matching ""