| 3 | * to the window hostname |
| 4 | */ |
| 5 | export function truncateUrl( |
| 6 | url: string, |
| 7 | num: number, |
| 8 | truncateBack?: boolean, |
| 9 | ): string { |
| 10 | // Remove the schema, the https:// part |
| 11 | let noSchema = url.replace(/(^\w+:|^)\/\//, ''); |
| 12 | if ( |
| 13 | typeof window !== 'undefined' && |
| 14 | window?.location && |
| 15 | noSchema.startsWith(window.location.hostname) |
| 16 | ) { |
| 17 | noSchema = noSchema.slice(window.location.hostname.length); |
| 18 | } |
| 19 | if (noSchema.length <= num) { |
| 20 | return noSchema; |
| 21 | } |
| 22 | if (truncateBack) { |
| 23 | const tooMuch = noSchema.length - num; |
| 24 | return '...' + noSchema.slice(tooMuch); |
| 25 | } |
| 26 | return noSchema.slice(0, num) + '...'; |
| 27 | } |