Code Monkey home page Code Monkey logo

shallow-equal's Introduction

Description

If you know you have two arrays or two objects in hand, and you want to know if they are shallowly equal or not, this library is for you.

Features

  • Super light
  • No dependencies
  • Thoroughly tested

Installation

npm install shallow-equal --save

or

yarn add shallow-equal

Usage

import { shallowEqualArrays } from "shallow-equal";

shallowEqualArrays([1, 2, 3], [1, 2, 3]); // => true
shallowEqualArrays([{ a: 5 }], [{ a: 5 }]); // => false
import { shallowEqualObjects } from "shallow-equal";

shallowEqualObjects({ a: 5, b: "abc" }, { a: 5, b: "abc" }); // => true
shallowEqualObjects({ a: 5, b: {} }, { a: 5, b: {} }); // => false

You can also use the generic form, shallowEqual. But note that it does runtime type checking in order to decide whether it's comparing arrays or objects, so the convenience comes with a runtime penalty.

import { shallowEqual } from "shallow-equal";

shallowEqual([1, 2, 3], [1, 2, 3]); // => true
shallowEqual({ a: 5, b: {} }, { a: 5, b: {} }); // => false

shallow-equal's People

Contributors

fivecar avatar moroshko avatar noomorph avatar trysound avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

shallow-equal's Issues

ESM support

Hey,

we needed shallow equal function here and it turned out neither fbjs/lib/shallowEqual nor this package support ESM. The problem here is that we configured rollup to only take ESM modules and we actively avoid any cjs modules for both reasons, moving ecosystem forward and having only treeshakable dependencies.

Can we please add an ESM entry point to this module?

TS support

That would be cool to ship TS defs for this package ;)

version 3 module will not load due to the wrong file extension in package.json

When using version 3 I'm getting the error Could not load './dist/index.modern.js' from module 'shallow-equal' found in package.json#module

    5 |   "main": "dist/index.js",
  > 6 |   "module": "dist/index.modern.js",
  >   |             ^^^^^^^^^^^^^^^^^^^^^^ './dist/index.modern.js' does not exist, did you mean './dist/index.modern.mjs'?'
    7 |   "source": "src/index.ts",
    8 |   "types": "dist/index.d.ts",

The file is actually index.modern.mjs in the dist folder.

Inheritence is not handled well

The object implementation essentially checks whether:

  • a and b have the same number of own properties, and
  • all of the own properties in a equal any kind of properties in b, including inherited ones.

Because the implementation does not check whether the property in b is its own or not, it can be fooled. Try adding this test case:

  {
    should: 'return false for the false king',
    objA: {
      crownWearer: true
    },
    objB: (function createFalseKing() {
      var spider = {
        crownWearer: true
      }

      function FalseKing() {
        this.objective = 'world domination';
      }

      FalseKing.prototype = spider;

      return new FalseKing();
    })(),
    result: false
  }

Array comparison allows primitives to pass

If you pass two equal primitive to the array comparison method it'll return true. Perhaps add this at the top:

if (!Array.isArray(arrA) || !Array.isArray(arrB)) {
  return false
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.