Socket
Socket
Sign inDemoInstall

@discoveryjs/natural-compare

Package Overview
Dependencies
Maintainers
3
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@discoveryjs/natural-compare

Compare strings in a natural order


Version published
Weekly downloads
136K
increased by0.37%
Maintainers
3
Weekly downloads
 
Created

What is @discoveryjs/natural-compare?

@discoveryjs/natural-compare is an npm package that provides a natural order string comparison algorithm. This is particularly useful for sorting strings in a way that humans would expect, such as sorting file names, version numbers, or any other strings that contain numbers.

What are @discoveryjs/natural-compare's main functionalities?

Basic Natural Comparison

This feature allows you to sort an array of strings in a natural order. In the example, file names are sorted in a way that a human would expect, with 'file10.txt' coming after 'file2.txt'.

const naturalCompare = require('@discoveryjs/natural-compare');

const arr = ['file10.txt', 'file2.txt', 'file1.txt'];
arr.sort(naturalCompare);
console.log(arr); // Output: ['file1.txt', 'file2.txt', 'file10.txt']

Case Insensitive Comparison

This feature allows you to perform a case-insensitive natural comparison. By converting all strings to lowercase before comparison, the sorting ignores case differences.

const naturalCompare = require('@discoveryjs/natural-compare');

const arr = ['File10.txt', 'file2.txt', 'file1.txt'];
arr.sort((a, b) => naturalCompare(a.toLowerCase(), b.toLowerCase()));
console.log(arr); // Output: ['file1.txt', 'file2.txt', 'File10.txt']

Other packages similar to @discoveryjs/natural-compare

Keywords

FAQs

Package last updated on 16 Sep 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc