npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ip-wiki

v1.0.0

Published

An IP address utility library for Wikipedia and NodeJS.

Downloads

7

Readme

ip-wiki — IP Address Utility Library for Wikipedia and NodeJS

ip-wiki is a JavaScript library written in ES6 that provides classes for manipulating IP and CIDR addresses. As the name suggests, it was developed for use on (front-end) Wikipedia; however, it also works as a NodeJS module in back-end environments.

  • API documentation is avaiable!
  • The library is TypeScript-compatible!
  • No external dependencies!

Installation

npm install ip-wiki

If you only need type definitions:

npm install -D ip-wiki

Usage

NodeJS

In CommonJS:

const {IP, IPUtil} = require('ip-wiki');

In ES modules:

import {IP, IPUtil} from 'ip-wiki';

Then:

Intellisense for NodeJS projects.

Wikipedia

Load and import ja:MediaWiki:Gadget-ip-wiki.js. In this case, this package is for Intellisense (you may also want to install types-mediawiki as a dev dependency).

/**
 * @returns {JQueryPromise<import('ip-wiki')>}
 */
function getIpWiki() {
	const gadget = 'ext.gadget.ip-wiki';
	return mw.loader.using(gadget).then((req) => req(gadget));
}

getIpWiki().then((ipWiki) => {
	const {IP, IPUtil} = ipWiki;
	// ...
});

Note that you may need to cross-wiki-load the gadget if a module named ip-wiki is not defined in the local Gadgets-definition:

/**
 * @returns {JQueryPromise<import('ip-wiki')>}
 */
function getIpWikiX() {
	const gadget = 'ext.gadget.ip-wiki';
	return mw.loader.getScript('https://ja.wikipedia.org/w/load.php?modules=' + gadget).then(() => {
		return mw.loader.using(gadget).then((req) => req(gadget));
	});
}

Then:

Intellisense for Wikipedia projects.

Class showcases

This library has two main classes: the IP class and the static IPUtil class:

  • Use the IP class when you need to do manipulations on the same IP recursively (more efficient than to use IPUtil because we can skip the parsing process of the relevant input IP string).
  • Use the IPUtil class for one-time manipulations (i.e. when there is no need to create a class instance).

Class IP

Suppose that you need to retrieve the indexes of IP-representing elements in the ipArr array that equal the IP address 192.168.1.1:

const ip = IP.newFromText('192.168.1.1');
if (!ip) {
	return;
}
const ipArr = [
	'192.168.1.1/32',
	'::1',
	'192.168.001.001'
];
const indexes = ipArr.reduce(/** @param {number[]} acc */ (acc, ipStr, i) => {
	if (ip.equals(ipStr)) { // Not IPUtil.equals('192.168.1.1', ipStr)
		acc.push(i);
	}
	return acc;
}, []);
console.log(indexes); // [ 0, 2 ]

Class IPUtil

Suppose that you need to filter the ipArr array so that it will only contain IPv6 addresses and CIDRs:

const ipArr = [
	'192.168.1.1/32',
	'::1',
	'192.168.001.001'
];
const filtered = ipArr.filter((ip) => IPUtil.isIPv6(ip, true));
console.log(filtered); // [ '::1' ]

Suppose that you need to create an array of IPv6 CIDRs in their sanitized notations out of the ipArr array:

const ipArr = [
	'foo',
	'192.168.1.0',
	'fd12:3456:789a:1::1',
	'fd12:3456:789a:0:0:0:0:0/48',
	'fd12:3456:789a:1::/64'
];
const ipv6Cidrs = ipArr.reduce(/** @param {string[]} acc */ (acc, ipStr) => {
	const sanitized = IPUtil.sanitize(ipStr, false, (version, isCidr) => version === 6 && isCidr);
	if (sanitized) {
		acc.push(sanitized);
	}
	return acc;
}, []);
console.log(ipv6Cidrs); // [ 'fd12:3456:789a:0:0:0:0:0/48', 'fd12:3456:789a:1:0:0:0:0/64' ]

Methods

For a number of other methods, see the API documentation!