UNPKG

@datatables/type-detector

Version:
128 lines (127 loc) 4.63 kB
declare type TReturnType = 'date' | 'datetime' | 'time' | 'mixed' | 'string' | 'number' | 'html' | 'sequence'; interface IDetails { dp: null | number; format: null | string; locale: null | string; postfix: null | string; prefix: null | string; type: null | TReturnType; } interface ILangOpts { abbrDays?: { [key: string]: RegExp; }; abbrMonths?: { [key: string]: RegExp; }; days?: { [key: string]: RegExp; }; months?: { [key: string]: RegExp; }; postFixes?: { [key: string]: RegExp; }; } export default class TypeDetect { private decimalCharacter; private thousandsSeparator; private langOpts; constructor(decimalCharacter?: string, thousandsSeparator?: string); /** * Detects the type and additional details from the overall data set. * * @param data The dataset to have a type detected * @returns string - the type that has been detected and any additional details for that type */ typeDetect(data: any[]): IDetails; /** * Extends the language options that are available by default. * * @param langOpts The extra language options that are to be added to/override the existing language options * @returns self for chaining */ i18n(langOpts: ILangOpts): TypeDetect; private _isBoolean; private _isEmpty; /** * Gets the actual type of the data as a string * * @param data The array of data to be processed * @param prefix Any prefix that has been detected within the dataset * @param postfix Any postfix that has been detected within the dataset * @returns string - the actual type of the data and occasionally the * format of the data in dates this is underscore separated */ private _getType; /** * Determines a date format for a value that is passed in. * A big major condition here is that some combinations of tokens are not allowed. * For example, `2021 2021` will not be picked up as `YYYY YYYY` and * `2021 21` will not be picked up as `YYYY YY` Full details on this are provided in the readme * https://github.com/DataTables/Type-Detection#tokens * * @param el The potential date that is to have a value determined * @param suggestion The previously suggested format for other values in the same field * @returns the suggested format for the field */ private _getDateFormat; private _isSequence; /** * Determine whether to use a double or single token if there is a leading 0 * * @param format The format object currently being constructed * @param idx The part number * @param a The first option if double token * @param b The second option if single token * @param has Any flag to be set * @returns the updated format */ private _determineTokenFormat; /** * Set's all of the relevant values when a new token is determined * * @param format The format object currently being constructed * @param idx The part number * @param value The token to be set * @param definite Whether this is definitely the token's type * @param firm Whether this is the exact token * @param locale Optional, any locale that is associated with this value/token combination * @param has Any flag to be set * @returns the updated format */ private _setDateFormat; /** * Identifies a common prefix amongst an array of data * * @param data The data that is to be parsed to determine a prefix * @returns string, the prefix that has been identified */ private _getPrefix; private _determinePrefix; /** * Find the first non-empty value in an array * * @param d Array to find * @returns undefined if nothing found, otherwise the value found */ private _firstNonNull; /** * Identifies a common postfix amongst an array of data * * @param data The data that is to be parsed to determine a postfix * @returns string, the postfix that has been identified */ private _getPostfix; /** * Identifies the highest number of decimal places within the dataset * * @param data The data that is to be parsed to determine the number of decimal places * @param postfix The datas postfix that is stripped from the data to accurately determine number of decimal places * @returns number, the highest number of decimal places in the entire dataset */ private _getDP; private _escapeRegExp; } export {};