Skip to content

Commit

Permalink
Make Owner addressable via RFC 2822 standard
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed Mar 27, 2024
1 parent 995709a commit 54e3dc6
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
{{#if owner}}
<h3 class="pane__section-title">{{t 'testResult.owner.name'}}</h3>
<div>{{owner}}</div>
{{/if}}
{{t 'testResult.owner.name'}}:
{{#if owner.url}}
<a href="{{owner.url}}">{{owner.displayName}}</a>
{{else}}
{{owner.displayName}}
{{/if}}
{{/if}}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { View } from "backbone.marionette";
import { className } from "../../decorators/index";
import parseAddress from "../../utils/parseAddress";
import template from "./OwnerView.hbs";

@className("pane__section")
Expand All @@ -9,7 +10,7 @@ class OwnerView extends View {
serializeData() {
const extra = this.model.get("extra");
return {
owner: extra ? extra.owner : null,
owner: extra ? parseAddress(extra.owner) : null,
};
}
}
Expand Down
60 changes: 60 additions & 0 deletions allure-generator/src/main/javascript/utils/parseAddress.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const RFC2822_ADDRESS = /^(.*) <(.*)>$/;
const LOOKS_LIKE_EMAIL = /^[^@]+@[^@]+$/;

/**
* Parse a potentially RFC 2822 address into a display name and an address.
*
* @param {string | null | undefined} maybeAddress
* @returns {{ displayName: string, url?: string } | null
*/
export default function parseAddress(maybeAddress) {
if (!maybeAddress) {
return null;
}

const match = maybeAddress.match(RFC2822_ADDRESS);
if (match) {
return {
displayName: match[1],
url: toHref(match[2]),
};
}

return {
displayName: maybeAddress,
url: toHref(maybeAddress),
};
}

/**
* If the address is a valid URL, returns the URL.
* If the address resembles an email address, returns a mailto: URL.
* Otherwise, returns undefined.
*
* @param {string} address
* @returns {string | undefined}
*/
function toHref(address) {
if (isValidURL(address)) {
return address;
}

if (LOOKS_LIKE_EMAIL.test(address)) {
return `mailto:${address}`;
}
}

/**
* If the address is a valid URL, returns the URL.
*
* @param {string} maybeURL
* @returns {boolean}
*/
function isValidURL(maybeURL) {
try {
new URL(maybeURL);
return true;
} catch (_error) {
return false;
}
}

0 comments on commit 54e3dc6

Please sign in to comment.