mde / true

A JavaScript port of the Unix utility 'true'. Returns the Boolean value `true`
101 stars 26 forks source link

true is not always true #5

Open patrick-steele-idem opened 10 years ago

patrick-steele-idem commented 10 years ago

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.

kentcdodds commented 10 years ago

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...

patrick-steele-idem commented 10 years ago

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.

mde commented 10 years ago

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.

patrick-steele-idem commented 10 years ago

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.

Ginden commented 8 years ago
Object.defineProperty(require.cache, require.resolve('true'), {
   writable: false,
   configurable: false,
   enumerable: true,
   value: module.exports
})
mde commented 8 years ago

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.

mariomac commented 8 years ago

Is this serious or just joke?

mde commented 8 years ago

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

amigrave commented 3 years ago

I think this function should be available in v8

kpatterson-klick commented 2 years ago

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.

tj-commits commented 5 months ago

Hey guys, I included @Ginden 's fix in my PR #34

tj-commits commented 5 months ago

I also included many other crucial fixes such as misexamples in the README.md and following the single responsibility principle and getting some things off this package's hands