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

evaluable

v1.2.1

Published

A tiny library to compare objects as values in ECMAScript

Downloads

6

Readme

Evaluable

A tiny library to compare objects as values in ECMAScript

View on NPM License

Installation

Use the npm package manager to install Evaluable.

npm i evaluable

Usage

Evaluable provides a function compare equality of anything in ECMAScript as a value. It is similar to the “same-value-zero” algorithm, which is used in collections such as Set and Map, but it considers the methods equals or overwritten valueOf. See the API bellow to check every rule.

import { is } from 'evaluable';

is(null, null); // returns true

is(null, undefined); // returns false

is(null, ''); // returns false

is(+0, -0); // returns true

is(0.1 + 0.2, 0.3); // returns true

is(NaN, NaN); // returns true

is('abc', 'abc'); // returns true

is('\u00F1', '\u006E\u0303'); // returns true

is(new Date(0), new Date(0)); // returns true

is(new Number(2), new Number(2)); // returns true

You can also create custom classes with the equals method, and the is function will call it.

import { is } from 'evaluable';

class Box {
  constructor(value) {
    this.value = value;
  }

  equals(other) {
    return (
      this === other || (other instanceof Box && this.value === other.value)
    );
  }
}

is(new Box(0), new Box(0)); // returns true

is(new Box(0), new Box(1)); // returns false

For TypeScript users, you can import the Evaluable<T> interface to guide the creation of objects that compared as values.

import { type Evaluable, is } from 'evaluable';
import { hash } from 'cruxhash';

class Point2D implements Evaluable {
  x: number;
  y: number;

  constructor(x: number, y: number) {
    this.x = x;
    this.y = y;
  }

  equals(other: unknown): boolean {
    return (
      this === other ||
      (other !== null &&
        other instanceof Point2D &&
        this.x === other.x &&
        this.y === other.y)
    );
  }

  hashCode(): number {
    return hash(this);
  }
}

const a = new Point2D(3, 4);
const b = new Point2D(5, 12);
const c = new Point2D(3, 4);

is(a, b); // returns false
is(a, c); // returns true
is(b, c); // returns false

To ensure compatibility with Immutable.js, the Evaluable<T> interface will enforce the implementation of the hashCode method. You can use the CRUXHash library to easily create hashes from objects.

API

is

is(a, b, delta?): boolean

Returns true if the inputs have the same value, false otherwise.

Two inputs, a and b, have the same value if:

  • Both are undefined;
  • Both are null;
  • Both are true or both false;
  • Both are the same symbol;
  • Both are the same bigint;
  • Both are strings with same length and with the same sequence of code points in the Unicode Normalized NFC form;
  • Both are numbers and:
    • both are NaN;
    • both are Infinite or both are -Infinite;
    • both are equals by some delta tolerance. Default: Number.EPSILON.
  • Both are objects and:
    • have the equals method and a.equals(b) returns true, or;
    • have the valueOf method overwritten and is(a.valueOf(), b.valueOf()) returns true, or;
    • are the same object, i.e., both references the same memory address.

The is function differs from:

  • the == operator, because it does not perform a type conversion when comparing the inputs;
  • the === operator, because it returns true when comparing NaN with NaN;
  • the Object.is method, because it returns true when comparing +0 and -0;
  • the “same-value-zero” algorithm, which is used in collections such as Set and Map, because it considers the results of equals and overwritten valueOf methods.
  • the Immutable.is method because it only considers the results of equals methods and does not check the delta tolerance when comparing numbers.

Parameters

| Name | Type | Default value | Description | | :------ | :-------- | :--------------- | :----------------------------------------- | | a | unknown | - | an input | | b | unknown | - | another input | | delta | number | Number.EPSILON | the minimum difference between two numbers |

Returns

boolean

true if the inputs have the same value, false otherwise.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT