Skip to content

Instantly share code, notes, and snippets.

@shifatul-i
Created October 8, 2018 21:03
Show Gist options
  • Save shifatul-i/f1eb212042f43ea6e2054394c1341f95 to your computer and use it in GitHub Desktop.
Save shifatul-i/f1eb212042f43ea6e2054394c1341f95 to your computer and use it in GitHub Desktop.
Angular - short domain pipe (URL to domain - https://github.com/ThunderRoid → github.com)
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'shortDomain'
})
export class ShortDomainPipe implements PipeTransform {
transform(url: string, args?: any): any {
if (url) {
if (url.length > 3) {
let result;
let match;
if (match = url.match(/^(?:https?:\/\/)?(?:www\.)?([^:\/\n?=]+)/im)) {
result = match[1];
if (match = result.match(/^[^.]+\.(.+\..+)$/))
result = match[1];
}
return result;
}
return url;
}
return url;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment