Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lodash Dependency #52

Open
Joncom opened this issue May 13, 2020 · 3 comments
Open

Lodash Dependency #52

Joncom opened this issue May 13, 2020 · 3 comments

Comments

@Joncom
Copy link

Joncom commented May 13, 2020

May I suggest removing Lodash as a dependency?

The single call to the Lodash isEqual function could be probably replaced with a simple local implemention:

function isEqual(a: any, b: any) {
  if (a === b) {
    return true;
  }
  if (a === null && b === null) {
    return true;
  }
  if (typeof (a) !== typeof (b)) {
    return false;
  }
  if (typeof (a) === 'object') {
    // Array
    if (Array.isArray(a)) {
      if (!Array.isArray(b)) {
        return false;
      }
      if (a.length !== b.length) {
        return false;
      }
      for (let i = 0; i < a.length; i++) {
        if (!isEqual(a[i], b[i])) {
          return false;
        }
      }
      return true;
    }
    // Hash table
    const keys = Object.keys(a);
    if (keys.length !== Object.keys(b).length) {
      return false;
    }
    for (let i = 0; i < keys.length; i++) {
      if (!b.hasOwnProperty(keys[i])) {
        return false;
      }
      if (!isEqual(a[keys[i]], b[keys[i]])) {
        return false;
      }
    }
    return true;
  }
}
@Joncom
Copy link
Author

Joncom commented May 17, 2020

One reason to not use Lodash: its heavy use of CommonJS/Node style modules means no compiling to ES6 modules from TS.

const isEqual = require('lodash.isequal'); // this syntax avoids TS1192

@kat-ya
Copy link

kat-ya commented Jun 11, 2020

Also I was unable to use json-type-validation library in Stencil project due Cannot Use External Module due to a call to require in dependency #652, which is unfortunate because your json-type-validation is just what I was looking for in dynamic validation of a JSON object.

@Addeuz
Copy link

Addeuz commented Oct 8, 2021

``At my work we heavily relied on this dependency but since we started using svelte we had troubles with lodash.isequal and how its built.

So I forked this repo here and made the change that you suggested. It solves our problem and your function works great. Except for when b is null and a is not and vice versa. Which I found out today after our system tests failed.

So modified the function slightly:

function isEqual(a: any, b: any) {
  if (a === b) {
    return true;
  }
  if (a === null && b === null) {
    return true;
  }
  if (typeof (a) !== typeof (b)) {
    return false;
  }
  if (typeof (a) === 'object') {
    // new if that fixes that bug
    if (a === null && b !== null || a !== null && b === null) {
      return false;
    }
    // Array
    if (Array.isArray(a)) {
      if (!Array.isArray(b)) {
        return false;
      }
      if (a.length !== b.length) {
        return false;
      }
      for (let i = 0; i < a.length; i++) {
        if (!isEqual(a[i], b[i])) {
          return false;
        }
      }
      return true;
    }
    // Hash table
    const keys = Object.keys(a);
    if (keys.length !== Object.keys(b).length) {
      return false;
    }
    for (let i = 0; i < keys.length; i++) {
      if (!b.hasOwnProperty(keys[i])) {
        return false;
      }
      if (!isEqual(a[keys[i]], b[keys[i]])) {
        return false;
      }
    }
    return true;
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants