Skip to content

Commit

Permalink
Update href implementation based on PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
macklinu committed Mar 17, 2017
1 parent f81184f commit 17e70fa
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
17 changes: 11 additions & 6 deletions source/dsl/DangerUtilsDSL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,22 @@ export interface DangerUtilsDSL {
/**
* Creates an HTML link.
*
* @param {string} href The HTML link's destination
* @param {string} text The HTML link's text
* @returns {string} The HTML <a> tag
* If `href` and `text` are falsy, null is returned.
* If `href` is falsy and `text` is truthy, `text` is returned.
* If `href` is truthy and `text` is falsy, an <a> tag is returned with `href` as its href and text value.
* Otherwise, if `href` and `text` are truthy, an <a> tag is returned with the `href` and `text` inserted as expected.
*
* @param {string} href The HTML link's destination.
* @param {string} text The HTML link's text.
* @returns {string|null} The HTML <a> tag.
*/
href(href: string, text: string): string
href(href: string, text: string): string | null

/**
* Converts an array of strings into a sentence.
*
* @param {Array<string>} array The array of strings
* @returns {string} The sentence
* @param {Array<string>} array The array of strings.
* @returns {string} The sentence.
*/
sentence(array: Array<string>): string
}
10 changes: 8 additions & 2 deletions source/runner/DangerUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ export function sentence(array: Array<string>): string {
return array.slice(0, array.length - 1).join(", ") + " and " + array.pop()
}

export function href(href: string, text: string): string {
return `<a href="${href || "#"}">${text || ""}</a>`
export function href(href: string, text: string): string | null {
if (!href && !text) {
return null
}
if (!href && text) {
return text
}
return `<a href="${href}">${text || href}</a>`
}
12 changes: 9 additions & 3 deletions source/runner/_tests/DangerUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ describe("sentence()", () => {
})

describe("href()", () => {
it("returns null when href and text are falsy", () => {
expect(href("", "")).toEqual(null)
})
it("returns just the text when the href is missing", () => {
expect(href("", "Some text")).toEqual("Some text")
})
it("returns <a> tag with href as text when text is missing", () => {
expect(href("/path/to/file", "")).toEqual(`<a href="/path/to/file">/path/to/file</a>`)
})
it("returns <a> tag for supplied href and text", () => {
expect(href("http://danger.systems", "Danger"))
.toEqual(`<a href="http://danger.systems">Danger</a>`)
})
it("handles falsy input", () => {
expect(href(null, undefined)).toEqual(`<a href="#"></a>`)
})
})

0 comments on commit 17e70fa

Please sign in to comment.