-
-
Notifications
You must be signed in to change notification settings - Fork 364
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #134 from danger/danger-utils-dsl
Add danger.utils DSL
- Loading branch information
Showing
6 changed files
with
102 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* The Danger Utils DSL contains utility functions | ||
* that are specific to universal Danger use-cases. | ||
*/ | ||
export interface DangerUtilsDSL { | ||
|
||
/** | ||
* Creates an HTML link. | ||
* | ||
* 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 | null | ||
|
||
/** | ||
* Converts an array of strings into a sentence. | ||
* | ||
* @param {Array<string>} array The array of strings. | ||
* @returns {string} The sentence. | ||
*/ | ||
sentence(array: Array<string>): string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export function sentence(array: Array<string>): string { | ||
if ((array || []).length === 0) { | ||
return "" | ||
} | ||
if (array.length === 1) { | ||
return array[0] | ||
} | ||
return array.slice(0, array.length - 1).join(", ") + " and " + array.pop() | ||
} | ||
|
||
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>` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { href, sentence } from "../DangerUtils" | ||
|
||
describe("sentence()", () => { | ||
it("handles falsy input", () => { | ||
expect(sentence(null)).toEqual("") | ||
}) | ||
it("handles empty array", () => { | ||
expect(sentence([])).toEqual("") | ||
}) | ||
it("handles array with one item", () => { | ||
expect(sentence(["Hello"])).toEqual("Hello") | ||
}) | ||
it("handles array with multiple items", () => { | ||
expect(sentence(["This", "that", "the other thing"])) | ||
.toEqual("This, that and the other thing") | ||
}) | ||
}) | ||
|
||
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>`) | ||
}) | ||
}) |