go-equality

1.0.4 • Public • Published

Go Equality

Equality checking functions that work for both primitive types and objects, including nested objects and cyclic objects with a circular reference.

codecov.io Code Coverage jsdoc donation

  • version: 1.0.4
  • license: GNU LGPLv3

Installation

npm i go-equality

or

yarn add go-equality

Usage

ES6

import { isEqual } from 'go-equality'

isEqual([1, 2, 3], [1, 2, 3]); // => true

Node

const { isEqual } = require('go-equality');

isEqual([1, 2, 3], [1, 2, 3]); // => true

Web browser

<script src="dist/go-equality.min.js"></script>
<script>
    const { isEqual } = Equality;

	isEqual([1, 2, 3], [1, 2, 3]); // => true
</script>

Documentation

Table of Contents

isEqual

Determines whether two values are equal.

Two values are considered equal if they are:

  • Primitive values whose types and values are the same
  • Identical objects (comparing against itself)
  • One of them is a primitive wrapper object whose value is the same as the other value
  • Objects that have the same property names and equal values (including non-enumerable properties)
  • Objects that have the equals method and both return true when tested against each other
  • NaN values

These values are considered not equal:

  • Primitive values whose types or values are different
  • Objects that have a different constructor or property name or value (including non-enumerable properties)
  • One of the objects has the equals method and it returns false when tested against the other value
  • Functions
  • Symbol values

Parameters

  • value any The value to compare
  • other any The other value to compare with

Examples

Equal values

// returns true
isEqual(1, 1);
isEqual(1, new Number(1));
isEqual([1, 2, 3], [1, 2, 3]);
isEqual({name: "foo", value: 10}, {value: 10, name: "foo"});
isEqual(NaN, NaN);

Not equal values

// returns false
isEqual(1, "1");
isEqual(null, undefined);
isEqual({}, {a: undefined});
isEqual([1, 2, 3], [2, 3, 1]);
isEqual(function() {}, function() {});
isEqual(Symbol("abc"), Symbol("abc"));

Returns boolean true if the two values are considered equal; false otherwise.

Meta

  • since: 1.0.0

isLooseEqual

Determines whether two values are loosely equal.

Two values are considered loosely equal if they are:

  • Primitive values whose types are the same or compatible and the values are equal (after conversion if required)
  • Identical objects (comparing against itself)
  • One of them is a primitive wrapper object whose value is the same as the other value
  • Objects that have the same property names and loosely equal values (including non-enumerable properties)
  • Objects that have the equals method and both return true when tested against each other
  • NaN values

These values are considered not equal:

  • Primitive values whose types are not compatible or values are different
  • Objects that have a different property name or value (including non-enumerable properties)
  • One of the objects has the equals method and it returns false when tested against the other value
  • Functions
  • Symbol values

Parameters

  • value any The value to compare
  • other any The other value to compare with

Examples

Equal values

// returns true
isLooseEqual(null, undefined);
isLooseEqual(1, "1");
isLooseEqual(1, new Number(1));
isLooseEqual(1, BigInt(1));
isLooseEqual(1, true);
isLooseEqual(NaN, NaN);

Not equal values

// returns false
isLooseEqual(true, "true");
isLooseEqual(false, undefined);
isLooseEqual({}, {a: undefined});
isLooseEqual("abc", ["a", "b", "c"]);
isLooseEqual([1, 2, 3], [2, 3, 1]);
isLooseEqual(function() {}, function() {});
isLooseEqual(Symbol("abc"), Symbol("abc"));

Returns boolean true if the two values are loosely equal; false otherwise.

Meta

  • since: 1.0.0

isStrictEqual

Determines whether two values are strictly equal.

Two values are considered strictly equal if they are:

  • Primitive values whose types and values are the same
  • Identical objects (comparing against itself)
  • Objects that have the equals method and both return true when tested against each other
  • Objects that have the same constructor and the same property names and values (including non-enumerable properties)
  • Primitive NaN values or Number objects that represent NaN

These values are considered not equal:

  • Primitive values whose types or values are different
  • Objects that have a different constructor or property name or value (including non-enumerable properties)
  • One of the objects has the equals method and it returns false when tested against the other value
  • Functions
  • Symbol values

Parameters

  • value any The value to compare
  • other any The other value to compare with

Examples

Equal values

// returns true
isStrictEqual(1, 1);
isStrictEqual([1, 2, 3], [1, 2, 3]);
isStrictEqual({name: "foo", value: 10}, {value: 10, name: "foo"});
isStrictEqual(NaN, NaN);

Not equal values

// returns false
isStrictEqual(1, "1");
isStrictEqual(1, new Number(1));
isStrictEqual(null, undefined);
isStrictEqual({}, {a: undefined});
isStrictEqual(function() {}, function() {});
isStrictEqual(Symbol("abc"), Symbol("abc"));

Returns boolean true if the two values are strictly equal; false otherwise.

Meta

  • since: 1.0.0

Package Sidebar

Install

npm i go-equality

Weekly Downloads

39

Version

1.0.4

License

GNU LGPLv3

Unpacked Size

63.8 kB

Total Files

22

Last publish

Collaborators

  • fishgold.org