hostname-natural-order

1.2.1 • Public • Published

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.

Package Sidebar

Install

npm i hostname-natural-order

Weekly Downloads

1

Version

1.2.1

License

MIT

Unpacked Size

9.58 kB

Total Files

7

Last publish

Collaborators

  • adhisimon