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 🙏

© 2026 – Pkg Stats / Ryan Hefner

compare-shield

v2.1.0

Published

A coercion-free numeric comparison utility for <, <=, >, >= within JavaScript's safe integer range.

Downloads

23

Readme

compare-shield

Production-ready numeric comparisons that eliminate JavaScript's most dangerous pitfalls.

JavaScript's comparison operators are a source of critical bugs in production applications. Type coercion, special value handling, and precision issues cause silent failures that are expensive to debug and fix. compare-shield provides strict, predictable numeric comparisons with zero tolerance for ambiguous inputs.

The cost of comparison bugs:

  • Silent data corruption in financial calculations
  • Incorrect sorting and filtering in user interfaces
  • Security vulnerabilities from type confusion attacks
  • Hours of debugging time tracking down coercion issues

The solution: Explicit validation with guaranteed behavior. Every comparison either succeeds with valid finite numbers or fails fast with a clear false result.

Critical JavaScript Comparison Problems

JavaScript's native comparison operators create unpredictable behavior that ships bugs to production:

// Type coercion creates logic errors
"10" > 5              // true (string coerced to number)
"10" > "5"            // false (lexicographic comparison)
[] < 1                // true (empty array coerced to 0)
[2] < 1               // false (array coerced to 2)

// Special values produce inconsistent results
null >= 0             // true (null coerced to 0)
null > 0              // false
null == 0             // false
undefined >= 0        // false (undefined coerced to NaN)
NaN < NaN             // false
NaN >= NaN            // false

// Precision loss with large numbers
9007199254740993 > 9007199254740992  // false (precision loss)

// Object coercion unpredictability
{} > 0                // false
{} >= 0               // true (both coerced to NaN)
[1,2] > 0             // false (coerced to NaN)

Safe, Predictable Comparisons

compare-shield eliminates these issues with strict validation and explicit failure handling:

import { compare } from "compare-shield";

// Type safety - rejects non-numeric inputs
compare("10", ">", 5); // false (string rejected)
compare([], "<", 1); // false (array rejected)
compare({}, ">=", 0); // false (object rejected)

// Consistent special value handling
compare(null, ">=", 0); // false (null rejected)
compare(undefined, "<", 5); // false (undefined rejected)
compare(NaN, ">=", NaN); // false (NaN rejected)
compare(Infinity, ">", 1000); // false (Infinity rejected)

// Only valid numeric comparisons succeed
compare(10, ">", 5); // true
compare(3.14, "<=", 3.14); // true
compare(-100, "<", 0); // true

Installation

(npm/yarn) (install/add) compare-shield

API Reference

function compare(
  a: number | null | undefined,
  operator: "<" | "<=" | ">" | ">=",
  b: number | null | undefined
): boolean;

Parameters

  • a: First operand (must be a finite number)
  • operator: Comparison operator ("<", "<=", ">", ">=")
  • b: Second operand (must be a finite number)

Return Value

Returns true only when both operands are valid finite numbers and the comparison condition is met.

Returns false for any invalid input:

  • Non-numeric values (strings, objects, arrays, null, undefined)
  • Special numeric values (NaN, Infinity, -Infinity)
  • Invalid operators

Production Use Cases

Financial Calculations

// Prevent currency comparison errors
if (compare(transaction.amount, "<=", account.balance)) {
  processPayment(transaction);
}

Data Validation

// Reliable input validation
const isValid = compare(userAge, ">=", 18) && compare(userAge, "<=", 120);

API Response Processing

// Safe handling of external data
const results = apiResponse.items.filter((item) =>
  compare(item.score, ">", threshold)
);

Sorting and Filtering

// Predictable data operations
const sorted = data.sort((a, b) => {
  if (compare(a.value, "<", b.value)) return -1;
  if (compare(a.value, ">", b.value)) return 1;
  return 0;
});

License

MIT © wolf-software