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

hostname-natural-order

v1.2.1

Published

Natural order (sort) array of hostname

Downloads

18

Readme

hostname-natural-order

Version npm Npm package total downloads Module type: CJS node.js version License Unit test status

Natural order (natural sort) for array of hostnames.

This library is using compare function from natural-orderby.

When do you need this package?

You need it when you need to sort hostname list like this correctly:

  • www1.example.org
  • www2.example.org
  • www100.example.org
  • www200.example.org
  • www1.test.org
  • www2.test.org

If you just use standard Array.prototype.sort, it would sort incorrectly and return:

  • www1.example.org
  • www1.test.org
  • www100.example.org
  • www2.example.org
  • www2.test.org
  • www200.example.org

That order is not what you expect? Here come a package to order that correctly.

Installation

npm i hostname-natural-order

Usage

Using Array.prototype.sort

const { compare: compareHostname } = require('hostname-natural-order');

const domains = [
  'test.org',
  'org',
  'a.test.net',
  'net',
  'c.test.net',
  '3.b.test.net',
  '100.b.test.net',
  'example.org',
  '2.b.test.net',
  'test.net',
  'c.test.net',
  '1.b.test.net',
];

domains.sort(compareHostname);
console.log(JSON.stringify(domains, null, 2));

Will print:

[
  "net",
  "test.net",
  "a.test.net",
  "1.b.test.net",
  "2.b.test.net",
  "3.b.test.net",
  "100.b.test.net",
  "c.test.net",
  "c.test.net",
  "org",
  "example.org",
  "test.org"
]

Natural order/sort array of object

Since v1.1.0, this package export "orderBy" method so you can order array of object easily.

const { orderBy } = require('hostname-natural-order');

const hostnameList = [
  { name: '2.b.test.net' },
  { name: 'test.net' },
  { name: '1.b.test.net' },
  { name: '8.8.4.4' },
  { name: '100.100.100.100' },
  { name: 'example.org' },
  { name: '100.b.test.net' },
  { name: '8.8.8.8' },
  { name: 'test.org' },
  { name: 'a.test.net' },
  { name: 'c.test.net' },
  { name: '1.1.1.1' },
  { name: 'net' },
  { name: 'org' },
  { name: 'c.test.net' },
];

const hostnameListSorted = orderBy(
  hostnameList,
  // example to order by "name" property
  (item) => item.name,
);

Command-Line Interface (CLI)

This module provide CLI tool so you can use it from CLI shell directly without programming any code.

CLI Installation

sudo npm i --global hostname-natural-order

CLI Usage

hostname-natural-order microsoft.com example.org google.com

It will print:

google.com
microsoft.com
example.org

This program can also read hostname list from file. Just pipe "cat" output.

cat hostnames.txt | hostname-natural-order

Limitation

This library expect and has been tested if strings to compare is a valid hostname or IP address (validate ip addresses using ip-toolkit).

Unexpected results might be happen if you don't validate the input. See unit test files for what we've test.

It required node.js version > 16.x because we use ip-toolkit who has requirement of node.js > 16.x. If you need to run it on older version of node.js, please raise an issue so I can know demand for that exists.

Changelog

See CHANGELOG.md file.

License

License under MIT License. Feel free to use and modified this library.