image-js icon indicating copy to clipboard operation
image-js copied to clipboard

fix(Image): use instance check on isImage

Open twlite opened this issue 1 year ago • 1 comments

This PR updates static Image.isImage method to initially verify the object via instance check.

Not sure if the verification should be done via toString though, because of the following issue:

// here we have a class named Demo
class Demo {
    // We return "Demo" here
    get [Symbol.toStringTag]() {
        return "Demo";
    }
    // this is implemented in same way as Image
    static isDemo(obj) {
        return Object.prototype.toString.call(obj) === "[object Demo]";
    }
}

// here we have another class named Demo2
class Demo2 {
    // Here we are returning the same "Demo" string
    get [Symbol.toStringTag]() {
        return "Demo";
    }
    // this too is equivalent to Image's isImage method
    static isDemo(obj) {
        return Object.prototype.toString.call(obj) === "[object Demo]";
    }
}

// instantiating each of these classes
const demo1 = new Demo();
const demo2 = new Demo2();

// since we are just verifying via toString return value, we don't know if demo1 and demo2 are actually equal
console.log(`${demo1.toString()} ${demo2.toString()}`) // Demo Demo
console.log(`${Demo.isDemo(demo2)} ${Demo.isDemo(demo1)}`) // true true

Due to this, Image.isImage may not always validate the actual Image which can cause problems. Let me know if toString implementation should not be removed.

twlite avatar Oct 07 '22 08:10 twlite

Codecov Report

Base: 84.43% // Head: 84.42% // Decreases project coverage by -0.00% :warning:

Coverage data is based on head (acc160d) compared to base (e3ba8d4). Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##           legacy     #611      +/-   ##
==========================================
- Coverage   84.43%   84.42%   -0.01%     
==========================================
  Files         163      163              
  Lines        5697     5696       -1     
  Branches     1424     1424              
==========================================
- Hits         4810     4809       -1     
  Misses        790      790              
  Partials       97       97              
Impacted Files Coverage Δ
src/image/Image.js 94.25% <100.00%> (-0.07%) :arrow_down:

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

:umbrella: View full report at Codecov.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.

codecov[bot] avatar Oct 07 '22 08:10 codecov[bot]

Does this fix a real issue?

targos avatar Oct 08 '22 07:10 targos

Does this fix a real issue?

With instance check, it should only verify the instances of Image. Basically similar approach to the current one but a bit more strict.

twlite avatar Oct 08 '22 11:10 twlite

isImage was implemented in the current way to allow for interoperability with multiple versions of image-js. We are not going to do the same with the future TypeScript version of the project but I don't want to risk breaking existing code in the legacy branch.

targos avatar Oct 08 '22 11:10 targos

isImage was implemented in the current way to allow for interoperability with multiple versions of image-js. We are not going to do the same with the future TypeScript version of the project but I don't want to risk breaking existing code in the legacy branch.

alright thanks

twlite avatar Oct 08 '22 12:10 twlite