Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
natural-compare
Advanced tools
Package description
The natural-compare npm package provides a simple way to perform 'natural' string comparisons. This means it can compare strings in a way that is more intuitive for humans, taking into account numerical values within the strings to order them in a way that makes sense when read. For example, it would order 'item2' before 'item10', which is not the case with traditional ASCII-based string comparison methods.
Natural String Comparison
This feature allows for the comparison of strings containing numerical values in a way that respects the numerical values, rather than comparing them purely as strings. The provided code sample demonstrates sorting an array of strings in a natural order using the package.
"use strict";
var naturalCompare = require('natural-compare');
console.log(['item10', 'item2'].sort(naturalCompare)); // ['item2', 'item10']
alphanum-sort is an npm package that offers similar functionality to natural-compare, providing an algorithm for sorting strings that contain numbers in a way that 'naturally' orders them. Compared to natural-compare, alphanum-sort might offer more customization options, such as case sensitivity and whether to treat whitespace as significant.
string-natural-compare is another npm package that provides natural string comparison capabilities. It is similar to natural-compare but might offer different API options or performance characteristics. Users might choose one over the other based on specific needs or preferences in API design.
Readme
Compare strings containing a mix of letters and numbers in the way a human being would in sort order. This is described as a "natural ordering".
Standard sorting: Natural order sorting:
img1.png img1.png
img10.png img2.png
img12.png img10.png
img2.png img12.png
String.naturalCompare returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order. Use it with builtin sort() function.
<script src=min.natural-compare.js></script>
npm install natural-compare
require("natural-compare-lite")
// Simple case sensitive example
var a = ["z1.doc", "z10.doc", "z17.doc", "z2.doc", "z23.doc", "z3.doc"];
a.sort(String.naturalCompare);
// ["z1.doc", "z2.doc", "z3.doc", "z10.doc", "z17.doc", "z23.doc"]
// Use wrapper function for case insensitivity
a.sort(function(a, b){
return String.naturalCompare(a.toLowerCase(), b.toLowerCase());
})
// In most cases we want to sort an array of objects
var a = [ {"street":"350 5th Ave", "room":"A-1021"}
, {"street":"350 5th Ave", "room":"A-21046-b"} ];
// sort by street, then by room
a.sort(function(a, b){
return String.naturalCompare(a.street, b.street) || String.naturalCompare(a.room, b.room);
})
// When text transformation is needed (eg toLowerCase()),
// it is best for performance to keep
// transformed key in that object.
// There are no need to do text transformation
// on each comparision when sorting.
var a = [ {"make":"Audi", "model":"A6"}
, {"make":"Kia", "model":"Rio"} ];
// sort by make, then by model
a.map(function(car){
car.sort_key = (car.make + " " + car.model).toLowerCase();
})
a.sort(function(a, b){
return String.naturalCompare(a.sort_key, b.sort_key);
})
It is possible to configure a custom alphabet to achieve a desired order.
// Estonian alphabet
String.alphabet = "ABDEFGHIJKLMNOPRSŠZŽTUVÕÄÖÜXYabdefghijklmnoprsšzžtuvõäöüxy"
["t", "z", "x", "õ"].sort(String.naturalCompare)
// ["z", "t", "õ", "x"]
// Russian alphabet
String.alphabet = "АБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюя"
["Ё", "А", "Б"].sort(String.naturalCompare)
// ["А", "Б", "Ё"]
Copyright (c) 2012, 2014 Lauri Rooden <lauri@rooden.ee>
The MIT License
FAQs
Compare strings containing a mix of letters and numbers in the way a human being would in sort order.
The npm package natural-compare receives a total of 26,764,874 weekly downloads. As such, natural-compare popularity was classified as popular.
We found that natural-compare demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.