true icon indicating copy to clipboard operation
true copied to clipboard

true is not always true

Open patrick-steele-idem opened this issue 9 years ago • 10 comments

I ran into problems when trying to use this module. I added the following code for debugging:

var t = require('true');
var myTrueValue = t();
console.log(myTrueValue === true);

I expected to see the program output true, but I instead saw false!

After spending hours tracing the problem down I found that another library was doing the following:

require.cache[require.resolve('true')].exports = function() {
    return false;
};

I'm not sure why this particular third party library is doing this, but I feel like something should be done to prevent it since truthiness being correct is very important for any application.

patrick-steele-idem avatar Sep 16 '14 20:09 patrick-steele-idem

Yep, and I think it's definitely the responsibility of this true library to ensure that it handles every single edge case and odd usage/impact from other third parties...

kentcdodds avatar Sep 16 '14 20:09 kentcdodds

Yes, exactly! What we need is a way to make true immutable. Since JavaScript doesn't support such a thing, I came up with the following solution:

setInterval(function() {
    if (require('true')() !== true) {
        // Fix it!
        require.cache[require.resolve('true')].exports = function() {
            return true;
        };
    }
}, 10);

This guarantees that true will always be returned even if some code tries to redefine it. I would be happy to submit a pull request.

patrick-steele-idem avatar Sep 16 '14 20:09 patrick-steele-idem

This is brilliant, although I wonder if perhaps the correct place to fix this problem is at the language-level. Perhaps you should submit something like this solution directly to TC39: http://www.ecma-international.org/memento/TC39.htm They have a long history of being incredibly receptive to outside feedback.

mde avatar Sep 16 '14 20:09 mde

Good idea. I will also include a recommendation to fix truthiness in JavaScript. I can't begin to tell you how many times I have been bitten by the following problem with JavaScript:

assert.equal(true, 'true'); // Error!

Clearly they are the same... It's issues like this that almost make we want to switch back to Bash for all of my programming needs.

patrick-steele-idem avatar Sep 16 '14 20:09 patrick-steele-idem

Object.defineProperty(require.cache, require.resolve('true'), {
   writable: false,
   configurable: false,
   enumerable: true,
   value: module.exports
})

Ginden avatar Mar 25 '16 12:03 Ginden

This could be really useful for ensuring the integrity of this module. A PR would be helpful to solicit comments, to make sure we get adequate feedback about this potential approach.

mde avatar Mar 27 '16 17:03 mde

Is this serious or just joke?

mariomac avatar Apr 25 '16 06:04 mariomac

What could possibly be funny about tiny modules? The good thing about tiny modules is that they're composable!

mde avatar Apr 25 '16 18:04 mde

I think this function should be available in v8

amigrave avatar Oct 28 '21 15:10 amigrave

I'm so grateful it's 2022 and all of the npm issues which were so problematic in 2014 have been solved forever. Truly it's a great time to be alive.

kpatterson-klick avatar Mar 01 '22 17:03 kpatterson-klick