Skip to content

Commit

Permalink
✨ Add String#link function
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Jul 1, 2021
1 parent b9bb4c8 commit 724e0ab
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,9 @@ The module that bundles the dependencies is obtained from
### String
`endsWith`, `match`, `repeat`, `replace`, `startsWith`, `toLowerCase`,
`toUpperCase`, `trimEnd`, `trimLeft`, `trimRight`, `trimStart`, `trim`
`charAt`, `endsWith`, `link`, `match`, `repeat`, `replace`, `startsWith`,
`toLowerCase`, `toUpperCase`, `trimEnd`, `trimLeft`, `trimRight`, `trimStart`,
`trim`
### Symbol
Expand Down
20 changes: 20 additions & 0 deletions string/link.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license.
import { curry } from "../deps.ts";
import { link as _link } from "../uncurry/string/link.ts";

/**
* Returns an `<a>` HTML element and sets the href attribute value.
* @param url - href url string
* @param val - Any `String`
* @returns The result of `val.link(url)`
*
* @example
* ```ts
* link('https://google.com', 'google') // '<a href="https://google.com">google</a>'
* ```
*
* @beta
*/
const link = curry(_link);

export { link };
9 changes: 9 additions & 0 deletions string/link_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license.
import { link } from "./link.ts";
import { assertEqualsType } from "../dev_deps.ts";

Deno.test("link", () => {
assertEqualsType<(val: string) => string>(link(""));
assertEqualsType<string>(link("")(""));
assertEqualsType<string>(link("", ""));
});
1 change: 1 addition & 0 deletions string/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export { trimEnd } from "./trim_end.ts";
export { trimStart } from "./trim_start.ts";
export { match } from "./match.ts";
export { charAt } from "./char_at.ts";
export { link } from "./link.ts";
27 changes: 27 additions & 0 deletions uncurry/string/link.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license.

/**
* @internal
*/
type Link<T extends string, U extends string> = `\<a href="${T}">${U}\</a>`;
/**
* Returns an `<a>` HTML element and sets the href attribute value.
* @param url - href url string
* @param val - Any `String`
* @returns The result of `val.link(url)`
*
* @example
* ```ts
* link('https://google.com', 'google') // '<a href="https://google.com">google</a>'
* ```
*
* @beta
*/
const link = <T extends string, U extends string>(
url: T,
val: U,
): Link<T, U> => val.link(url) as Link<T, U>;

export { link };
export type { Link };
18 changes: 18 additions & 0 deletions uncurry/string/link_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright 2021-present the Core-fn authors. All rights reserved. MIT license.
import { assertEquals } from "../../dev_deps.ts";
import { link } from "./link.ts";

Deno.test("link", () => {
const table: [string, string, string][] = [
["", "", '<a href=""></a>'],
["abc", "", '<a href="abc"></a>'],
["abc", "efg", '<a href="abc">efg</a>'],
];
table.forEach(([pos, val, expected]) => {
assertEquals(
link(pos, val),
expected,
`link(${pos}, ${val}) -> ${expected}`,
);
});
});
1 change: 1 addition & 0 deletions uncurry/string/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export { endsWith } from "./ends_with.ts";
export { repeat } from "./repeat.ts";
export { match } from "./match.ts";
export { charAt } from "./char_at.ts";
export { link } from "./link.ts";

0 comments on commit 724e0ab

Please sign in to comment.