public
Created

  • Download Gist
jquery.number.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
function run( input, expected, expectedIs, description ) {
console.log(
(((Number(input) === expected) === expectedIs) && "PASS") || "FAIL",
"Number: " + description
);
}
 
 
var number = function( obj ) {
return 0 * parseFloat( obj ) * obj + 1 ? +obj : undefined;
},
Traditionalists = function(n) {
this.value = n;
this.toString = function(){
return String(this.value);
};
},
answer = new Traditionalists( "42" ),
rong = new Traditionalists( "Devo" );
 
 
 
run( "-10", -10, true, "Negative integer string");
run( "0", 0, true, "Zero string");
run( "5", 5, true, "Positive integer string");
run( -16, -16, true, "Negative integer number");
run( 0, 0, true, "Zero integer number");
run( 32, 32, true, "Positive integer number");
run( "040", 40, true, "Octal integer literal string");
run( 0144, 0144, true, "Octal integer literal");
run( "0xFF", 0xFF, true, "Hexadecimal integer literal string");
run( 0xFFF, 0xFFF, true, "Hexadecimal integer literal");
run( "-1.6", -1.6, true, "Negative floating point string");
run( "4.536", 4.536, true, "Positive floating point string");
run( -2.6, -2.6, true, "Negative floating point number");
run( 3.1415, 3.1415, true, "Positive floating point number");
run( 8e5, 8e5, true, "Exponential notation");
run( "123e-2", 123e-2, true, "Exponential notation string");
run( answer, 42, true, "Custom .toString returning number");
run( "", undefined, false, "Empty string");
run( " ", undefined, false, "Whitespace characters string");
run( "\t\t", undefined, false, "Tab characters string");
run( "abcdefghijklm1234567890", undefined, false, "Alphanumeric character string");
run( "xabcdefx", undefined, false, "Non-numeric character string");
run( true, undefined, false, "Boolean true literal");
run( false, undefined, false, "Boolean false literal");
run( "bcfed5.2", undefined, false, "Number with preceding non-numeric characters");
run( "7.2acdgs", undefined, false, "Number with trailling non-numeric characters");
run( undefined, undefined, false, "Undefined value");
run( null, undefined, false, "Null value");
run( NaN, undefined, false, "NaN value");
run( Infinity, undefined, false, "Infinity primitive");
run( Number.POSITIVE_INFINITY, undefined, false, "Positive Infinity");
run( Number.NEGATIVE_INFINITY, undefined, false, "Negative Infinity");
run( rong, undefined, false, "Custom .toString returning non-number");
run( {}, undefined, false, "Empty object");
run( function(){}, undefined, false, "Instance of a function");
run( new Date, undefined, false, "Instance of a Date");
run( function(){}, undefined, false, "Instance of a function");
results
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
PASS Number: Negative integer string
PASS Number: Zero string
PASS Number: Positive integer string
PASS Number: Negative integer number
PASS Number: Zero integer number
PASS Number: Positive integer number
PASS Number: Octal integer literal string
PASS Number: Octal integer literal
PASS Number: Hexadecimal integer literal string
PASS Number: Hexadecimal integer literal
PASS Number: Negative floating point string
PASS Number: Positive floating point string
PASS Number: Negative floating point number
PASS Number: Positive floating point number
PASS Number: Exponential notation
PASS Number: Exponential notation string
PASS Number: Custom .toString returning number
PASS Number: Empty string
PASS Number: Whitespace characters string
PASS Number: Tab characters string
PASS Number: Alphanumeric character string
PASS Number: Non-numeric character string
PASS Number: Boolean true literal
PASS Number: Boolean false literal
PASS Number: Number with preceding non-numeric characters
PASS Number: Number with trailling non-numeric characters
PASS Number: Undefined value
PASS Number: Null value
PASS Number: NaN value
PASS Number: Infinity primitive
PASS Number: Positive Infinity
PASS Number: Negative Infinity
PASS Number: Custom .toString returning non-number
PASS Number: Empty object
PASS Number: Instance of a function
PASS Number: Instance of a Date
PASS Number: Instance of a function

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.