Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Aug 9, 2021
1 parent 937b6b2 commit f20aa25
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 34 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ jobs:
fail-fast: false
matrix:
node-version:
- 14
- 12
- 10
- 8
- 16
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
17 changes: 8 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
'use strict';
const url = require('url');
const prependHttp = require('prepend-http');
import {parse as parseUrl} from 'node:url';
import prependHttp from 'prepend-http';

module.exports = (inputUrl, options) => {
if (typeof inputUrl !== 'string') {
throw new TypeError(`Expected \`url\` to be of type \`string\`, got \`${typeof inputUrl}\` instead.`);
export default function urlParseLax(url, options) {
if (typeof url !== 'string') {
throw new TypeError(`Expected \`url\` to be of type \`string\`, got \`${typeof url}\` instead.`);
}

const finalUrl = prependHttp(inputUrl, options);
return url.parse(finalUrl); // eslint-disable-line node/no-deprecated-api
};
const finalUrl = prependHttp(url, options);
return parseUrl(finalUrl);
}
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{
"name": "url-parse-lax",
"version": "4.0.0",
"description": "Lax url.parse() with support for protocol-less URLs & IPs",
"description": "Lax `url.parse()` with support for protocol-less URLs & IPs",
"license": "MIT",
"repository": "sindresorhus/url-parse-lax",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
},
"scripts": {
"test": "xo && ava"
Expand All @@ -20,7 +23,6 @@
],
"keywords": [
"url",
"uri",
"parse",
"parser",
"loose",
Expand All @@ -33,10 +35,10 @@
"ipv6"
],
"dependencies": {
"prepend-http": "^3.0.1"
"prepend-http": "^4.0.0"
},
"devDependencies": {
"ava": "^2.1.0",
"xo": "^0.24.0"
"ava": "^3.15.0",
"xo": "^0.44.0"
}
}
13 changes: 4 additions & 9 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

> Lax [`url.parse()`](https://nodejs.org/docs/latest/api/url.html#url_url_parse_urlstr_parsequerystring_slashesdenotehost) with support for protocol-less URLs & IPs

## Install

```
$ npm install url-parse-lax
```


## Usage

```js
const urlParseLax = require('url-parse-lax');
import urlParseLax from 'url-parse-lax';

urlParseLax('sindresorhus.com');
/*
Expand Down Expand Up @@ -55,7 +53,7 @@ urlParseLax('[2001:db8::]:8000');
And with the built-in `url.parse()`:

```js
const url = require('url');
import url from 'url';

url.parse('sindresorhus.com');
/*
Expand Down Expand Up @@ -94,7 +92,6 @@ url.parse('[2001:db8::]:8000');
*/
```


## API

### urlParseLax(url, options?)
Expand All @@ -103,25 +100,23 @@ url.parse('[2001:db8::]:8000');

Type: `string`

URL to parse.
The URL to parse.

#### options

Type: `object`

##### https

Type: `boolean`<br>
Type: `boolean`\
Default: `true`

Prepend `https://` instead of `http://` to protocol-less URLs.


## Related

- [url-format-lax](https://github.com/sindresorhus/url-format-lax) - Lax `url.format()` that formats a hostname and port into IPv6-compatible socket form of `hostname:port`


---

<div align="center">
Expand Down
8 changes: 5 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import test from 'ava';
import urlParseLax from '.';
import urlParseLax from './index.js';

test('wrong input', t => {
t.throws(() => {
urlParseLax(5);
}, 'Expected `url` to be of type `string`, got `number` instead.');
}, {
message: 'Expected `url` to be of type `string`, got `number` instead.',
});
});

test('parse urls', t => {
test('parse URLs', t => {
t.is(urlParseLax('sindresorhus.com').hostname, 'sindresorhus.com');
t.is(urlParseLax('192.168.0.1:80').hostname, '192.168.0.1');
t.is(urlParseLax('[2001:db8::]:80').hostname, '2001:db8::');
Expand Down

0 comments on commit f20aa25

Please sign in to comment.