https://github.com/lukeed/local-hostname
A tiny (171B) utility to check if a hostname is local
https://github.com/lukeed/local-hostname
Last synced: 10 months ago
JSON representation
A tiny (171B) utility to check if a hostname is local
- Host: GitHub
- URL: https://github.com/lukeed/local-hostname
- Owner: lukeed
- License: mit
- Created: 2019-10-13T04:57:03.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-10-13T05:06:28.000Z (almost 7 years ago)
- Last Synced: 2025-10-06T01:35:58.043Z (10 months ago)
- Language: JavaScript
- Size: 7.81 KB
- Stars: 32
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
# local-hostname [](https://github.com/lukeed/local-hostname/actions) [](https://codecov.io/gh/lukeed/local-hostname)
> A tiny (171B) utility to check if a hostname is local
Compares an incoming URL `hostname` segment against a manually constructed RegExp pattern.
The pattern supports IPV4 and IPV6 addresses and is derived from [RFC1918](https://tools.ietf.org/html/rfc1918#section-3) – see [Wikipedia](https://en.wikipedia.org/wiki/Localhost) explainer.
This module supports browser and Node.js environments.
It is made available in three formats:
* **CommonJS**: `dist/localhost.js`
* **ES Module**: `dist/localhost.mjs`
* **UMD**: `dist/localhost.min.js`
## Install
```
$ npm install --save local-hostname
```
## Usage
```js
import localhost from 'local-hostname';
localhost('localhost'); //=> true
localhost('127.0.254.1'); //=> true
localhost('hello.localhost'); //=> true
localhost('192.168.0.1'); //=> true
localhost('localhost.123'); //=> false
localhost('192.167.100.300'); //=> false
localhost('168.254.1.255'); //=> false
localhost('example.com'); //=> false
```
## API
### localhost(hostname)
Returns: `Boolean`
#### hostname
Type: `String`
The `hostname` segment of a valid URL.
> **Important:** The `localhost` function does not parse or format the URL for you!
## Contributing
PRs are welcome, of course! However, please open an issue first to discuss the planned changes :)
I am _by no stretch of the imagination_ a networking expert – I most likely missed cases and/or ranges.
To change the `RegExp` pattern, modify or add the segment(s) inside `src/build.js`, run it with `node`, and then paste the result into the `src/index.js` file.
## License
MIT © [Luke Edwards](https://lukeed.com)