From 823e64cccbbf28b8e2ed19438c02061964804a66 Mon Sep 17 00:00:00 2001 From: Allan Jardine Date: Fri, 2 Nov 2012 08:43:37 +0000 Subject: [PATCH] Dev: A bit of love for hte type detection functions. Tidy them up and improve the comments. --- media/js/jquery.dataTables.js | 82 ++++++++++------------------------- media/src/ext/ext.types.js | 46 ++++++-------------- media/src/model/model.ext.js | 36 +++++---------- 3 files changed, 48 insertions(+), 116 deletions(-) diff --git a/media/js/jquery.dataTables.js b/media/js/jquery.dataTables.js index 7ab55529..511f2f0c 100644 --- a/media/js/jquery.dataTables.js +++ b/media/js/jquery.dataTables.js @@ -6809,41 +6809,27 @@ * column) or they can be automatically detected by the methods in this array. The functions * defined in the array are quite simple, taking a single parameter (the data to analyse) * and returning the type if it is a known type, or null otherwise. - * + * + * * Function input parameters: + * * {*} Data from the column cell to be analysed + * * Function return: + * * {string|null} Data type detected, or null if unknown (and thus pass it + * on to the other type detection functions. + * * @type array * @default [] * * @example * // Currency type detection plug-in: * jQuery.fn.dataTableExt.aTypes.push( - * function ( sData ) { - * var sValidChars = "0123456789.-"; - * var Char; - * + * function ( data ) { * // Check the numeric part - * for ( i=1 ; i') != -1 ) - { - return 'html'; - } - return null; + return typeof data === 'string' && data.indexOf('<') != -1 && data.indexOf('>') != -1 ? + 'html' : null; } ] ); diff --git a/media/src/ext/ext.types.js b/media/src/ext/ext.types.js index 6714c4b0..9de58ef9 100644 --- a/media/src/ext/ext.types.js +++ b/media/src/ext/ext.types.js @@ -1,48 +1,28 @@ +// Built in type detection. See model.ext.aTypes for information about +// what is required from this methods. $.extend( DataTable.ext.aTypes, [ - /* - * Function: - - * Purpose: Check to see if a string is numeric - * Returns: string:'numeric' or null - * Inputs: mixed:data - string to check - */ + // Numeric data type function ( data ) { return data==='' || data==='-' || (!isNaN( parseFloat(data) ) && isFinite( data )) ? - 'numeric' : - null; + 'numeric' : null; }, - /* - * Function: - - * Purpose: Check to see if a string is actually a formatted date - * Returns: string:'date' or null - * Inputs: string:sText - string to check - */ - function ( sData ) + // Dates (only those recognised by the browser's Date.parse) + function ( data ) { - var iParse = Date.parse(sData); - if ( (iParse !== null && !isNaN(iParse)) || (typeof sData === 'string' && sData.length === 0) ) - { - return 'date'; - } - return null; + var parsed = Date.parse(data); + return (parsed !== null && !isNaN(parsed)) || (typeof data==='string' && data.length===0) ? + 'date' : null; }, - /* - * Function: - - * Purpose: Check to see if a string should be treated as an HTML string - * Returns: string:'html' or null - * Inputs: string:sText - string to check - */ - function ( sData ) + // HTML + function ( data ) { - if ( typeof sData === 'string' && sData.indexOf('<') != -1 && sData.indexOf('>') != -1 ) - { - return 'html'; - } - return null; + return typeof data === 'string' && data.indexOf('<') != -1 && data.indexOf('>') != -1 ? + 'html' : null; } ] ); diff --git a/media/src/model/model.ext.js b/media/src/model/model.ext.js index 45566a62..f01a363c 100644 --- a/media/src/model/model.ext.js +++ b/media/src/model/model.ext.js @@ -165,41 +165,27 @@ DataTable.models.ext = { * column) or they can be automatically detected by the methods in this array. The functions * defined in the array are quite simple, taking a single parameter (the data to analyse) * and returning the type if it is a known type, or null otherwise. - * + * + * * Function input parameters: + * * {*} Data from the column cell to be analysed + * * Function return: + * * {string|null} Data type detected, or null if unknown (and thus pass it + * on to the other type detection functions. + * * @type array * @default [] * * @example * // Currency type detection plug-in: * jQuery.fn.dataTableExt.aTypes.push( - * function ( sData ) { - * var sValidChars = "0123456789.-"; - * var Char; - * + * function ( data ) { * // Check the numeric part - * for ( i=1 ; i