ScottHamper / Cookies

JavaScript Client-Side Cookie Manipulation Library
The Unlicense
1.77k stars 170 forks source link

Maybe add a has method? #31

Closed slavafomin closed 9 years ago

slavafomin commented 9 years ago

Hello!

I would propose to add a has method to simplify testing for cookie existence.

So we can write: if (Cookies.has('foo')) { ... } instead of if ('undefined' !== typeof Cookies.get('foo')) { ... }.

Thanks!

ScottHamper commented 9 years ago

Hey Slava,

It might be adequate for you to do something like the following:

if (Cookies.get('foo')) {
    // ...
}

The only practical difference between this and checking that a value is not undefined, is that an empty string value would also evaluate as false. This is usually an insignificant difference, however.

slavafomin commented 9 years ago

Hey! Thanks for suggestion.

However, I do believe that loose type conversion will always have some weak points. Introducing a has method will provide a robust and convenient way to check the existence of the cookie. I think it's practically a "must have" for any collection interface in programming.

ScottHamper commented 9 years ago

Hey Slava,

If you can provide me with a common, legitimate, real world scenario where loose type conversion presents a problem, then I will consider adding a has method. I'm not particularly interested in extending the API of the library just because it seems like a specific feature is practically a "must have". Instead, I am much more interested in providing a pragmatic interface that is shaped to actual needs.

It seems to me that in the overwhelming majority of cases, loose type conversion will not be problematic. As a result, it seems excessive to bloat the library with another method that will likely only need to be used very rarely, if ever.