https://github.com/scalajs-io/ip
IP address utilities for node.js
https://github.com/scalajs-io/ip
Last synced: 4 months ago
JSON representation
IP address utilities for node.js
- Host: GitHub
- URL: https://github.com/scalajs-io/ip
- Owner: scalajs-io
- License: apache-2.0
- Created: 2017-03-29T15:46:09.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2019-06-17T23:09:15.000Z (about 6 years ago)
- Last Synced: 2025-01-17T22:42:34.556Z (5 months ago)
- Language: Scala
- Size: 12.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
IP API for Scala.js
================================
[ip](https://www.npmjs.com/package/ip) - IP address utilities for node.js.### Description
IP address utilities for node.js
### Build Dependencies
* [SBT v1.2.x](http://www.scala-sbt.org/download.html)
### Build/publish the SDK locally
```bash
$ sbt clean publish-local
```### Running the tests
Before running the tests the first time, you must ensure the npm packages are installed:
```bash
$ npm install
```Then you can run the tests:
```bash
$ sbt test
```### Examples
```scala
import io.scalajs.nodejs.buffer.Buffer
import io.scalajs.npm.ip._
import scala.scalajs.jsIP.address() // my ip address
IP.isEqual("::1", "::0:1") // true
IP.toBuffer("127.0.0.1") // Buffer([127, 0, 0, 1])
IP.toString(new Buffer(js.Array(127, 0, 0, 1))) // 127.0.0.1
IP.fromPrefixLen(24) // 255.255.255.0
IP.mask("192.168.1.134", "255.255.255.0") // 192.168.1.0
IP.cidr("192.168.1.134/26") // 192.168.1.128
IP.not("255.255.255.0") // 0.0.0.255
IP.or("192.168.1.134", "0.0.0.255") // 192.168.1.255
IP.isPrivate("127.0.0.1") // true
IP.isV4Format("127.0.0.1") // true
IP.isV6Format("::ffff:127.0.0.1") // true// operate on buffers in-place
val buf = new Buffer(128)
val offset = 64
IP.toBuffer("127.0.0.1", buf, offset) // [127, 0, 0, 1] at offset 64
IP.toString(buf, offset, 4) // "127.0.0.1"
// subnet information
IP.subnet("192.168.1.134", "255.255.255.192")
// { networkAddress: "192.168.1.128",
// firstAddress: "192.168.1.129",
// lastAddress: "192.168.1.190",
// broadcastAddress: "192.168.1.191",
// subnetMask: "255.255.255.192",
// subnetMaskLength: 26,
// numHosts: 62,
// length: 64,
// contains: function(addr){...} }IP.cidrSubnet("192.168.1.134/26")
// Same as previous.
// range checking
IP.cidrSubnet("192.168.1.134/26").contains("192.168.1.190") // true
```### Artifacts and Resolvers
To add the `IP` binding to your project, add the following to your build.sbt:
```sbt
libraryDependencies += "io.scalajs.npm" %%% "ip" % "0.5.0"
```Optionally, you may add the Sonatype Repository resolver:
```sbt
resolvers += Resolver.sonatypeRepo("releases")
```