Permalink
Browse files

Update: Much smaller numeric type detection method, based on isNumeri…

…c in jQuery. Also a whole lot faster.
  • Loading branch information...
1 parent a073515 commit 33d3667bbecae845307f7c4f7564d37343ede20a @DataTables committed Nov 2, 2012
Showing with 10 additions and 90 deletions.
  1. +5 −45 media/js/jquery.dataTables.js
  2. +5 −45 media/src/ext/ext.types.js
@@ -11654,53 +11654,13 @@
* Function: -
* Purpose: Check to see if a string is numeric
* Returns: string:'numeric' or null
- * Inputs: mixed:sText - string to check
+ * Inputs: mixed:data - string to check
*/
- function ( sData )
+ function ( data )
{
- /* Allow zero length strings as a number */
- if ( typeof sData === 'number' )
- {
- return 'numeric';
- }
- else if ( typeof sData !== 'string' )
- {
- return null;
- }
-
- var sValidFirstChars = "0123456789-";
- var sValidChars = "0123456789.";
- var Char;
- var bDecimal = false;
-
- /* Check for a valid first char (no period and allow negatives) */
- Char = sData.charAt(0);
- if (sValidFirstChars.indexOf(Char) == -1)
- {
- return null;
- }
-
- /* Check all the other characters are valid */
- for ( var i=1 ; i<sData.length ; i++ )
- {
- Char = sData.charAt(i);
- if (sValidChars.indexOf(Char) == -1)
- {
- return null;
- }
-
- /* Only allowed one decimal place... */
- if ( Char == "." )
- {
- if ( bDecimal )
- {
- return null;
- }
- bDecimal = true;
- }
- }
-
- return 'numeric';
+ return data==='' || data==='-' || (!isNaN( parseFloat(data) ) && isFinite( data )) ?
+ 'numeric' :
+ null;
},
/*
@@ -5,53 +5,13 @@ $.extend( DataTable.ext.aTypes, [
* Function: -
* Purpose: Check to see if a string is numeric
* Returns: string:'numeric' or null
- * Inputs: mixed:sText - string to check
+ * Inputs: mixed:data - string to check
*/
- function ( sData )
+ function ( data )
{
- /* Allow zero length strings as a number */
- if ( typeof sData === 'number' )
- {
- return 'numeric';
- }
- else if ( typeof sData !== 'string' )
- {
- return null;
- }
-
- var sValidFirstChars = "0123456789-";
- var sValidChars = "0123456789.";
- var Char;
- var bDecimal = false;
-
- /* Check for a valid first char (no period and allow negatives) */
- Char = sData.charAt(0);
- if (sValidFirstChars.indexOf(Char) == -1)
- {
- return null;
- }
-
- /* Check all the other characters are valid */
- for ( var i=1 ; i<sData.length ; i++ )
- {
- Char = sData.charAt(i);
- if (sValidChars.indexOf(Char) == -1)
- {
- return null;
- }
-
- /* Only allowed one decimal place... */
- if ( Char == "." )
- {
- if ( bDecimal )
- {
- return null;
- }
- bDecimal = true;
- }
- }
-
- return 'numeric';
+ return data==='' || data==='-' || (!isNaN( parseFloat(data) ) && isFinite( data )) ?
+ 'numeric' :
+ null;
},
/*

0 comments on commit 33d3667

Please sign in to comment.