Giter VIP home page Giter VIP logo

Comments (8)

goto-bus-stop avatar goto-bus-stop commented on May 14, 2024

I think if the condition on the next line is true, the function can still return true.

from nanomorph.

tunnckoCore avatar tunnckoCore commented on May 14, 2024

Ordering of the ifs are pretty important. That's why. So, no, it's not useless.

from nanomorph.

c-smile avatar c-smile commented on May 14, 2024

Consider this:

function same(a,b) is called for a and b of Element type so effectively you get this:

function same (a, b) {
  ...
  if (a.tagName !== b.tagName) return false; // either false
  // if (a.type === TEXT_NODE) return a.nodeValue === b.nodeValue - dead statement if Elements
  return false; // or false again
}

If a and b are Node's then a.tagName is always undefined thus that part looks as this

function same (a, b) {
  ...
  // if (a.tagName !== b.tagName) return false; - dead statment if Nodes 
  if (a.type === TEXT_NODE) return a.nodeValue === b.nodeValue;
  return false;
}

from nanomorph.

tunnckoCore avatar tunnckoCore commented on May 14, 2024

Even if this makes sense, still. The thing is that we don't know what a and b are at the point where we call the same(). In one case it can be Element, in another, it can be Node. It's not completely useless or just sitting there, never used. I'm pretty sure it is covered when you run the coverage.

There are tons of tests. I also had reimplemented it (in demo-minmorph) a dozen of times just out of curiosity.

from nanomorph.

c-smile avatar c-smile commented on May 14, 2024

Try this:

<html>
    <head>
        <title>Test</title>
    </head>
    <body>
    <div>1</div>
    <div>2</div>
    </body>
<script type="text/javascript">

function same (a, b) {
  if (a.id) return a.id === b.id;
  if (a.isSameNode) return a.isSameNode(b);
  if (a.tagName !== b.tagName) return false;
  if (a.type === TEXT_NODE) return a.nodeValue === b.nodeValue;
  return false;
}

var divs = document.body.getElementsByTagName("div");
console.log( same(divs[0],divs[1]) );
</script>

</html>

You will see false in console. But I think true is expected there, right?

from nanomorph.

tunnckoCore avatar tunnckoCore commented on May 14, 2024

But I think true is expected there, right?

Mmm, nope. Why it would be? It goes to the a.nodeValue === b.nodeValue which should return false, because it's 1 === 2.

I'll say that again, same is called recursively anyway, so 1) we don't know what's what in a given point in time and 2) it makes sense to have exactly these checks.

from nanomorph.

c-smile avatar c-smile commented on May 14, 2024

Mmm, nope. Why it would be?

Then if (a.tagName !== b.tagName) return false; is useless.

QED.

from nanomorph.

tunnckoCore avatar tunnckoCore commented on May 14, 2024

Omg, in this exact case, yes of course. Remove it and run the tests, and you will see that we need it.

Even if we remove it or put it in separate function... We still need to have this check anyway. That's why it is there in the same.

I'm done here.

from nanomorph.

Related Issues (20)

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.