Skip to content

Commit 4e052ff

Browse files
authored
Export functions based on return type (#43)
1 parent ef991e9 commit 4e052ff

File tree

5 files changed

+96
-128
lines changed

5 files changed

+96
-128
lines changed

index.d.ts

+27-27
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,16 @@ export interface Options {
66
*/
77
readonly attributes?: HTMLAttributes;
88

9-
/**
10-
The format of the generated content.
11-
12-
`'string'` will return it as a flat string like `'Visit <a href="https://example.com">https://example.com</a>'`.
13-
14-
`'dom'` will return it as a `DocumentFragment` ready to be appended in a DOM safely, like `DocumentFragment(TextNode('Visit '), HTMLAnchorElement('https://example.com'))`. This type only works in the browser.
15-
*/
16-
readonly type?: 'string' | 'dom';
17-
189
/**
1910
Set a custom HTML value for the link.
2011
2112
Default: The URL.
2213
2314
@example
2415
```
25-
import linkifyUrls from 'linkify-urls';
16+
import {linkifyUrlsToHtml} from 'linkify-urls';
2617
27-
linkifyUrls('See https://sindresorhus.com/foo', {
18+
linkifyUrlsToHtml('See https://sindresorhus.com/foo', {
2819
value: url => new URL(url).pathname
2920
});
3021
//=> 'See <a href="https://sindresorhus.com/foo">/foo</a>'
@@ -34,20 +25,18 @@ export interface Options {
3425

3526
}
3627

37-
export interface TypeDomOptions extends Options {
38-
readonly type: 'dom';
39-
}
40-
4128
/**
42-
Linkify URLs in a string.
29+
Linkify URLs in a string, returns an HTML string.
4330
4431
@param string - A string with URLs to linkify.
4532
33+
@returns An HTML string like `'Visit <a href="https://example.com">https://example.com</a>'`.
34+
4635
@example
4736
```
48-
import linkifyUrls from 'linkify-urls';
37+
import {linkifyUrlsToHtml} from 'linkify-urls';
4938
50-
linkifyUrls('See https://sindresorhus.com', {
39+
linkifyUrlsToHtml('See https://sindresorhus.com', {
5140
attributes: {
5241
class: 'unicorn',
5342
one: 1,
@@ -56,24 +45,35 @@ linkifyUrls('See https://sindresorhus.com', {
5645
}
5746
});
5847
//=> 'See <a href="https://sindresorhus.com" class="unicorn" one="1" foo multiple="a b">https://sindresorhus.com</a>'
48+
```
49+
*/
50+
export function linkifyUrlsToHtml(
51+
string: string,
52+
options?: Options
53+
): string;
5954

60-
// In the browser
61-
const fragment = linkifyUrls('See https://sindresorhus.com', {
62-
type: 'dom',
55+
/**
56+
Linkify URLs in a string, returns a `DocumentFragment`.
57+
58+
@param string - A string with URLs to linkify.
59+
60+
@returns a `DocumentFragment` ready to be appended in a DOM safely, like `DocumentFragment(TextNode('Visit '), HTMLAnchorElement('https://example.com'))`. This type only works in the browser.
61+
62+
@example
63+
```
64+
import {linkifyUrlsToDom} from 'linkify-urls';
65+
66+
const fragment = linkifyUrlsToDom('See https://sindresorhus.com', {
6367
attributes: {
6468
class: 'unicorn',
6569
}
6670
});
6771
document.body.appendChild(fragment);
6872
```
6973
*/
70-
export default function linkifyUrls(
71-
string: string,
72-
options: TypeDomOptions
73-
): DocumentFragment;
74-
export default function linkifyUrls(
74+
export function linkifyUrlsToDom(
7575
string: string,
7676
options?: Options
77-
): string;
77+
): DocumentFragment;
7878

7979
export {HTMLAttributes} from 'create-html-element';

index.js

+10-24
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import createHtmlElement from 'create-html-element';
44
const urlRegex = () => (/((?<!\+)https?:\/\/(?:www\.)?(?:[-\w.]+?[.@][a-zA-Z\d]{2,}|localhost)(?:[-\w.:%+~#*$!?&/=@]*?(?:,(?!\s))*?)*)/g);
55

66
// Get `<a>` element as string
7-
const linkify = (href, options) => createHtmlElement({
7+
const linkify = (href, options = {}) => createHtmlElement({
88
name: 'a',
99
attributes: {
1010
href: '',
@@ -23,12 +23,16 @@ const isTruncated = (url, peek) =>
2323
url.endsWith('...') // `...` is a matched by the URL regex
2424
|| peek.startsWith('…'); // `…` can follow the match
2525

26-
const getAsString = (string, options) => string.replace(urlRegex(), (url, _, offset) =>
27-
(isTruncated(url, string.charAt(offset + url.length)))
28-
? url // Don't linkify truncated URLs
29-
: linkify(url, options));
26+
export function linkifyUrlsToHtml(string, options) {
27+
const replacer = (url, _, offset) =>
28+
isTruncated(url, string.charAt(offset + url.length))
29+
? url // Don't linkify truncated URLs
30+
: linkify(url, options);
3031

31-
const getAsDocumentFragment = (string, options) => {
32+
return string.replace(urlRegex(), replacer);
33+
}
34+
35+
export function linkifyUrlsToDom(string, options) {
3236
const fragment = document.createDocumentFragment();
3337
const parts = string.split(urlRegex());
3438

@@ -42,22 +46,4 @@ const getAsDocumentFragment = (string, options) => {
4246
}
4347

4448
return fragment;
45-
};
46-
47-
export default function linkifyUrls(string, options) {
48-
options = {
49-
attributes: {},
50-
type: 'string',
51-
...options,
52-
};
53-
54-
if (options.type === 'string') {
55-
return getAsString(string, options);
56-
}
57-
58-
if (options.type === 'dom') {
59-
return getAsDocumentFragment(string, options);
60-
}
61-
62-
throw new TypeError('The type option must be either `dom` or `string`');
6349
}

index.test-d.ts

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import {expectType} from 'tsd';
2-
import linkifyUrls from './index.js';
2+
import {linkifyUrlsToHtml, linkifyUrlsToDom} from './index.js';
33

44
expectType<string>(
5-
linkifyUrls('See https://sindresorhus.com', {
5+
linkifyUrlsToHtml('See https://sindresorhus.com', {
66
attributes: {
77
class: 'unicorn',
88
one: 1,
@@ -12,26 +12,23 @@ expectType<string>(
1212
}),
1313
);
1414
expectType<string>(
15-
linkifyUrls('See https://sindresorhus.com', {
15+
linkifyUrlsToHtml('See https://sindresorhus.com', {
1616
value: 'foo',
1717
}),
1818
);
1919
expectType<string>(
20-
linkifyUrls('See https://sindresorhus.com/foo', {
20+
linkifyUrlsToHtml('See https://sindresorhus.com/foo', {
2121
value: url => {
2222
expectType<string>(url);
2323
return url;
2424
},
2525
}),
2626
);
2727
expectType<string>(
28-
linkifyUrls('See https://sindresorhus.com/foo', {
29-
type: 'string',
30-
}),
28+
linkifyUrlsToHtml('See https://sindresorhus.com/foo'),
3129
);
3230

33-
const fragment = linkifyUrls('See https://sindresorhus.com', {
34-
type: 'dom',
31+
const fragment = linkifyUrlsToDom('See https://sindresorhus.com', {
3532
attributes: {
3633
class: 'unicorn',
3734
},

readme.md

+17-18
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ npm install linkify-urls
1111
## Usage
1212

1313
```js
14-
import linkifyUrls from 'linkify-urls';
14+
import {linkifyUrlsToHtml, linkifyUrlsToDom} from 'linkify-urls';
1515

16-
linkifyUrls('See https://sindresorhus.com', {
16+
linkifyUrlsToHtml('See https://sindresorhus.com', {
1717
attributes: {
1818
class: 'unicorn',
1919
one: 1,
@@ -28,8 +28,7 @@ linkifyUrls('See https://sindresorhus.com', {
2828

2929

3030
// In the browser
31-
const fragment = linkifyUrls('See https://sindresorhus.com', {
32-
type: 'dom',
31+
const fragment = linkifyUrlsToDom('See https://sindresorhus.com', {
3332
attributes: {
3433
class: 'unicorn',
3534
}
@@ -39,7 +38,9 @@ document.body.appendChild(fragment);
3938

4039
## API
4140

42-
### linkifyUrls(string, options?)
41+
### linkifyUrlsToHtml(string, options?)
42+
43+
Returns an HTML string like `'Visit <a href="https://example.com">https://example.com</a>'`.
4344

4445
#### string
4546

@@ -57,18 +58,6 @@ Type: `object`
5758

5859
HTML attributes to add to the link.
5960

60-
##### type
61-
62-
Type: `string`\
63-
Values: `'string' | 'dom'`\
64-
Default: `'string'`
65-
66-
The format of the generated content.
67-
68-
`'string'` will return it as a flat string like `'Visit <a href="https://example.com">https://example.com</a>'`.
69-
70-
`'dom'` will return it as a `DocumentFragment` ready to be appended in a DOM safely, like `DocumentFragment(TextNode('Visit '), HTMLAnchorElement('https://example.com'))`. This type only works in the browser.
71-
7261
##### value
7362

7463
Type: `string | Function`\
@@ -79,12 +68,22 @@ Set a custom HTML value for the link.
7968
If it's a function, it will receive the URL as a string:
8069

8170
```js
82-
linkifyUrls('See https://sindresorhus.com/foo', {
71+
linkifyUrlsToHtml('See https://sindresorhus.com/foo', {
8372
value: url => new URL(url).pathname
8473
});
8574
//=> 'See <a href="https://sindresorhus.com/foo">/foo</a>'
8675
```
8776

77+
### linkifyUrlsToDom(string, options?)
78+
79+
Returns a `DocumentFragment` ready to be appended in a DOM safely, like `DocumentFragment(TextNode('Visit '), HTMLAnchorElement('https://example.com'))`.
80+
81+
This type only works in the browser.
82+
83+
#### options
84+
85+
See [options](#options) above.
86+
8887
## Browser compatibility
8988

9089
Version 3 of this package uses [negative lookbehind regex syntax](https://kangax.github.io/compat-table/es2016plus/#test-RegExp_Lookbehind_Assertions). Stay on version 2 if you need to support browsers that doesn't support this feature.

0 commit comments

Comments
 (0)