Skip to content

Instantly share code, notes, and snippets.

@shifatul-i
Created October 8, 2018 21:03
Show Gist options
  • Save shifatul-i/b99c7c484d663a0a802d2c4ec0e0e9b0 to your computer and use it in GitHub Desktop.
Save shifatul-i/b99c7c484d663a0a802d2c4ec0e0e9b0 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