Skip to content

Commit

Permalink
feat: refactor to typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
bjarneo committed Aug 5, 2023
1 parent c56b1f4 commit 87ebc1d
Show file tree
Hide file tree
Showing 12 changed files with 166 additions and 143 deletions.
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,45 @@ This package provides a performant way to extract domain names from URLs without

Learn more about [What is a URL](https://developer.mozilla.org/en-US/docs/Learn/Common_questions/What_is_a_URL)


## Supports

Both the browser and nodejs.

## Usage

### Installation

```bash
$ npm i --save extract-domain
```

### API
- urls: A string or an array of URLs.
- options { tld: true }: If it should take TLD domains to consideration (i.e. .co.uk)
- returns: A string representing the extracted domain name or an array of domain names.

/\*\*

- @param {Urls} urls ["https://www.google.com", "https://www.github.com"] or "https://www.google.com"
- @param {GetDomainOptions} opts `{ tld: true }` permit to get Top Level Domain like `*.co.uk`
- @returns {Urls | Promise<Urls>} Returns URL(s) or a promise of URL(s) if the PSL lib is being used
\*/

```js
extractDomain(urls, options);
const res = extractDomain(urls, options);
```

ES6 Import

```js
import extractDomain from 'extract-domain';
```

CommonJS Require

```js
const extractDomain = require('extract-domain');
```

Examples

```js
const urls = [
'https://www.npmjs.com/package/extract-domain',
Expand All @@ -58,8 +65,6 @@ extractDomain(urls); // [ 'npmjs.com', 'example.com', 'example.com', 'npmjs.com'

TLD support requires the optional dependency of the [`psl` library](https://www.npmjs.com/package/psl).

To use the code with the PSL support, you have to wrap it in either an async function or use it as a promise.

Examples

```bash
Expand All @@ -80,7 +85,7 @@ extractDomain(url, { tld: true }).then(console.log);
// example.co.uk
```

Please note that using the tld flag may significantly slow down the process. Benchmark results:
Please note that using the tld flag may significantly slow down the process. Benchmark (old) results:

```
# extract domain 10,000 times
Expand Down
2 changes: 1 addition & 1 deletion dist/extract-domain.cjs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
function r(r){if(r&&r.__esModule)return r;var e=Object.create(null);return r&&Object.keys(r).forEach(function(t){if("default"!==t){var n=Object.getOwnPropertyDescriptor(r,t);Object.defineProperty(e,t,n.get?n:{enumerable:!0,get:function(){return r[t]}})}}),e.default=r,e}function e(){throw new TypeError("The given URL is not a string. Please verify your string|array.")}var t=["/",":","?","#"],n=[".","/","@"];function o(o,i){"string"!=typeof o&&e();for(var f,u=0,s=0,a=0,c=o.length,l=0;c--&&++l&&!(u&&t.indexOf(o[l])>-1);)"."===o[l]&&(++u,s=l);for(f=l,l=s;l--;)if(-1!==n.indexOf(o[l])){a=l+1;break}if(0===a&&f>3)return o;if(a>0&&a<2)return"";if(i.tld){for(var p=0,y=["/","@"],g=s;g--;)if(y.indexOf(o[g])>-1){p=g+1;break}try{return Promise.resolve().then(function(){/*#__PURE__*/return r(require("psl"))}).then(function(r){return Promise.resolve(r.get(o.slice(p,f)))}).catch(console.error)}catch(r){throw console.error(r),Error("You must install psl library (https://www.npmjs.com/package/psl) to use `tld` option")}}return o.slice(a,f)}module.exports=function(r,t){if(void 0===t&&(t={}),"string"==typeof r)return o(r,t);if(Array.isArray(r)){for(var n=[],i=r.length,f=0;f<i;f++)n.push(o(r[f],t));return n}e()};
function r(r){if(r&&r.__esModule)return r;var e=Object.create(null);return r&&Object.keys(r).forEach(function(t){if("default"!==t){var n=Object.getOwnPropertyDescriptor(r,t);Object.defineProperty(e,t,n.get?n:{enumerable:!0,get:function(){return r[t]}})}}),e.default=r,e}var e=["/",":","?","#"],t=[".","/","@"];function n(n,o){for(var i,u=0,f=0,s=0,a=n.length,c=0;a--&&++c&&!(u&&e.indexOf(n[c])>-1);)"."===n[c]&&(++u,f=c);for(i=c,c=f;c--;)if(-1!==t.indexOf(n[c])){s=c+1;break}if(0===s&&i>3)return n;if(s>0&&s<2)return"";if(o.tld){for(var l=0,p=["/","@"],v=f;v--;)if(p.indexOf(n[v])>-1){l=v+1;break}return Promise.resolve().then(function(){/*#__PURE__*/return r(require("psl"))}).then(function(r){return Promise.resolve(r.get(n.slice(l,i)))}).catch(function(r){throw console.error(r),Error("You must install psl library (https://www.npmjs.com/package/psl) to use `tld` option")})}return n.slice(s,i)}module.exports=function(r,e){if(void 0===e&&(e={}),"string"==typeof r)return n(r,e);if(Array.isArray(r)){for(var t=[],o=r.length,i=0;i<o;i++){var u=n(r[i],e);t.push(u)}return t}!function(){throw new TypeError("The given URL is not a string. Please verify your string|array.")}()};
//# sourceMappingURL=extract-domain.cjs.map
2 changes: 1 addition & 1 deletion dist/extract-domain.cjs.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/extract-domain.modern.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 87ebc1d

Please sign in to comment.