Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"is.Number" is accepting Arrays with numbers and string numbers #41

Open
brunocasanova opened this issue Sep 28, 2015 · 2 comments
Open

Comments

@brunocasanova
Copy link
Member

In my project i've found two strange cases with is.Number function, that i think that's not behaving like it should.

I have a condition like the example underneath, that only accept Numbers, but in fact is.Number is accepting arrays with numbers or string numbers. I don't think this is the right behavior to handle it, unless it have a good reason for that, that i'm not seeing clearly.

var Util = require( 'findhit-util' );

var array1 = [ "123" ];
var array2 = [ 123 ];

// Example 1
if( Util.is.Number( array1 ) ){
   console.log( "array1 it's a number..."  );
}else{
   console.log( "array1 it's not a number..."  );
}

// Example 2
if( Util.is.Number( array2 ) ){
   console.log( "array2 it's a number..." );
}else{
   console.log( "array2 it's not a number..." );
}

// example 1 console.log -> "array1 it's a number..."
// example 2 console.log -> "array2 it's a number..."

// the cases above should not be a number...

And the Same ( opposite ) thing for isnt.Number:

// Example 3
if( Util.isnt.Number( array1 ) ){
   console.log( "array1 it's not a number..." );
}else{
   console.log( "array1 it's a number..." );
}

// Example 4
if( Util.isnt.Number( array2 ) ){
   console.log( "array2 it's not a number..." );
}else{
   console.log( "array2 it's a number..." );
}

// example 3 console.log -> "array1 it's a number..."
// example 4 console.log -> "array2 it's a number..."

// the cases above should not be a number...

Maybe the proto of the array is assuming the number since the array contains it.

@cusspvz
Copy link
Member

cusspvz commented Sep 28, 2015

Seems important enough. f.is.Number parses the input to an Integer as the process of checking. That happens for some reasons, one of them is that we may check if a certain string is a number. Obviously, if we wan't to check if something is a number on Javascript, we use typeof x === "number", but f.is.Number was built to be clever enough.

Imagine that you are receiving something trough an http request:

var qs = // some request query parsing code
/*
{
  id: [ "123" ]
}
*/

// If you use `f.is.Number` it won't break even if you gave params wrong, but you must parse always before using it:

var id;

if ( Util.is.Number( qs.id ) {
  id = +qs.id
  // id = 123
}

@cusspvz
Copy link
Member

cusspvz commented Sep 28, 2015

Seems important enough to be documented, but firstly we have to discuss which changes we have to made on this, i have some plans that increases loading performance on browsers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants